def test_directed_graph():
    g = DirectedGraph()

    assert num_vertices(g) == 0
    u = add_vertex(g)
    assert num_vertices(g) == 1
    v = add_vertex(g)
    assert num_vertices(g) == 2
    w = add_vertex(g)
    assert num_vertices(g) == 3

    assert num_edges(g) == 0
    e_uv, _ = add_edge(u, v, g)
    assert num_edges(g) == 1
    e_uv1, _ = add_edge(u, v, g)
    assert num_edges(g) == 2
    e_uw, _ = add_edge(u, w, g)
    assert num_edges(g) == 3
    e_vw, _ = add_edge(v, w, g)
    assert num_edges(g) == 4

    print("Edges = %s" % {e for e in edges(g)})

    print("Out-edges(%s) = %s" % (u, {e for e in out_edges(u, g)}))
    assert out_degree(u, g) == 3
    assert out_degree(v, g) == 1
    assert out_degree(w, g) == 0

    print("Removing %s" % e_uv)
    remove_edge(e_uv, g)
    assert num_edges(g) == 3

    print("Removing %s" % e_uv1)
    remove_edge(e_uv1, g)
    assert num_edges(g) == 2

    print("Removing %s" % v)
    remove_vertex(v, g)
    assert num_vertices(g) == 2

    print("Edges = %s" % {e for e in edges(g)})
    assert num_edges(g) == 1

    print("Out-edges(%s) = %s" % (u, {e for e in out_edges(u, g)}))
    assert out_degree(u, g) == 1
    assert out_degree(w, g) == 0

    print(g.to_dot())
Exemple #2
0
def test_cut_diamond():
    g = DirectedGraph(4)
    (e01, _) = add_edge(0, 1, g)
    (e02, _) = add_edge(0, 2, g)
    (e13, _) = add_edge(1, 3, g)
    (e23, _) = add_edge(2, 3, g)

    obtained = cut(0, g, lambda e, g: e in {e01, e02})
    assert obtained == {1, 2}

    obtained = cut(0, g, lambda e, g: e in {e13, e23})
    assert obtained == {3}
Exemple #3
0
 def make_graph(edges_set: set) -> DirectedGraph:
     g = DirectedGraph()
     n = 0
     for (u, v) in edges_set:
         assert u >= 0
         while n <= u:
             add_vertex(g)
             n += 1
         while n <= v:
             add_vertex(g)
             n += 1
         add_edge(u, v, g)
     return g
Exemple #4
0
def test_cut():
    g = DirectedGraph(3)
    (e01, _) = add_edge(0, 1, g)
    (e12, _) = add_edge(1, 2, g)

    obtained = cut(0, g, lambda e, g: e in {e01})
    assert obtained == {1}

    obtained = cut(0, g, lambda e, g: e in {e12})
    assert obtained == {2}

    obtained = cut(0, g, lambda e, g: False)
    assert obtained == {2}

    obtained = cut(0, g, lambda e, g: True)
    assert obtained == {1}
Exemple #5
0
def test_topological_sort():
    g = DirectedGraph(6)
    for (u, v) in [(0, 1), (2, 4), (2, 5), (0, 3), (1, 4), (4, 3)]:
        add_edge(u, v, g)
    l = topological_sort(g)
    assert list(l) == [2, 5, 0, 1, 4, 3]
Exemple #6
0
def make_binary_tree(n=10):
    g = DirectedGraph(n)
    for u in range(n - 1):
        add_edge(int(u / 2), u + 1, g)
    return g