示例#1
0
    elif action == 'DFSpath':
        source = args['source']
        graph = Graph.from_file(fname)
        paths = DepthFirstPaths(graph, source)
        for v in graph.vertices():
            if paths.has_path(v):
                print("%d to %d: " % (source, v) +
                      " -> ".join([str(i) for i in paths.path_to(v)]))
            else:
                print("%d to %d: not connected" % (source, v))

    # Shortest paths with BFS
    elif action == 'BFSpath':
        source = args['source']
        graph = Graph.from_file(fname)
        paths = BreadthFirstSearch(graph, source)
        for v in graph.vertices():
            if paths.has_path(v):
                print("%d to %d: " % (source, v) +
                      " -> ".join([str(i) for i in paths.path_to(v)]))
            else:
                print("%d to %d: not connected" % (source, v))

    # Connected components
    elif action == 'CC':
        graph = Graph.from_file(fname)
        cc = ConnectedComponents(graph)
        print("%d components" % cc.count())
        for c, nodes in enumerate(cc.components()):
            print("%d: %s" % (c, " ".join([str(v) for v in nodes])))
示例#2
0
 def test_compute_resilience(self):
     longest_ccc_sizes = BreadthFirstSearch.compute_resilience(
         EX_GRAPH1, [1, 2])
     self.assertEqual(longest_ccc_sizes, [4, 2, 1])
示例#3
0
        print(" ".join([str(v) for v in dfs_order.reverse_post()]))

    elif action == 'topological':
        delim = args['delim']
        sg = SymbolDigraph.from_file(fname, sep=delim)
        topological = TopologicalSort(sg.graph)
        for v in topological.order:
            print(sg.name(v))

    elif action == 'strongly_connected':
        delim = args['delim']
        sg = SymbolDigraph.from_file(fname, sep=delim)
        scc = KosarajuSharirSCC(sg.graph)
        for v in sorted([sg.name(x) for x in sg.graph.vertices()]):
            print("%s: %d" % (v, scc.ids[sg.int(v)]))

    elif action == 'BFSpath':
        source = args['source']
        graph = Digraph.from_file(fname)
        paths = BreadthFirstSearch(graph, source)
        for v in graph.vertices():
            if paths.has_path(v):
                print("%d to %d: " % (source, v) +
                      " -> ".join([str(i) for i in paths.path_to(v)]))
            else:
                print("%d to %d: not connected" % (source, v))

    if action == 'graph':
        graph = Digraph.from_file(fname)
        print(graph)
示例#4
0
 def test_cc_visited(self):
     giraffe = BreadthFirstSearch.cc_visited(EX_GRAPH0)
     self.assertEqual(len(giraffe), 3)
     self.assertTrue(set([4, 5, 6, 7, 8]) in giraffe)
示例#5
0
 def test_largest_cc_size(self):
     self.assertTrue(BreadthFirstSearch.largest_cc_size(EX_GRAPH0), 5)
     self.assertTrue(BreadthFirstSearch.largest_cc_size(EX_GRAPH1), 4)
示例#6
0
 def test_bfs_visited(self):
     giraffe = BreadthFirstSearch.bfs_visited(EX_GRAPH0, 0)
     self.assertEqual(len(giraffe), 3)
     self.assertTrue(0 in giraffe)
     self.assertTrue(1 in giraffe)
     self.assertTrue(0 in giraffe)
示例#7
0
    3: [0, 6],
    6: [3, 7],
    7: [6, 8],
    8: [7]
}


def evaluate(correct_solution, solution):
    if (correct_solution == solution):
        return "Test Case Passed"
    return "Test Case Failed"


# Testing for Breadth First Search Algorithm
print("--- Testing BFS ---")
BFS = BreadthFirstSearch(graph, 2)
solution, closed_set = BFS.fit()
print(solution, closed_set)
print(str(evaluate([2, 0, 3, 1], solution)))

BFS = BreadthFirstSearch(tree, 0)
solution, closed_set = BFS.fit()
print(solution, closed_set)
print(str(evaluate([0, 1, 2, 3, 4, 5], solution)))

BFS = BreadthFirstSearch(tree, 0, 2)
solution, closed_set = BFS.fit()
print(solution, closed_set)
print(str(evaluate([0, 1, 2], solution)))

BFS = BreadthFirstSearch(tree_string, "0")
示例#8
0
def spyVsSpyShortestPath(mapGraph=None, airportVertex=None, spy1Vertex=None, spy2Vertex=None):
    print("Spy 1 shortest path: {}.".format(mapGraph.iterate(spy1Vertex, BreadthFirstSearch()).getShortestPath(airportVertex)))
    print("Spy 2 shortest path: {}.".format(mapGraph.iterate(spy2Vertex, BreadthFirstSearch()).getShortestPath(airportVertex)))
示例#9
0
def spyVsSpy(mapGraph=None, airportVertex=None, spy1Vertex=None, spy2Vertex=None):
    print("Spy 1 arrives in {} steps.".format(mapGraph.iterate(spy1Vertex, BreadthFirstSearch()).getLevel(airportVertex)))
    print("Spy 2 arrives in {} steps.".format(mapGraph.iterate(spy2Vertex, BreadthFirstSearch()).getLevel(airportVertex)))
示例#10
0
        print(" ".join([str(v) for v in dfs_order.reverse_post()]))

    elif action == 'topological':
        delim = args['delim']
        sg = SymbolDigraph.from_file(fname, sep=delim)
        topological = TopologicalSort(sg.graph)
        for v in topological.order:
            print(sg.name(v))

    elif action == 'strongly_connected':
        delim = args['delim']
        sg = SymbolDigraph.from_file(fname, sep=delim)
        scc = KosarajuSharirSCC(sg.graph)
        for v in sorted([sg.name(x) for x in sg.graph.vertices()]):
            print("%s: %d" % (v, scc.ids[sg.int(v)]))

    elif action == 'BFSpath':
        source = args['source']
        graph = Digraph.from_file(fname)
        paths = BreadthFirstSearch(graph, source)
        for v in graph.vertices():
            if paths.has_path(v):
                print("%d to %d: " % (source, v) +
                      " -> ".join([str(i) for i in paths.path_to(v)]))
            else:
                print("%d to %d: not connected" % (source, v))

    if action == 'graph':
        graph = Digraph.from_file(fname)
        print(graph)