undirected.addVertex(vertex) for source,dest in [('r','s'),('r','v'),('s','w'),('w','t'),('w','x'),('t','x'),('t','u'),('x','u'),('x','y'),('u','y')]: undirected.addEdge(source,dest) print(undirected) print('BFS:\n%s'%undirected.traverseBFS('s')) print('DFS:%s\n'%undirected.traverseDFS()) print('\nTesting Copy Undirected:') copy = undirected.copyGraph() print(copy) print('BFS:\n%s'%copy.traverseBFS('s')) print('\nDFS:%s\n'%copy.traverseDFS()) print('\nAfter Delete Vertex r:') undirected.removeVertex('r') print(undirected) print('BFS:\n%s'%undirected.traverseBFS('s')) print('\nDFS:%s\n'%undirected.traverseDFS()) print('\nAfter Delete Edge x->u:') undirected.removeEdge('x','u') print(undirected) print('BFS:\n%s'%undirected.traverseBFS('s')) print('\nDFS:%s\n'%undirected.traverseDFS()) print('Shortest Path v->y: %s'%undirected.shortestPath('v','y')) print('Shortest Path s->y: %s'%undirected.shortestPath('s','y')) undirected.removeEdge('x','y') print('Shortest Path s->y after delete edge x->y: %s'%undirected.shortestPath('s','y'))
copy = dag.copyGraph() print(copy) print('BFS:\n%s'%copy.traverseBFS('s')) print('\nDFS:%s\n'%copy.traverseDFS()) print('Graph is a DAG: %s\n'%copy.isDAG()) print('Copy Graph after removing vertex "v"') copy.removeVertex('v') print(copy) print('DFS:%s\n'%copy.traverseDFS()) print('Compared to original graph:') print(dag) print('After Delete Vertex r:') dag.removeVertex('r') print(dag) print('BFS:\n%s'%dag.traverseBFS('s')) print('\nDFS:%s\n'%dag.traverseDFS()) print('\nAfter Delete Edge x->u:') dag.removeEdge('x','u') print(dag) print('BFS:\n%s'%dag.traverseBFS('s')) print('\nDFS:%s\n'%dag.traverseDFS()) print('Shortest Path v->y: %s'%dag.shortestPath('v','y')) print('Shortest Path s->y: %s'%dag.shortestPath('s','y')) dag.removeEdge('x','y')