from adjList import AdjList

if __name__ == '__main__':
    
    print('Undirected Graph:')
    undirected = AdjList(False)
    for vertex in ['r','s','t','u','v','w','x','y']:
        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'))
Exemple #2
0
from adjList import AdjList
    
if __name__ == '__main__':
    
    print('Directed Cycle:')
    cycle = AdjList(True)
    for vertex in ['r','s','t','u']:
        cycle.addVertex(vertex)
    for source,dest in [('r','s'),('s','t'),('t','r'),('t','u')]:
        cycle.addEdge(source,dest)
        
    print(cycle)
    print('BFS:\n%s'%cycle.traverseBFS('s'))
    print('\nDFS: %s\n'%cycle.traverseDFS())
    
    print('Graph is a DAG: %s'%cycle.isDAG())
    print('Topological Sort: %s'%cycle.topologicalSort())
    print('Strongly Connected Components: %s\n'%cycle.stronglyConnectedComponents())
    
from adjList import AdjList

if __name__ == '__main__':

    print('DAG:')
    dag = AdjList(True)
    for vertex in ['r','s','t','u','v','w','x','y']:
        dag.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')]:
        dag.addEdge(source,dest)
    
    print(dag)
    print('BFS:\n%s'%dag.traverseBFS('s'))
    print('\nDFS: %s'%dag.traverseDFS())
    
    print('Topological Sort: %s'%dag.topologicalSort())
    transpose = dag.computeTranspose()
    print('\nTranspose of dag:\n%s'%transpose)
    print('Strongly Connected Components: %s\n'%dag.stronglyConnectedComponents())
    
    print('\nTesting Copy:\n')
    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())