Commit 4d90201c authored by Bertrand Néron's avatar Bertrand Néron

add main to use Undirected and Directed weghted Graph

parent e8438b35
from graph.graph_2 import NDGraph, NDWGraph, Node, BFS, DFS
from graph.graph_3 import UnDirectedGraph, DirectedGraph, Node, Edge
from graph.traversing import BFS, DFS
print("=============== UnDirected weight Graph =============")
# We want to create a toy graph (not directed) to check our algos
# no
# / \
# n1 n3
# | |
# | / |
# n2 n4
# \ /
# n5
nodes = [Node() for _ in range(6)]
g = NDGraph(nodes)
g = UnDirectedGraph()
g.add_edge(nodes[0], nodes[1], weight=1)
g.add_edge(nodes[0], nodes[3], weight=3)
g.add_edge(nodes[1], nodes[2], weight=2)
g.add_edge(nodes[2], nodes[5], weight=5)
g.add_edge(nodes[3], nodes[4], weight=4)
g.add_edge(nodes[4], nodes[5], weight=6)
g.add_edge(nodes[3], nodes[2], weight=15)
print("BFS")
for n, p in BFS(g, nodes[0]):
print(n, [str(e) for e in p])
print(f"distance = {sum([e.weight for e in p])}")
g.add_edge(nodes[0], (nodes[1], nodes[3]))
g.add_edge(nodes[1], (nodes[2],))
g.add_edge(nodes[2], (nodes[5],))
g.add_edge(nodes[3], (nodes[4],))
g.add_edge(nodes[4], (nodes[5],))
# no
# / \
# n1 n3
# | X |
# n2 n4
# \ /
# n5
g.add_edge(nodes[1], nodes[4], weight=20)
# The graph is created we will test
print("Order", g.order())
print("Size", g.size())
# a Depth First Search
# starting from n0
# the possible solutions are
# n0, n1,n2,n5,n4,n3
# n0, n3, n4, n5, n2, n1
print("BFS")
for n, p in BFS(g, nodes[0]):
print(n, [str(e) for e in p])
print(f"distance = {sum([e.weight for e in p])}")
print("DFS")
for n in DFS(g, nodes[0]):
print(n.id)
for n, p in DFS(g, nodes[0]):
print(n, [str(e) for e in p])
print(f"distance = {sum([e.weight for e in p])}")
# a Breadth First Search
# starting n0
# the possible solutions are
# n0, n1, n3, n2, n4, n5
# n0, n3, n1, n2, n4, n5
# n0, n1, n3, n4, n2, n5
# ....
print("BFS")
for n, _ in BFS(g, nodes[0]):
print(n.id)
print("=============== Directed weighted Graph =============")
nodes += [Node() for _ in range(6)]
g = DirectedGraph()
g.add_edge(nodes[6], nodes[7], weight=1)
g.add_edge(nodes[7], nodes[8], weight=2)
g.add_edge(nodes[8], nodes[11], weight=5)
g.add_edge(nodes[11], nodes[10], weight=4)
g.add_edge(nodes[6], nodes[9], weight=6)
g.add_edge(nodes[9], nodes[10], weight=7)
g.add_edge(nodes[8], nodes[9], weight=8)
g.add_edge(nodes[7], nodes[10], weight=9)
g.add_edge(nodes[10], nodes[7], weight=10)
nodes = [Node() for _ in range(6)]
g = NDWGraph(nodes)
# n6
# / \
# v V
# n7 n9
# |^ ^|
# | X |
# v vv
# n8 n10
# \ ^
# v/
# n11
g.add_edge(nodes[0], nodes[1], 1)
g.add_edge(nodes[0], nodes[3], 3)
g.add_edge(nodes[1], nodes[2], 2)
g.add_edge(nodes[2], nodes[5], 5)
g.add_edge(nodes[3], nodes[4], 4)
g.add_edge(nodes[4], nodes[5], 5)
print("Order", g.order())
print("Size", g.size())
print("BFS")
path_len = 0
for n, w in BFS(g, nodes[0]):
path_len += w
print(n, w, path_len)
for n, p in BFS(g, nodes[6]):
print(n, [str(e) for e in p])
print(f"distance = {sum([e.weight for e in p])}")
print("DFS")
for n, p in DFS(g, nodes[6]):
print(n, [str(e) for e in p])
print(f"distance = {sum([e.weight for e in p])}")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment