This preview shows page 1. Sign up to view the full content.
Unformatted text preview: thm Initialize each vertex’s distance as infinity Start at a given vertex s Update s’s distance to be 0 Repeat Pick the next unknown vertex with the shortest distance to
be the next v If no more vertices are unknown, terminate loop Mark v as known For each edge from v to adjacent unknown vertices w 80 If the total distance to w is less than the current distance to w Update w’s distance and the path to w v0 v1 2
5 1 2 1 v2 1 v3
6 Known Dist path v0 * (default) 0 N/A 6 v3 v1
v2 KNOWN 2 v0 v3 KNOWN 1 v0 v4 2 v3 v5 74 v3v2 v6 6 v3 v5 v4
5 2
V 1 10 3
v6 void Graph::dijkstra(Vertex s){
Vertex v,w;
s.dist = 0;
while (there exist unknown vertices, find the
unknown v with the smallest distance)
v.known = true;
for each w adjacent to v
if (!w.known)
if (v.dist + Cost_VW < w.dist){
w.dist = v.dist + Cost_VW;
82 w.path = v;
} Analysis How long does it take to find the smallest unknown
distance? Total running time: simple scan using an array: O(v) Using a simple scan: O(v2+e) = O(v2) Optimizations? Use adjacency graphs and heaps Assuming that the graph is connected (i.e. e > v1), then
the running time decreases to O(e + v log v) We can simplify this to O(e log v) 83 Negative Cost Edges? Perhaps the graph weights are the amount of fuel
expended Positive means fuel was used
And passing by a fuel station is a refueling, which is a
negative cost edge Dijkstra’s algorithm does...
View
Full
Document
This note was uploaded on 02/25/2014 for the course CS 4102 taught by Professor Horton during the Spring '10 term at UVA.
 Spring '10
 HORTON
 Algorithms

Click to edit the document details