Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C16 Megan Hagar #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions lib/dijkstra.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@

def dijkstra(adjacency_matrix, start_node):
pass
parent_list = [None]*len(adjacency_matrix)
shortest_distances = [float('inf')]*len(adjacency_matrix)

traversal_queue = [start_node]
visited_nodes = set()
shortest_distances[start_node] = 0

while traversal_queue:
current_node = traversal_queue.pop(0)

# check if we looked at this node's adjacency list yet, if we have, skip
if current_node in visited_nodes:
continue

visited_nodes.add(current_node)

# iterate through the current nodes adjacency matrix
for node, cost in enumerate(adjacency_matrix[current_node]):
# if there is a relationship to another node, add it to queue
if cost != 0:
traversal_queue.append(node)

# calculate the distance from current node to each additional connection
# if the distance is less than shortest distance to that node already, or
# there is no recorded distance to that node, update distance list, and parent list
current_distance = cost + shortest_distances[current_node]
if shortest_distances[node] == float('inf') or shortest_distances[node] > current_distance:
parent_list[node] = current_node
shortest_distances[node] = current_distance

return {"start_node": start_node, "parent_list": parent_list, "shortest_distances": shortest_distances}