Shortest Path
Problems
Section 29.15 in Carrano/Savitch
Section 4.2 in Neapolitan/Naimipour
Single source shortest path problems
We want to find the shortest path from a given vertex
to all the others
The input is a
graph (stored either as a adjacency matrix or list)
The cost of a
path is the sum of the cost of each edge in the path
Two types
Weighted shortest path
Given as input a weighted graph, G=(V,E) and a
distinguished vertex s, find the
shortest path from s to every other vertex in G
Unweighted shortest path
A
special case of the problem above. Consider all the
weights one.
Negative weights
Suppose a graph G has a cycle whose total weight is
negative.
By repeatedly going through the negative cycle, any
path length could become negative.
So, even if there is a path from v to u in G, the
distance may be undefined if a negative cycle exists in G.
Therefore, the only graphs we will look at have all positive
edges.
Unweighted shortest path
Our algorithm will first find path lengths of length
zero, then of length one, then two etc.
First we choose the beginning vertex. The length from it to itself is zero
Then we find all the paths adjacent to original
vertex. They are path length one.
Unweighted shortest path
We want to find the shortest path from s to all
the other vertices.
The algorithm
keeps track of three things for each vertex
Whether or not the vertex has been processed a boolean known
Initially all paths are unknown; it is set to true
after a vertex is processed
The distance from the start vertex call it dist
Initially,
the distance to all vertices except s is infinity
The path
to vertex sthis will be the last vertex to cause a change to the dist
Initially this is zero for all vertices
Draw this unweighted graph
-- means infinity; i.e. no edge
between the vertices
|
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
|
1
|
0
|
1
|
--
|
1
|
--
|
--
|
--
|
|
2
|
--
|
0
|
--
|
1
|
1
|
--
|
--
|
|
3
|
1
|
--
|
0
|
--
|
--
|
1
|
--
|
|
4
|
--
|
|
1
|
0
|
1
|
1
|
1
|
|
5
|
--
|
--
|
--
|
--
|
0
|
--
|
1
|
|
6
|
--
|
--
|
--
|
--
|
--
|
0
|
--
|
|
7
|
--
|
--
|
--
|
--
|
--
|
1
|
0
|
Let V3
be the start vertex
Mark as
known all vertices that are path length 1 from v3
The path will be v3
Now mark as know
all the path lengths of 2
These will be adjacent to the nodes whose dist =1
The path will be the vertex at dist = 1
Mark as known the
vertices whose path length from
v3 is 3, then 4 etc, until all the vertices are known
Weighted graphshortest path
The general
method to solve this problem is called Dijkstras algorithm
This is a classic
example of a greedy algorithm
Greedy algorithms
usually solve a problem in stages by doing what is best at that stage
The problem with
greedy algorithms is that sometimes they do not give optimal solutions
Dijkstras algorithm works much like the unweighted
shortest path algorithm
We keep track of
the same things
Dijkstras algorithm to solve the single source shortest
path problem
Initial configuration
known = false for
all vertices except start
dist is infinity for all vertices except s; it is 0 for s
path is zero for all vertices
Look at the graph on the overhead, we will find the
shortest path from v1 to all the other vertices
Going through Dijkstras algorit6hm
Mark V1 as known,
the dist from v1 to v1 is 0; so is the
path
The two vertices adjacent to V1 are V2 and V4
Mark their dist from
V1
Mark their path
as V1
Select a vertex V which has the smallest dist among all the unknown vertices,
mark it known
Mark V4 known
put down the dist of V4s adjacent vertices from
v1 (the start)
This
involves adding the dist to v4 to
v4s adjacent vertices
The path for all of V4s adjacent vertices is through v4
Now find the smallest dist that is not
marked known
repeat the above step, replacing the dist if the dist
through that vertex is smaller than the dist marked down.
Draw this graph,
then find the shortest path using Dijkstras algorithm. Show the table.
Let V1 be the start vertex
-- means infinity; i.e. no edge
|
1
|
2
|
3
|
4
|
5
|
1
|
0
|
7
|
4
|
6
|
1
|
2
|
--
|
0
|
--
|
--
|
--
|
3
|
--
|
2
|
0
|
5
|
--
|
4
|
--
|
3
|
--
|
0
|
--
|
5
|
--
|
--
|
--
|
1
|
0
|