Overview
Definitions
Graph representations
Exercise
Shortest Path Problems -- Dijkstra's Algorithm
Pseudo-code
To help make the algorithm more understandable, here is a substantial
revision of the author's code. Assume that in the graph, V is an
array of vertices (type Vertex). Each vertex will store a list
of its edges, and each edge will represented by a vertex index and a
weight in the following structure
struct AdjVertex
{
int W; // index of the neighboring vertex
float Weight; // edge weight
};
Next, as discussed in outlining the algorithm, a certain amount of
additional information for each vertex, specific to Dijkstra's
algorithm rather than to a general graph, must be stored in an
auxiliary PathTable. Here is a structure to store this
information
struct PathTableEntry
{
bool PathKnown; // has the shortest path been found?
float Distance; // length of current shortest path
int PathPred; // predecessor on the shortest path
};
Now, here is a function to initialize the PathTable for a given
graph.
void
InitTable( int StartVertex, const Graph& G,
PathTableEntry T[] ) // assumes the table is pre-allocated
{
for ( int i=0; i<G.NumVertices(); i++ ) {
T[i].PathKnown = false;
T[i].Distance = Infinity; // assume this is defined
T[i].PathPred = -1;
}
T[StartVertex].Distance = 0;
}
Finally, here is the algorithm
void
Dijkstra( int StartVertex, const Graph& G,
PathTableEntry T[] )
{
InitTable( StartVertex, G, T );
int V;
AdjVertex Nbr;
for( ; ; )
{
V = MinimumUnknown( G, T );
if( V == -1 ) break;
T[V].PathKnown = true;
for each Nbr in V.Edges() {
if( !T[Nbr.W].PathKnown &&
T[V].Distance + Nbr.Weight < T[Nbr.W].Distance )
{
// Update TableEntry for Nbr.W
DecreaseWeight( Nbr.W, V, T[V].Distance + Nbr.Weight );
T[Nbr.W].PathPred = V;
}
}
}
}
MinimumUnknown and DecreaseWeight
and the method for extracting an actual path once the algorithm is
completed will be discussed in class.
Exercises
W, and the Weight
(distance or cost) of the edge. These are the two member variables in
struct AdjVertex. Thus,
for example, vertex 3 has two outgoing edges: (1,4) goes to
vertex 1 with a weight of 4, and (4,8) goes to vertex 4 with
a weight of 8.
1: (2,2), (3,2), (4,1), (5,7)
2: (1,3), (3,1), (4,5)
3: (1,4), (4,8)
4: (5,2)
5: (3,9)
MinimumUnknown( G, T ) in the line
V = MinimumUnknown( G, T );
As discussed in class, this function should find the vertex having the
smallest distance among the vertices with !PathKnown.
There are several ways to implement MinimumUnknown( G, T ):
T to find the vertex with the
smallest distance having PathKnown == false. What is the overall
worst case complexity of the algorithm in this case?
Distance
of the vertices with PathKnown == false.
MinimumUnknown and which occur in
DecreaseWeight?
Minimum Spanning Trees -- Prim's Algorithm
Exercises
Topological Sort
Here is a modified version of the author's pseudo-code for the
algorithm. The code assumes there is a Vertex class object for
each vertex which stores the InDegree and TopNum. The
code also assumes the InDegree has been precomputed for each
vertex.
void
Topsort( Graph& G )
{
unsigned int counter = 0;
Vertex v, w;
queue<Vertex> Q;
for each vertex v in G {
if( v.InDegree == 0 )
Q.push( v );
}
while( !Q.empty( ) ) {
v = Q.front( );
Q.pop();
v.TopNum = ++counter;
for each Vertex w adjacent to v {
if( --w.InDegree == 0 )
Q.push( w );
}
}
if( counter < G.Num_Vertex() )
Error( "Graph has a cycle" );
}
Exercises
TopNum value assigned to the vertices in
the following graphs. Each graph is represented by an adjacency list,
where numbers appearing after a ``:'' indicate directed edges in the digraph.
For example, 3: 2, 8 means there is a directed edge from vertex
3 to vertex 2 and a directed edge from vertex 3 to vertex 8.
Graph 1
1:
2: 1
3: 2, 8
4: 5, 6
5: 3, 6, 7
6: 3
7: 2, 1
8: 1
Graph 2
1: 5
2: 1
3: 1, 2
4: 1
5: 2, 7
6: 1, 3, 4
7:
Q was a stack instead of a queue in Topsort.
Would the algorithm still work? Do you prefer this solution?
Topsort? Justify your answer.
Vertex.
InDegree and TopNum shouldn't be public member
variables of class Vertex, yet the pseudo-code makes them
appear to be. How should this apparent conflict be resolved?
InDegree for each
vertex.
queue need to store Vertex class objects?
What is the cost of doing so? What else might it store?
Depth-First Search and Breadth-First Search
!V.Visited for each vertex.) Algorithms built on DFS
enhance the step of marking a vertex as visited with more substantial
operations.
DFS( Vertex& V )
{
V.Visited = true;
for each W adjacent to V
if ( ! W.Visited ) DFS(W);
}
This algorithm requires O( |V| + |E| ) time.
BFS( Vertex& V )
{
V.Visited = true;
queue<Vertex> q;
q.push( V );
while ( !q.empty() ) {
W = q.front(); q.pop();
for each U adjacent to W
if ( ! U.Visited ) {
U.Visited = true;
q.push( U );
}
}
}
This algorithm also requires O( |V| + |E| ) time.
Exercises
V and U marked as visited before
pushing them onto the queue instead of afterwards?
Finding Articulation Points
v.Num and v.Low, which can be implemented as
member variables in the Vertex class or in a subclass of
Vertex specific for use in the articulation point algorithm.
// Counter is global and initialized to 1.
// Parent is the parent of a vertex in the DFS tree
void
Find_Art( Vertex & V, list<Vertex> & art_points )
{
V.Visited = true;
V.Low = V.Num = Count++; // Rule 1.
for each vertex W adjacent to V
if( ! W.Visited ) // Forward edge.
{
W.Parent = V;
Find_Art( W, art_points );
if( W.Low >= V.Num ) art_points.push(V);
V.Low = Min( V.Low, W.Low ); // Rule 3.
}
else if( V.Parent != W ) // Back edge.
V.Low = Min( V.Low, W.Num ); // Rule 2.
}
Exercises
Find_Art for the following graph?
Assume A is the first node visited. Also, show the values of
Num and Low for each node of the graph. Note that
letters are used as vertex labels instead of numbers to help avoid
confusion in hand-simulating the algorithm.
A: B, C
B: A, D
C: A, D, E, F
D: B, C
E: C, I
F: C, G, H
G: F, H
H: F, G
I: E
Find_Art algorithm is not completely correct, as
you might have discovered: it does not handle the root of the DFS
tree properly. Augment the code to correctly decide if the root is an
articulation point.
Review Problems:
This is a short set of problems which is not comprehensive. Rework the problems in the notes for more complete review.