Shortest Path and Minimum Spanning Tree - PowerPoint PPT Presentation

Loading...

PPT – Shortest Path and Minimum Spanning Tree PowerPoint presentation | free to download - id: 685221-Y2EzY



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Shortest Path and Minimum Spanning Tree

Description:

Shortest Path and Minimum Spanning Tree HKOI Training 2009 7 Feb 2009 – PowerPoint PPT presentation

Number of Views:227
Avg rating:3.0/5.0
Slides: 33
Provided by: hkoiOrgtra
Learn more at: http://www.hkoi.org
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Shortest Path and Minimum Spanning Tree


1
Shortest Path and Minimum Spanning Tree
  • HKOI Training 2009
  • 7 Feb 2009

2
Graph
  • Graph G (V, E)
  • Vertex/Node V
  • Number V or simply V
  • Degree degv,in-degv,out-degv
  • Edge E
  • Number E or simply E
  • Direction e (u, v) or u, v
  • E V2 i.e. degv V

3
Graph Representations
Adjacency Matrix Adjacency List Edge List
Memory Q(V2) Q(V E) Q(E)
Check if (u, v) connected Q(1) O(out-degreeu) O(E)
List Edges from u Q(V) O(out-degreeu) Q(E)
List Edges to v Q(V) Q(E) Q(E)
List All Edges Q(V2) Q(E) Q(E)
4
Graph Modelling
S



E
Assuming you can only move between grey cells.
5
Why do we need to learn more algorithm for
shortest path?
  • BFS is already doing a great job!
  • In what context?
  • Last graph P
  • Any well model-ed graph?

6
Another Graph
S



E
  • Greedy?
  • BFS?
  • 2 queues?
  • 1 queue with re-insert?
  • Now assume
  • Move to adjacent grey cell costs 1 unit
  • Move to adjacent black cell costs 2 unit

7
Another Graph Again
S



E
From To Cost
Grey Grey 1000
Grey Black 999
Black Grey 10
Black Black 888
So troublesome, lets learn / find other
algorithm(s)
8
S
3
2
1
3
T
3
3
1
9
S
T
10
BFS in Weighted Graph
  • Problems
  • Queue does not promise smallest dv anymore
  • Expanding path caused unnecessary searching of
    artificial vertices
  • Solution
  • We can simply pick the shortest real vertex
  • We need Sorted Queue which dequeues vertices
    in increasing order of dv.
  • It is called a Priority Queue and negation of
    dv is called the Priority of vertex v

11
S
3
2
1
3
T
3
3
1
12
S
3
2
1
3
T
3
3
1
13
S
3
2
1
3
T
3
3
1
We can see the end now!
14
S
3
2
1
3
T
3
3
1
15
S
3
2
1
3
T
3
3
1
16
S
3
2
1
3
T
3
3
1
17
Dijkstras Algorithm
  • for-each v, dv ? 8
  • ds ? 0
  • Q.Insert(s,ds)
  • while not Q.Empty() do
  • u Q.ExtractMin()
  • for-each v where (u, v) in E
  • if dv gt du wuv then
  • dv du wuv
  • Q.DecreaseKey(v,dv)

18
Dijkstras Algorithm
  • for-each v, dv ? 8
  • ds ? 0
  • Q.Insert(s,ds)
  • while not Q.Empty() do
  • u Q.ExtractMin()
  • for-each v where (u, v) in E
  • if dv gt du wuv then
  • dv du wuv
  • Q.Insert(v,dv)

Lazy Deletion
19
Implementations of Priority Queue
Insert ExtractMin DecreaseKey
Array Q(1) Q(n) Q(1)
Sorted Array O(n) Q(1) O(n)
Binary Heap O(log n) Q(log n) Q(log n)
Fibonacci Heap (amortized) Q(1) O(log n) Q(1)
20
Complexity
Memory Time
Array O(V) O(V2)
Array (Lazy) O(E) O(E2)
Sorted Array O(V) O(VE)
Binary Heap O(V) O(E log V)
Binary Heap (Lazy) O(E) O(E log V)
Fibonacci Heap (amortized) O(V) O(E V log V)
21
Dijkstras Algorithm
  • Edsger Dijkstra is doing a great job!
  • Can we now use it on all kinds of graph?

22
Another Graph
S



E
  • Add a constant to the weight?
  • Now assume
  • Move to adjacent grey cell costs 1 unit
  • Move to adjacent black cell gains 1 unit

23
Dijkstras Algorithm
  • Problems
  • Cannot tackle graph with negative edges
  • With negative edge, some unvisited nudes may have
    smaller distance to source than some visited
    nudes
  • Solutions

24
Another idea
  • Consider the following code segment
  • For each edge (u, v)
  • If dv gt du wuv
  • dv ? du wuv
  • Assume one of the shortest paths is (s, v1, v2,
    , vk)
  • If dvi its shortest path from s
  • After this loop, dvi1 its shortest path from
    s
  • By MI, After k such loops, found shortest path
    from s to vk

25
Bellman-Ford Algorithm
  • All v1, v2, ,vk distinct
  • for-each v, dv ? 8
  • ds ? 0
  • Do V-1 times
  • for-each (u, v) in E
  • if dv gt du wuv then
  • dv ? du wuv
  • O(VE)
  • Support Negative-weight Edges

26
Negative Cycle
  • A negative cycle is a cycle whose sum of edge
    weights is negative
  • What happens of there are negative cycles in the
    graph?
  • Doesnt matter if the negative cycle is not
    reachable from the source
  • If a negative cycle is reachable from the source,
    can we detect it?
  • Answer one more round of relaxations

27
All-pairs Shortest Path
  • Sometimes we want to find the distances between
    any pair of vertices
  • Use Dijkstras algorithm V times
  • O(VElogV)
  • Use Bellman-Ford V times
  • O(V2E)

28
We want to be better
  • Intermediate vertices may shorten the distance
    between two vertices
  • Label the vertices as v1, v2, , vn
  • Let Vk-path be a path which uses only v1, v2, ,
    vk as intermediate vertices
  • A s-t-V1-path must either be
  • a s-t-V0-path, or
  • concatenation of a s-v1-V0-path and v1-t-V0-path
  • A s-t-V2-path must either be
  • a s-t-V1-path, or
  • concatenation of a s-v2-V1-path and v2-t-V1-path
  • By MI

29
Recurrence Relation
  • A s-t-Vk-path must either be
  • a s-t-Vk-1-path, or
  • concatenation of a s-vk-Vk-1-path and
    vk-t-Vk-1-path
  • dij(k)length of the shortest vi-vj-Vk-path
  • dij(k) wij if k0
  • min(dij(k-1), dik(k-1) dkj(k-1) ) if
    kgt1

30
Warshalls Algorithm
dij(k) wij if k0 min(dij(k-1),
dik(k-1) dkj(k-1) ) if kgt1
  • d ? 8
  • for-each (u, v) in E
  • duv ? wuv
  • for-each k in V
  • e ? d
  • for-each i in V
  • for-each j in V
  • if eij gt dik dkj
  • eij ? dik dkj
  • d ? e
  • Time Complexity O(V3)

31
Warshalls Algorithm
  • d ? 8
  • for-each (u, v) in E
  • duv ? wuv
  • for-each k in V
  • for-each i in V
  • for-each j in V
  • if eij gt dik dkj
  • eij ? dik dkj
  • Time Complexity O(V3)

32
1
3
2
2
1
3
3
3
4
3
1
5
from \ to 1 2 3 4 5
1 8 3 8 2 3
2 8 8 1 8 8
3 8 8 8 8 8
4 8 8 3 8 1
5 8 8 3 8 8
About PowerShow.com