def test_is_connected(): # directed g = create_graph(directed=True, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True) g.add_vertices_from([0, 1]) g.create_edge(0, 1) is_connected, components = connectivity.is_connected(g) assert not is_connected g.create_edge(1, 0) is_connected, components = connectivity.is_connected(g) assert is_connected # undirected g = create_graph(directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True) g.add_vertices_from([0, 1]) g.create_edge(0, 1) is_connected, components = connectivity.is_connected(g) assert is_connected
def test_iso_no(): g1 = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=False, ) g1.add_vertices_from([0, 1, 2, 3]) g1.add_edge(0, 1) g1.add_edge(1, 2) g1.add_edge(2, 3) g1.add_edge(3, 0) g2 = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=False, ) g2.add_vertices_from([5, 6, 7]) g2.add_edge(5, 6) g2.add_edge(6, 7) g2.add_edge(7, 5) it = iso.vf2(g1, g2) assert it is None
def test_any_graph_of_graphs(): g1 = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=True, weighted=True, ) g1.add_vertex(0) g1.add_vertex(1) g1.add_edge(0, 1) g2 = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=True, weighted=True, ) g2.add_vertex(2) g2.add_vertex(3) g2.add_edge(2, 3) g3 = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=True, weighted=True, ) g3.add_vertex(4) g3.add_vertex(5) g3.add_edge(4, 5) # create the graph of graphs g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=True, weighted=True, any_hashable=True, ) g.add_vertex(g1) g.add_vertex(g2) g.add_edge(g1, g2, edge=g3) assert ( str(g) == "({({0, 1}, {0=(0,1)}), ({2, 3}, {0=(2,3)})}, {({4, 5}, {0=(4,5)})=(({0, 1}, {0=(0,1)}),({2, 3}, {0=(2,3)}))})" ) assert g.contains_vertex(g1) assert g.contains_vertex(g2) assert g.contains_edge(g3) assert len(g.vertices) == 2 assert len(g.edges) == 1 assert g.edge_source(g3) == g1 assert g.edge_target(g3) == g2
def test_complement(): g_source = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, ) g_source.add_vertex(0) g_source.add_vertex(1) g_source.add_vertex(2) g_source.add_edge(0, 1) g_source.add_edge(0, 2) g = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, ) generators.complement_graph(g, g_source) assert g.vertices == {0, 1, 2} assert g.edges == {0} assert g.edge_tuple(0) == (1, 2, 1.0)
def test_iso(): g1 = create_graph(directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=False) g1.add_vertices_from([0, 1, 2, 3]) g1.create_edge(0, 1) g1.create_edge(1, 2) g1.create_edge(2, 3) g1.create_edge(3, 0) g2 = create_graph(directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=False) g2.add_vertices_from([5, 6, 7, 8]) g2.create_edge(5, 6) g2.create_edge(6, 7) g2.create_edge(7, 8) g2.create_edge(8, 5) it = iso.vf2(g1, g2) assert it is not None gm = next(it) assert gm.vertices_correspondence() == {0: 5, 1: 6, 2: 7, 3: 8} assert gm.vertices_correspondence(forward=False) == { 5: 0, 6: 1, 7: 2, 8: 3 } assert gm.edges_correspondence() == {0: 0, 1: 1, 2: 2, 3: 3} gm = next(it) assert gm.vertices_correspondence() == {0: 5, 1: 8, 2: 7, 3: 6} assert gm.edges_correspondence() == {0: 3, 1: 2, 2: 1, 3: 0} gm = next(it) assert gm.vertices_correspondence() == {0: 6, 1: 5, 2: 8, 3: 7} assert gm.edges_correspondence() == {0: 0, 1: 3, 2: 2, 3: 1} gm = next(it) assert gm.vertices_correspondence() == {0: 8, 1: 5, 2: 6, 3: 7} assert gm.edges_correspondence() == {0: 3, 1: 0, 2: 1, 3: 2} gm = next(it) assert gm.vertices_correspondence() == {0: 7, 1: 6, 2: 5, 3: 8} assert gm.edges_correspondence() == {0: 1, 1: 0, 2: 3, 3: 2} gm = next(it) assert gm.vertices_correspondence() == {0: 7, 1: 8, 2: 5, 3: 6} assert gm.edges_correspondence() == {0: 2, 1: 3, 2: 0, 3: 1} gm = next(it) assert gm.vertices_correspondence() == {0: 6, 1: 7, 2: 8, 3: 5} assert gm.edges_correspondence() == {0: 1, 1: 2, 2: 3, 3: 0} gm = next(it) assert gm.vertices_correspondence() == {0: 8, 1: 7, 2: 6, 3: 5} assert gm.edges_correspondence() == {0: 2, 1: 1, 2: 0, 3: 3}
def test_anyhashableg_dag(): g = create_graph(allowing_multiple_edges=True, weighted=True, dag=True, any_hashable=True) assert g.type.directed assert not g.type.undirected assert g.type.allowing_multiple_edges assert not g.type.allowing_self_loops assert g.type.weighted assert not g.type._allowing_cycles for i in range(0,11): g.add_vertex(str(i)) g.add_edge("0", "1") g.add_edge("1", "3") g.add_edge("0", "2") g.add_edge("2", "4") g.add_edge("3", "5") g.add_edge("4", "5") g.add_edge("5", "6") g.add_edge("5", "7") g.add_edge("6", "8") g.add_edge("7", "8") g.add_edge("9", "10") g.add_edge("10", "8") with pytest.raises(ValueError): g.add_edge("8", "1") topological_order = [v for v in g] assert topological_order == ["0", "1", "2", "3", "4", "5", "6", "7", "9", "10", "8"] assert g.ancestors("5") == {"0", "1", "2", "3", "4"} assert g.descendants("5") == {"6", "7", "8"} assert g.descendants("9") == {"10", "8"} with pytest.raises(ValueError): g.descendants("unknown") with pytest.raises(ValueError): g.ancestors("unknown") g1 = create_graph(allowing_multiple_edges=False, weighted=False, dag=True, any_hashable=True) assert g1.type.directed assert not g1.type.undirected assert not g1.type.allowing_multiple_edges assert not g1.type.allowing_self_loops assert not g1.type.weighted assert not g1.type._allowing_cycles g1.add_vertices_from(["0", "1"]) g1.add_edge("0", "1") with pytest.raises(ValueError): g1.add_edge("0", "1")
def test_export_import(tmpdir): g = create_graph( directed=True, allowing_self_loops=False, allowing_multiple_edges=True, weighted=False, ) for i in range(0, 10): g.add_vertex(i) g.add_edge(0, 1) g.add_edge(0, 2) g.add_edge(0, 3) g.add_edge(0, 4) g.add_edge(0, 5) g.add_edge(0, 6) g.add_edge(0, 7) g.add_edge(0, 8) g.add_edge(0, 9) g.add_edge(1, 2) g.add_edge(2, 3) g.add_edge(3, 4) g.add_edge(4, 5) g.add_edge(5, 6) g.add_edge(6, 7) g.add_edge(7, 8) g.add_edge(8, 9) g.add_edge(9, 1) assert len(g.edges) == 18 tmpfile = tmpdir.join("csv.out") tmpfilename = str(tmpfile) write_csv(g, tmpfilename) # read back g1 = create_graph( directed=True, allowing_self_loops=False, allowing_multiple_edges=True, weighted=False, ) read_csv(g1, tmpfilename) assert g1.vertices == set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) assert g1.contains_edge_between(6, 7) assert not g1.contains_edge_between(6, 8) assert len(g1.edges) == 18
def test_dag(): g = create_graph(allowing_multiple_edges=True, weighted=True, dag=True) assert g.type.directed assert not g.type.undirected assert g.type.allowing_multiple_edges assert not g.type.allowing_self_loops assert g.type.weighted assert not g.type._allowing_cycles g.add_vertices_from(range(0, 11)) g.add_edge(0, 1) g.add_edge(1, 3) g.add_edge(0, 2) g.add_edge(2, 4) g.add_edge(3, 5) g.add_edge(4, 5) g.add_edge(5, 6) g.add_edge(5, 7) g.add_edge(6, 8) g.add_edge(7, 8) g.add_edge(9, 10) g.add_edge(10, 8) with pytest.raises(ValueError): g.add_edge(8, 1) topological_order = [v for v in g] assert topological_order == [0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 8] assert g.ancestors(5) == {0, 1, 2, 3, 4} assert g.descendants(5) == {6, 7, 8} assert g.descendants(9) == {10, 8} with pytest.raises(ValueError): g.descendants(50) g1 = create_graph(allowing_multiple_edges=False, weighted=False, dag=True) assert g1.type.directed assert not g1.type.undirected assert not g1.type.allowing_multiple_edges assert not g1.type.allowing_self_loops assert not g1.type.weighted assert not g1.type._allowing_cycles g1.add_vertices_from([0, 1]) g1.add_edge(0, 1) with pytest.raises(ValueError): g1.add_edge(0, 1)
def test_union_with_combiner(): g1 = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g2 = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) def max_weight_combiner(x, y): return max(x, y) g = as_graph_union(g1, g2, edge_weight_combiner_cb=max_weight_combiner) assert g.type.directed assert g.type.allowing_multiple_edges assert not g.type.modifiable g1.add_vertex(0) g1.add_vertex(1) g1.add_vertex(2) g1.add_vertex(3) g1.add_edge(2, 3, weight=7.0, edge=0) g1.add_edge(0, 1, weight=5.0, edge=1) g1.add_edge(1, 2, weight=6.0, edge=2) g2.add_vertex(2) g2.add_vertex(3) g2.add_vertex(4) g2.add_vertex(5) g2.add_vertex(6) g2.add_edge(3, 2, weight=8.0, edge=0) g2.add_edge(3, 4, weight=9.0, edge=1) g2.add_edge(4, 5, weight=3.0, edge=2) g2.add_edge(5, 6, weight=10.0, edge=3) assert g.vertices == {0, 1, 2, 3, 4, 5, 6} assert g.edges == {0, 1, 2, 3} assert g.edge_tuple(0) == (2, 3, 8.0) assert g.edge_tuple(1) == (0, 1, 9.0) assert g.edge_tuple(2) == (1, 2, 6.0) assert g.edge_tuple(3) == (5, 6, 10.0)
def test_anyhashableg_iso_induced_subgraph(): g1 = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=False, any_hashable=True, ) g1.add_vertices_from([0, 1, 2, 3]) g1.add_edge(0, 1) g1.add_edge(1, 2) g1.add_edge(2, 3) g1.add_edge(3, 0) g2 = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=False, any_hashable=True, ) g2.add_vertices_from([5, 6, 7]) g2.add_edge(5, 6) g2.add_edge(6, 7) it = iso.vf2_subgraph(g1, g2) assert it is not None gm = next(it) assert gm.vertices_correspondence() == {0: 5, 1: 6, 2: 7, 3: None} gm = next(it) assert gm.vertices_correspondence() == {0: 5, 1: None, 2: 7, 3: 6} gm = next(it) assert gm.vertices_correspondence() == {0: 6, 1: 5, 2: None, 3: 7} gm = next(it) assert gm.vertices_correspondence() == {0: None, 1: 5, 2: 6, 3: 7} gm = next(it) assert gm.vertices_correspondence() == {0: 7, 1: 6, 2: 5, 3: None} gm = next(it) assert gm.vertices_correspondence() == {0: 7, 1: None, 2: 5, 3: 6} gm = next(it) assert gm.vertices_correspondence() == {0: 6, 1: 7, 2: None, 3: 5} gm = next(it) assert gm.vertices_correspondence() == {0: None, 1: 7, 2: 6, 3: 5}
def test_union(): g1 = create_graph( directed=False, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g2 = create_graph( directed=False, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g = as_graph_union(g1, g2) assert not g.type.directed assert g.type.allowing_multiple_edges assert not g.type.modifiable g1.add_vertex(0) g1.add_vertex(1) g1.add_vertex(2) g1.add_vertex(3) g1.add_edge(2, 3, weight=7.0, edge=0) g1.add_edge(0, 1, weight=5.0, edge=1) g1.add_edge(1, 2, weight=6.0, edge=2) g2.add_vertex(2) g2.add_vertex(3) g2.add_vertex(4) g2.add_vertex(5) g2.add_vertex(6) g2.add_edge(2, 3, weight=8.0, edge=0) g2.add_edge(3, 4, weight=9.0, edge=1) g2.add_edge(4, 5, weight=9.0, edge=2) g2.add_edge(5, 6, weight=10.0, edge=3) assert g.vertices == {0, 1, 2, 3, 4, 5, 6} assert g.edges == {0, 1, 2, 3} assert g.edge_tuple(0) == (2, 3, 15.0) assert g.edge_tuple(1) == (0, 1, 14.0) assert g.edge_tuple(2) == (1, 2, 15.0) assert g.edge_tuple(3) == (5, 6, 10.0)
def test_anyhashableg_as_weighted_with_caching_and_write_throught_with_unweighted( ): g = create_graph( directed=False, allowing_self_loops=True, allowing_multiple_edges=False, weighted=False, any_hashable=True, ) g.add_vertex(0) g.add_vertex(1) g.add_edge(0, 1, edge=0) with pytest.raises(ValueError): g.set_edge_weight(0, 5.0) with pytest.raises(ValueError): g.edge_attrs[0]['weight'] = 5.0 assert g.get_edge_weight(0) == 1.0 def edge_weight(e): return 100.5 with pytest.raises(ValueError): wg = as_weighted(g, edge_weight, cache_weights=True, write_weights_through=True)
def test_read_csv_property_graph_from_string(): g = create_graph(directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, any_hashable=True, vertex_supplier=create_vertex_supplier(), edge_supplier=create_edge_supplier()) input_string = """1,2 2,3 3,4 4,1 """ def import_id_cb(id): return 'v{}'.format(int(id) + 1) parse_csv(g, input_string, import_id_cb=import_id_cb) print(g.vertices) assert g.vertices == {'v2', 'v3', 'v4', 'v5'} assert g.edge_tuple('e2') == ('v4', 'v5', 1.0) assert g.vertex_attrs == {} assert g.edge_attrs == {}
def test_as_unmodifiable(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g.add_vertex(0) v1 = 0 g.add_vertex(1) v2 = 1 g.add_vertex(2) v3 = 2 g.add_vertex(3) v4 = 3 g.add_vertex(4) v5 = 4 g.add_edge(v1, v2) g.add_edge(v2, v3) g.add_edge(v1, v4) g.add_edge(v1, v1) g.add_edge(v4, v5) g.add_edge(v5, v1) # unmodifiable g3 = as_unmodifiable(g) assert g3.type.modifiable is False with pytest.raises(ValueError): g3.add_edge(v2, v2)
def test_anyhashableg_as_weighted_with_None_function(): g = create_graph( directed=False, allowing_self_loops=True, allowing_multiple_edges=False, weighted=False, any_hashable=True, ) g.add_vertex(0) g.add_vertex(1) g.add_edge(0, 1, edge=0) with pytest.raises(ValueError): g.set_edge_weight(0, 5.0) assert g.get_edge_weight(0) == 1.0 def edge_weight(e): return 100.5 wg = as_weighted(g, edge_weight_cb=None, cache_weights=False, write_weights_through=False) assert wg.get_edge_weight(0) == 1.0 with pytest.raises(ValueError): wg.set_edge_weight(0, 5.0)
def test_as_edge_reversed(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g.add_vertex(0) v1 = 0 g.add_vertex(1) v2 = 1 g.add_vertex(2) v3 = 2 g.add_vertex(3) v4 = 3 g.add_vertex(4) v5 = 4 g.add_edge(v1, v2) g.add_edge(v2, v3) g.add_edge(v1, v4) g.add_edge(v1, v1) e45 = g.add_edge(v4, v5) g.add_edge(v5, v1) # edge reversed g4 = as_edge_reversed(g) assert g.edge_source(e45) == v4 assert g.edge_target(e45) == v5 assert g4.edge_source(e45) == v5 assert g4.edge_target(e45) == v4
def test_anyhashableg_as_weighted(): g = create_graph( directed=False, allowing_self_loops=True, allowing_multiple_edges=False, weighted=False, any_hashable=True, ) g.add_vertex('0') g.add_vertex('1') g.add_edge('0', '1', edge='0') with pytest.raises(ValueError): g.set_edge_weight('0', 5.0) assert g.get_edge_weight('0') == 1.0 def edge_weight(e): return 100.5 wg = as_weighted(g, edge_weight, cache_weights=False, write_weights_through=False) assert wg.get_edge_weight('0') == 100.5 print(g) print(wg) with pytest.raises(ValueError): wg.set_edge_weight('0', 5.0)
def test_anyhashableg_as_unweighted(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, any_hashable=True, ) g.add_vertex("0") g.add_vertex("1") g.add_edge("0", "1", edge="e1") g.set_edge_weight("e1", 100.0) assert g.get_edge_weight("e1") == 100.0 ug = as_unweighted(g) assert g.type.directed == ug.type.directed assert g.type.allowing_self_loops == ug.type.allowing_self_loops assert g.type.allowing_multiple_edges == ug.type.allowing_multiple_edges assert g.type.weighted != ug.type.weighted assert g.get_edge_weight("e1") == 100.0 assert ug.get_edge_weight("e1") == 1.0
def test_bipartite_max_cardinality(): g = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, ) for i in range(0, 6): g.add_vertex(i) e03 = g.add_edge(0, 3) e13 = g.add_edge(1, 3) g.add_edge(2, 3) e14 = g.add_edge(1, 4) e25 = g.add_edge(2, 5) g.set_edge_weight(e13, 15.0) weight, m = matching.bipartite_max_cardinality(g) assert weight == 3.0 assert set(m) == set([e03, e14, e25]) weight, m = matching.bipartite_max_weight(g) assert weight == 16.0 assert set(m) == set([e13, e25])
def test_as_undirected(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g.add_vertex(0) v1 = 0 g.add_vertex(1) v2 = 1 g.add_vertex(2) v3 = 2 g.add_vertex(3) v4 = 3 g.add_vertex(4) v5 = 4 g.add_edge(v1, v2) g.add_edge(v2, v3) g.add_edge(v1, v4) g.add_edge(v1, v1) g.add_edge(v4, v5) g.add_edge(v5, v1) # undirected g2 = as_undirected(g) assert g2.type.directed is False assert not g.contains_edge_between(2, 1) assert g2.contains_edge_between(2, 1)
def bfs(g, root): if not root in g.vertices: return None, None, None i = 0 Q = [] # Lista que representa a fila Q i = i + 1 visited = [root] # Lista com vértices que foram pintados l = {} l[root] = 0 t = {} t[root] = i Q = [root] tree = create_graph(directed=True, weighted=False, dag=True) tree.add_vertex(root) while Q != []: x = Q[0] # x é o vértice da cabeça de Q neighbors = [ g.opposite(e, x) for e in g.edges_of(x) if g.opposite(e, x) not in visited ] if neighbors != []: # x ainda tem vizinhos não pintados # escolhe um dos vizinhos y aleatoriamente y = neighbors[randint(0, len(neighbors) - 1)] i = i + 1 visited.append(y) tree.add_vertex(y) tree.add_edge(x, y) l[y] = l[x] + 1 t[y] = i Q.append(y) #adiciona y ao fim da fila else: Q.pop(0) # remove x da cabeça da fila Q return tree, l, t
def dfs(g, root): if not root in g.vertices: return None, None, None f = {} # função tempo de inclusão na árvore t = {} # função tempo de saída da pilha i = 0 S = [] # Lista que representa a pilha S i = i + 1 visited = [root] # Lista com vértices que foram pintados f[root] = i S.append(root) tree = create_graph(directed=True, weighted=False, dag=True) tree.add_vertex(root) while S != []: x = S[0] # x é o topo da pilha S neighbors = [ g.opposite(e, x) for e in g.edges_of(x) if g.opposite(e, x) not in visited ] i = i + 1 if neighbors != []: # escolhe um dos vizinhos y aleatoriamente y = neighbors[randint(0, len(neighbors) - 1)] visited.append(y) tree.add_vertex(y) tree.add_edge(x, y) f[y] = i S.insert(0, y) #adiciona y ao topo de S else: t[x] = i S.pop(0) #remove x do topo return tree, f, t
def test_as_weighted_with_no_caching_and_write_through(): g = create_graph( directed=False, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g.add_vertex(0) g.add_vertex(1) g.add_edge(0, 1) g.set_edge_weight(0, 5.0) assert g.get_edge_weight(0) == 5.0 def edge_weight(e): return 100.5 wg = as_weighted(g, edge_weight, cache_weights=False, write_weights_through=True) assert wg.get_edge_weight(0) == 100.5 with pytest.raises(ValueError): wg.set_edge_weight(0, 5.0) assert wg.get_edge_weight(0) == 100.5
def test_as_unweighted(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, ) g.add_vertex(0) v1 = 0 g.add_vertex(1) v2 = 1 g.add_vertex(2) v3 = 2 g.add_vertex(3) v4 = 3 g.add_vertex(4) v5 = 4 g.add_edge(v1, v2) g.add_edge(v2, v3) g.add_edge(v1, v4) g.add_edge(v1, v1) e45 = g.add_edge(v4, v5) g.add_edge(v5, v1) g.set_edge_weight(e45, 100.0) g1 = as_unweighted(g) assert g.type.directed == g1.type.directed assert g.type.allowing_self_loops == g1.type.allowing_self_loops assert g.type.allowing_multiple_edges == g1.type.allowing_multiple_edges assert g.type.weighted != g1.type.weighted assert g.get_edge_weight(e45) == 100.0 assert g1.get_edge_weight(e45) == 1.0
def test_read_csv_property_graph_from_file(tmpdir): tmpfile = tmpdir.join("csv.out") tmpfilename = str(tmpfile) input_string = """1,2 2,3 3,4 4,1 """ with open(tmpfilename, "w") as f: f.write(input_string) g = create_graph(directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, any_hashable=True, vertex_supplier=create_vertex_supplier(), edge_supplier=create_edge_supplier()) def import_id_cb(id): return 'v{}'.format(int(id) + 1) read_csv(g, tmpfilename, import_id_cb=import_id_cb) print(g.vertices) assert g.vertices == {'v2', 'v3', 'v4', 'v5'} assert g.edge_tuple('e2') == ('v4', 'v5', 1.0) assert g.vertex_attrs == {} assert g.edge_attrs == {}
def test_anyhashableg_as_undirected(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, any_hashable=True, ) g.add_vertex("0") g.add_vertex("1") g.add_vertex("2") g.add_edge("0", "1", edge="e1") g.add_edge("1", "2", edge="e2") g.edge_attrs['e1']['capacity'] = 5.0 g.edge_attrs['e2']['capacity'] = 15.0 assert not g.contains_edge_between("1", "0") ug = as_undirected(g) assert g.type.directed != ug.type.directed assert g.type.allowing_self_loops == ug.type.allowing_self_loops assert g.type.allowing_multiple_edges == ug.type.allowing_multiple_edges assert g.type.weighted == ug.type.weighted assert ug.contains_edge_between("1", "0") assert ug.edge_attrs['e1']['capacity'] == 5.0 assert ug.edge_attrs['e2']['capacity'] == 15.0 # test that properties are shared ug.edge_attrs['e1']['capacity'] = 105.0 assert g.edge_attrs['e1']['capacity'] == 105.0
def test_read_gexf_property_graph_from_file(tmpdir): tmpfile = tmpdir.join("gexf.out") tmpfilename = str(tmpfile) # write file json with escaped characters with open(tmpfilename, "w") as f: f.write(expected2) g = create_graph( directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True, any_hashable=True, vertex_supplier=create_vertex_supplier(), edge_supplier=create_edge_supplier() ) def import_id_cb(id): return 'v{}'.format(id) read_gexf(g, tmpfilename, import_id_cb=import_id_cb) print(g.vertices) print(g.edges) print(g.vertex_attrs) print(g.edge_attrs) assert g.vertices == {'vv1', 'vv2', 'vv3'} assert g.edges == {'e0', 'e1'} assert g.edge_tuple('e0') == ('vv1', 'vv2', 1.0) assert g.vertex_attrs['vv1']['label'] == '0' assert g.edge_attrs['e0']['id'] == 'e12'
def test_anyhashableg_two_wrappers(): g = create_graph( directed=True, allowing_self_loops=True, allowing_multiple_edges=False, weighted=True, any_hashable=True, ) g.add_vertex("0") g.add_vertex("1") g.add_edge("0", "1", edge="e1") ug = as_undirected(g) ug = as_unmodifiable(ug) assert g.type.directed != ug.type.directed assert g.type.allowing_self_loops == ug.type.allowing_self_loops assert g.type.allowing_multiple_edges == ug.type.allowing_multiple_edges assert g.type.weighted == ug.type.weighted assert g.type.modifiable != ug.type.modifiable with pytest.raises(ValueError): ug.add_vertex("2") assert ug.contains_edge_between("1", "0")
def test_tangle (self): f,input_string,expected = self.param g = jgrapht.create_graph (weighted=False) v_attrs = {} e_attrs = {} read_dot(g,input_string,v_attrs,e_attrs) self.assertCountEqual(f(g),expected)
def create_test_graph(): g = create_graph(directed=False, allowing_self_loops=False, allowing_multiple_edges=False, weighted=True) for i in range(0, 10): g.add_vertex(i) g.create_edge(0, 1) g.create_edge(0, 2) g.create_edge(0, 3) g.create_edge(0, 4) g.create_edge(0, 5) g.create_edge(0, 6) g.create_edge(0, 7) g.create_edge(0, 8) g.create_edge(0, 9) g.create_edge(1, 2) g.create_edge(2, 3) g.create_edge(3, 4) g.create_edge(4, 5) g.create_edge(5, 6) g.create_edge(6, 7) g.create_edge(7, 8) g.create_edge(8, 9) g.create_edge(9, 1) return g