From 9ba52e80f1f309af9bf99310dce4b107403d81e0 Mon Sep 17 00:00:00 2001 From: Daniel Knuettel Date: Wed, 27 Feb 2019 11:41:44 +0100 Subject: [PATCH] fixed one bug in dijkstra backtracking --- exam/ex01/cfg.py | 2 +- exam/ex01/graph.py | 5 +++-- exam/ex01/main.py | 8 +++++--- exam/ex01/sql.py | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/exam/ex01/cfg.py b/exam/ex01/cfg.py index 891f03d..15a65db 100644 --- a/exam/ex01/cfg.py +++ b/exam/ex01/cfg.py @@ -1,6 +1,6 @@ config = { - "use_sqlite": False + "use_sqlite": True , "mysql_server": "172.17.0.2" , "mysql_user": "wikipedia" , "mysql_password": "wikipediastuff" diff --git a/exam/ex01/graph.py b/exam/ex01/graph.py index 79a8eea..81ff8f7 100644 --- a/exam/ex01/graph.py +++ b/exam/ex01/graph.py @@ -27,7 +27,8 @@ class DijkstraHelper(object): return cls(nodes, connections) def dijkstra(self, root): - self.recursive_dijkstra([root], 0) + self._nodes[root] = 0 + self.recursive_dijkstra([root], 1) def recursive_dijkstra(self, todos, depth): if(not todos): @@ -58,4 +59,4 @@ class DijkstraHelper(object): cursor.executemany(sql.statements["insert_dijkstra_values"], [(k, sqlize(v)) for k,v in self._nodes.items()]) - + connection.commit() diff --git a/exam/ex01/main.py b/exam/ex01/main.py index 4cacf25..fb6b8ad 100644 --- a/exam/ex01/main.py +++ b/exam/ex01/main.py @@ -9,7 +9,7 @@ from dijkstra import prepare_dijkstra, dijkstra from connectivity import shortest_path from graph import DijkstraHelper -from db_util import get_page_id +from db_util import get_page_id, get_page_title cache = get_cache("./cache/", "Angela_Merkel") receive_link_graph("Angela_Merkel", cache, 2) @@ -21,7 +21,6 @@ print(cursor.fetchall()) #prepare_dijkstra(cache) #dijkstra("Angela_Merkel", cache) # -#print(shortest_path("Angela_Merkel", "Germany", cache)) angela = get_page_id("Angela_Merkel", cache) @@ -29,4 +28,7 @@ angela = get_page_id("Angela_Merkel", cache) dijkstra = DijkstraHelper.from_db(cache) dijkstra.dijkstra(angela) dijkstra.write_back(cache) -print({k:v for k,v in dijkstra._nodes.items() if v != float("inf")}) +#print({k:v for k,v in dijkstra._nodes.items() if v != float("inf")}) +print([get_page_title(id_, cache) for id_ in shortest_path("Angela_Merkel", "Germany", cache)]) +print([get_page_title(id_, cache) for id_ in shortest_path("Angela_Merkel", "2012_Nobel_Peace_Prize", cache)]) + diff --git a/exam/ex01/sql.py b/exam/ex01/sql.py index 34b4989..352e103 100644 --- a/exam/ex01/sql.py +++ b/exam/ex01/sql.py @@ -29,7 +29,7 @@ sql_statements = { , "mysql": "SELECT COUNT(destination) FROM links WHERE destination=%s"} , "dijkstra_backtrack_one": {"sqlite": '''SELECT links.source FROM links - LEFT JOIN dijkstra_helper ON links.destination=dijkstra_helper.page + LEFT JOIN dijkstra_helper ON links.source=dijkstra_helper.page WHERE links.destination=? ORDER BY dijkstra_helper.value ASC LIMIT 1'''