- By
**sol** - Follow User

- 74 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Shortest Paths' - sol

**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

### Shortest Paths

08-07-2006

PowerPoint adapted from Alan Tam’sShortest Path, 2004

with slight modifications

Breadth First Search (BFS)

- Techniques to Enumerate all Vertices for Goal
- BFS needs O(V) queue and O(V) set for duplicate elimination and runs in O(V + E) time
- BFS can find Shortest Path if Graph is Not Weighted
- BFS works because a Queue ensures a specific Order

What Order?

- Define d[v] be the length of 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 shortest known path has potential to become Black
- The Head of Queue is always the case in an unweighted graph
- Why?

Weighted Graph

- Queue does not promise smallest d[v] anymore
- Expanding weighted edge causeds unnecessary searching of artificial vertices
- We can simply pick the real vertex nearest to the starting 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

Dijkstra’s Algorithm

d ← ∞

d[s] ← 0

enqueue(queue, s)

whilenot empty(queue) do

u = dequeue(queue)

for-eachvwhere (u, v) in E

if d[v] > d[u] + wuvthen

d[v] = d[u] + wuv

enqueue(queue, v)

- Note that d[v] represents the known shortest distance of v from s during the process and may not be the real shortest distance until v is marked black.

Requirements for Priority Queue

- Map<V, int>
- Store temporary values of d[v]

- PriorityQueue<V, function: V→int >
- Extract vertices in increasing order of d[v]

- Initially all d[v] are infinity
- When we visit a White vertex
- Set d[v]
- Insert v into Queue

- When we visit a Gray vertex
- Update d[v]
- What to do to the Priority Queue?

Insert: V

Extract: V

Decrease: E - V

Array

Insert: O(1)

Extract: O(n)

Decrease: O(1)

Sorted Array

Insert: O(n)

Extract: O(1)

Decrease: O(n)

Heap

Insert: O(lg n)

Extract: O(lg n)

Decrease: O(lg n)

Fibonacci Heap

Insert: O(1)

Extract: Amortized O(lg n)

Decrease: O(1)

Implementing Priority Queue?Another Attempt

- 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

Bellman-Ford Algorithm

- All v1, v2, …,vk distinct?
Do V-1 times

for-each (u, v) in E

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

d[v] ← d[u] + wuv

- Order = O(VE)
- Support Negative-weight Edges

Another Attempt

- Number vertices as v1, v2, …, vV
- Define no≤k-path as a path which does not pass through vertices v1, v2, …, vk
- A path s-t must either be
- a no≤1-path, or
- concatenation of a no≤1-path s-v1 and a no≤1-path v1-t

- A no≤1-path s-t must either be
- a no≤2-path, or
- concatenation of a no≤2-path s-v2 and a no≤2-path v2-t

- By MI …

For all R

For all S

Direct Path

from R to S

R

S

Direct Path

from R to T

For all S

For all T

Direct Path

from S to T

S

T

For S = vV

For all R

For all S

Shortest Path

from R to Svia vV

R

S

Shortest Path

from R to Tvia vV

For all S

For all T

Shortest Path

from S to Tvia vV

S

T

For S = vV-1

For all R

For all S

Shortest Path

from R to Svia v3,v4,...,vV

R

S

Shortest Path

from R to Tvia v3,v4,...vV

For all S

For all T

Shortest Path

from S to Tvia v3,v4,...,vV

S

T

For S = V2

For all R

For all S

Shortest Path

from R to Svia v2,v3,...,vV

R

S

Shortest Path

from R to Tvia v2,v3,...vV

For all S

For all T

Shortest Path

from S to Tvia v2,v3,...,vV

S

T

For S = V1

Warshall-Floyd Algorithm

for-each (u, v) in E

d[u][v] ← wuv

for-eachi in V

for-eachjin V

for-eachkin V

if d[j][k] > d[j][i] + d[i][k]

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

- Time Complexity: O(V3)

Graph Modeling

- Conversion of a problem into a graph problem
- Sometimes a problem can be easily solved once its underlying graph model is recognized
- Graph modeling appears almost every year in NOI or IOI
(cx, 2004)

Basics of Graph Modeling

- A few steps:
- identify the vertices and the edges
- identify the objective of the problem
- state the objective in graph terms
- implementation:
- construct the graph from the input instance
- run the suitable graph algorithms on the graph
- convert the output to the required format
(cx, 2004)

Download Presentation

Connecting to Server..