1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25...

Post on 16-Jan-2016

217 views 0 download

Transcript of 1 Shortest Path Problem Topic 11 ITS033 – Programming & Algorithms C B A E D F 0 328 58 4 8 71 25...

1

Shortest Path Problem

Topic 11

ITS033 – Programming & Algorithms

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

bunyarit@siit.tu.ac.th02 5013505 X 2005

2

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.vcharkarn.com/vlesson/7

Midterm

3

Weighted Graphs

In a weighted graph, each edge has an associated numerical value, called the weight of the edge.

Edge weights may represent, distances, costs, etc.

4

Weighted Graphs

Example: In a flight route graph, the weight of an edge

represents the distance in miles between the endpoint airports

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

1205

5

Shortest Path Problem

Given a weighted graph and two vertices u and v, we want to find a path of minimum total weight between u and v.

Length of a path is the sum of the weights of its edges.

6

Shortest Path Problem

Example:Find the Shortest path between SFO and MIA

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

1205

7

Shortest Path Problem Example:

Shortest path between HNL and PVD

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

1205

8

Shortest Path Problem

Applications Internet packet routing Flight reservations Driving directions

9

Internet as a graph

10

Shortest Path Properties

Property 1:A subpath of a shortest path is itself a shortest path

Property 2:There is a tree of shortest paths from a start vertex to all the other vertices

11

Shortest Path Properties

Example:Tree of shortest paths from PVD

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

1205

12

Shortest Path Problem: Dijkstra Algorithm

Topic 11.1 ITS033 – Programming & Algorithms

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

bunyarit@siit.tu.ac.th02 5013505 X 2005

13

Dijkstra’s Algorithm single-source shortest-paths problem: for a

given vertex called the source in a weighted

connected graph, find shortest paths to all its other vertices.

The single-source shortest-paths problem asks for a family of paths, each leading from the source to a different vertex in the graph, though some paths may, of course, have edges in common.

The best-known algorithm for the single-source shortest-paths problem,: Dijkstra’s algorithm.

However, this algorithm is applicable to graphs with nonnegative weights only.

14

Shortest-Path Algorithm

Single shortest path problem:Single shortest path problem:Given as input a weighted graph, G = (V,E), and a Given as input a weighted graph, G = (V,E), and a

distinguished vertex, distinguished vertex, ss, find the shortest weighted , find the shortest weighted

path from path from ss to any vertex in G. to any vertex in G.

15

Dijkstra’s Algorithm

Assumptions: the graph is connected the edges are undirected (or directed) the edge weights are nonnegative

16

Dijkstra’s Algorithm

The distance of a vertex v from a

vertex s is the length of a shortest

path between s and v

Dijkstra’s algorithm computes the distances of all the vertices from a

given start vertex s

17

Dijkstra’s Algorithm

Idea of Dijkstra’s algorithm.

The subtree of the shortest paths already found is shown in bold. The next nearest to the source v0

vertex, u*, is selected by comparing the lengths of the subtree’s paths increased by the distances to vertices adjacent to the subtree’s vertices.

18

Dijkstra’s Algorithm First, it finds the shortest path from the

source to a vertex nearest to it,

then to a second nearest, and so on.

Before its ith iteration commences, the algorithm has already identified the shortest paths to i - 1 other vertices nearest to the source.

These vertices, the source, and the edges of the shortest paths leading to them from the source form a subtree Ti of the given graph

19

Dijkstra’s Algorithm

We grow a “group” of vertices, beginning with s and eventually covering all the vertices

We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices

20

Dijkstra’s Algorithm To facilitate the algorithm’s operations, we label each

vertex with two labels.

(1) The numeric label d indicates the length of the shortest path from the source to this vertex found by the algorithm so far; when a vertex is added to the tree, d indicates the length of the shortest path from the source to that vertex.

(2) Label indicates the name of the next-to-last vertex on such a path, i.e., the parent of the vertex in the tree being constructed.

21

Dijkstra’s Algorithm After we have identified a vertex u* to be added

to the tree, we need to perform two operations:

Move u* from the fringe to the set of tree vertices.

For each remaining fringe vertex u that is connected to u* by an edge of weight w(u*, u) such that du* + w(u*, u) < du, update the labels of u by u* and du* + w(u*, u), respectively.

22

Dijkstra’s Algorithm

At each step We add to the cloud the vertex u outside the

cloud with the smallest distance label, d(u)

We update the labels of the vertices adjacent to u

23

Dijkstra’s Algorithm

24

Dijkstra’s Algorithm Analysis The time efficiency of Dijkstra’s algorithm

depends on the data structures used for implementing the priority queue and for representing an input graph itself.

it is in θ(|V |2) for graphs represented by their weight matrix and the priority queue implemented as an unordered array.

For graphs represented by their adjacency linked lists and the priority queue implemented as a min-heap, it is in O(|E| log |V |).

25

Example 2

CB

A

E

D

F

0

442288

48

7 1

2 5

2

3 9

3Step 1Step 1

26

Example

CB

A

E

D

F

0

332288

55 1111

48

7 1

2 5

2

3 9

Step 2Step 2

27

Example

CB

A

E

D

F

0

332288

55 88

48

7 1

2 5

2

3 9

Step 3Step 3

28

Example

CB

A

E

D

F

00

33277

55 88

48

7 1

2 5

2

3 9

Step 4Step 4

29

Example (cont.)

CB

A

E

D

F

0

332277

55 88

48

7 1

2 5

2

3 9

Step 5Step 5

30

Example (cont.)

CB

A

E

D

F

0

332277

55 88

48

7 1

2 5

2

3 9

Step 6Step 6

31

Find shortest path from Melbourne to every town ?

32

Dijkstra’s algorithm – Exercise

33

An example on Directed graph

34

Why Dijkstra’s Algorithm Works

Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.

CB

A

E

D

F

0

327

5 8

48

7 1

2 5

2

3 9

35

Why It Doesn’t Work for Negative-Weight Edges

If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.

CB

A

E

D

F

0

457

5 9

48

7 1

2 5

6

0 -8-8

Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.

C’s true distance is 1, but it is already in the cloud with d(C)=5!

36

Shortest Path Problem: Bellman-Ford Algorithm

Topic 11.2 ITS033 – Programming & Algorithms

CB

A

E

D

F

0

328

5 8

48

7 1

2 5

2

3 9

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

bunyarit@siit.tu.ac.th02 5013505 X 2005

37

Bellman-Ford Algorithm

The Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph (where some of the edge weights may be negative). Dijkstra's algorithm accomplishes the same problem with a lower running time, but requires edge weights to be non-negative. Thus, Bellman–Ford is usually used only when there are negative edge weights.

38

Bellman-Ford Algorithm

Bellman-Ford is in its basic structure very similar to Dijkstra's algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this |V| − 1 times, where |V| is the number of vertices in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since, in the absence of negative cycles, the shortest path can only visit each node at most once.

Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case.

39

Bellman-Ford Algorithm

Works even with negative-weight edges

Must assume directed edges (for otherwise we would have negative-weight cycles)

Running time: O(nm).

40

Bellman-Ford Algorithm

Algorithm BellmanFord(G, s)for all v G.vertices()

if v ssetDistance(v, 0)

else setDistance(v, )

for i 1 to n-1 dofor each e G.edges()

{ relax edge e }u G.origin(e)z G.opposite(u,e)r getDistance(u) weight(e)if r getDistance(z)

setDistance(z,r)

41

Bellman-Ford Example

0

48

7 1

-2 5

-2

3 9

Nodes are labeled with their d(v) values

Step 1Step 1

42

-2

Bellman-Ford Example

0

48

7 1

-2 53 9

Nodes are labeled with their d(v) values

88 -2-2 44

Step 2Step 2

43

Bellman-Ford ExampleNodes are labeled with their d(v) values

-2

-2-28

0

4

48

7 1

-2 53 9

-1-155

611

99

Step 3Step 3

44

Bellman-Ford ExampleNodes are labeled with their d(v) values

-25

0

1

-1

9

48

7 1

-2 5

-2

3 94

Step 4Step 4

45

Summary

• Dijkstra's algorithmDijkstra's algorithmDefinition:Definition: An algorithm to find the shortest paths from a single An algorithm to find the shortest paths from a single source vertex to all other vertices in a weighted, undirected source vertex to all other vertices in a weighted, undirected graph. All weights must be nonnegative. The time complexity O(E graph. All weights must be nonnegative. The time complexity O(E + V log V), where V is the number of vertices and E is the number + V log V), where V is the number of vertices and E is the number of edges. of edges.

• Bellman-Ford ExampleBellman-Ford ExampleWorks even with negative-weight edgesWorks even with negative-weight edgesIteration i finds all shortest paths that use i edges.Iteration i finds all shortest paths that use i edges.Running time: O(nm).Running time: O(nm).

46

Maximum Profit

47

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.vcharkarn.com/vlesson/7

Midterm

48

End of Chapter 11

Thank you!