scientific-programming-exer.../exam/ex01/connectivity.py

28 lines
757 B
Python
Raw Normal View History

from collections import deque
2019-02-15 10:47:50 +00:00
from cfg import config
2019-02-19 13:16:22 +00:00
from db_util import get_page_id
2019-02-25 13:14:55 +00:00
import sql
2019-02-15 10:47:50 +00:00
def can_reach(title, connection):
2019-02-19 13:16:22 +00:00
page = get_page_id(title, connection)
cursor = connection.cursor()
2019-02-25 13:14:55 +00:00
cursor.execute(sql.statements["count_links_to"], (page, ))
count = cursor.fetchone()[0]
return count > 0
def shortest_path(center, title, connection):
if(not can_reach(title, connection)):
return []
cursor = connection.cursor()
2019-02-19 13:16:22 +00:00
current_page = get_page_id(title, connection)
center_page = get_page_id(center, connection)
path = deque()
2019-02-19 13:16:22 +00:00
while(current_page != center_page):
2019-02-21 16:14:17 +00:00
path.append(current_page)
2019-02-25 13:14:55 +00:00
cursor.execute(sql.statements["dijkstra_backtrack_one"], (current_page,))
2019-02-21 16:14:17 +00:00
current_page = cursor.fetchone()[0]
return list(reversed(path))