"""Find the group containing p and return the position of its leader. """ if p._parent != p: # overwrite pos._parent after recursion p._parent = self.find(p._parent) return p._parent def union(self, p, q): """Merges the groups containing elements p and q.""" a = self.find(p) b = self.find(q) if a is not b: if a._size > b._size: b._parent = a a._size += b._size else: a._parent = b b._size += a._size if __name__ == "__main__": from graph_examples import figure_14_15 as example graph = example() edges = MST_PrimJarnik(graph) for edge in edges: print(str(edge)) print("=" * 20) edges = MST_Kruskal(graph) for edge in edges: print(str(edge))
def find(self, p): """Find the group containing p and return the position of its leader. """ if p._parent != p: # overwrite pos._parent after recursion p._parent = self.find(p._parent) return p._parent def union(self, p, q): """Merges the groups containing elements p and q.""" a = self.find(p) b = self.find(q) if a is not b: if a._size > b._size: b._parent = a a._size += b._size else: a._parent = b b._size += a._size if __name__ == "__main__": from graph_examples import figure_14_15 as example graph = example() edges = MST_PrimJarnik(graph) for edge in edges: print(str(edge)) print("="*20) edges = MST_Kruskal(graph) for edge in edges: print(str(edge))
discovered[ v] = e #e is the tree edge that is discovered in v next_level.append( v) #v will be further considered in next pass level = next_level def BFS_complete(g): """Perform BFS for entire graph and return forest as a dictionary. Result maps each vertex v to the edge that was used to discover it. (vertices that are roots of a BFS tree are mapped to None). """ forest = {} for u in g.vertices(): if u not in forest: forest[u] = None #u will the root of our tree BFS(g, u, forest) return forest if __name__ == '__main__': #from graph_examples import figure_14_11 as example from graph_examples import figure_14_9_smaller as example #from graph_examples import figure_14_12_smaller as example g = example() print("Number of vertices is", g.vertex_count()) print("Number of edges is", g.edge_count()) bfs = BFS_complete(g) print("BFS", [str(v) for v in bfs])
from copy import deepcopy def floyd_warshall(g): """Return a new graph that is the transitive closure of g.""" closure = deepcopy(g) # imported from copy module verts = list(closure.vertices()) # make indexable list n = len(verts) for k in range(n): for i in range(n): # verify that edge (i,k) exists in the partial closure if i != k and closure.get_edge(verts[i], verts[k]) is not None: for j in range(n): # verify that edge (k,j) exists in the partial closure if i != j != k and closure.get_edge(verts[k], verts[j]) is not None: # if (i,j) not yet included, add it to the closure if closure.get_edge(verts[i], verts[j]) is None: closure.insert_edge(verts[i], verts[j]) return closure if __name__ == '__main__': from graph_examples import figure_14_11 as example g = example() print("Number of vertices is", g.vertex_count()) print("Number of edges is", g.edge_count()) closure = floyd_warshall(g) print("Number of edges in closure is", closure.edge_count())