Loading...

PPT – Dijkstra's Algorithm PowerPoint presentation | free to download - id: 6851fb-MzM4Y

The Adobe Flash plugin is needed to view this content

Dijkstra's Algorithm

- Using a Weighted Graph

Objective

- You will be able to
- Describe an ADT for a weighted graph.
- Implement an ADT for a weighted graph.
- Implement Dijkstra's algorithm for finding the

minimum cost path between two nodes in a weighted

graph.

Edsger Dijkstra

1930 - 2002

- 1959 Shortest Path Algorithm (Graph Theory)
- 1968 The "THE" Operating System
- 1968 "Go To Consider Harmful" (Niklaus Wirth,

Editor) - 1972 ACM Turing Award
- 1984 - 2002 University of Texas
- http//en.wikipedia.org/wiki/Edsger_W._Dijkstra

A Weighted Graph ADT

- A Set of Nodes (Vertices)
- Numbered 1 .. N
- A Set of bidirectional Edges (pairs of Nodes)
- A nonnegative weight for each edge.
- "Cost" or "Distance"

Problem Find Best Path

- Find a Path from a specified starting node to a

specified destination node such that the sum of

the weights of the edges is minimized.

A Weighted Graph ADT

- Create a new project.
- Weighted_Graph_Demo
- Add main.cpp
- Start with "Hello, World!"

main.cpp

- include ltiostreamgt
- using namespace std
- int main (void)
- cout ltlt "This is the Weighted Graph Demo\n"
- cin.get()
- return 0

Weighted_Graph.h

- Add new item
- Weighted_Graph.h
- This will be our weighted graph ADT template.
- Will represent the graph with an adjacency

matrix. - distanceij is distance from node i to node j
- weight of the edge between node i and node j
- 0 if no edge between i and j

The Weighted Graph ADT

- Nodes will be an arbitrary type.
- Template parameter T
- Must define operators , , and ltlt
- Externally nodes are identified by objects of

class T. - Example Strings
- Internally nodes are identified by integer Node

IDs, 1 ... N

Weighted_Graph.h

- pragma once
- ifndef MAX_NODES
- define MAX_NODES 100
- endif
- using namespace std
- template lttypename Tgt
- class Weighted_Graph
- private
- int number_of_nodes
- // The Node ID 0 is not used. The first real

node has ID 1 - T nodesMAX_NODES1
- int distanceMAX_NODES1MAX_NODES1
- public
- Weighted_Graph()

Implementation

- Constructor
- template lttypename Tgt
- Weighted_GraphltTgtWeighted_Graph()

number_of_nodes(0)

Add_Node

- template lttypename Tgt
- void Weighted_GraphltTgtAdd_Node (const T node)
- assert(number_of_nodes lt MAX_NODES)
- int n number_of_nodes
- nodesn node
- for (int i 1 i lt n i)
- distancein 0
- distanceni 0

Add_Edge

- template lttypename Tgt
- void Weighted_GraphltTgtAdd_Edge(const T Node_1,

- const T Node_2,

- int dist)
- int Node_ID_1 Get_Node_ID(Node_1)
- int Node_ID_2 Get_Node_ID(Node_2)
- assert ((Node_ID_1 gt 0) (Node_ID_2 gt 0))
- distanceNode_ID_1Node_ID_2 dist
- distanceNode_ID_2Node_ID_1 dist
- Add at top
- include ltcassertgt

Get_Node_ID

- template lttypename Tgt
- int Weighted_GraphltTgtGet_Node_ID(const T node)

const - for (int i 1 i lt number_of_nodes i)
- if (nodesi node)
- return i
- cout ltlt "Node " ltlt node ltlt " not found\n"
- return -1

Get_Node_ID

- Also add to class definition.
- private
- int Get_Node_ID(const T Node) const

Display

- template lttypename Tgt
- void Weighted_GraphltTgtDisplay() const
- cout ltlt endl
- cout ltlt setw(10) ltlt " "
- for (int i 1 i lt number_of_nodes i)
- cout ltlt setw(10) ltlt nodesi ltlt " "
- cout ltlt endl
- for (int i 1 i lt number_of_nodes i)
- cout ltlt setw(10) ltlt nodesi
- for (int j 1 j lt number_of_nodes

j) - cout ltlt setw(10) ltlt distanceij ltlt

" " - cout ltlt endl

Weighted_Graph.h

- Add at top
- include ltiostreamgt
- include ltiomanipgt

Example Airline connections

1000

900

600

1400

780

1300

200

800

160

main.cpp

- include ltiostreamgt
- include ltstringgt
- include "Weighted_Graph.h"
- using namespace std
- Weighted_Graphltstringgt connections

Add to main()

- connections.Add_Node(string("Atlanta"))
- connections.Add_Node(string("Austin"))
- connections.Add_Node(string("Chicago"))
- connections.Add_Node(string("Dallas"))
- connections.Add_Node(string("Denver"))
- connections.Add_Node(string("Houston"))
- connections.Add_Node(string("Washington"))
- connections.Add_Edge(string("Atlanta"),

string("Denver"), 1400) - connections.Add_Edge(string("Atlanta"),

string("Houston"), 800) - connections.Add_Edge(string("Atlanta"),

string("Washington"), 600) - connections.Add_Edge(string("Austin"),

string("Dallas"), 200) - connections.Add_Edge(string("Austin"),

string("Houston"), 160) - connections.Add_Edge(string("Chicago"),

string("Dallas"), 900) - connections.Add_Edge(string("Chicago"),

string("Denver"), 1000) - connections.Add_Edge(string("Dallas"),

string("Denver"), 780) - connections.Add_Edge(string("Dallas"),

string("Washington"), 1300) - connections.Display()

Program Running

Dijkstra's Algorithm

- Given a starting node and a destination node
- Push outward from the starting node
- Keeping track of the shortest total distance seen

so far to each node and the predecessor to that

node on the shortest path seen so far. - Initially if the node is adjacent to the start
- Shortest total distance seen so far is the weight

of the link - Predecessor on best path is the start
- If node is not adjacent
- Shortest distance seen so far is infinity

(INT_MAX)

Dijkstra's Algorithm

- On each step of the iteration, the shortest total

distance to one more node will be determined - And its predecessor on the best path to it from

the starting node. - Keep track of which nodes we know the shortest

distance to. - Boolean array indexed by Node ID

Dijkstra's Algorithm

- At each step of the iteration
- Determine a node with smallest "Best total

distance seen so far" among the nodes for which

the best total distance has not yet been

determined. - Call it node N.
- The best distance so far for node N is the actual

best distance. - The predecessor for which that distance was

determined is is predecessor on the best path. - The best path to that node is now known.

Dijkstra's Algorithm

- Update best distances
- For each node, i, for which the best path has not

yet been determined -- - Check if the node N provides a better path than

the best seen so far. - Is Total DistanceN Distance(N,i) less than

Best Total Distance Seen So for for Node i? - If so, make that the new best total distance so

far and make node N the predecessor.

Dijkstra's Algorithm

- Continue the iteration until the actual shortest

total distance for the destination has been

determined. - We then know the shortest path length from Start

to Destination and the best path. - Follow predecessors from Destination back to

Start.

Best_Path

- Add to Weighted_Graph class defintion
- include ltdequegt
- public
- ...
- dequeltTgt Best_Path(const T Start, const T

Dest) const - Will return the best path from Start to Dest to

the caller in the form of an STL deque. - doubly ended queue

Best_Path

- // Dijkstra's Algorithm
- template lttypename Tgt
- dequeltTgt Weighted_GraphltTgtBest_Path(const T

Start, const T Dest) const - dequeltTgt best_path
- int best_total_distance_so_farMAX_NODES1
- int predecessorMAX_NODES1
- bool best_total_distance_is_knownMAX_NODES1

- if (Dest Start)
- cout ltlt "Dest Start in call to

Best_Path\n" - return best_path // Return empty deque.
- int Start_ID Get_Node_ID(Start)
- int Dest_ID Get_Node_ID(Dest)
- if ((Start_ID lt 0) (Dest_ID lt 0))

Best_Path

- for (int i 1 i lt MAX_NODES i)
- if (distanceStart_IDi gt 0)
- best_total_distance_so_fari

distanceStart_IDi - predecessori Start_ID
- else
- best_total_distance_so_fari INT_MAX
- predecessori -1
- best_total_distance_is_knowni false
- best_total_distance_so_farStart_ID 0
- best_total_distance_is_knownStart_ID true

Best_Path (continued)

- while (!best_total_distance_is_knownDest_ID)
- // Determine the node with least distance

among - // all nodes whose best distance is not yet

known. - int min_best_dist INT_MAX
- int best_node_id -1
- for (int i 1 i lt number_of_nodes i)
- if (best_total_distance_is_knowni)
- continue
- if (best_total_distance_so_fari lt

min_best_dist) - min_best_dist best_total_distance_so

_fari - best_node_id i

Best_Path (continued)

- if (best_node_id -1)
- // Destination is unreachable.
- cout ltlt Dest ltlt " is unreachable from " ltlt

Start ltlt endl - return best_path // empty deque
- // Best total distance so far for this node is

the actual - // best total distance .
- int n best_node_id
- best_total_distance_is_knownn true

Best_Path (continued)

- // Check if this node provdes a better route

to the destination - // for other nodes whose best distance is not

yet known. - for (int i 1 i lt number_of_nodes i)
- if (best_total_distance_is_knowni)
- continue
- if (distanceni lt 0)
- continue // No connection from node

n to node i - if ((best_total_distance_so_farn

distanceni) lt - best_total_distance_so_fari)
- // It does.
- best_total_distance_so_fari

Best_Path (continued)

- // At this point we know predecessor of each

node on the - // best path from Start to Dest
- best_path.push_front(Dest)
- int next_node_id Dest_ID
- while (next_node_id ! Start_ID)
- next_node_id predecessornext_node_id
- best_path.push_front(nodesnext_node_id)

- return best_path

main.cpp

- void Show_Best_Path(string Start, string Dest)
- dequeltstringgt best_path connections.Best_Pat

h(Start, Dest) - if (best_path.size() 0)
- cout ltlt "No path found\n"
- else
- cout ltlt "Best path\n"
- while (best_path.size() gt 0)
- string next best_path.front()
- best_path.pop_front()
- cout ltlt next ltlt endl
- cout ltlt endl

main.cpp

- connections.Display()
- cout ltlt endl
- while (true)
- string start
- string dest
- cout ltlt "Start "
- getline(cin, start)
- cout ltlt "Destination "
- getline(cin, dest)
- Show_Best_Path(start, dest)
- cin.get()
- return 0

Program in Action