CSCE 411 Lecture 20

From Notes
Jump to navigation Jump to search
Lecture Slides

« previous | Friday, October 12, 2012 | next »


Shortest Paths

Weighted Graph (no negative weights)

If all weights are same, BFS would give shortest path.

Algorithm makes use of a priority queue

Dijkstra's Single Source Shortest Path Algorithm

  • Assume all edge weights are nonnegative
  • Similar to Prim's MST algorithm (greedily choose the lightest edge)
  1. Keep an estimate du of the shortest path distance from s to u
  2. Use d as the key in a priority queue
  3. When u is added to the tree, check each of u's neighbors v to see if u provides v with a cheaper path from s: compare dv to du+w(u,v)
def dijkstra_sssp graph, source

  pq = PriorityQueue.new
  graph.each_vertex do |v|
    v.distance = Float::INFINITY
    pq.insert v.distance, v
  end
  source.distance = 0

  until pq.empty?
    u = pq.extract_min
    u.neighbors.each do |v|
      if u.distance + graph.weight(u,v) < v.distance
        v.distance = u.distance + graph.weight(u,v)
        pq.decrease_key(v, v.distance)
        v.parent = u
      end
    end
  end
end

Correctness

Let Ti be the tree constructed after the ith iteration of the while loop:

  • The nodes in Ti are not in Q
  • The edges in Ti are indicated by their parent variables


Claim. The pathi n Ti from s to u is a shortest path and has a distance du for all uTi

Proof by induction.
Basis: When i=1, s is the only node in T1 and ds=0.

Induction: Assume Ti is a correct shortest path tree. We need to show that Ti+1 is a correct shortest path tree as well.

Let u be the node added in iteration i. Let x=u.parent. We need to show that the path P in Ti+1 from sx to u is a shortest path, and has distance du.

Let P be another path from s to u. Let (a,b) be the first edge in P that leaves Ti.

If P'1 is the part before (a,b) and P'2 is the part after (a,b), then

w(P)=w(P'1)+w(a,b)+w(P'2)w(P'1)+w(a,b)w(saTi)+w(a,b)*w(sxTi)+w(x,u)=w(P)

* w(saT1) represents the shortest path, and provides a lower bound for w(P'1) (any path from s to a).

Therefore,

w(P)>w(P)

, and the claim holds by induction on

i

.

Q.E.D.

Running Time

Basic running time: O(V(Tins+Tex)+ETdec)

using binary heap
Tins=Tex=Tdec=O(logV)
O(ElogV)
using Fibonacci heap
Tins=O(1), Tex=O(logn), Tdec=O(1)
O(VlogV+E)