Loading...

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

The Adobe Flash plugin is needed to view this content

Shortest Path and Minimum Spanning Tree

- HKOI Training 2009
- 7 Feb 2009

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

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)

Graph Modelling

S

E

Assuming you can only move between grey cells.

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?

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

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)

S

3

2

1

3

T

3

3

1

S

T

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

S

3

2

1

3

T

3

3

1

S

3

2

1

3

T

3

3

1

S

3

2

1

3

T

3

3

1

We can see the end now!

S

3

2

1

3

T

3

3

1

S

3

2

1

3

T

3

3

1

S

3

2

1

3

T

3

3

1

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)

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

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)

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)

Dijkstras Algorithm

- Edsger Dijkstra is doing a great job!
- Can we now use it on all kinds of graph?

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

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

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

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

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

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)

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

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

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)

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)

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