From 546ef4a9fc6dc8e313549c0ce50791b0a57e9cfc Mon Sep 17 00:00:00 2001 From: Bertrand <bneron@pasteur.fr> Date: Fri, 29 Nov 2019 16:34:40 +0100 Subject: [PATCH] :art: improve output by printing graph ascii art --- Graph/main_3.py | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/Graph/main_3.py b/Graph/main_3.py index cd985e5..ff430ee 100644 --- a/Graph/main_3.py +++ b/Graph/main_3.py @@ -4,14 +4,17 @@ from graph.traversing import BFS, DFS print("=============== UnDirected weight Graph =============") # We want to create a toy graph (not directed) to check our algos +graph_schema = """ # no -# / \ +# 1/ \\3 # n1 n3 -# | / | +# 2| 15/|4 # n2 n4 -# \ / +# 5\ /6 # n5 +""" +print(graph_schema) nodes = [Node() for _ in range(6)] g = UnDirectedGraph() @@ -28,13 +31,18 @@ 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("=" * 20, "UnDirected weight Graph 2", "=" * 20) +graph_schema = """ # no -# / \ +# 1/ \\3 # n1 n3 -# | X | +# 2| X |4 (3-2 15) (1-4 20) # n2 n4 -# \ / +# 5\ /6 # n5 +""" + +print(graph_schema) g.add_edge(nodes[1], nodes[4], weight=20) @@ -52,6 +60,22 @@ for n, p in DFS(g, nodes[0]): print(f"distance = {sum([e.weight for e in p])}") print("=============== Directed weighted Graph =============") +graph_schema = """ +# n6 +# 1/ \\6 +# v V +# n7 n9 +# 2|^ ^|7 +# | X | (10 -> 7 10) (8 -> 9 8) +# v vv +# n8 n10 +# 5\ ^ +# v/4 +# n11 +""" + +print(graph_schema) + nodes += [Node() for _ in range(6)] g = DirectedGraph() @@ -65,17 +89,6 @@ 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) -# n6 -# / \ -# v V -# n7 n9 -# |^ ^| -# | X | -# v vv -# n8 n10 -# \ ^ -# v/ -# n11 print("Order", g.order()) print("Size", g.size()) -- GitLab