Loading...

PPT – Paths in Graphs PowerPoint presentation | free to download - id: 691502-ZGE2Y

The Adobe Flash plugin is needed to view this content

- Paths in Graphs
- Oscar Miguel Alonso M

Outline

- The problem to be solved
- Breadth first search
- Dijkstra's Algorithm
- Bellman-Ford Algorithm
- Shortest path in DAGs
- Efficient implementation of Dijkstra (Heap)
- Bonus graph diameter, all to all shortest path

(Floyd algorithm)

Paths in graphs

- Find the shortest path between two nodes
- Are all the edges of the same cost?
- Are there edges with negative cost?
- Is the graph directed?
- Are there cycles of negative cost?
- Given a graph, which are the most distant nodes?

Breadth first search

- Idea
- You begin with one node S
- Then, visit all the nodes at distance 1 from S
- Then, visit the all nodes at distance 2 from S
- You continue until you meet your target node or

you have reached all the nodes

Breadth first search

s

Breadth first search

s

1

1

1

Breadth first search

s

1

2

1

1

Breadth first search

- BFS(G,s)
- Input Graph G(V,E), node s
- Output array dist // distu is the

distance from s to u - Vars Queue Q
- for-each u in V
- distu8
- dist s0
- Q.add(s)
- while(!Q.empty())
- u Q.get()
- for-each edge (u,v) in E
- if distv8
- Q.add(v)
- distvdistu1

O(E)

Breadth first search

0

s

2

3

1

4

Application of the algorithm see whiteboard

Dijkstra's algorithm

- Edge Relaxation
- if(distu gt distkWku)
- distu distkWku
- Used in weighted graphs
- Do not use it if there are negative weighs!

Dijkstra's algorithm

- Idea
- Keep a list of nodes which you already know the

shortest path to - Such list begin with only one node (s), and you

add one node in each iteration of the algorithm - The node added is connected to one node in the

list, and is the one with minimum distance to the

origin from all candidates

Dijkstra's algorithm

0

0

3

1

2

1

3

2

3

3

3

1

4

Dijkstra's algorithm

0

0

3

1

2

1

3

2

3

3

2

3

1

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

3

3

2

3

1

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

4

3

3

2

3

1

3

4

Dijkstra's algorithm

- For the implementation
- Keep an array indicating the minimum distance so

far to each node (initialize with infinite) - Update the distance each time you add a node

(relax the edges from that node) - Keep a list of the nodes not reached, instead of

the ones reached - Now, instead of searching the node with minimum

distance, just pick the one with lower distance

Dijkstra's algorithm

0

8

3

1

8

2

1

3

2

8

3

3

8

3

1

8

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

8

3

3

2

3

1

3

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

5

3

3

2

3

1

3

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

4

3

3

2

3

1

3

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

4

3

3

2

3

1

3

4

Dijkstra's algorithm

0

0

3

1

3

2

1

3

2

4

3

3

2

3

1

3

4

Dijkstra's algorithm

- DIJKSTRA(G,s)
- for-each i in V
- di ? 8
- Q.add(i)
- ds ? 0
- while not empty(queue) do
- u Q.extract_min() // take node with

minimum dist - for-each v where (u, v) in E
- if dv gt du wuv then
- dv du wuv
- Note that dv 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.

(No Transcript)

Bellman-Ford algorithm

- Useful when there are negative-weighted edges
- Idea relax all the edges n times
- A path can be found with max n-i edge relaxations
- If after that, the graph allow other edge

relaxation, the graph has a negative weight cycle

5

-3

-3

Bellman-Ford algorithm

Do n-1 times for-each (u, v) in E if

dv gt du wuv then dv ? du

wuv Complexity O(VE)

Shortest path in DAGs

- In DAGs the problem is easier
- Idea sort the nodes (topological sort), and

traverse the nodes, relaxing all their edges

- ShortestPath-DAG(G,s)
- for-each i in V
- di ? 8
- ds ? 0
- TS TopologicalSort(G)
- for-each u in TS
- for-each v where (u, v) is and edge
- if dv gt du wuv then
- dv du wuv

O(E)

Shortest path in DAGs

(No Transcript)

(No Transcript)

Heap data structure

Example extract-min

Heap data structure

Heap data structure

Heap data structure

Warshall-Floyd Algorithm

- All to all shortest path
- dij is the minimum distance from vertex i to

vertex j - the diameter of the graph is the maximum dij

after the execution of the algorithm

Warshall-Floyd Algorithm

Floyd(G) for-each (u, v) in E duv ?

wuv for-each k in V for-each i in

V for-each j in V if dij gt dik

dkj dij ? dik dkj Time

Complexity O(V3)

- THANK YOU !!