
Graph Tester
# This program creates a graph and runs some tests on the graph methods.
from graph import Graph
#from graphsimp import Graph
g = Graph()
# See if an empty graph is really empty.
print("empty:", g.nnodes(), g.narcs(), g.weight("fred", "barney"),end=' ')
print(g.successors("bill"), g.predecessors("frank"))
print()
# How about a single link?
g.link("dust", "mop", 34)
print("unit:", g.nnodes(), g.narcs(), g.weight("dust", "mop"),end=' ')
print(g.weight("dust", "rag"), g.weight("dry","mop"))
print(" ", g.successors("dust"), g.predecessors("dust"),end=' ')
print(g.successors("mop"), g.predecessors("mop"),end=' ')
print(g.successors("bogus"), g.predecessors("bogus"))
print()
# Test with a larger graph.
cost = 3
for src in [ "trucks", "cars", "horses" ]:
for snk in [ "red", "blue", "green", "orange" ]:
if src == "cars" and snk == "green": continue;
g.link(src, snk, cost)
cost = 3*cost - 5
g.link("red", "cars", 44)
g.link("green", "red", 21)
g.link("green", "trucks", 60)
g.link("green", "emerald", 33)
g.link("emerald", "city", 85)
g.link("trucks", "trucks", 2)
print("several:", g.nnodes(), g.narcs(),end=' ')
print(g.weight("trucks", "green"), g.weight("trucks", "trucks"))
for n in [ "cars", "green", "emerald", "horses", "orange", "mop", "bogus" ]:
print(" ", g.weight(n, "blue"), sorted(g.successors(n)), sorted(g.predecessors(n)))
print()
# See if the delink call works by trying it a couple of times and looking at the
# changes.
g.delink("red", "cars")
g.delink("trucks", "orange")
print("delinked:", g.nnodes(), g.narcs(),end=' ')
print(g.weight("trucks", "green"), g.weight("trucks", "trucks"),end=' ')
print(g.weight("red", "cars"), g.weight("trucks", "orange"))
for n in [ "red", "orange", "cars" ]:
print(" ", sorted(g.successors(n)), sorted(g.predecessors(n)))
print()
g.clear()
print("cleared:", g.nnodes(), g.narcs(), g.weight("emerald", "city"))