def main(): G = get_random_graph() from graph_bfs import BFS print "Test graph BFS", BFS, '-'*20 print 'Start with node id=5 =>', BFS(G, 5) from graph_dfs import DFS print "Test graph DFS", DFS, '-'*20 print 'Start with node id=5 =>', DFS(G) from graph_toposort import topological_sort print "Test topological sort", topological_sort, '-'*20 print 'Sorted list =>', topological_sort(G) from graph_strongly_connected_component import SCC print "Test SCC", SCC, '-'*20 print 'SCC => done', SCC(G) from graph_mst_prim import MST_PRIM print "Test graph MST Prim", MST_PRIM, '-'*20 print 'Start with node 5 =>', MST_PRIM(G, 5) from graph_mst_kruskal import MST_KRUSKAL print "Test graph MST Kruskal", MST_KRUSKAL, '-'*20 print 'MST =>', len(G.edges()), len(MST_KRUSKAL(G)) pass
def SCC(G): G = DFS(G) L = [(u[0], u[1]['f']) for u in G.nodes(data=True)] L.sort(key=lambda x: -x[1]) L = [x[0] for x in L] H = transpose(G) # DFS the G^T in decreasing finish time for n in H.nodes(data=True): n[1]['c'] = 0 n[1]['d'] = float('inf') n[1]['f'] = 0 n[1]['p'] = None time = 0 for n in L: if H.node[n]['c'] == 0: DFS_VISIT(H, n) return H
def SCC(G): G = DFS(G) L = [ (u[0],u[1]['f']) for u in G.nodes(data=True) ] L.sort(key=lambda x: -x[1]) L = [ x[0] for x in L ] H = transpose(G) # DFS the G^T in decreasing finish time for n in H.nodes(data=True): n[1]['c'] = 0 n[1]['d'] = float('inf') n[1]['f'] = 0 n[1]['p'] = None time = 0 for n in L: if H.node[n]['c'] == 0: DFS_VISIT(H, n) return H
def topological_sort(G): G = DFS(G) L = [(u[0], u[1]['f']) for u in G.nodes(data=True)] L.sort(key=lambda x: -x[1]) return [x[0] for x in L]
def topological_sort(G): G = DFS(G) L = [ (u[0],u[1]['f']) for u in G.nodes(data=True) ] L.sort(key=lambda x: -x[1]) return [ x[0] for x in L ]