algorithms and datastructures n.
Skip this Video
Loading SlideShow in 5 Seconds..
Algorithms ( and Datastructures ) PowerPoint Presentation
Download Presentation
Algorithms ( and Datastructures )

Loading in 2 Seconds...

play fullscreen
1 / 28

Algorithms ( and Datastructures ) - PowerPoint PPT Presentation

  • Uploaded on

Algorithms ( and Datastructures ). Lecture 6 MAS 714 part 2 Hartmut Klauck. Shortest paths in weighted graphs. We are given a graph G (adjacency list with weights W( u,v )) No edge means W( u,v )= 1 We look for shortest paths from start vertex s to all other vertices

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Algorithms ( and Datastructures )' - leone

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
algorithms and datastructures

Algorithms (andDatastructures)

Lecture 6

MAS 714 part 2

Hartmut Klauck

shortest paths in weighted graphs
Shortest paths in weighted graphs
  • We are given a graph G (adjacency list with weights W(u,v))
  • No edge means W(u,v)=1
  • We look for shortest paths from start vertex s to all other vertices
  • Length of a path is the sum of edge weights
  • Distance±(u,v) is the minimum path length on any path u to v
  • Single-Source Shortest-Path (SSSP):Shortest paths from s to all v
  • All-Pairs Shortest-Path (APSP):Shortest paths between all u,v
  • We will now consider SSSP
  • Solved in unweighted graphs by BFS
  • We don‘t allow negative weight cycles
  • Consider a minimal path from u to v
  • Then: all subpaths of minimal paths are also minimal!
  • Idea: We need to find shortest paths with few edges first
    • We will use estimates d(v), starting from d(s)=0 and d(v)=1for all other v (Pessimism!)
    • Improve estimates until tight
storing paths
Storing paths
  • Again we store predecessors (v), starting at NIL
  • In the end they will form a shortest path tree
relaxing an edge
Relaxing an edge
  • Basic Operation:
    • Relax(u,v,W)
      • if d(v)>d(u)+W(u,v)then d(v):=d(u)+W(u,v); (v):=u
  • D.h. If we find a better estimate we go for it
  • Every sequence of relax operations satisfies:
    • d(v) ¸(s,v) at all time
    • Clearly: vertices with (s,v)=1always have d(v)=1
    • If s u v is a shortest path and (u,v) an edge and d(u)=(s,u).Relaxing (u,v) gives d(v)=(s,v)
      • d(v)· d(u)+W(u,v) after relaxing=(s,u)+W(u,v)=(s,v), minimality of partial shortest paths
dijkstra s algorithm
Dijkstra‘s Algorithm
  • Solves SSSP
  • Needs: W(u,v)¸ 0 for all edges
  • Idea: store vertices so that we can choose a vertex with minimal distance estimate
  • Choose v with minimal d(v), relax all edges
  • until all v are processed
data structure
Data Structure
  • Store n vertices and their distance estimate d(v)
  • Operations:
    • ExtractMin: Get the vertex with minimum d(v)
    • DecreaseKey(v,x): replace d(v) with a smaller value
    • Initialize
    • Insert(v)
    • Test for empty
  • Priority Queue
dijkstra s algorithm1
Dijkstra‘s Algorithm
  • Initialize (v)=NIL for all v andd(s)=0, d(v)=1otherwise
  • S=;set of vertices processed
  • Insert all vertices into Q (priority queue)
  • While Q;
    • u:=ExtractMin(Q)
    • S:=S [ {u}
    • For all neighbors v of u: relax(u,v,W)
      • relax uses DecreaseKey
things to do
Things to do:
  • Prove correctness
  • Implement Priority Queues
  • Analyze the running time
3 running time
3) Running Time
  • n ExtractMin Operations and m DecreaseKey Operations
  • Their time depends on the implementation
  • Later (?) we will see a datastructurethatpermitsthefollowingrunningtimes:
    • Amortised Time ofExtractMinisO(log n), i.e. the total time for (any) n operationsisO(n log n)
    • Amortised Time ofDecreaseKeyisO(1), i.e. total time for m operationsO(m)
  • Withthistherunning time of Dijkstra isO(m + n log n)
simple implementation
Simple Implementation
  • Store d(v) in an array
    • DecreaseKey in time O(1)
    • ExtractMin in time O(n)
  • Total running time: O(n2) forExtractMinandO(m) forDecreaseKey
  • Thisisgoodenoughif G isgivenasadjacencymatrix
  • First some observations
    • d(v) ¸(s,v) at all times (proof by induction)
    • Hence: vertices with (s,v)=1always haved(v)=1
    • Let s u v be a shortest path with final edge (u,v) and let d(u)=(s,u) (at some time). Relaxing (u,v) gives d(v)=(s,v)
    • If at any time d(v)=1for all v2V-S, then all remaining vertices are not reachable from s
  • Theorem:Dijkstra‘s Algorithm terminates with d(v)=(s,v) for all v.
  • Proof by induction over the time a vertex is taken from the priority queue and put into S
    • Invariant: For all v2S we have d(v)=(s,v)
    • In the beginning this is true since S=;
  • Have to show that the next chosen vertex has d(v) =±(s,v)
  • For s this is trivially true
  • For vs:
    • Only vertices with finite d(v) are chosen, or all remaining vertices are not reachable (and have correct d(v) )
    • There must be a (shortest) path s to v
    • Let p be such a path, and y the first vertex outside S on p, x its predecessor
  • Claim: d(y)=(s,y), when v is chosen from Q
  • Then: d(v)· d(y)=(s,y)·(s,v). QED
  • Proof of Claim:
    • d(x)=(s,x) by induction hypothesis
    • edge (x,y) has been relaxed, so d(y)=(s,y)
  • Hence d(v) is correct for all vertices in S
  • In the end S=V, all distances are correct
  • Still need to show: the predecessor tree computed is also correct
priority queues
Priority Queues
  • Operations: ExtractMin, DecreaseKey, Insert
  • We will showhowtoimplement a priorityqueuewith time O(log n) for all operations
  • Thisleadsto total time O((n+m) log n)
  • Slightly suboptimal : wewouldlike O(n log n + m)
    • Much moredifficulttoachieve
  • We will implement a priorityqueuewith a heap
  • Heaps can also beusedforsorting!
    • Heapsort:Insert all elements, ExtractMinuntilempty
  • If all operationstake time log n wehavesorting in time O(n log n)
  • A heapis an arrayoflength n
    • can hold atmost n keys/vertices/numbers
  • The keys in thearrayare not sorted, but their order hastheheap-property
  • Namelytheycanbeviewedas a tree, in whichparentsaresmallerthantheirchildren
    • ExtractMinis easy!
    • Unfortunatelyweneedtoworktomaintaintheheap-property after removingtheroot
  • keys in a heap are arranged as a full binary tree who’s last level is filled up from the left up to some point
  • Example:
  • Heap property:
    • Element in cell i is smaller than elements in cells 2i and 2i+1
  • Example:Tree Array
  • Besides the array storing the keys we also keep a counter SIZE that tells us how many keys are in H
    • in cells 1…SIZE
simple procedures for heaps
Simple Procedures for Heaps
  • Initialize:
    • Declare the array H of correct length n
    • Set SIZE to 0
  • Test for Empty:
    • Check SIZE
  • FindMin:
    • Minimum is in H[1]
  • All this in time O(1)
simple procedures for heaps1
Simple Procedures for Heaps
  • Parent(i) is bi/2c
  • LeftChild(i) is 2i
  • Rightchild(i) is 2i+1