graphs shortest path
TRANSCRIPT
-
7/29/2019 Graphs Shortest Path
1/35
James Tam
Graph Traversal Algorithms
What you know:-How to traverse a graph
What you will learn:-How to traverse a graph in an optimal fashion
-
7/29/2019 Graphs Shortest Path
2/35
James Tam
Categories Of Shortest Path Algorithms
Unweighted shortest paths
Weighted shortest paths
-
7/29/2019 Graphs Shortest Path
3/35
James Tam
Unweighted Graphs
1 1 1
1
1
1
1 1
1 1
1
1
1
A B C D E F G H I
A
B
C
D
E
F
G
H
I
-
7/29/2019 Graphs Shortest Path
4/35
James Tam
Unweighted Shortest Paths
There is an equal cost incurred from traveling from one node to
another (traversing an arc)
To find the shortest path, traverse the series of nodes which
requires the fewest arcs to be traversed.
-
7/29/2019 Graphs Shortest Path
5/35
James Tam
An Application Of Finding The Shortest Path
Airplane flights: Minimizing the number of cities to pass
through (stop-overs)
Calgary
Houston
San Jose
Miami
Vancouver
LosAngeles
-
7/29/2019 Graphs Shortest Path
6/35
James Tam
Example Of An Unweighted ShortestPath Traversal: A -> F
C
A
D
G
B
F
E
-
7/29/2019 Graphs Shortest Path
7/35James Tam
Definition Of The Node Class
public class Node
{
private boolean visited;
private int pathLength;
private Node predecessor;
private List neighbors;
: : :
}
-
7/29/2019 Graphs Shortest Path
8/35James Tam
Unweighted Shortest Path Traversal: Algorithm
unweightedTraversal (Node start, Node end)
{
boolean done = false;
Queue nodeQueue = new Queue ();
Node temp;
Node nextNeighbor;
start.setVisited (true);
start.setPathLength (0);
nodeQueue.enqueue (start);
-
7/29/2019 Graphs Shortest Path
9/35James Tam
Unweighted Shortest Path Traversal: Algorithm (2)
while ((done == false) && (nodeQueue.isEmpty() == false))
{
temp = nodeQueue.dequeue();
while ((done == false) && (temp.hasUnvisitedNeighbor())
{
nextNeighbor = temp.getNextUnvistedNeighbor();
nextNeighbor.setVisited(true);
nextNeighbor.setPathLength (temp.getPathLength() + 1);
nextNeighbor.setPredecessor (temp);
nodeQueue.enqueue (nextNeighbor);if (end.equals (nextNeighbor) == true)
done = true;
}
}
}
-
7/29/2019 Graphs Shortest Path
10/35James Tam
The Worse Possible Case For DeterminingThe Shortest Path?
-
7/29/2019 Graphs Shortest Path
11/35James Tam
Efficiency Of The Shortest Path Algorithm:No Path Weights?
O (n2)
while ((done == false) && (nodeQueue.isEmpty() == false))
{
: : : : :
while ((done == false) && (temp.hasUnvisitedNeighbor())
{
: : : : :
}
}
-
7/29/2019 Graphs Shortest Path
12/35James Tam
Finding The Shortest Weighted Path
Origin
Destination
-
7/29/2019 Graphs Shortest Path
13/35James Tam
Finding The Shortest Weighted Path
Origin
Destination
100
75
60
-
7/29/2019 Graphs Shortest Path
14/35James Tam
Finding The Shortest Weighted Path
Origin
Destination
100
75
60
Pick the choice that appears to be the best choice at aparticular stage
-
7/29/2019 Graphs Shortest Path
15/35James Tam
Finding The Shortest Weighted Path
Origin
Destination
100
75
60
100
25
120
-
7/29/2019 Graphs Shortest Path
16/35James Tam
Finding The Shortest Weighted Path
Origin
Destination
100
75
60
100
25
120
Based on the information received at the next stage: pick
the choice that is currently the best option
-
7/29/2019 Graphs Shortest Path
17/35
-
7/29/2019 Graphs Shortest Path
18/35James Tam
An Application Of Finding The ShortestWeighted Path
Hotel Local clubsLocal malls
Arenal volcano
Beaches
Riding
Hiking
Canopy tour
1 0.1
64
2
2
8 5
X
X X
X
http://www.csbsju.edu/bcll/images/Costa%20Rica/arenal-volcano.jpghttp://www.mauioceanfronthome.com/newimages/Costa%20Rica%20beach%20in%20evening.jpghttp://www.csbsju.edu/bcll/images/Costa%20Rica/arenal-volcano.jpg -
7/29/2019 Graphs Shortest Path
19/35James Tam
An Example Of Finding The ShortestWeighted Path: Start
C
A0
D
G
B
F
E
4
2
110
22
3
5 8 4 6
1
Node Known Distance Predecessor
A F 0 0
B F 0
C F 0
D F 0
E F 0
F F 0
G F 0
-
7/29/2019 Graphs Shortest Path
20/35James Tam
An Example Of Finding The Shortest WeightedPath: Declare A As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B F 2 A
C F 0
D F 1 A
E F 0
F F 0
G F 0
C
A0
D1
G
B2
F
E
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
21/35James Tam
An Example Of Finding The Shortest WeightedPath: Declare D As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B F 2 A
C F 3 D
D T 1 A
E F 3 D
F F 9 D
G F 5 D
C3
A0
D1
G5
B2
F9
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
22/35James Tam
An Example Of Finding The Shortest WeightedPath: Declare B As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B T 2 A
C F 3 D
D T 1 A
E F 3 D
F F 9 D
G F 5 D
C3
A0
D1
G5
B2
F9
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
23/35
James Tam
An Example Of Finding The Shortest WeightedPath: Declare C As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B T 2 A
C T 3 D
D T 1 A
E F 3 D
F F 8 C
G F 5 D
C3
A0
D1
G5
B2
F8
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
24/35
James Tam
An Example Of Finding The Shortest WeightedPath: Declare E As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B T 2 A
C T 3 D
D T 1 A
E T 3 D
F F 8 C
G F 5 D
C3
A0
D1
G5
B2
F8
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
25/35
James Tam
An Example Of Finding The Shortest WeightedPath: Declare G As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B T 2 A
C T 3 D
D T 1 A
E T 3 D
F F 6 G
G T 5 D
C3
A0
D1
G5
B2
F6
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
26/35
James Tam
An Example Of Finding The Shortest WeightedPath: Declare F As Known
Node Known Distance
(from A)
Predecessor
A T 0 0
B T 2 A
C T 3 D
D T 1 A
E T 3 D
F T 6 G
G T 5 D
C3
A0
D1
G5
B2
F6
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
27/35
James Tam
An Example Of Finding The Shortest WeightedPath: Algorithm Ends
Node Known Distance
(from A)
Predecessor
A T 0 0
B T 2 A
C T 3 D
D T 1 A
E T 3 D
F T 6 G
G T 5 D
C3
A0
D1
G5
B2
F6
E3
4
2
110
22
3
5 8 4 6
1
-
7/29/2019 Graphs Shortest Path
28/35
James Tam
Initializing The Starting Values
public void initializeTable (Table t, Graph g,
Node start)
{
for (int i = 1; i < NO_NODES; i++)
{
t[i].setNode (g.getNextNode())
t[i].distance (1);
t[i].setPredecessor (null);
}
t [index of starting node].distance = 0;
}
1 Just pick a distance value that will be greater than any existing distance in the graph
Node Distance Predecessor
A 0 0
B 0
C 0
D 0
E 0
F 0
G 0
-
7/29/2019 Graphs Shortest Path
29/35
James Tam
Algorithm For Finding The Shortest Path
public void dijkstraShortestPath (Graph g, Table t, Node start)
{
List toBeChecked = new List ();
Node temp;
for (all nodes in the graph)
toBeChecked.add (graph.traverse());
E.W. Dijkstra
-
7/29/2019 Graphs Shortest Path
30/35
James Tam
Algorithm For Finding The Shortest Path(2)
while (toBeChecked.empty() == false)
{
temp = toBeChecked.removeMinDistanceNode ();
for (all nodessuc adjacent to temp which are contained in toBeChecked)
{
if (t [suc].getDistance () >(t [temp].getDistance () + distance fromsuc to temp))
{
t [suc].setDistance (t[temp].getDistance () +
distance fromsuc to temp));
t [suc].setPredecessor (temp);
}
}
}
E.W. Dijkstra
ffi i Of S A i
-
7/29/2019 Graphs Shortest Path
31/35
James Tam
Efficiency Of The Shortest Path Algorithm:Weighted
O (n2)
for (all nodes in the graph)
toBeChecked.add (graph.traverse());
while (toBeChecked.empty() == false)
{
temp = toBeChecked.removeMinDistanceNode ();
for (all nodessuc adjacent to temp which are contained in toBeChecked)
{: : : : :
}
}
O (n)
O (n2)
-
7/29/2019 Graphs Shortest Path
32/35
James Tam
A Related Algorithm: Making Change
Ideal case: The number of coins returned is
minimized
$0.55
$0.30
$0.05
http://www.collectible-treasures.com/catalog/images/item1589.jpghttp://www.collectible-treasures.com/catalog/images/item1591.jpghttp://www.collectible-treasures.com/catalog/images/item1591.jpg -
7/29/2019 Graphs Shortest Path
33/35
James Tam
Similarities Of Both Algorithms
At each stage the selection made is what appears to be best at
that point in time:- Shortest path: based on the current paths traversed select the shortestcombination of paths.
- Making change: at each stage in the change making process, give back thelargest possible denomination (without having the total amount of change
given back exceed the original amount of change owed).
-
7/29/2019 Graphs Shortest Path
34/35
James Tam
You Should Now Know
How to determine the shortest path for traversing a graph when:
- The graph is unweighted
- The graph is weighted (Dijkstras algorithm)
What is a greedy algorithm and how Dijkstras algorithm is an
example of a greedy approach.
-
7/29/2019 Graphs Shortest Path
35/35
Sources Of Lecture Material
Data Structures and Algorithm Analysis in C++by Mark
Allen Weiss
Data Structures and Algorithms with Javaby Frank M.
Carrano and Walter Savitch
"Data Structures and Algorithms in Javaby Adam DrozdekCPSC 331 course notes by Marina L. Gavrilova
http://pages.cpsc.ucalgary.ca/~marina/331/
http://pages.cpsc.ucalgary.ca/~marina/331/http://pages.cpsc.ucalgary.ca/~marina/331/