Algorithm Analysis (CSC 301 2015F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning]
Current: [Outline] [EBoard] [Reading] [Lab] [Assignment]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Algorist]
Related Courses: [Walker (2014F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker]
Overview
Slightly modified version of the version from Wikipedia, available at https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm.
1 function Dijkstra(Graph, source, target): 2 3 dist[source] = 0 // Distance from source to source 4 prev[source] = undefined // Previous node in optimal path initialization 5 6 create vertex set Q // Unvisited nodes 7 8 for each vertex v in Graph: // Initialization 9 if v != source: // (unvisited nodes) 10 dist[v] = INFINITY // Unknown distance from source to v 11 prev[v] = UNDEFINED // Previous node in optimal path from source 12 add v to Q // All nodes initially in Q (unvisited nodes) 13 14 while Q is not empty: 15 u = vertex in Q with min dist[u] // Source node in the first case 16 remove u from Q 17 18 for each neighbor v of u: // where v is still in Q. 19 alt = dist[u] + length(u, v) // length of path through u 20 if alt < dist[v]: // A shorter path to v has been found 21 dist[v] = alt 22 prev[v] = u 23 24 return dist[target] // or the path using prev
What's the running time? You can use n for the number of nodes and m for the total number of edges.
for each vertex, v, in Graph runs n times.
while Q is not empty runs n times
remove is implemented.Morals
What's the underlying design principle?
Questions for the class