Esempio n. 1
0
#!/usr/bin/env python3

from test_more import ok, eq
from graph_tools import Graph

g = Graph(directed=False)
ok(g)
ok(not g.is_directed())
ok(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(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.neighbors(1)), 1)
eq(len(g.neighbors(2)), 2)
Esempio n. 2
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)
Esempio n. 3
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)