6
Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

Embed Size (px)

Citation preview

Page 1: Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

Discrete Maths

Chapter 4: Finding the Shortest Path

Lesson 1: Dijkstra’s Algorithm

Page 2: Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

Dijkstra’s Algorithm• Dijkstra’s algorithm is used in problems relating to finding

the shortest path.

• Each node is given a temporary label denoting the length of the shortest path from the start node so far.

• This label is replaced if another shorter route is found.

• Once it is certain that no other shorter paths can be found, the temporary label becomes a permanent label.

• Eventually all the nodes have permanent labels.

• At this point the shortest path is found by retracing the path backwards.

Page 3: Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

Dijkstra’s Algorithm

• Step 1: Label the start node with zero and box this label.

• Step 2: Consider the node with the most recently boxed label. Suppose this node to be X and let D be its permanent label. Then, in turn, consider each node directly joined to X but not yet permanently boxed. For each such node, Y say, temporarily label it with the lesser of D + (the weight of arc XY) and its existing label (if any).

• Step 3: Choose the least of all temporary labels on the network. Make this label permanent by boxing it.

• Step 4: Repeat Steps 2 and 3 until the destination node has a permanent label.

• Step 5: Go backwards through the network, retracing the path of shortest length from the destination node to the start node.

Aim: To find the shortest path connecting two nodes

Page 4: Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

EXAMPLE 1: Find the shortest path from A to G

Page 5: Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

EXAMPLE 2: Find the shortest path from A to G

Page 6: Discrete Maths Chapter 4: Finding the Shortest Path Lesson 1: Dijkstra’s Algorithm

EXAMPLE 3: Find the shortest path from A to D