shortest path

38
Shortest Path HKOI 2006 March 11, 2006

Upload: winola

Post on 08-Jan-2016

30 views

Category:

Documents


0 download

DESCRIPTION

Shortest Path. HKOI 2006 March 11, 2006. Graph. GraphG = (V, E) Vertex/NodeV Number|V| or simply V Degree deg[ v ],in-deg[ v ],out-deg[ v ] EdgeE Number|E| or simply E Directione = ( u , v ) or { u , v } Weightw e , w uv E ≤ V 2 i.e.deg[ v ] ≤ |V|. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Shortest Path

Shortest Path

HKOI 2006

March 11, 2006

Page 2: Shortest Path

Graph

• Graph G = (V, E)• Vertex/Node V

– Number |V| or simply V– Degree deg[v],in-deg[v],out-deg[v]

• Edge E– Number |E| or simply E– Direction e = (u, v) or {u, v}

– Weight we, wuv

• E ≤ V2 i.e. deg[v] ≤ |V|

Page 3: Shortest Path

Graph Representations

Adjacency Matrix

Adjacency List Edge List

Memory (V2) (V + E) (E)

Check if (u, v) connected

(1) (out-degree[u]) (E)

List Edges from u (V) (out-degree[u]) (E)

List Edges to v (V) (E) (E)

List All Edges (V2) (E) (E)

Page 4: Shortest Path

Breadth First Search

• BFS needs O(V) queue and O(V) set for visiting state information and runs in O(V + E) time

• BFS can find Shortest Path if Graph is NOT weighted

Page 5: Shortest Path

Order of Vertices• BFS works because a Queue ensures a specific

Order

• Define d[v] be the shortest path from s to v

• At any time, vertices are classified into:– Black With known d[v]– Grey With some known path– White Not yet touched

• The Grey vertex with smallest d[v] has potential to become Black

Page 6: Shortest Path

S

T

3

1

31

32

3

Page 7: Shortest Path

S

T

Page 8: Shortest Path

S

T

Page 9: Shortest Path

S

T

Page 10: Shortest Path

S

T

Page 11: Shortest Path

S

T

Page 12: Shortest Path

S

T

Page 13: Shortest Path

S

T

Page 14: Shortest Path

S

T

Page 15: Shortest Path

S

T

Page 16: Shortest Path

S

T

Page 17: Shortest Path

S

T

Page 18: Shortest Path

S

T

Done?

Page 19: Shortest Path

Weighted Graph• Queue does not promise smallest d[v] anymore

• Expanding path caused unnecessary searching of artificial vertices

• We can simply pick the shortest real vertex

• We need “Sorted Queue” which “dequeues” vertices in increasing order of d[v].

• It is called a “Priority Queue” and negation of d[v] is called the Priority of vertex v

Page 20: Shortest Path

0 3

1

31

32

3

Page 21: Shortest Path

0

3

2

3

1

31

32

3

Page 22: Shortest Path

0

3

3

2

5

3

1

31

32

3

Done?

Page 23: Shortest Path

0

3

3

2

4

3

1

31

32

3

Done?

Page 24: Shortest Path

0

3

3

2

4

3

1

31

32

3

Page 25: Shortest Path

0

3

3

2

4

3

1

31

32

3

Done?

Page 26: Shortest Path

Dijkstra’s Algorithm

for-each v, d[v] ← ∞d[s] ← 0Q.Insert(s,d[s])while not Q.Empty() do

u = Q.ExtractMin()for-each v where (u, v) in E

if d[v] > d[u] + wuv thend[v] = d[u] + wuv

Q.Insert(v,d[v])

Lazy Deletion

Page 27: Shortest Path

Dijkstra’s Algorithm

for-each v, d[v] ← ∞d[s] ← 0Q.Insert(s,d[s])while not Q.Empty() do

u = Q.ExtractMin()for-each v where (u, v) in E

if d[v] > d[u] + wuv thend[v] = d[u] + wuv

Q.DecreaseKey(v,d[v])

Page 28: Shortest Path

Implementations of Priority Queue

Insert ExtractMin DecreaseKey

Array (1) (n) (1)

Sorted Array (n) (1) (n)

Binary Heap (log n) (log n) (log n)

Fibonacci Heap (amortized)

(1) (log n) (1)

Page 29: Shortest Path

ComplexityMemory Time

Array (V) (V2)

Array (Lazy) (E) (E2)

Sorted Array (V) (VE)

Binary Heap (V) (E log V)

Binary Heap (Lazy)

(E) (E log V)

Fibonacci Heap (amortized)

(V) (E + V log V)

Page 30: Shortest Path

Another idea

• Consider the following code segment:For each edge (u, v)

If d[v] > d[u] + wuv

d[v] ← d[u] + wuv

• Assume one of the shortest paths is(s, v1, v2, …, vk)

• If d[vi] = its shortest path from s• After this loop, d[vi+1] = its shortest path from s• By MI, After k such loops, found shortest path from s

to vk

Page 31: Shortest Path

Bellman-Ford Algorithm

• All v1, v2, …,vk distinctfor-each v, d[v] ← ∞d[s] ← 0

Do V-1 timesfor-each (u, v) in E

if d[v] > d[u] + wuv then

d[v] ← d[u] + wuv

• O(VE)• Support Negative-weight Edges

Page 32: Shortest Path

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?

• Doesn’t 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

Page 33: Shortest Path

All-pairs Shortest Path• Sometimes we want to find the distances between any

pair of vertices• 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 …

Page 34: Shortest Path

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 k=0

min(dij(k-1), dik

(k-1) + dkj(k-1) ) if

k>=1

Page 35: Shortest Path

Warshall’s Algorithm

d ← ∞

for-each (u, v) in Ed[u][v] ← wuv

for-each k in Ve ← dfor-each i in V

for-each j in Vif e[i][j] > d[i][k] + d[k][j]

e[i][j] ← d[i][k] + d[k][j]d ← e

• Time Complexity: O(V3)

dij(k) = wij if k=0

min(dij(k-1), dik

(k-1) + dkj(k-1) ) if k>=1

Page 36: Shortest Path

Warshall-Floyd Algorithm

d ← ∞

for-each (u, v) in Ed[u][v] ← wuv

for-each k in Vfor-each i in V

for-each j in Vif d[i][j] > d[i][k] + d[k][j]

d[i][j] ← d[i][k] + d[k][j]

Time Complexity: O(V3)

Page 37: Shortest Path

Summary

• Dijkstra’s Algorithm

• Bellman-Ford Algorithm

• Warshall-Floyd Algorithm

Page 38: Shortest Path

Construction of Shortest Path

• If the whole shortest path from source vertex s to any vertex v is stored during computation, time and space complexities of the algorithms will increase

• For single-source shortest path algorithms, only the parent vertex p[v] of a vertex v is stored

• Shortest path may be constructed based on p[v]• A shortest path tree Gp is formed with root s

• How to contruct shortest paths in Floyd-Warshall algorithm?