From 36b5b63c00df703e88168bff57256bfd5dd7dec4 Mon Sep 17 00:00:00 2001 From: Daniel Xu Date: Tue, 4 Jun 2019 10:23:47 -0400 Subject: [PATCH 1/3] Move prim.py from Graphs to graphs --- graphs/prim.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 graphs/prim.py diff --git a/graphs/prim.py b/graphs/prim.py new file mode 100644 index 000000000000..792a585a5203 --- /dev/null +++ b/graphs/prim.py @@ -0,0 +1,74 @@ +""" +Prim's Algorithm. +Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm +Create a list to store x the vertices. +G = [vertex(n) for n in range(x)] +For each vertex in G, add the neighbors: +G[x].addNeighbor(G[y]) +G[y].addNeighbor(G[x]) +For each vertex in G, add the edges: +G[x].addEdge(G[y], w) +G[y].addEdge(G[x], w) +To solve run: +MST = prim(G, G[0]) +""" + +import math + + +class vertex(): + """Class Vertex.""" + + def __init__(self, id): + """ + Arguments: + id - input an id to identify the vertex + Attributes: + neighbors - a list of the vertices it is linked to + edges - a dict to store the edges's weight + """ + self.id = str(id) + self.key = None + self.pi = None + self.neighbors = [] + self.edges = {} # [vertex:distance] + + def __lt__(self, other): + """Comparison rule to < operator.""" + return (self.key < other.key) + + def __repr__(self): + """Return the vertex id.""" + return self.id + + def addNeighbor(self, vertex): + """Add a pointer to a vertex at neighbor's list.""" + self.neighbors.append(vertex) + + def addEdge(self, vertex, weight): + """Destination vertex and weight.""" + self.edges[vertex.id] = weight + + +def prim(graph, root): + """ + Prim's Algorithm. + Return a list with the edges of a Minimum Spanning Tree + prim(graph, graph[0]) + """ + A = [] + for u in graph: + u.key = math.inf + u.pi = None + root.key = 0 + Q = graph[:] + while Q: + u = min(Q) + Q.remove(u) + for v in u.neighbors: + if (v in Q) and (u.edges[v.id] < v.key): + v.pi = u + v.key = u.edges[v.id] + for i in range(1, len(graph)): + A.append([graph[i].id, graph[i].pi.id]) + return(A) From e7d863b70d1c5cb8df38614e41b26d108ec66ae8 Mon Sep 17 00:00:00 2001 From: Daniel Xu Date: Tue, 4 Jun 2019 10:24:23 -0400 Subject: [PATCH 2/3] Removed prim.py from Graphs --- Graphs/prim.py | 82 -------------------------------------------------- 1 file changed, 82 deletions(-) delete mode 100644 Graphs/prim.py diff --git a/Graphs/prim.py b/Graphs/prim.py deleted file mode 100644 index c9f91d4b0700..000000000000 --- a/Graphs/prim.py +++ /dev/null @@ -1,82 +0,0 @@ -""" -Prim's Algorithm. - -Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm - -Create a list to store x the vertices. -G = [vertex(n) for n in range(x)] - -For each vertex in G, add the neighbors: -G[x].addNeighbor(G[y]) -G[y].addNeighbor(G[x]) - -For each vertex in G, add the edges: -G[x].addEdge(G[y], w) -G[y].addEdge(G[x], w) - -To solve run: -MST = prim(G, G[0]) -""" - -import math - - -class vertex(): - """Class Vertex.""" - - def __init__(self, id): - """ - Arguments: - id - input an id to identify the vertex - - Attributes: - neighbors - a list of the vertices it is linked to - edges - a dict to store the edges's weight - """ - self.id = str(id) - self.key = None - self.pi = None - self.neighbors = [] - self.edges = {} # [vertex:distance] - - def __lt__(self, other): - """Comparison rule to < operator.""" - return (self.key < other.key) - - def __repr__(self): - """Return the vertex id.""" - return self.id - - def addNeighbor(self, vertex): - """Add a pointer to a vertex at neighbor's list.""" - self.neighbors.append(vertex) - - def addEdge(self, vertex, weight): - """Destination vertex and weight.""" - self.edges[vertex.id] = weight - - -def prim(graph, root): - """ - Prim's Algorithm. - - Return a list with the edges of a Minimum Spanning Tree - - prim(graph, graph[0]) - """ - A = [] - for u in graph: - u.key = math.inf - u.pi = None - root.key = 0 - Q = graph[:] - while Q: - u = min(Q) - Q.remove(u) - for v in u.neighbors: - if (v in Q) and (u.edges[v.id] < v.key): - v.pi = u - v.key = u.edges[v.id] - for i in range(1, len(graph)): - A.append([graph[i].id, graph[i].pi.id]) - return(A) From e078d9587cffd3060eb9168cd023022066cd2858 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 5 Jun 2019 09:36:43 +0800 Subject: [PATCH 3/3] Update prim.py --- graphs/prim.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/graphs/prim.py b/graphs/prim.py index 792a585a5203..f7e08278966d 100644 --- a/graphs/prim.py +++ b/graphs/prim.py @@ -1,14 +1,19 @@ """ Prim's Algorithm. + Determines the minimum spanning tree(MST) of a graph using the Prim's Algorithm + Create a list to store x the vertices. G = [vertex(n) for n in range(x)] + For each vertex in G, add the neighbors: G[x].addNeighbor(G[y]) G[y].addNeighbor(G[x]) + For each vertex in G, add the edges: G[x].addEdge(G[y], w) G[y].addEdge(G[x], w) + To solve run: MST = prim(G, G[0]) """