def test_depthfirstsearch(): """Unit tests DepthFirstSearch data type""" ## fin_graph = "mediumG.txt" fin_graph = "tinyG.txt" graph_array = cli_get_fin(join(TEST_DIR, fin_graph)) ## print(graph_array) graph = Graph(graph_array) # Run test from main in algs4/DepthFirstSearch.java # % java DepthFirstSearch tinyG.txt 0 # 0 1 2 3 4 5 6 # NOT connected # # % java DepthFirstSearch tinyG.txt 9 # 9 10 11 12 # NOT connected if len(sys.argv) == 1: results_0 = _run(graph, src_node=0) results_9 = _run(graph, src_node=9) if fin_graph == 'tinyG.txt': assert results_0 == [0, 1, 2, 3, 4, 5, 6] assert results_9 == [9, 10, 11, 12] # Run user tests from the command-line else: for src_node in sys.argv[1:]: if src_node.isdigit(): _run(graph, int(src_node))
def main(prt=sys.stdout): """Plot tiny graph""" fin_graphs = ['tinyG.txt', 'mediumG.txt'] for fin_graph in fin_graphs: graph = Graph(cli_get_fin(join(TEST_DIR, fin_graph))) prt.write("{}\n".format(graph)) fout_png = fin_graph.replace('txt', 'png') graph.wr_png(fout_png)
def cli(prt=sys.stdout): if len(sys.argv) == 1: import doctest doctest.testmod() else: # read in digraph from command-line argument digraph_txt = cli_get_fin(sys.argv[1]) # read in sources from command-line arguments sources = [int(s) for s in sys.argv[2:]] print "SSSS", sources # print out vertices reachable from sources reachable = test_main(digraph_txt, *sources) prt.write("{}\n".format(' '.join(str(r) for r in reachable)))
def main(prt=sys.stdout): L = len(sys.argv[1:]) g = cli_get_fin(sys.argv[1] if L != 0 else "../thirdparty/tinyCG.txt") G = Graph(g) prt.write(str(G)) s = int(sys.argv[2]) if L > 1 else 0 bfs = BreadthFirstPaths(G, s) for v in range(G.V()): if bfs.hasPathTo(v): prt.write("{} to {} ({}): ".format(s, v, bfs.distTo(v))) for x in reversed(bfs.pathTo(v)): if x == s: prt.write(str(x)) else: prt.write("-{}".format(x)) prt.write("\n") else: prt.write.printf("{} to {} (-): not connected\n".format(s, v))
def main(prt=sys.stdout): L = len(sys.argv[1:]) g = cli_get_fin(sys.argv[1] if L != 0 else "../thirdparty/tinyG.txt") G = Graph(g) cc = CC(G) # number of connected components M = cc.count() prt.write("{M} components\n".format(M=M)) # compute list of vertices in each connected component components = [cx.deque() for i in range(M)] for v in range(G.V()): components[cc.id(v)].append(v) # enqueue(v) # print results for i in range(M): for v in components[i]: prt.write("{v} ".format(v=v)) prt.write("\n")
def test_0(prt=sys.stdout): L = len(sys.argv[1:]) g = cli_get_fin(sys.argv[1] if L != 0 else "../thirdparty/tinyG.txt") G = Graph(g) cc = CC(G) # number of connected components M = cc.count() prt.write("{M} components\n".format(M=M)) # compute list of vertices in each connected component components = [cx.deque() for i in range(M)] for v in range(G.V()): components[cc.id(v)].append(v) # enqueue(v) # print results for i in range(M): for v in components[i]: prt.write("{v} ".format(v=v)) prt.write("\n")
def main(prt=sys.stdout): """Plot tiny graph""" fin_graphs = OrderedDict([ ('tinyG.txt', Graph), ('mediumG.txt', Graph), ('tinyDG.txt', Digraph), ('tinyDG2.txt', Digraph), ('tinyDAG.txt', Digraph), ]) for fin_graph, cls in fin_graphs.items(): graph = cls(cli_get_fin(join(TEST_DIR, fin_graph))) prt.write("Class, {CLS}, has {N} vertices\n".format( CLS=type(graph).__name__, N=graph.V())) if graph.V() < 30: prt.write("{}\n".format(graph)) fout_png = join(TEST_DIR, 'images', fin_graph.replace('txt', 'png')) graph.wr_png(fout_png)
def test_main(digraph, *sources): """Determine single-source or multiple-source reachability in a digraph using depth first search. Runs in O(E + V) time. >>> test_main("tinyDG.txt", 1) [1] >>> test_main("tinyDG.txt", 2) [0, 1, 2, 3, 4, 5] >>> test_main("tinyDG.txt", 1, 2, 6) [0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12] """ if isinstance(digraph, str): digraph_fin = join(DIR_TEST, digraph) digraph_arr = cli_get_fin(digraph_fin) grph = Digraph(digraph_arr) dfs = DirectedDFS(grph, sources) # multiple-source reachability return [v for v in grph.keys if dfs.marked(v)]
def test_0(prt=sys.stdout): """Test BFS using Graph from file represented with ints.""" prt.write("\ntest_0: BFS using Graph with ints\n") L = len(sys.argv[1:]) g = cli_get_fin(sys.argv[1] if L != 0 else join(TEST_DIR, "tinyCG.txt")) G = Graph(g) prt.write(str(G)) s = int(sys.argv[2]) if L > 1 else 0 bfs = BreadthFirstPaths(G, s) for v in range(G.V()): if bfs.hasPathTo(v): prt.write("{} to {} ({}): ".format(s, v, bfs.distTo(v))) for x in reversed(bfs.pathTo(v)): if x == s: prt.write(str(x)) else: prt.write("-{}".format(x)) prt.write("\n") else: prt.write.printf("{} to {} (-): not connected\n".format(s, v))
def test_0(prt=sys.stdout): run(cli_get_fin("../thirdparty/tinyG.txt"))
#!/usr/bin/env python from AlgsSedgewickWayne.Graph import Graph from AlgsSedgewickWayne.testcode.InputArgs import cli_get_fin import sys def run(a, prt=sys.stdout): g = Graph(a) prt.write("{}\n".format(g)) g.wr_png("Graph.png") if __name__ == '__main__': run(cli_get_fin("../thirdparty/tinyG.txt"))
def main(prt=sys.stdout): g = cli_get_fin(sys.argv[1] if len(sys.argv) != 1 else "../thirdparty/tinyDG.txt") print "GGGGGG", g G = Digraph(g) prt.write("{}\n".format(G))
def test_lecture(prt=sys.stdout): """Plot tinyDG2.txt from 'Diagraph Search' Coursera Algs2 lecture.""" g = cli_get_fin("../thirdparty/tinyDG2.txt") G = Digraph(g) prt.write("{}\n".format(G)) G.wr_png("Digraph_lecture.png", prt)
def test_main(prt=sys.stdout): g = cli_get_fin( sys.argv[1] if len(sys.argv) != 1 else join(TEST_DIR, "tinyDG.txt")) G = Digraph(g) prt.write("{}\n".format(G))
def test_main(prt=sys.stdout): g = cli_get_fin( sys.argv[1] if len(sys.argv) != 1 else "../thirdparty/tinyDG.txt") G = Digraph(g) prt.write("{}\n".format(G))
def test_0(): """Test reading tiny Graph""" graph = _run(cli_get_fin(join(TEST_DIR, "tinyG.txt"))) assert graph.num_nodes == 13 assert graph.num_edges == 13