Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node...

11
Shortest Path

Transcript of Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node...

Page 1: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

Shortest Path

Page 2: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

What is Shortest PathIn a network, there are often many possible paths from one node in a network to another. It is often desirable (for reasons of cost, time, etc.) to find the shortest of these paths.

For straightforward networks, the method of trial and error is usually the easiest way to find the shortest path.

• Find all possible paths/routes from the start node to the finish node

• Calculate the sum of the weights of each path/route

Page 3: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

Trial and ErrorExample

Q. What is the shortest path from A to G in the following network, where each length is measured in kms.

A

F

C E

D

B

G

7

33

25

2 7

1

4

4

4

Page 4: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

For trial and error methods, you would build a table of all possible paths

Path Total Distance

Page 5: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

For trial and error methods, you would build a table of all possible paths

Path Total DistanceA – B – F – G 4 + 4 + 4 = 12

A – B – D – G 4 + 1 + 7 = 12

A – B – D – C – E – G 4 + 1 + 3 + 5 + 2 = 15

A – B – D – E – G 4 + 1 + 2 + 2 = 9

A – D – G 7 + 7 = 14

A – D – E – G 7 + 2 + 2 = 11

A – D – C – E – G 7 + 3 + 5 + 2 = 17

A –C – D – G 3 + 3 + 7 = 13

A – C – D – E – G 3 + 3 + 2 + 2 = 10

A – C – E – G 3 + 5 + 2 = 10

Clearly the shortest path between A and G is: A B D E GThe total distance from A to G is 9 kms.

Page 6: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

Dijkstra’s algorithmTrial and error can be time consuming, and unreliable for complex problems.

For this unit, you need to understand and be able to use Dijkstra’s algorithm.

This algorithm considers all possible routes by summing the distances as we add each edge.

Page 7: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

Dijkstra’s algorithmStep 1: Represent each node with an empty circle. Label the starting point

circle 0 (i.e. the distance from the start).

Step 2: Find the smallest total you can get by starting with a circled number, and adding an edge from there to an empty circle. When you have found the smallest total, fill in the empty circle with that total and add a back arrow on that edge.

Step 3: Continue the process, repeating Step 2 until you have written a total in the end circle.

Step 4: Follow the back arrows from the end point back to the start point to determine the shortest path.

The shortest path is ………The length of the shortest path is ……..

Page 8: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

What is Shortest PathExample

A

F

C

D

E

B

G

7

33

25

2 7

1

4

4

4

Page 9: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

What is Shortest PathWhat is the shortest path from A to F in this network?

Start

AB 1

ACB

2+1=3

AC 2

ABC 1+1=2

ABD 1+3=4

ACBD

2+1+3=6

ACD 2+1=3

ABCD

1+1+1=3

ABE 1+2=3

ACBE

2+1+2=5

ACE 2+2=4

ABCE

1+1+2=4

ACDF 2+1+3=6

ABCDF

1+1+1+3=6

ABEF 1+2+3=6

Page 10: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

Minimum Cost

Page 11: Shortest Path. What is Shortest Path In a network, there are often many possible paths from one node in a network to another. It is often desirable (for.

Another ProblemA new airline is setting up a network of flights between New Zealand towns and cities. This matrix shows the towns and cities, and the distances of possible flights.

Auckland

Tauranga

Whakatane

Hamilton

Rotorua

Wellington

Auckland 150 160

Tauranga 70 120 90

Whakatane 65

Hamilton 80 350

Rotorua 400

Wellingtona) At the start of the operation the airline wants to ensure all towns and cities are

connected, with a minimum total distance for the flight paths. b) How would the tree be adjusted if it is essential that there are flights between

Auckland and Hamilton and between Auckland and Tauranga?c) What is the shortest distance that a customer can fly to get from Auckland to

Wellington?d) A flight inspector is based in Wellington. He wants to fly on all the routes to inspect

the flight services. Investigate this and suggest a shortest route he might take.