#------------------------------------------------------------------------------ # DigraphTest.py # Test the DirectedGraph class. #------------------------------------------------------------------------------ from graphs import * def main(): V = [1,2,3,4,5,6,7,8] E = [(1,2),(1,4),(2,3),(3,2),(3,5),(4,4),(5,2),(5,4),(6,3),(6,5),(6,8),(7,6),(8,7)] G = DirectedGraph(V,E) print() print(G.vertices) print(G.edges) print() print(G) # test in_degree(), out_degree() print() print('vertex in-degree out-degree') print('---------------------------------') for v in G.vertices: print(f' {v:<9} {G.in_degree(v):<12} {G.out_degree(v)}') # end print() # test reachable() print(f'vertices reachable from 1: {G.reachable(1)}') print(f'vertices reachable from 7: {G.reachable(7)}') print() # test findStrongComponents and print strong component labels n = G.findStrongComponents() print('number of strongly connected components = ', n) print() print('vertex strong component') print('---------------------------') for x in G.vertices: print(f' {x} {G.getStrongComponent(x)}') # end # test sameStrongComponent() print() print(f'{2} <--> {8} is {G.sameStrongComponent(2, 8)}') print() # test BFS() and getPath() G.BFS(1) print('vertex distance predecessor') print('----------------------------------') for v in G.vertices: print(f' {v:<9} {G.getDistance(v):<12} {G.getPredecessor(v)}') # end print() L = list() G.getPath(1, 5, L) print(L) print() L = list() G.getPath(1, 8, L) print(L) print() # end #------------------------------------------------------------------------------ if __name__=='__main__': main() # end