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

Pauline - Fire #2

Open
wants to merge 5 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
106 changes: 48 additions & 58 deletions lib/dijkstra.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@

# Time Complexity: O(v^2) - Since we are performing a variety of BFS on an adjacency matrix,
# the time complexity to search all edges will be, at worst case, O(v^2), assuming all vertices v
# are interconnected. This is because for each node visited, the entire associated row on the
# adjacency matrix has to be checked to confirm which edges are actual graph edges.

# Space Complexity: O(v) - A couple arrays are create, size directly equal to the number of vertices v.
# Likewise, the Queue and Set created to track nodes to visit and visited nodes are also directly
# equal to tne number of vertices in the graph. The return hash is simply a wrapper that envelops
# the shortest_distances and parent_list arrays.
def dijkstra(adjacency_matrix, start_node)
shortest_distances = Array.new(adjacency_matrix[0].length){Float::INFINITY}
parent_list = Array.new(shortest_distances.length){nil}
tracking_q = Queue.new
visited_nodes = Set.new

# start with start node
# start node has no previous node, marked as 0
visited_nodes.add(start_node)
shortest_distances[start_node] = 0

adjacency_matrix[start_node].each_with_index do |weight, i|
if weight > 0 && i != start_node
parent_list[i] = start_node
shortest_distances[i] = weight
tracking_q.push(i)
end
end

#int nVertices = adjacencyMatrix[0].length;
num_nodes = adjacency_matrix.length

# shortest_distances will hold the shortest distances from start_node to i
# it starts with infinity as the value
shortest_distances = Array.new(num_nodes, Float::INFINITY)


# added[i] will be true if the path to i
# from the source has been found
added = Array.new(num_nodes, false)


# Distance of source vertex from
# itself is always 0
shortest_distances[start_node] = 0

# parent array to store the shortest path tree
parents = Array.new(num_nodes)
# no parent for the start node
parents[start_node] = nil


# Find shortest path for all nodes
(num_nodes - 1).times do

# Pick the minimum distance vertex
# from the set of vertices not yet
# processed. nearest_node is
# always equal to start_node in
# first iteration.
nearest_node = -1
shortest_distance = Float::INFINITY
(0...num_nodes).each do |node_index|
if (!added[node_index] &&
shortest_distances[node_index] < shortest_distance)
nearest_node = node_index
shortest_distance = shortest_distances[node_index]
end
end

# Mark the picked vertex as visited
added[nearest_node] = true;
# Update dist value of the
# adjacent nodes of the picked node.
(0...num_nodes).each do |node_index|
edge_distance = adjacency_matrix[nearest_node][node_index];

if (edge_distance > 0 &&
((shortest_distance + edge_distance) <
shortest_distances[node_index]))
parents[node_index] = nearest_node;
shortest_distances[node_index] = shortest_distance +
edge_distance
end
until (tracking_q.empty?)
cur_node = tracking_q.pop
# skip already visited nodes
next if visited_nodes.include?(cur_node)
# check all adjacent nodes
visited_nodes.add(cur_node)

# check for adjacent nodes
adjacency_matrix[cur_node].each_with_index do |weight, i|
# ignore those with no weight
if weight > 0 && i != cur_node
if shortest_distances[cur_node] + weight < shortest_distances[i]
shortest_distances[i] = shortest_distances[cur_node] + weight
parent_list[i] = cur_node
end
tracking_q.push(i)
end
end
end
end
return {start_node: start_node, parent_list: parents, shortest_distances: shortest_distances }

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