Presentation is loading. Please wait.

Presentation is loading. Please wait.

Shortest Path Algorithms

Similar presentations


Presentation on theme: "Shortest Path Algorithms"— Presentation transcript:

1 Shortest Path Algorithms

2 Single-pair shortest paths. Path from a single vertex to another one.
Dijkstra  Single-source shortest paths. Bellman-Ford  Single-source shortest paths (having negative edges). Floyd-Warshall  All-pairs shortest paths.

3 The shortest path algorithms are effected by:
Directed/Undirected graphs. Weights of the edges (positive or negative). Representation of the graph in the memory, matrix or a linked list (effecting the performance). Single or all pairs paths. Dense or sparse graphs...

4

5 Shortest Path Unweighted path length is simply the number of edges on the path, namely N-1.

6 Single-Source Shortest Path Problem
Given as input a weighted graph, G = (V,E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G. 2 v1 v2 10 4 1 3 2 v3 v4 2 v5 8 4 6 5 1 v6 v7 Shortest (Weighted) Path from V1 to V6 is: v1, v4, v7, v6. Cost = 6

7 Single-Source Shortest Path Algorithms
We will examine different algorithms: Unweighted shortest path algorithm Weighted shortest path algorithm Assuming no negative edges.

8 Weighted Shortest Paths
Dijkstra’s Algorithm Example of a greedy algorithm Do the best thing in each step. At each state, the algorithm chooses a vertex v, which has the smallest distance to s(d[v]) among all the unknown vertices. Then, the adjacent nodes of v (which are denoted as u) are updated with new distance information.

9

10

11

12 Using Array Data Structure

13

14

15

16

17

18

19

20

21

22

23

24 Shortest Path Example: Using Heap Structure
Weighted Directed Graph G is shown! v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 We are interested in all shortest paths to all nodes from node v1

25 Initial Configuration
vertex known Distance to S Previous node (parent) v1 F v2 v3 v4 V5 V6 V7

26 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 V1 0

27 V4 1 V2 2 2 2 v1 v1 v2 10 4 1 3 2 2 v3 v4 v5 1 8 4 6 5 1 v6 v7

28 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9 V2 2 V5 3 V3 3 V7 5

29 V5 3 V3 3 V7 5 V6 9 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9

30 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9 V3 3 V7 5 V6 9

31 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 9 V7 5 V6 8

32 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 V6 6

33 v1 v2 v5 v3 v6 v7 4 2 1 5 8 3 10 6 v4 Finished! Empty Heap

34 Final Configuration vertex known Distance to S Previous node v1 T v2 2
v2 2 v3 3 v4 1 V5 V6 6 v7 V7 5

35 Unweighted Shortest Paths

36 Unweighted Shortest Paths
v1 v2 v3 v4 v5 v6 v7 Assume all edges are unweighted. We are interested in all shortest paths to all nodes from a given node, s.

37

38

39

40

41

42

43 Example Assume that we want to find out all the shortest paths to all nodes from node v3. v1 v2 S v3 v4 v5 v6 v7

44 Vertex Adjacent Vertices v3 v1, v6 1 v1 v2 S v3 v3 v4 v5 v6 v7 1

45 Vertex Adjacent Vertices v1 v2, v4 2 1 2 1 v1 v1 v2 S v3 v3 v4 v5 v6
2 1 v6 v7

46 Vertex Adjacent Vertices v2 v4, v5 2 1 3 2 1 v1 v1 v2 v2 S v3 v3 v4 v5
2 1 v6 v7

47 Vertex Adjacent Vertices v4 v6, v7 2 1 3 2 1 3 v1 v1 v2 v2 S v3 v3 v4
2 1 v6 v7 3

48 Vertex Adjacent Vertices v5 v4, v7 2 1 3 2 1 3 v1 v1 v2 v2 S v3 v3 v4
2 1 v6 v7 3

49 Vertex Adjacent Vertices v6 none v7 2 1 3 2 1 3 v1 v1 v2 v2 S v3 v3 v4
2 1 v6 v6 v7 v7 3

50 Algorithm – Initial Configuration
vertex known Distance to S Previous node v1 F v2 v3 v4 V5 V6 V7

51 Last Configuration vertex known Distance to S Previous node v1 T ? v2


Download ppt "Shortest Path Algorithms"

Similar presentations


Ads by Google