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

49 lines
1.2 KiB
Python

from collections import deque
from cfg import config
from db_util import get_page_id
import sql
def prepare_dijkstra(connection):
cursor = connection.cursor()
cursor.execute(sql.statements["dijkstra_insert_pages"])
cursor.execute(sql.statements["dijkstra_set_infinity"])
connection.commit()
def dijkstra_one(page, value, connection):
cursor = connection.cursor()
if(isinstance(page, tuple)):
# Idk why this happens.
title = title[0]
cursor.execute(sql.statements["dijkstra_get_to_update"], (page, value + 1))
# This is the list of nodes that have to be updated
result = cursor.fetchall()
cursor.execute(sql.statements["dijkstra_update"], (value + 1, page, value + 1))
connection.commit()
return result
def recursive_dijkstra(titles, value, connection):
if(not titles):
return
todos = deque()
for title in titles:
todos.extend(dijkstra_one(title, value, connection))
recursive_dijkstra(todos, value + 1, connection)
def dijkstra(title, connection):
page = get_page_id(title, connection)
cursor = connection.cursor()
cursor.execute(sql.statements["dijkstra_set_root"], (page,))
todos = dijkstra_one(page, 1, connection)
recursive_dijkstra(todos, 2, connection)