Graph Algorithms: Definitions & Analysis
Graph Algorithms: Definitions & Analysis
Chapter 5
Graph Algorithm
Definitions
• A graph G = ( V, E ) consists of a set of vertices, V, and a set of
edges, E.
v2 Adjacent v1
v1 V2
V3 V5
V4
V6 V7
v5 Not Adjacent v7
30 70 V5
V3 V4
45 10 13
55
28
V6 V7
V3 V5
V4
2
OR: v1, v4, v6
3
V6 V7 OR: v1, v4, v3, v6
...
• If the graph contains an edge ( v, v ) from a vertex to itself, then
the path v, v is sometimes referred to as a loop.
v1 V2
V3 V5
V4
V6 V7
• A simple path is a path such that all vertices are distinct, except
that the first and last could be the same.
v1 V2
V3 V5
V4
V6 V7
v1 V2
V3 V5
V4
V6 V7
v1 V2
V3 V5
V4
V6 V7
v1 V2
V3 V5
V4
V6 V7
v1 V2
V3 V5
V4
V6 V7
Not strongly connected
weakly connected
v1 V2
V3 V5
V4
connected V6 V7
Examples:
• Airport system can be modeled by a graph.
• Traffic flow can be modeled by a graph.
Representation of graphs
v1 V2
V3 V5
V4
V6 V7
2. For each edge (u, v), we set a[u][v] = 1; otherwise the entry
in the array is 0.
3. If the edge has a weight associated with it, then we can set
a[u][v] = to the weight and use either a very large or a very
small weight as a sentinel to indicate non exit tent edge.
v1 v2 v3 v4 v5 v6 v7
v1 0 1 1 1 0 0 0
v2 0 0 0 1 1 0 0
v3 0 0 0 0 0 1 0
v4 0 0 1 0 0 1 1
v5 0 0 0 1 0 0 1
v6 0 0 0 0 0 0 0
v7 0 0 0 0 0 1 0
v1 2 4
3
v2
4 5
v3
6
v4
3 6 7
v5
4 7
v6
v7
6
Topological Sort
A topological sort is an ordering of vertices in a directed acyclic graph,
such that if there is a path from vi to v j, then vj appears after vi in the
ordering.
Example:
Represents the course prerequisite structure at the Birzeit university:
431
C3321
C439
C233
C333
• The ordering is not necessarily unique; any legal ordering will do.
An acyclic graph
v1 V2
V3 V5
V4
Indegree Known
V6 V7
V1 0 F
V2 1 F
And V4 3 F
Algorithm
void topSort (Graph G)
int counter;
Vertex v, w;
for ( counter = 1; counter <= numOfVertex; counter++)
v = findMinVertexOfIndegreeZero();
if ( v == notAVertex )
error (“ Graph has a cycle “);
break;
end if
topNum [v] = counter;
for each w adjacent to v
indegree[ w ] --;
end for
end for
end.
Algorithm 2
• The time to perform this algorithm is O(|E| + |V|) if adjacency lists
are used. This is apparent where one realizes that the body of the
for loop is executed at most once per edge. The queue operations
are done at most once per vertex, and the initialization steps also
take time proportional to the size of the graph
Example
v1 V2
V3 V5
V4
V6 V7
Vertex 1 2 3 4 5 6 7
v1 0 0 0 0 0 0 0
v2 1 0 0 0 0 0 0
v3 2 1 1 1 0 0 0
v4 3 2 1 0 0 0 0
v5 1 1 0 0 0 0 0
v6 3 3 3 3 2 1 0
v7 2 2 2 1 0 0 0
enqueue v1 v2 v5 v4 v3, v7 v6
dequeue v1 v2 v5 v4 v3 v7 v6
2
v1 V2
4 1 10
3
V3 2 2 V5
V4
5 8 4 6
V6 1
V7
• The graph below shoes the problems that negative edges can
cause. The path from v5 to v4 has cost 1, but a shorter path exists
by following the loop v5, v4, v2, v5, v4, which has cost -5. This
path is still not the shortest, because we could stay in the loop
arbitrarily long. Thus, the shortest path between these two points
is undefined.
2
v1 V2
4 1 -10
3
V3 5 1 V5
V4
2 6 2 6
V6 1
V7
v1 V2
V3 V5
V4
V6 V7
Suppose we choose s to be v3
Algorithm #1
void unweighted ( Table T)
int currentDist;
Vertex V, W;
for ( currentDist = 0; currentDist < numVertex; currentDist++)
for each vertex v
if ( ( !T[ v ].known ) And ( T[ v ].dist == currentDist )
T[ v ].known = true;
for each w adjacent to v
if ( T[ w ].dist == IntMax )
T[ w ].dist = currentDist + 1;
T[ w ].path = v;
end if
end for
end if
end for
end for
end
• The running time of the algorithm is O(V 2), because of the doubly
nested for loop.
Algorithm #2
void unweighted ( Table T)
Queue Q;
Vertex v, w;
Q = createQueue ( numVertex);
makeNull (Q);
// enqueue the start vertex s,
enqueue(Q, s);
while ( ! isEmpty (Q))
v = dequeue (Q);
T[ v ].known = true;
for each w adjacent to v
if ( T[ w ].dist == IntMax )
T[ w ].dist = T[ v ].dist +1;
T [w ].path = v;
enqueue (Q, w);
end if
end for
end while
dispaoseQueue (Q);
end.
2) Dijkstra’s Algorithm
• If the graph is weighted, the problem becomes harder, but we can
still use the ideas from the unweighted case.
• This solution is an example of a greedy algorithms generally solve
a problem in stages by doing what appears to be the best thing at
each stage.
2
v1 V2
4 1 10
3
V3 2 2 V5
V4
5 8 4 6
V6 1
V7
known
known
known
known
known
known
known
V dv pv dv pv dv pv dv pv dv pv dv pv dv pv
v1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
v2 0 Ꝏ 0 0 2 v1 0 2 v1 1 2 v1 1 2 v1 1 2 v1 1 2 v1
v3 0 Ꝏ 0 0 Ꝏ 0 0 3 v4 0 3 v4 1 3 v4 1 3 v4 1 3 v4
v4 0 Ꝏ 0 0 1 v1 1 1 v1 1 1 v1 1 1 v1 1 1 v1 1 1 v1
v5 0 Ꝏ 0 0 Ꝏ 0 0 3 v4 0 3 v4 1 3 v4 1 3 v4 1 3 v4
v6 0 Ꝏ 0 0 Ꝏ 0 0 9 v4 0 9 v4 0 8 v3 0 6 v7 1 6 v7
v7 0 Ꝏ 0 0 Ꝏ 0 0 5 v4 0 5 v4 0 5 v4 1 5 v4 1 5 v4
2
v1 V2
4 1 -10
3
V3 2 2 V5
V4
5 8 4 6
V6 1 V7
4) Acyclic Graphs
• If the graph is known to be acyclic, we can improve Dijkstra’s
Algorithm by changing the order in which vertices are declared
known, otherwise known as vertex selection rule.
• The new rule is to select vertices in topological order.
• The selection rule works because when a vertex v is selected, its
distance, dv, can no longer be lowered, since by the topological
ordering rule it has no incoming edge emarating from unknown
nodes.
• A more important use of acyclic graphs is critical path analysis.
C(3)
A(3) F(3)
B(2) G(2)
E(1) K(4)
C/3
2 4 0
A/3 0 F/3
0 7 7 0 H/1
6 D/2 6 10
1 0 0 10
G/2
0 8 8 0
B/2 0
E/1 K/4 9
3 5
C(3)
A(3) F(3)
B(2) G(2)
E(1) K(4)
C/3
2 4 0
A/3 0 F/3
0 7 7 0 H/1
6 D/2 6 10
1 0 0 10
G/2
0 8 8 0
B/2 0
E/1 K/4 9
3 5
Max
C/3
2 4 0
A/3 3 0 6
F/3
0 7 7 0 10
H/1
6 D/2 6 10
1 0 6 9 0 10
0 4 6 G/2 9 10
0 8 8 0
B/2 0
7 9
E/1 K/4 9
3 5
4 5 9
Min
2
v1 V2
4 1 10
3
V3 2 7 V5
V4
5 8 4 6
V6 1 V7
Result:
2
v1 V2
1
V3 2 V5
V4
4 6
V6 1 V7
Example:
If we need to wire a house with minimum of cable, then a
minimum spanning tree problem needs to be solved.
• There are two basic algorithms to solve this problem, both are
greedy.
1) Prim’s Algorithm
• One way to compute a minimum spanning tree it so grow
the tree in successive stages. In each stage, one node is
picked as the root, and we add an edge, and thus an
associated vertex to the tree.
• We can see that prim’s algorithm is essentially identical to
Dijkstra’s algorithm for shortest paths. As before, for each
vertex we keep values dv and pv and an indication of
whether it is known or unknown. dv is the weight of the
shortest arc connecting v to a known vertex, and pv, as
before, is the last vertex to cause a change in dv. The rest
of the algorithm is exactly the same, with the exception that
since the definition of dv is different, so is the update rule.
2
v1 V2
4 1 10
3
V3 2 7 V5
V4
5 8 4 6
V6 1 V7
v1 V2
v1 V2
1
V3 V4 V5 V3 V4 V5
V6 V7 V7
V6
2 V2 2
v1 V2
v1
1 1
V3 V4 V5 V3 2 V4 V5
V6 V7 V7
V6
2 2
v1 V2 v1 V2
1 1
V3 2 V3 2
V4 V5 V4 V5
4 4 6
V6 V7 V6 1 V7
2
v1 V2
4 1 10
3
V3 2 7 V5
V4
5 8 4 6
V6 1 V7
known
known
known
known
known
known
known
V dv pv dv pv dv pv dv pv dv pv dv pv dv pv
v1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0
v2 0 Ꝏ 0 0 2 v1 0 2 v1 1 2 v1 1 2 v1 1 2 v1 1 2 v1
v3 0 Ꝏ 0 0 4 v1 0 2 v4 1 2 v4 1 2 v4 1 2 v4 1 2 v4
v4 0 Ꝏ 0 0 1 v1 1 1 v1 1 1 v1 1 1 v1 1 1 v1 1 1 v1
v5 0 Ꝏ 0 0 Ꝏ 0 0 7 v4 0 7 v4 0 6 v7 0 6 v7 1 6 v7
v6 0 Ꝏ 0 0 Ꝏ 0 0 8 v4 0 5 v3 0 1 v7 1 1 v7 1 1 v7
v7 0 Ꝏ 0 0 Ꝏ 0 0 4 v4 0 4 v4 1 4 v4 1 4 v4 1 4 v4
V3 is declared
2) Kruskal’s Algorithm
• A second greedy strategy is continually to select the edge in
order of smallest weight and accept an edge if it does not
cause a cycle.
• Formally, kruskal’s algorithm maintain a forest (a collection of
tree). Initially, there are v single node trees. Adding an edge
merges two trees into one, when the algorithm terminates,
there is only one tree, and this is the minimum spanning tree.
• The invariant we will use is that at any point in the process, two
vertices belong to the same set of and only if they one
connected in the current spanning forest.
• Thus, each vertex is initially in its own set. If u and v are in the
same set, the edge is rejected, because since they are already
connected, adding (u,v) would form a cycle. Otherwise, the
edge is accepted, and a union is performed on the two sets
containing u and v.
• The edges could be sorted to facilitate the selection, but
building a heap in linear time is a much better idea. Then delete
minimum give the edges to be tested in order.
v1 V2
v1 V2
1
V3 V4 V5 V3 V4 V5
V6 V7 V7
V6
v1 V2 2
v1 V2
1
1
V3 V4 V5 V3 V4 V5
1
V6 V7 1 V7
V6
2 2
v1 V2 v1 V2
1 1
V3 2 V3 2
V4 V5 V4 V5
4 6
V6 1 V7 V6 1
V7
Algorithm
void Kruskal ( Graph G )
int edgesAccepted = 0;
disjoinedSet s;
Heap h;
vertex u, v;
setType uSet, vSet;
edge e;
initialize ( s);
read_Graph_into_Heap_Array( G, h);
while ( edgesAccepted < numOfVertex )
e = deleteMin( h );
uSet = find( u, s);
vSet = find( v ,s);
if ( uSet != vSet )
edgesAccepted ++;
setUnion( s, uSet, vSet );
end if
end while
end.
Ideas
disjointSet s
V1 V2 V3 V4 V5 V6 V7
1 2 3 4 5 6 7
union ( s, u, v ) V4
V1 V2 V3 V5 V6 V7
1 2 3 4 5 6 7