0% found this document useful (0 votes)
15 views37 pages

Graph Algorithms: Definitions & Analysis

Chapter 5 discusses graph algorithms, defining key concepts such as vertices, edges, paths, cycles, and various types of graphs including directed, undirected, acyclic, and connected graphs. It covers graph representation methods like adjacency matrices and lists, as well as algorithms for topological sorting and finding shortest paths. The chapter emphasizes the implications of negative cycles on shortest path calculations and outlines the complexity of different algorithms.

Uploaded by

1231439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views37 pages

Graph Algorithms: Definitions & Analysis

Chapter 5 discusses graph algorithms, defining key concepts such as vertices, edges, paths, cycles, and various types of graphs including directed, undirected, acyclic, and connected graphs. It covers graph representation methods like adjacency matrices and lists, as well as algorithms for topological sorting and finding shortest paths. The chapter emphasizes the implications of negative cycles on shortest path calculations and outlines the complexity of different algorithms.

Uploaded by

1231439
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Iyad Jaber - Algorithm Analysis Page |1

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

• Each edge is a pair (v,w), where v, w ϵ V. E edges are sometimes


referred to as arcs.
• If the pair is ordered, then the graph is directed. Directed graphs
are sometimes referred to as diagraphs.

• Vertex w is adjacent to v, if and only if (v, w) ϵ E.


• In an undirected graph with edge (v, w), and hence (w, v), w is
adjacent to v and v is adjacent to w.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |2

• Sometimes an edge has a third components kwon as either a


weight or a cost.
50
v1 V2
30 15 10 10

30 70 V5
V3 V4
45 10 13
55
28
V6 V7

• A path in a graph is a sequence of vertices w1, w2, w3,…, wn such


that (wi, wi+1) ϵ E for 1 <= i <= n.

• The length of such a path is the number of edged on the path,


which is equal to n-1. Path(v1,v6):
v1 V2 v1, v4, v7, v6
1 Length = 3

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |3

• A simple path is a path such that all vertices are distinct, except
that the first and last could be the same.

• A cycle in a directed graph is a path of length at least 1, such


that w1 = wn, this cycle is simple if the path is simple.

v1 V2

V3 V5
V4

V6 V7

• A directed graph is acyclic if it has no cycles. A directed acyclic


graph is sometimes referred to by its abbreviation, DAG.

v1 V2

V3 V5
V4

V6 V7

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |4

• An undirected graph is connected if there is a path from every


vertex to every other vertex.

v1 V2

V3 V5
V4

V6 V7

• A directed graph with this property is called strongly connected.

v1 V2

V3 V5
V4

V6 V7

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |5

• If the directed graph is not strongly connected, but the underlying


graph (without direction to the arcs) is connected, then the graph
is said to be weakly connected.

v1 V2

V3 V5
V4

V6 V7
Not strongly connected

weakly connected

v1 V2

V3 V5
V4

connected V6 V7

• A complete graph is a graph in which there is an edge between


every pair of vertices.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |6

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

1. Adjacency matrix representation


1. One simple way to represent a graph is to use a two
dimensional array.

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.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |7

4. The space requirement is O(|V|2), which can be prohibitive


if the graph does not have very many edges.

5. An adjacency matrix is an appropriate representation if the


graph is dense ➔ |E| = O(|V|2).

6. In most of the applications that we shell see, this is not true.


For instance, suppose the graph represents a street map.
Where almost all the streets run either north-south or east-
west. Therefore, any intersection is attached to roughly
four streets, so if the graph is directed and all streets are
two-way, then |E| ≈ 4|V|. If there are 3,000 intersections,
then we have a 3,000 vertex graph with 12,000 edges
entries, which would require an array of size 9,000,000.

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |8

2. Adjacency list representation


• If the graph is not dense, in other words, if the graph is
sparse.
• For each vertex, we keep a list of all adjacent vertices.

• The space requirement is then (|E| + |V|).

• If the edges have weights, then this additional information


is also stored in the cells.

• Adjacency lists are the standard way to represent graphs.

• In most real life applications, the vertices have names,


which are unknown at compile time. Since we cannot index
an array by a unknown names, we must provide a mapping
of names to numbers. The easiest way to do this is to use
hash table.

v1 2 4
3
v2
4 5
v3
6
v4
3 6 7
v5
4 7
v6

v7
6

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis Page |9

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:

C231 C242 C336

431
C3321

C439
C233

C333

• It is clear that a topological ordering is not possible if the graph


has a cycle, since for two vertices v and w on the cycle, v
precedes w and w precedes v.

• The ordering is not necessarily unique; any legal ordering will do.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 10

An acyclic graph

v1 V2

V3 V5
V4

Indegree Known
V6 V7
V1 0 F
V2 1 F

Order: v1 -> v2 -> v5 -> v4 -> v3 -> v7 -> v6 V3 2 F

And V4 3 F

V1 -> v2 -> v5 -> v4 -> v7 -> v3 -> v6 V5 1 F


V6 3 F
Are both topological orderings
V7 2 F

• A simple algorithm to find a topological ordering is first to find any


vertex with no incoming edges. We can then print this vertex, and
remove it, along with its edges, from the graph. Then we apply
this same strategy to the rest of the graph.

• To formalize this, we define the indegree of a vertex v as the


number of edges (u, v). We compute the indegrees of all vertices
in the graph. Assume that the indegree array is initialized and
that the graph is read into an adjacency list.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 11

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.

• Because findMinVertexOfIndegreeZero() is a simple sequential


scan of the indegree array, each call to it takes O(|V|) time. Since
there are |V| such calls, the running time of the algorithm is O(|V|2).

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 12

void topSort ( Graph G)


Queue Q;
Int counter;
Vertex v, w;
Q = createQueue( numVertex);
makeNull(Q);
counter = 0;
for each vertex v
if ( indegree[ v ] == 0 )
enqueue(v, Q);
end if
end for
while ( Not isEmpty( Q ))
v = dequeue( Q );
topNum[ v ] = ++counter;
for each w adjacent to v
if ( --indegree[ w ] == 0)
enqueue(w,Q);
end if
end for
end while
if ( counter != numVertex )
error( “ Graph has a cycle”);
end if
diposeQueue( Q );
end.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 13

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 14

Shortest Path Algorithm


n-1
• The cost of a path v 1, v2, … , vn is ∑ ci, ci+1. This is referred to as
i= 1
the weighted path length.
• The unweighted path length is merely the number of edges on the
path, ( n-1 ).

For example, the shortest weighted path from v1 to v6 has a cost of 6


and goes from v1 to v4 to v7 to v6. The shortest unweighted path
between these vertices is 2.

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.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 15

• This loop is known as a negative cost cycle; When one is present


in the graph, the shortest path are not defined.

2
v1 V2
4 1 -10
3
V3 5 1 V5
V4
2 6 2 6
V6 1
V7

• We will examine algorithms to solve four versions of this problem.


o First, we will consider the unweighted shortest path problem
and show how to it in O (|E| + |V|).
o Next, we will show how to solve the weighted shortest path
problem if we assume that there are no negative edges. The
running time for this algorithm is O (|E| log|V|) when
implemented with reasonable data structures.
o If the graph has negative edges, we will provide a simple
solution, which unfortunately has a poor time bound of O (|E|.
|V|).
o Finally, we will solve the weighted problem for the special
case of acyclic graphs in the linear time.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 16

1) Unweighted shortest paths


We would like to find the shortest path from s to all other vertices.
We are only interested in the number of edges contained on the
path. So there are no weights on the edges. This is clearly a special
case of the weighted shortest path problem, since we could assign
all edges a weight of 1.

v1 V2

V3 V5
V4

V6 V7

Initial configuration of table used in unweighted shortest path


computation
V Known dv pv
v1 0 Ꝏ 0
v2 0 Ꝏ 0
v3 0 0 0
v4 0 Ꝏ 0
v5 0 Ꝏ 0
v6 0 Ꝏ 0
v7 0 Ꝏ 0

Suppose we choose s to be v3

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 17

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.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 18

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.

• The running time of the algorithm is O ( |E| + |V| )

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 19

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 20

Declarations for Dijkstra’s Algorithm


int vertex;
tableEntry
{
List header;
boolean known;
distType dist;
vertex path;
}
tableEntry Table[ numberOfVertex + 1];

void intializeTable ( vertex start, Graph g, Table T)


begin
int i;
readGraph(G, T);
for ( i = numberOfVertex; i > 0; i--)
T[ i ].known = false;
T[ i ].dist = INT_MAX;
T[ i ].path = notVertex;
end for
T[ start ].dist = 0;
end.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 21

void Dijkstra ( Table T )


begin
vertex v, w;
for ( ; ; )
v = smallest_Unknown_Distance_Vertex;
if ( v == notVertex )
break;
end if
T[ v ].known = true;
for each w adjacent to v
if ( ! T[ w ].known )
if ( T[ v ].dist + c v,w < T[ w ].dist )
T[ w ].dist = T[ v ].dist + c v,w ;
T[ w ].path = v;
end if
end if
end for
end for
end.
Time = O(V2)
void printPath( vertex v, Table T )
if ( T[ v ].path != notVertex )
printPath( T[ v ].path, T);
print( “ to “);
end if
print( v );
end.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 22

3) Graphs with Negative Edge costs


• If the graph has negative edge costs. Then Dijkstra’s Algorithm
does not work.
• The problem is that once a vertex u is declared known, it is
possible that from some other unknown vertex v there is a path
back to u that is very negative. In such a case, taking a path from
s to v back to u is better than going from s to u without using v.
• A combination of the weighted and unweighted algorithms will
solve the problem, but at the cost of a drastic increase in running
time.
• We forget about the concept of known vertices, since our
algorithm needs to be able to change its mind.
• The running time is O( E.V ) if adjacency lists are used.

2
v1 V2
4 1 -10
3
V3 2 2 V5
V4
5 8 4 6
V6 1 V7

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 23

void weightesNegative( Table T)


Queue Q;
veretex v, w;
Q = createQueue( numOfVertex );
enqueue( Q, s);
while ( ! isEmpty( Q ))
v = dequeue( Q );
for each w adjacent to v
if( T[ v ].dist + c v,w < T[ w ].dist )
T[ w ].dist = T[ v ].dist + c v,w ;
T[ w ].path = v;
if ( w is not already in Q )
enqueue( Q, w);
end if
end if
end for
end while
disposeQueue( Q );
end.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 24

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)

Start D(2) Finish


H(1)

B(2) G(2)

E(1) K(4)

• This graph is thus known as an activity node graph.


• The edges represent procedure relationship.
• An edge (v, w) means that activity v must be completed before
activity w may begin.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 25

• There is several important questions which would be of interest to


answer

1. What is the earliest completion time for the project?


We can see from the graph that 10 time units are required
along the path A, C, F, H.
2. Determine which activities can be delayed, and by how
long without affecting the minimum completion time.

• To find the earliest completion time of the project, we merely need


to find the length of the longest path from the first event to the last
event.
• We can also compute the latest time, that each event can finish
without affecting the final completion time.
• The slack time for each edge in the event node graph represents
the amount of time that the completion of the corresponding
activity can be delayed without delaying the overall completion.
• To perform these calculations, we convert the activity node graph
to an event node graph.

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 26

Activity node graph

C(3)

A(3) F(3)

Start D(2) Finish


H(1)

B(2) G(2)

E(1) K(4)

Event node graph.

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 27

• Earliest completion times


3 6
C/3
2 4 0
A/3 6 9
0 F/3
0 3 5 9 10
0 7 7 0 H/1
6 D/2 6 10
1 0 5 7 0 10
G/2
0 8 8 0
B/2 2 3 0 7
E/1 K/4 9
3 5

Max

• Latest completion times

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 28

• Earliest completion times, Latest completion times and slack


3 6
C/3/0
2 4 0
A/3/0 6 6 9
3 0 F/3/0 9
3 5
0 0 7 7 0 H/1/0 10
1 6 D/2/1 6 0 56 79 0 10 10
0 4 6 G/2/2 9 10
0 8 8 0
B/2/2 2 3 0 7
7 9
E/1/2 K/4/2 9
3 5
4 5 9

• Some activities have zero slack. These are critical activities,


which must finish on schedule. There is at least one path
considering entirely of zero slack edges, such a path is a critical
path.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 29

Minimum Spanning Tree


• A minimum spanning tree of an undirected graph G is a tree
formed from graph edges that connects all the vertices of G at
lowest total cost.
• A minimum spanning tree exist if and only if G is connected.

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 30

• The minimum spanning tree is a tree because it is acyclic. It is


spanning because it covers every edge, and it is minimum for the
obvious reason.

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.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 31

• For this problem, the update rule is even simpler than


before; After a vertex v is selected, for each unknown w
adjacent to v
dv = min (dw, cwv)
• The running time is O(V2) without heaps, which is optional
for dense graphs, and O( E logv ) using binary heaps,
which is good for sparse graphs.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 32

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 33

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

Initial V1 is declared V4 is declared V2 is declared V7 is declared V6 is declared V5 is declared

V3 is declared

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 34

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.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 35

Edge Weight Action


(v1, v4) 1 Accepted
(v6, v7) 1 Accepted
(v1, v2) 2 Accepted 2
v1 V2
(v3, v4) 2 Accepted 4 1 10
3
(v2, v4) 3 Rejected V3 2 7 V5
V4
(v1, v3) 4 Rejected 5 8 4 6
(v4, v7) 4 Accepted 1
V6 V7
(v3, v6) 5 Rejected
(v5, v7) 6 Accepted
… .. …

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

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 36

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.

[Link] Uploaded By: anonymous


Iyad Jaber - Algorithm Analysis P a g e | 37

Ideas

disjointSet s
V1 V2 V3 V4 V5 V6 V7

1 2 3 4 5 6 7

edge ➔ (v1, v4)


find ( u, s); // 1
find ( v, s); // 4

union ( s, u, v ) V4

V1 V2 V3 V5 V6 V7

1 2 3 4 5 6 7

[Link] Uploaded By: anonymous

You might also like