Exemple #1
0
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 test_1(prt=sys.stdout):
  adjtxtblk = """
    A:  E B 
    B:  E A F 
    C:  D F 
    D:  C G H 
    E:  A B 
    F:  C G B 
    G:  D H F 
    H:  G D """
  # Convert adjacency list in text block to array, fmt=[V, E, edge_pairs]
  g = Graph(adjtxt=adjtxtblk)
  prt.write("{}\n".format(g))
  g.wr_png("Graph_test_1.png")
Exemple #4
0
def test_0(prt=sys.stdout):
    txtblk = """
    A:  F B E 
    B:  F A 
    C:  G F 
    D:  H G 
    E:  A 
    F:  G A B C 
    G:  F C D 
    H:  D 
  """
    G = Graph(adjtxt=txtblk)
    dfs = DepthFirstPaths(G, 'A', prt)
    prt.write("\n{}\n".format(G))
def test_1(prt=sys.stdout):
    """Test BFS using Graph from text-block represented with letters."""
    txtblk = """
    A:  E B 
    B:  E A F 
    C:  D F 
    D:  C G H 
    E:  A B 
    F:  C G B 
    G:  D H F 
    H:  G D 
  """
    prt.write("\ntest_1: BFS using Graph with letters\n")
    G = Graph(adjtxt=txtblk)
    bfs = BreadthFirstPaths(G, 'A', prt)
    prt.write("\n{}\n".format(G))
Exemple #6
0
def test_1(prt=sys.stdout):
    txtblk = """
    A:  F B 
    B:  F A 
    C:  G 
    D:  I E J H 
    E:  D J 
    F:  A B 
    G:  C 
    H:  I D 
    I:  D H J 
    J:  D I E 
  """
    G = Graph(adjtxt=txtblk)
    cc = CC(G)
    cc.prt_ids(prt)
    prt.write("\n{}\n".format(G))
Exemple #7
0
def test_1(prt=sys.stdout):
    """Test that Graph is implemented properly"""
    adjtxtblk = """
      A:  E B 
      B:  E A F 
      C:  D F 
      D:  C G H 
      E:  A B 
      F:  C G B 
      G:  D H F 
      H:  G D """
    # Convert adjacency list in text block to array, fmt=[V, E, edge_pairs]
    graph = Graph(adjtxt=adjtxtblk)
    assert graph.num_nodes == 8
    assert graph.num_edges == 10
    prt.write("{}\n".format(graph))
    graph.wr_png("Graph_test_1.png")
Exemple #8
0
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 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 run(a, prt=sys.stdout):
  g = Graph(a)
  prt.write("{}\n".format(g))
  g.wr_png("Graph_test_0.png")
Exemple #11
0
def _run(data, prt=sys.stdout):
    graph = Graph(data)
    prt.write("{}\n".format(graph))
    graph.wr_png("Graph_test_0.png")
    return graph