Beispiel #1
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True)
ok(g)
ok(g.is_directed())
ok(not g.is_undirected())

g.add_vertices(1, 2, 3, 4)
ok(g.has_vertex(1))
ok(g.has_vertex(2))
ok(g.has_vertex(3))
ok(g.has_vertex(4))
ok(not g.has_vertex(5))
ok(not g.has_vertex(0))

g.add_edge(1, 2)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 4)
ok(g.has_edge(1, 2))
ok(g.has_edge(2, 3))
ok(not g.has_edge(2, 1))
ok(not g.has_edge(1, 3))
ok(not g.has_edge(4, 1))

eq(len(g.vertices()), 4)
eq(len(g.successors(1)), 1)
eq(len(g.predecessors(1)), 0)
Beispiel #2
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True)

eq(len(g.get_multiedge_ids(1, 2)), 0)

g.add_edge(1, 2)
g.add_edge(2, 1)

ok(g.has_edge(1, 2))
ok(g.has_edge(2, 1))
eq(len(g.get_multiedge_ids(1, 2)), 1)
eq(len(g.get_multiedge_ids(2, 1)), 1)
eq(len(g.neighbors(1)), 1)
eq(len(g.neighbors(2)), 1)

g.add_edge(1, 2)

ok(g.has_edge(1, 2))
ok(g.has_edge(2, 1))
eq(len(g.get_multiedge_ids(1, 2)), 2)
eq(len(g.get_multiedge_ids(2, 1)), 1)
eq(len(g.neighbors(1)), 1)
eq(len(g.neighbors(2)), 1)
Beispiel #3
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False, multiedged=True)
ok(g)
ok(not g.directed())
ok(g.multiedged())

g = Graph(directed=True, multiedged=True)
ok(g)
ok(g.directed())
ok(g.multiedged())
Beispiel #4
0
#!/usr/bin/env python3

from test_more import ok
from graph_tools import Graph

g = Graph(directed=True)

g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_vertex(4)
ok(g.is_reachable(1, 2))
ok(g.is_reachable(2, 3))
ok(g.is_reachable(1, 3))
ok(not g.is_reachable(2, 1))
ok(not g.is_reachable(3, 2))
ok(not g.is_reachable(3, 1))
Beispiel #5
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True, multiedged=True)
g = g.create_graph('tree', 10)
ok(g)
eq(len(g.vertices()), 10)
eq(len(g.edges()), 10 - 1)

g = Graph(directed=True, multiedged=True)
g = g.create_graph('tree', 100)
ok(g)
eq(len(g.vertices()), 100)
eq(len(g.edges()), 100 - 1)

g = Graph(directed=False, multiedged=True)
g = g.create_graph('tree', 10)
ok(g)
eq(len(g.vertices()), 10)
eq(len(g.edges()), 10 - 1)
ok(g.is_connected())
Beispiel #6
0
#!/usr/bin/env python3

from test_more import ok
from graph_tools import Graph

g = Graph(directed=False)

g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_vertex(4)
ok(len(g.components()) == 2)
ok(len(g.maximal_component()) == 3)
Beispiel #7
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True, multiedged=True)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 1)
g.set_vertex_attribute(1, 'foo', 123)
g.set_edge_attribute_by_id(1, 2, 0, 'bar', 456)
T = g.copy_graph()
ok(T.directed())
ok(T.multiedged())
ok(T.has_edge(1, 2))
ok(not T.has_edge(2, 1))
eq(T.get_vertex_attribute(1, 'foo'), 123)
eq(T.get_edge_attribute_by_id(1, 2, 0, 'bar'), 456)

g = Graph(directed=False, multiedged=True)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(3, 1)
g.set_vertex_attribute(1, 'foo', 123)
g.set_edge_attribute_by_id(1, 2, 0, 'bar', 456)
T = g.copy_graph()
ok(T.undirected())
ok(T.multiedged())
ok(T.has_edge(1, 2))
ok(T.has_edge(2, 1))
eq(T.get_vertex_attribute(1, 'foo'), 123)
Beispiel #8
0
import re
from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=True, multiedged=True)
buf = """digraph sample {
  1;
  2;
  1 -> 2;
}
""".splitlines()
g.import_graph('dot', buf)
eq(len(g.vertices()), 2)
eq(len(g.edges()), 1)
ok(g.is_directed())
ok(g.has_edge(1, 2))

g = Graph(directed=True, multiedged=True)
buf = """// comment here
digraph sample {
  1;
/* another comment
   here */
  2;
  4;
  1 -> 2;
  1 -> 4;
}
""".splitlines()
g.import_graph('dot', buf)
#!/usr/bin/env python3

from test_more import ok
from graph_tools import Graph

g = Graph(directed=True)

g.add_edge(1, 2)
g.add_edge(2, 3)
g.floyd_warshall()
ok(g.T[1][2] == 1)
ok(g.T[2][3] == 1)
ok(g.T[1][3] == 2)
ok(not g.T[2].get(1, None))
ok(not g.T[3].get(2, None))
ok(not g.T[3].get(1, None))
Beispiel #10
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False)
g.create_configuration_graph([3, 2, 2, 1, 1, 1, 0])
ok(g.degree(1) == 3)
ok(g.degree(2) == 2)
ok(g.degree(3) == 2)
ok(g.degree(4) == 1)
ok(g.degree(5) == 1)
ok(g.degree(6) == 1)
ok(g.degree(7) == 0)

g = Graph(directed=False)
g.create_configuration_graph([3, 3, 3, 3, 3, 3])
for v in g.vertices():
    ok(g.degree(v) == 3)

g = Graph(directed=False)
g.create_random_regular_graph(10, 3)
for v in g.vertices():
    ok(g.degree(v) == 3)
Beispiel #11
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False)
g.add_vertices(1, 2, 3)
g.add_edge(1, 2)
g.add_edge(2, 3)
ok(g.betweenness(1) == 0)
ok(g.betweenness(2) == 2)
ok(g.betweenness(3) == 0)
"""
   2
 /  \
1 -- 3 --4
"""

g = Graph(directed=False)
g.add_vertices(1, 2, 3, 4)
g.add_edge(1, 2)
g.add_edge(2, 3)
g.add_edge(1, 3)
g.add_edge(3, 4)
ok(g.betweenness(1) == 0)
ok(g.betweenness(2) == 0)
ok(g.betweenness(3) == 4)
ok(g.betweenness(4) == 0)
"""
   2
 // \
Beispiel #12
0
#!/usr/bin/env python3

import re
from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False, multiedged=True)
g.add_vertex(1)
astr = g.export_graph('dot')
ok(astr)
astr = re.sub(r'^//.*\n', '', astr, flags=re.M)
eq(astr, """graph export_dot {
  node [color=gray90,style=filled];
  "1";
}
""")

g = Graph(directed=False, multiedged=True)
g.add_edge(1, 2)
astr = g.export_graph('dot')
ok(astr)
astr = re.sub(r'^//.*\n', '', astr, flags=re.M)
eq(
    astr, """graph export_dot {
  node [color=gray90,style=filled];
  "1";
  "2";
  "1" -- "2";
}
""")