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

28 lines
757 B
Python

from collections import deque
from cfg import config
from db_util import get_page_id
import sql
def can_reach(title, connection):
page = get_page_id(title, connection)
cursor = connection.cursor()
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()
current_page = get_page_id(title, connection)
center_page = get_page_id(center, connection)
path = deque()
while(current_page != center_page):
path.append(current_page)
cursor.execute(sql.statements["dijkstra_backtrack_one"], (current_page,))
current_page = cursor.fetchone()[0]
return list(reversed(path))