shortest path graph theory basics anil kishore. introduction weighted graph – edges have weights...

27
Shortest Path Graph Theory Basics Anil Kishore

Upload: moses-hawkins

Post on 04-Jan-2016

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Shortest Path

Graph Theory Basics

Anil Kishore

Page 2: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Introduction

• Weighted Graph – Edges have weights• Shortest Path problem is finding a path

between two vertices such that the sum of the weights of edges along the path is minimized

A

B C

D

E

F

G

20

4

6

25

8

3

2

Page 3: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Variations

• Single-Source Shortest Path - find shortest paths from a source vertex S to all other vertices

• Single-Destination Shortest Path - find shortest paths from all vertices in the directed graph to a single destination vertex D

• All-Pairs Shortest Path - find shortest paths between every pair of vertices u, v in the graph.

Page 4: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Applications

• Road Network– Cities in a country can be considered as vertices and the

roads connecting them are edges– Edge weight is length of the road ( distance to be

travelled )– One-way roads ( Directed Edges )– Find shortest path from city A to city B

• … many other

Page 5: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Algorithms

• Dijkstra's algorithm : single-source shortest path problem

• Bellman–Ford algorithm : single-source shortest path problem if edge weights may be negative.

• Floyd–Warshall algorithm : all pairs shortest paths.

Page 6: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s AlgorithmDijkstra( S ) set dist[u] = INF for all vertices u

set dist[S] = 0 and insert S in a data structure q while( q is not empty )

u = remove the vertex with minimum dist in q

for v in nbrs(u)newDist = dist[u] + weight(u,v)if( newDist < dist[v] )

dist[v] = newDist;end-if

end-forend-while

end-Diijkstra

Page 7: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

Page 8: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

Page 9: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

6

Page 10: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

6

Page 11: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

69

12

Page 12: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

69

12

Page 13: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

68

12

Page 14: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

68

12

Page 15: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

68

12

Page 16: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

68

12

Page 17: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

68

12

15

Page 18: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

20

4

68

12

15

Page 19: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

17

4

68

12

15

Page 20: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

0

17

4

68

12

15

Page 21: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm example

A

B C

D

E

F

G

4

6

2

8

3

2

0

17

4

68

12

15

Shortest Path Tree

Page 22: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Dijkstra’s Algorithm Analysis

• Running time depends mainly on the associated data structure q

• q– Array : O(n2)– Binary Heap : O( (n+m) logn )– Fibonacci Heap : O( m + n logn )

• What if the edge weights are negative ?

Page 23: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Bellman-Ford Algorithm

Bellman Ford( S )set dist[u] = INF for all udist[S] = 0;Repeat (n-1) times

for all edges (u,v) in the graphif(dist[v] > dist[u] + weight(u,v) )

dist[v] = dist[u] + graph[u][v];end-if

end-forend-repeat

end-Bellman-Ford // Complexity : O( n m )

Page 24: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Bellman-Ford Algorithm example

A

B C

D

E

F

G

20

4

6

25

8

3

2

Page 25: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

All-Pairs Shortest Path

• Repeat Single-Source Shortest Path n times

• Matrix Exponentiation – Recursive Squaring

• Dynamic Programming – Floyd-Warshall

Page 26: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

Floyd-Warshall AlgorithmFloyd-Warshall

set dist[u][v] = INF for all u,vfor each edge (u,v) in the graph

dist[u][v] = weight(u,v)end-for// Each stage has shortest paths using intermediate vertices (1..k−1)for k: 1 to n for u : 1 to n

for v : 1 to ndist[u][v] = minimum( dist[u][v], dist[u][k] + dist[k][v] )

end-for-v end-for-uend-for-k

end-floyd-warshall // Complexity : O( n3 )

Page 27: Shortest Path Graph Theory Basics Anil Kishore. Introduction Weighted Graph – Edges have weights Shortest Path problem is finding a path between two vertices

References

• http://en.wikipedia.org/wiki/Shortest_path_problem

• Introduction to AlgorithmsThomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein