def test_parallel_edges():
    # Prepare graph, two parallel edges from 0 to 1
    g = DirectedGraph()
    u = add_vertex(g)
    v = add_vertex(g)
    (e1, added) = add_edge(u, v, g)
    assert added
    (e2, added) = add_edge(u, v, g)
    assert added
    w = 1
    map_eweight = {
        e1: w,
        e2: w,
    }

    # Call Dijkstra
    map_vpreds = defaultdict(set)
    map_vdist = dict()
    dijkstra_shortest_paths(g, u, make_assoc_property_map(map_eweight),
                            make_assoc_property_map(map_vpreds),
                            make_assoc_property_map(map_vdist))

    # Check
    assert map_vpreds == {
        v: {e1, e2},
    }

    assert map_vdist == {u: 0, v: w}
def test_simple_graph():
    # Prepare graph, just a 0 -> 1 arc
    g = DirectedGraph()
    u = add_vertex(g)
    v = add_vertex(g)
    e, added = add_edge(u, v, g)
    assert added
    w = 1
    map_eweight = {
        e: w,
    }

    # Call Dijkstra
    map_vpreds = defaultdict(set)
    map_vdist = dict()
    dijkstra_shortest_paths(g, u, make_assoc_property_map(map_eweight),
                            make_assoc_property_map(map_vpreds),
                            make_assoc_property_map(map_vdist))

    # Check
    assert map_vpreds == {
        v: {e},
    }

    assert map_vdist == {u: 0, v: w}
Exemple #3
0
def test_undirected_graph():
    g = UndirectedGraph()

    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(v, u, 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("In-edges(%s) = %s" % (u, {e for e in in_edges(u, g)}))
    assert in_degree(u, g) == 3
    assert in_degree(v, g) == 3
    assert in_degree(
        w, g) == 2, "in_edges(%s) = %s" % (w, {u
                                               for u in in_edges(w, 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) == 3
    assert out_degree(w, g) == 2

    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) == 1
    assert in_degree(u, g) == 1
    assert in_degree(w, g) == 1
Exemple #4
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 #5
0
 def dup_vertex(self, u: int, g: Graph) -> int:
     u_dup = add_vertex(self.m_g_dup)
     self.m_pmap_vertices[u] = u_dup
     self.m_dup_vertices.add(u)
     if self.m_callback_dup_vertex:
         self.m_callback_dup_vertex(u, g, u_dup, self.m_g_dup)
     return u_dup
Exemple #6
0
def test_undirected_graph_add_vertex():
    g = make_g2()
    assert num_vertices(g) == 3
    assert num_edges(g) == 5

    # Add vertex x
    x = add_vertex(g)
    assert num_vertices(g) == 4

    # Add edge (v -> x)
    add_edge(v, x, g)
    assert num_edges(g) == 6
    assert out_degree(v, g) == 4
Exemple #7
0
 def on_vertex(self, line: str, u_alias: int, opts: str) -> int:
     u = add_vertex(self.m_g)
     self.m_aliases[u_alias] = u
     if self.on_install_vertex_property:
         l = re.findall(
             "\\w+=[^\\s]+",
             opts)  # do not split on "," it fails if coma are in 'value'
         for opt in l:
             key, value = opt.split("=")
             key = key.strip().strip(",")
             value = value.strip().strip(",")
             value = value.strip("\"'").lstrip("<").rstrip(">")
             self.on_install_vertex_property(u, self.m_g, key, value)
     return u
Exemple #8
0
def make_g1() -> UndirectedGraph:
    g1 = UndirectedGraph()
    add_vertex(g1)
    add_vertex(g1)
    add_vertex(g1)
    return g1
 def add_node(un, g, d):
     u = d.get(un)
     if not u:
         u = add_vertex(g)
         d[un] = u
     return u