fixed one bug in dijkstra backtracking

This commit is contained in:
Daniel Knuettel 2019-02-27 11:41:44 +01:00
parent a7539c1cad
commit 9ba52e80f1
4 changed files with 10 additions and 7 deletions

View File

@ -1,6 +1,6 @@
config = { config = {
"use_sqlite": False "use_sqlite": True
, "mysql_server": "172.17.0.2" , "mysql_server": "172.17.0.2"
, "mysql_user": "wikipedia" , "mysql_user": "wikipedia"
, "mysql_password": "wikipediastuff" , "mysql_password": "wikipediastuff"

View File

@ -27,7 +27,8 @@ class DijkstraHelper(object):
return cls(nodes, connections) return cls(nodes, connections)
def dijkstra(self, root): def dijkstra(self, root):
self.recursive_dijkstra([root], 0) self._nodes[root] = 0
self.recursive_dijkstra([root], 1)
def recursive_dijkstra(self, todos, depth): def recursive_dijkstra(self, todos, depth):
if(not todos): 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()]) cursor.executemany(sql.statements["insert_dijkstra_values"], [(k, sqlize(v)) for k,v in self._nodes.items()])
connection.commit()

View File

@ -9,7 +9,7 @@ from dijkstra import prepare_dijkstra, dijkstra
from connectivity import shortest_path from connectivity import shortest_path
from graph import DijkstraHelper 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") cache = get_cache("./cache/", "Angela_Merkel")
receive_link_graph("Angela_Merkel", cache, 2) receive_link_graph("Angela_Merkel", cache, 2)
@ -21,7 +21,6 @@ print(cursor.fetchall())
#prepare_dijkstra(cache) #prepare_dijkstra(cache)
#dijkstra("Angela_Merkel", cache) #dijkstra("Angela_Merkel", cache)
# #
#print(shortest_path("Angela_Merkel", "Germany", cache))
angela = get_page_id("Angela_Merkel", 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 = DijkstraHelper.from_db(cache)
dijkstra.dijkstra(angela) dijkstra.dijkstra(angela)
dijkstra.write_back(cache) 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)])

View File

@ -29,7 +29,7 @@ sql_statements = {
, "mysql": "SELECT COUNT(destination) FROM links WHERE destination=%s"} , "mysql": "SELECT COUNT(destination) FROM links WHERE destination=%s"}
, "dijkstra_backtrack_one": {"sqlite": '''SELECT links.source , "dijkstra_backtrack_one": {"sqlite": '''SELECT links.source
FROM links 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=? WHERE links.destination=?
ORDER BY dijkstra_helper.value ASC ORDER BY dijkstra_helper.value ASC
LIMIT 1''' LIMIT 1'''