0% found this document useful (0 votes)
10 views44 pages

UNIT 3 Some

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)
10 views44 pages

UNIT 3 Some

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
You are on page 1/ 44

lOMoARcPSD|47448036

MC4101-ADSA Unit-III - DATA STRUCTURES AND


ALGORITHMS
Master of Computer Applications (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by MAHALAKSHMI ([email protected])
lOMoARcPSD|47448036

Paavai Institutions Department of MCA

UNIT-III
GRAPHS

UNIT-IV 1. 1

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

TECHNICAL TERMS

1. Graph
A graph G consist of a nonempty set V which is a set of nodes of the graph, a set E
which is the set of edges of the graph, and a mapping from the set for edge E to a set of pairs
of elements of V. It can also be represented as G=(V, E).

2. Adjacent nodes
Any two nodes which are connected by an edge in a graph are called adjacent nodes.
For example, if an edge x ε E is associated with a pair of nodes (u,v) where u, v ε V, then we
say that the edge x connects the nodes u and v.

3. Directed graph
A graph in which every edge is directed is called a directed graph.

4. Undirected graph
A graph in which every edge is undirected is called a directed graph.

5. Loop
An edge of a graph which connects to itself is called a loop or sling.

6. Simple graph
A simple graph is a graph, which has not more than one edge between a pair of nodes
than such a graph is called a simple graph.

7. Weighted graph
A graph in which weights are assigned to every edge is called a weighted graph.

8. Outdegree of a graph
In a directed graph, for any node v, the number of edges which have v as their initial
node is called the out degree of the node v.

9. Indegree of a graph
In a directed graph, for any node v, the number of edges which have v as their
terminal node is called the indegree of the node v.

10. Path in a graph


UNIT-IV 1. 2

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

The path in a graph is the route taken to reach terminal node from a starting node.

11. Simple path


A path in a diagram in which the edges are distinct is called a simple path. It is also
called as edge simple.

12. Cycle or a circuit


A path which originates and ends in the same node is called a cycle or circuit.

13. Acyclic graph


A simple diagram which does not have any cycles is called an acyclic graph.

14. Strongly connected in a graph


An undirected graph is connected, if there is a path from every vertex to every other
vertex. A directed graph with this property is called strongly connected.

15. Weakly connected graph


When a directed graph is not strongly connected but the underlying graph is
connected, then the graph is said to be weakly connected.

16. 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 the lowest total cost.

17. Graph traversals


Traversing a graph is an efficient way to visit each vertex and edge exactly once.

18. Breadth first search (BFS)


BFS performs simultaneous explorations starting from a common point and spreading
out independently.

19. Tree edge


If w is undiscovered at the time vw is explored, then vw is called a tree edge and v
becomes the parent of w.

20. Back edge


If w is the ancestor of v, then vw is called a back edge.

UNIT-IV 1. 3

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

21. Biconnectivity
A connected graph G is said to be biconnected, if it remains connected after removal of
any one vertex and the edges that are incident upon that vertex. A connected graph is
biconnected, if it has no articulation points.

22. Articulation point


If a graph is not biconnected, the vertices whose removal would disconnect the graph
are known as articulation points.

23. Shortest path


A path having minimum weight between two vertices is known as shortest path, in
which weight is always a positive number.

24. Adjacency list


Adjacency list is an array indexed by vertex number containing linked lists. Each
node Vi the ith array entry contains a list with information on all edges of G that leave Vi. It is
used to represent the graph related problems.

UNIT-IV 1. 4

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

CONTENTS

4. GRAPHS
4.1 Definitions
4.2 Representation of graph
4.3 Graph Traversals
4.4 Depth-first traversal
4.5 Breadth-first traversal
4.6 Applications of graphs
4.7 Topological sort
4.8 Shortest-path algorithms
4.9 Minimum spanning tree
4.10 Prim's and Kruskal's algorithms
4.11 Biconnectivity
4.12 Euler circuits.

UNIT-IV 1. 5

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

LECTURE NOTES
4.1 DEFINITIONS

4.1.1 GRAPHS AND MULTIGRAPHS


A graph G consists of two things:
(1) A set V of elements called nodes (or points or vertices)
(2) A set E of edges such that each edge e in E is identified with a unique (unordered)
pair [u, v] of nodes in V, denoted by e = [u, v]
Sometimes we indicate the parts of a graph by writing G = (V, E).
Suppose e = [u, v]. Then the nodes u and v are called the endpoints of e, and u and v are said
to be adjacent nodes or neighbors. The degree of a node u, written deg(u), is the number of
edges containing u. If deg(u) = 0 — that is, if u does not belong to any edge— then u is called
an isolated node.

4.1.2 PATH AND CYCLE


A path P of length n from a node u to a node v is defined as a sequence of n + 1 nodes.
P = (v0, v1, v2, . . . , vn)
such that u = v0; vi-1 is adjacent to vi for i = 1,2, . . ., n and vn = v.

4.1.3 TYPES OF PATH


(i) Simple Path
(ii) Cycle Path
(i) Simple Path
Simple path is a path in which first and last vertex are different (V0 ≠ Vn)
(ii) Cycle Path
Cycle path is a path in which first and last vertex are same (V0 = Vn). It is also called
as Closed path.

4.1.4 CONNECTED GRAPH


A graph G is said to be connected if there is a path between any two of its nodes.
Proposition 4.1: A graph G is connected if and only if there is a simple path between
any two nodes in G.

4.1.5 COMPLETE GRAPH

UNIT-IV 1. 6

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

A graph G is said to be complete if every node u in G is adjacent to every other node v


in G.

4.1.6 TREE
A connected graph T without any cycles is called a tree graph or free tree or, simply, a
tree.

4.1.7 Labeled or Weighted Graph


If the weight is assigned to each edge of the graph then it is called as Weighted or
Labeled graph.

Figure 4.1 Weighted or Labeled Graph

The definition of a graph may be generalized by permitting the following:


(1) Multiple edges: Distinct edges e and e' are called multiple edges if they connect the same
endpoints, that is, if e = [u, v] and e' = [u, v].
(2) Loops: An edge e is called a loop if it has identical endpoints, that is, if e = [u,u].
(3) Finite Graph:A multigraph M is said to be finite if it has a finite number of nodes and a
finite number of edges
Example
(a) Figure (a) is a picture of a connected graph with 5 nodes—A, B, C, D and E and 7
edges:
[A,B], [B,C] , [C,D] , [D,E], [A,E], [C, E] [A,C]

There are two simple paths of length 2 from B to E:

UNIT-IV 1. 7

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

(B, A, E) and (B, C, E). There is only one simple path of length 2 from B to D: (B, C, D).
We note that (B, A, D) is not a path, since [A, D] is not an edge.
There are two 4-cycles in the graph:

[A, B, C, E, A] and [A, C, D, E, A}.


Note that deg(A) = 3, since A belongs to 3 edges. Similarly, deg(C) = 4 and deg(D)
= 2.
(b) Figure 4-2(b) is not a graph but a multigraph. The reason is that it has multiple
edges—
e4 = [B, C] and e5 = [B, C]—and it has a loop, e6 = [D, D]. The definition of a graph usually
does not allow either multiple edges or loops.
(c) Figure 4.2(c) is a tree graph with m = 6 nodes and, consequently, m - 1 = 5 edges. The
reader can verify that there is a unique simple path between any two nodes of the tree
graph. Figure 4.2(d) is the same graph as in Fig. 4.2(a), except that now the graph is
weighted.

Figure 4.2 Example Graphs

4.1.8 DIRECTED GRAPHS

UNIT-IV 1. 8

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

A directed graph G, also called a digraph or graph is the same as a multigraph except
that each edge e in G is assigned a direction, or in other words, each edge e is identified with
an ordered pair (u, v) of nodes in G.
Suppose G is a directed graph with a directed edge e = (u, v). Then e is also called an arc.
Moreover, the following terminology is used:
(1) e begins at u and ends at v.
(2) u is the origin or initial point of e, and v is the destination or terminal point of e.
(3) u is a predecessor of v, and v is a successor or neighbor of w.
(4) H is adjacent to v, and v is adjacent to u.

4.1.9 OUTDEGREE AND INDEGREE


Indegree : The indegree of a vertex is the number of edges for which v is head

Figure 4.2 Indegree and outdegree


Indegree of 1 = 1
Indegree pf 2 = 2

Outdegree :The outdegree of a node or vertex is the number of edges for which v is tail.

Outdegree of 1 =1
Outdegree of 2 =2

4.3.9 SOURCE AND SINK


A node u is called a source if it has a positive outdegree but zero indegree. Similarly,
u is called a sink if it has a zero outdegree but a positive indegree.
A directed graph G is said to be connected, or strongly connected, if for each pair u, v
of nodes in G there is a path from u to v and there is also a path from v to u. On the other
hand, G is said to be unilaterally connected if for any pair u, v of nodes in G there is a path
from u to v or a path from v to u.

4.3.10 EXAMPLE

UNIT-IV 1. 9

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure shows a directed graph G with 4 nodes and 7 (directed) edges. The edges e2
and e3 are said to be parallel, since each begins at B and ends at A. The edge e7 is a loop,
since it begins and ends at the same point, B. The sequence Pl = (D, C, B, A) is not a path,
since (C, B) is not an edge—that is, the direction of the edge e5 = (B, C) does not agree with
the direction of the path P1. On the other hand, P2 = (D, B, A) is a path from D to A, since
(D, B) and (B, A) are edges. Thus A is reachable from D. There is no path from C to any other
node, so G is not strongly connected. However, G is unilaterally connected. Note that
indeg(D) = 1 and outdeg(D) = 2. Node C is a sink, since indeg(C) = 2 but outdeg(C) =0. No
node in G is a source.

Figure 4.3 Diricted Graph


Let T be any nonempty tree graph. Suppose we choose any node R in T. Then T with
this designated node R is called a rooted tree and R is called its root. Recall that there is a
unique simple path from the root R to any other node in T. This defines a direction to the
edges in T, so the rooted tree T may be viewed as a directed graph. Furthermore, suppose we
also order the successors of each node v in T. Then T is called an ordered rooted tree.

4.3.10 SIMPLE DIRECTED GRAPH


A directed graph G is said to be simple if G has no parallel edges. A simple graph G
may have loops, but it cannot have more than one loop at a given node.

4.2 REPRESENTATION OF GRAPHS


There are two standard ways of maintaining a graph G in the memory of a computer.
1. The sequential representation
2. The linked representation

UNIT-IV 1. 10

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

4.2.1 SEQUENTIAL REPRESENTATION OF GRAPHS


There are two different sequential representations of a graph. They are
1. Adjacency Matrix representation
2. Path Matrix representation

4.2.1.1 ADJACENCY MATRIX REPRESENTATION


Suppose G is a simple directed graph with m nodes, and suppose the nodes of G have
been ordered and are called v1, v2, . . . , vm. Then the adjacency matrix A = (aij) of the graph
G is the m x m matrix defined as follows:
1 if vi is adjacent to Vj, that is, if there is an edge (Vi, Vj)
aij =
0 otherwise
Suppose G is an undirected graph. Then the adjacency matrix A of G will be a symmetric
matrix, i.e., one in which aij = aji; for every i and j.

4.2.1.2 DRAWBACKS
1. It may be difficult to insert and delete nodes in G. This is because the size of A
may need to be changed and the nodes may need to be reordered, so there may be many,
many changes in the matrix A.
2. If the number of edges is 0(m) or 0(m log2 m), then the matrix A will be sparse
(will contain many zeros); hence a great deal of space will be wasted.

4.2.1.3 EXAMPLE
Consider the graph G in Fig. Suppose the nodes are stored in memory in a linear
array DATA as follows:
DATA: X, Y, Z, W
Then we assume that the ordering of the nodes in G is as follows: v1 = X, v2 = Y, v3 = Z
and u4 = W. The adjacency matrix A of G is as follows:

UNIT-IV 1. 11

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

The number of l's in A is equal to the number of edges in G.

Figure 4.4 Diricted Graph

Consider the powers A, A2, A3, . . . of the adjacency matrix A of a graph G.


Let ak(i,j) = the ij entry in the matrix AK
Observe that a1{i, j) = aij gives the number of paths of length 1 from node vi to node vj.
One can show that a2(i, j) gives the number of paths of length 2 from vi to vj.
Proposition 4.2: Let A be the adjacency matrix of a graph G. Then aK(i, j), the ij entry
in the matrix A , gives the number of paths of length K from vi to vj.

4.2.1.4 PATH MATRIX REPRESENATION


Let G be a simple directed graph with m nodes, v1 , v2 , . . . ,vm. The path matrix
of G is the m-square matrix P = (pij) defined as follows:
1 if there is a path from Vi to Vj
Pij =
0 otherwise
Proposition 4.3: Let A be the adjacency matrix and let P=(Pij) be the path matrix of a
digraph G. Then pij=1 if and only if there is a nonzero number in the ij entry of the matrix
Bm = A +A2 + A3 +…….+Am
Consider the graph G with m=4 nodes in Fig 4.6 .Adding the matrices A, A2 , A3 and A4, we
obtain the following matrix B4 , and , replacing the nonzero entries in B4 by 1, we obtain the
path matrix P of the graph G:
B4 = 1 0 2 3
5068
3035
2033
UNIT-IV 1. 12

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

and
P=1011
1011
1011
1011

4.2.2 LINKED LIST REPRESENTATION OF GRAPHS


G is usually represented in memory by a linked representation, also called an
adjacency structure, Consider the graph G in Fig. (a). The table in Fig. (b) shows each node
in G followed by its adjacency list, which is its list of adjacent nodes, also called its
successors or neighbors. Figure 4-8 shows a schematic diagram of a linked representation of
G in memory. Specifically, the linked representation will contain two lists (or files), a node
list NODE and an edge list EDGE, as follows.

UNIT-IV 1. 13

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.5 Linked list representation

(a) Node list. Each element in the list NODE will correspond to a node in G, and it
will be a record of the form:
NODE NEXT ADJ
Here NODE will be the name or key value of the node, NEXT will be a pointer to the
next node in the list NODE and ADJ will be a pointer to the first element in the adjacency list
of the node, which is maintained in the list EDGE. The shaded area indicates that there may
be other information in the record, such as the indegree INDEG of the node, the outdegree

UNIT-IV 1. 14

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

OUTDEG of the node, the STATUS of the node during the execution of an algorithm, and so
on. (Alternatively, one may assume that NODE is an array of records containing fields such
as NAME, INDEG, OUTDEG, STATUS,…………. ). The nodes themselves, as pictured in
Fig., will be organized as a linked list and hence will have a pointer variable START for the
beginning of the list and a pointer variable AVAILN for the list of available space.
(b)Edge list. Each element in the list EDGE will correspond to a edge in G, and it will
be a record of the form:

4.2.3 DEST LINK


The field DEST will point to the location in the list NODE of the destination or
terminal node of the edge. The field LINK will link together the edges with the same initial
node, that is, the nodes in the same adjacency list. The shaded area indicates that there may
be other information in the record corresponding to the edge, such as a field EDGE
containing the labeled data of the edge when G is a labeled graph, a field WEIGHT
containing the weight of the edge when G is a weighted graph, and so on. We also need a
pointer variable AVAILE for the list of available space in the list EDGE.

4.3 GRAPH TRAVERSALS


There are many different applications of graphs. As a result, there are many different
algorithms for manipulating them. However, many of the different graph algorithms have in
common the characteristic that they systematically visit all the vertices in the graph. That is,
the algorithm walks through the graph data structure and performs some computation at each
vertex in the graph. This process of walking through the graph is called a graph traversal.
While there are many different possible ways in which to systematically visit all the
vertices of a graph, certain traversal methods occur frequently enough that they are given
names of their own. This section presents three of them--depth-first traversal, breadth-first
traversal and topological sort.

4.4 DEPTH-FIRST TRAVERSAL


The depth-first traversal of a graph is like the depth-first traversal of a tree discussed
in Section. A depth-first traversal of a tree always starts at the root of the tree. Since a graph
has no root, when we do a depth-first traversal, we must specify the vertex at which to begin.
A depth-first traversal of a tree visits a node and then recursively visits the subtrees of
that node. Similarly, depth-first traversal of a graph visits a vertex and then recursively visits
UNIT-IV 1. 15

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

all the vertices adjacent to that node. The catch is that the graph may contain cycles, but the
traversal must visit every vertex at most once. The solution to the problem is to keep track of
the nodes that have been visited, so that the traversal does not suffer the fate of infinite
recursion.

Figure 4.6: Depth-first traversal.

For example, Figure illustrates the depth-first traversal of the directed graph G1
starting from vertex c. The depth-first traversal visits the nodes in the order c, a, b, d

A depth-first traversal only follows edges that lead to unvisited vertices. As shown in
Figure , if we omit the edges that are not followed, the remaining edges form a tree. Clearly,
the depth-first traversal of this tree is equivalent to the depth-first traversal of the graph

4.5 BREADTH-FIRST TRAVERSAL


The breadth-first traversal of a graph is like the breadth-first traversal of a tree
discussed in Section . The breadth-first traversal of a tree visits the nodes in the order of their
depth in the tree. Breadth-first tree traversal first visits all the nodes at depth zero (i.e., the
root), then all the nodes at depth one, and so on.
Since a graph has no root, when we do a breadth-first traversal, we must specify the
vertex at which to start the traversal. Furthermore, we can define the depth of a given vertex
to be the length of the shortest path from the starting vertex to the given vertex. Thus,
breadth-first traversal first visits the starting vertex, then all the vertices adjacent to the
starting vertex, and the all the vertices adjacent to those, and so on.
Section presents a non-recursive breadth-first traversal algorithm for N-ary trees that
uses a queue to keep track vertices that need to be visited. The breadth-first graph traversal
algorithm is very similar.

UNIT-IV 1. 16

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

First, the starting vertex is enqueued. Then, the following steps are repeated until the
queue is empty:
1. Remove the vertex at the head of the queue and call it vertex.
2. Visit vertex.

3. Follow each edge emanating from vertex to find the adjacent vertex and call it to. If to
has not already been put into the queue, enqueue it.
Notice that a vertex can be put into the queue at most once. Therefore, the algorithm must
somehow keep track of the vertices that have been enqueued.

Figure 4.7: Breadth-first traversal


Figure illustrates the breadth-first traversal of the directed graph G1 starting from
vertex a. The algorithm begins by inserting the starting vertex, a, into the empty queue. Next,
the head of the queue (vertex a) is dequeued and visited, and the vertices adjacent to it
(vertices b and c) are enqueued. When, b is dequeued and visited we find that there is only
adjacent vertex, c, and that vertex is already in the queue.
Next vertex c is dequeued and visited. Vertex c is adjacent to a and d. Since a has
already been enqueued (and subsequently dequeued) only vertex d is put into the queue.
Finally, vertex d is dequeued an visited. Therefore, the breadth-first traversal of G1 starting
from a visits the vertices in the sequence a, b, c ,d

UNIT-IV 1. 17

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

4.6 APPLICATIONS OF GRAPHS


1. Shortest path algorithm in a network
2. Finding a minimum spanning tree
3. Finding graph planarity
4. Algorithms to find adjacency matrices.
5. Algorithms to find the connectedness
6. Algorithms to find the cycles in a graph
7. Algorithms for searching an element in a data structure (DFS, BFS) and so on.
Graphs are applied widely in our days. They are used in economy, aeronautics,
physics, biology (for analyzing DNA), mathematics and other areas.

4.6.1 SHORTEST PATH


This is probably the most often used algorithm. It may be applied in situations where
the shortest path between 2 points is needed.
Examples of such applications would be:
 Computer games - finding the best/shortest route from one point to another.
 Maps - finding the shortest/cheapest path for a car from one city to another, by using
given roads.

 May be used to find the fastest way for a car to get from one point to another inside a
certain city. E.g. satellite navigation system that shows to drivers which way they
should better go.

4.6.2 MINIMAL SPANNING TREE


Consider some communications stations (for telephony, cable television, Internet etc.)
and a list of possible connections between them, having different costs. Find the cheapest
way to connect these stations in a network, so that a station is connected to any other
(directly, or through intermediate stations). This may be used for example to connect villages
to cable television, or to Internet.
 The same problem, but instead of connecting communications stations - villages are
to be connected with roads.

4.6.3 EULERIAN PATH/CIRCUIT

UNIT-IV 1. 18

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

A postman has to visit a set of streets in order to deliver mails and packages. It is
needed to find a path that starts and ends at the post-office, and that passes through each
street (edge) exactly once. This way the postman will deliver mails and packages to all streets
he has to, and in the same time will spend minimum efforts/time for the road.

4.6.4 CHINESE POSTMAN PROBLEM


 The same problem with the postman as above, but instead of visiting each street
(vertex) exactly once, the postman can visit them more than once if needed. Thus the
path should pass through each street at least one time and should have the minimum
cost.
 Drawing a circuit with a plotter in a fastest possible way, or with a minimum cost.
 It may be used to determine the cheapest path for garbage collection, street cleaning,
or snow removal.
 Also applied in routing robots, analysing DNA, and others.

4.6.5 HAMILTONIAN PATH/CIRCUIT


 The same problem with the postman as above, but instead of visiting a set of streets
(edges), he has to visit each point (house) exactly once.

4.6.6 NETWORK FLOWS


 With Maximum Flow algorithm it is possible to find the most loaded roads or rails in
a certain transportation network, and also to determine its maximum intensivity. This
information may be then used to improve the traffic situation in those places.

4.6.7 OPTIMAL GRAPH COLORING


 This algorithm may be used to color a map with a minimum number of colors.

4.6.8 GRAPH MEDIAN


A warehouse should be placed in a city (a region) so that the sum of shortest distances
to all other points (regions) is minimal. This is useful for lowering the cost of transporting
goods from a warehouase to clients. Same thing can be considered for selecting the place of a
shop, market, office and other buildings.

4.6.9 GRAPH CENTER


Suppose that a hospital, a fire department, or a police department, should be placed in

UNIT-IV 1. 19

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

a city so that the farthest point is as close as possible. For example a hospital should be
placed in such a way that an ambulance can get as a fast as possible to the farthest situated
house (point).

4.7 TOPOLOGICAL SORT


A topological sort is an ordering of the vertices of a directed acyclic graph given by
the following definition:

4.7.1 DEFINITION
Consider a directed acyclic graph G = (V, E). A topological sort of the vertices of G
is a sequence S={v1,v2,…Vn ) in which each element of V appears exactly once. For every
pair of distinct vertices Vi and Vj in the sequence S, if Vi ->Vj is an edge in G, i.e., (Vi,Vj),
then i<j.
Informally, a topological sort is a list of the vertices of a DAG in which all the
successors of any given vertex appear in the sequence after that vertex. Consider the directed
acyclic graph G7 shown in Figure. The sequence S={a,b,c,d,e,f,g,h,i} is a topological sort of
the vertices of G7. To see that this is so, consider the set of vertices:

The vertices in each edge are in alphabetical order, and so is the sequence S.

Figure 4.8 A directed acyclic graph.

It should also be evident from Figure that a topological sort is not unique. For example, the
following are also valid topological sorts of the graph G7.

UNIT-IV 1. 20

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

S’ = {a, c, b, f, e, d, h, g, i}
S” = {a, b, d, e, g, c, f, h, i}
S’” = {a, c, f, h, b, e, d, g, i}
One way to find a topological sort is to consider the in-degrees of the vertices. (The
number above a vertex in Figure is the in-degree of that vertex). Clearly the first vertex in a
topological sort must have in-degree zero and every DAG must contain at least one vertex
with in-degree zero. A simple algorithm to create the sort goes like this:
Repeat the following steps until the graph is empty:
1. Select a vertex that has in-degree zero.
2. Add the vertex to the sort.
3. Delete the vertex and all the edges emanating from it from the graph.

4.8 SHORTEST-PATH ALGORITHMS


We conclude this chapter by using performance models to compare four different
parallel algorithms for the all-pairs shortest-path problem. This is an important problem in
graph theory and has applications in communications, transportation, and electronics
problems. It is interesting because analysis shows that three of the four algorithms can be
optimal in different circumstances, depending on tradeoffs between computation and
communication costs.

Figure 4.9: A simple directed graph, G, and its adjacency matrix, A.


The all-pairs shortest-path problem involves finding the shortest path between all
pairs of vertices in a graph. A graph G=(V,E) comprises a set V of N vertices, {Vi}, and a set
E of edges connecting vertices in V . In a directed graph, each edge also has a direction. A
graph can be represented as an adjacency matrix A in which each element (I, j) represents the
edge between element i and j . If there is an edge (Vi, Vj); otherwise, Aij=0.
UNIT-IV 1. 21

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

A path from vertex Vi to vertex Vj is a sequence of edges (Vi,Vk), (Vk,Vi), ...,


(Vm,Vj) from E in which no vertex appears more than once. For example, (1,3) (3,0) is a path
from vertex 1 to vertex 0 in Figure. The shortest path between two vertices Vi and Vj in a
graph is the path that has the fewest edges. The single-source shortest-path problem requires
that we find the shortest path from a single vertex to all other vertices in a graph. The all-
pairs shortest-path problem requires that we find the shortest path between all pairs of
vertices in a graph. We consider the latter problem and present four different parallel
algorithms, two based on a sequential shortest-path algorithm due to Floyd and two based on
a sequential algorithm due to Dijkstra. All four algorithms take as input an N N adjacency
matrix A and compute an N N matrix S , with Sij the length of the shortest path from Vi to
Vj, or a distinguished value ( ) if there is no path.

4.8.1 FLOYD'S ALGORITHM

Floyd's all-pairs shortest-path algorithm is given as Algorithm. It derives the matrix


S in N steps, constructing at each step k an intermediate matrix I(k) containing the best-
known shortest distance between each pair of nodes. Initially, each Iij(0) is set to the length
of the edge (Vi, Vj) if the edge exists, and to otherwise. The k th step of the algorithm
considers each Iij in turn and determines whether the best-known path from Vi to Vj is longer
than the combined lengths of the best-known paths from Vi to Vk and from Vk to Vj. If so,

UNIT-IV 1. 22

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

the entry Iij is updated to reflect the shorter path. This comparison operation is performed a
total of N3 times; hence, we can approximate the sequential cost of this algorithm as tcN 3,
where tc is the cost of a single comparison operation.

Figure 4.10: The fundamental operation in Floyd's sequential shortest-path algorithm

4.8.2 PARALLEL FLOYD 1


The first parallel Floyd algorithm is based on a one-dimensional, rowwise domain
decomposition of the intermediate matrix I and the output matrix S . Notice that this means
the algorithm can use at most N processors. Each task has one or more adjacent rows of I and
is responsible for performing computation on those rows. That is, it executes the following
logic.

for k = 0 to N-1
for i = local_i_start to local_i_end
for j = 0
to N-1

(k+1) = min( (k), (k)+ (k))


endfor
endfor
endfor

UNIT-IV 1. 23

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.11: Parallel version of Floyd's algorithm based on a one-dimensional decomposition


of the I matrix. In (a), the data allocated to a single task are shaded: a contiguous block of
rows. In (b), the data required by this task in the k th step of the algorithm are shaded: its own
block and the k th row.

In the k th step, each task requires, in addition to its local data, the values Ik0, Ik1, ...,
Ik(n-1), that is, the k th row of I. Hence, we specify that the task with this row broadcast it to
all other tasks. This communication can be performed by using a tree structure in log P steps.
Because there are N such broadcasts and each message has size N , the cost is

Notice that each task must serve as the ``root'' for at least one broadcast (assuming
P<=N). Rather than defining P binary tree structures, it suffices to connect the P tasks using a
hypercube structure, which has the useful property of allowing any node to broadcast to all
other nodes in log P steps.

4.8.3 PARALLEL FLOYD 2


An alternative parallel version of Floyd's algorithm uses a two-dimensional
decomposition of the various matrices. This version allows the use of up to processors and
requires that each task execute the following logic.

for k = 0 to N-1
for i = local_i_start to local_i_end
for j = local_j_start to local_j_end

UNIT-IV 1. 24

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

(k+1) = min( (k), (k)+ (k))


endfor
endfor
endfor

Figure 4.12: Parallel version of Floyd's algorithm based on a two-dimensional decomposition


of the I matrix. In (a), the data allocated to a single task are shaded: a contiguous submatrix.
In (b), the data required by this task in the k th step of the algorithm are shaded: its own
block, and part of the k th row and column.
In each step, each task requires, in addition to its local data, N/√P values from two
tasks located in the same row and column of the 2-D task array. Hence, communication
requirements at the k th step can be structured as two broadcast operations: from the task in
each row that possesses part of column k to all other tasks in that row, and from the task in
each column that possesses part of row k to all other tasks in that column.
In each of N steps, N/√P values must be broadcast to the√ Ptasks in each row and
column, and the total cost is

UNIT-IV 1. 25

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Notice that each task must serve as the ``root'' node for at least one broadcast to each
task in the same row and column of the 2-D task array. These communication requirements
can be satisfied by connecting tasks in the same row or column in a hypercube structure.

4.8.4 DIJKSTRA'S ALGORITHM


Dijkstra's single-source shortest-path algorithm computes all shortest paths from a
single vertex, Vs. It can also be used for the all-pairs shortest-path problem, by the simple
expedient of applying it N times---once to each vertex V0, ..., V n-1.
Dijkstra's sequential single-source algorithm is given as Algorithm. It maintains as T
the set of vertices for which shortest paths have not been found, and as di the shortest known
path from Vs to vertex Vi. Initially, T=V and all di=∞. At each step of the algorithm, the
vertex Vm in T with the smallest d value is removed from T . Each neighbor of Vm in T is
examined to see whether a path through Vm would be shorter than the currently best-known
path.

UNIT-IV 1. 26

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.13: The comparison operation performed in Dijkstra's single-source shortest-path


algorithm.

An all-pairs algorithm executes Algorithm N times, once for each vertex. This
involves O(N3) comparisons and takes time N3Tc F , where tc is the cost of a single
comparison in Floyd's algorithm and F is a constant. Empirical studies show that F 1.6; that
is, Dijkstra's algorithm is slightly more expensive than Floyd's algorithm.

4.8.5 PARALLEL DIJKSTRA 1


The first parallel Dijkstra algorithm replicates the graph in each of P tasks. Each task
executes the sequential algorithm for N/P vertices. This algorithm requires no communication
but can utilize at most N processors. Because the sequential Dijkstra algorithm is F times
slower than the sequential Floyd algorithm, the parallel algorithm's execution time is

4.8.6 PARALLEL DIJKSTRA 2


The second parallel Dijkstra algorithm allows for the case when P>N . We define N
sets of P/N tasks. Each set of tasks is given the entire graph and is responsible for computing
shortest paths for a single vertex. Within each set of tasks, the vertices of the graph are
partitioned. Hence, the operation Find Vm € T with minimum dm requires first a local
computation to find the local vertex with minimum d and second a reduction involving all
P/N tasks in the same set in order to determine the globally minimum dm. The reduction can
be achieved by using the butterfly communication structure of Section, in log (P/N) steps.
Hence, as the reduction is performed N times and involves two values, the total cost of this
algorithm is

UNIT-IV 1. 27

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.14: The second parallel Dijkstra algorithm allocates P/N tasks to each of N
instantiations of Dijkstra's single-source shortest-path algorithm. In this figure, N=9 and P=36

4.9 MINIMUM SPANNING TREES

4.9.1 DEFINITION
An edge-weighted graph is a graph where we associate weights or costs with each
edge. A minimum spanning tree (MST) of an edge-weighted graph is a spanning tree whose
weight (the sum of the weights of its edges) is no larger than the weight of any other spanning
tree.

UNIT-IV 1. 28

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.15 An Edge Weighted Graph


4.9.2 ASSUMPTIONS
To streamline the presentation, we adopt the following conventions:
 The graph is connected. The spanning-tree condition in our definition implies that the
graph must be connected for an MST to exist. If a graph is not connected, we can
adapt our algorithms to compute the MSTs of each of its connected components,
collectively known as a minimum spanning forest.
 The edge weights are not necessarily distances. Geometric intuition is sometimes
beneficial, but the edge weights can be arbitrary.
 The edge weights may be zero or negative. If the edge weights are all positive, it
suffices to define the MST as the subgraph with minimal total weight that connects all
the vertices.
 The edge weights are all different. If edges can have equal weights, the minimum
spanning tree may not be unique. Making this assumption simplifies some of our
proofs, but all of our our algorithms work properly even in the presence of equal
weights.

4.9.3 UNDERLYING PRINCIPLES


We recall two of the defining properties of a tree:
 Adding an edge that connects two vertices in a tree creates a unique cycle.
 Removing an edge from a tree breaks it into two separate subtrees.

UNIT-IV 1. 29

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.15 Removing an Edge


A cut of a graph is a partition of its vertices into two disjoint sets. A crossing edge is
an edge that connects a vertex in one set with a vertex in the other. We recall For simplicity,
we assume all edge weights are distinct. Under this assumption, the MST is unique. Define
cut and cycle. The following properties lead to a number of MST algorithms.

4.9.3 PROPOSITION (CUT PROPERTY)


Given any cut in an edge-weighted graph (with all edge weights distinct), the crossing
edge of minimum weight is in the MST of the graph.

Figure 4.16 Minimum weight crossing edge

The cut property is the basis for the algorithms that we consider for the MST problem.
Specifically, they are special cases of the greedy algorithm.

4.9.4 PROPOSITION. (GREEDY MST ALGORITHM)


The following method colors black all edges in the the MST of any connected edge-
weighted graph with V vertices: Starting with all edges colored gray, find a cut with no black
edges, color its minimum-weight edge black, and continue until V-1 edges have been colored
black.

UNIT-IV 1. 30

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.17 MST Algorithm

4.9.5 EDGE-WEIGHTED GRAPH DATA TYPE

We represent the weighted edges using the following API:

The either() and other() methods are useful for accessing the edge's vertices; the compareTo()
method compares edges by weight.

UNIT-IV 1. 31

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

We represent edge-weighted graphs using the following API:

We allow parallel edges and self-loops. EdgeWeightedGraph.java implements the API using
the adjacency-lists representation.

Figure 4.18 Edge-weighted graph representation

4.10 PRIM'S ALGORITHM


Prim's algorithm works by attaching a new edge to a single growing tree at each step:
Start with any vertex as a single-vertex tree; then add V-1 edges to it, always taking next
(coloring black) the minimum-weight edge that connects a vertex on the tree to a vertex not
yet on the tree (a crossing edge for the cut defined by tree vertices).

UNIT-IV 1. 32

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.19 Prim’s MST algorithm

The one-sentence description of Prim's algorithm leaves unanswered a key question: How
do we (efficiently) find the crossing edge of minimal weight?

 Lazy implementation. We use a priority queue to hold the crossing edges and find one
of minimal weight. Each time that we add an edge to the tree, we also add a vertex to
the tree. To maintain the set of crossing edges, we need to add to the priority queue all
edges from that vertex to any non-tree vertex. But we must do more: any edge
connecting the vertex just added to a tree vertex that is already on the priority queue
now becomes ineligible (it is no longer a crossing edge because it connects two tree
vertices). The lazy implementation leaves such edges on the priority queue, deferring
the ineligibility test to when we remove them.

UNIT-IV 1. 33

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.20 Prim’s MST algorithm


 Eager implementation. To improve the lazy implementation of Prim's algorithm, we
might try to delete ineligible edges from the priority queue, so that the priority queue
contains only the crossing edges. But we can eliminate even more edges. The key is to
note that our only interest is in the minimal edge from each non-tree vertex to a tree
vertex. When we add a vertex v to the tree, the only possible change with respect to
each non-tree vertex w is that adding v brings w closer than before to the tree. In
short, we do not need to keep on the priority queue all of the edges from w to vertices
tree—we just need to keep track of the minimum-weight edge and check whether the
addition of v to the tree necessitates that we update that minimum (because of an edge
v-w that has lower weight), which we can do as we process each edge in s adjacency
list. In other words, we maintain on the priority queue just one edge for each non-tree
vertex: the shortest edge that connects it to the tree.

UNIT-IV 1. 34

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.21 Prim’s MST algorithm


4.10.1 PROPOSITION
Prim's algorithm computes the MST of any connected edge-weighted graph. The lazy
version of Prim's algorithm uses space proportional to E and time proportional to E log E (in
the worst case) to compute the MST of a connected edge-weighted graph with E edges and V
vertices; the eager version uses space proportional to V and time proportional to E log V (in
the worst case).

4.10.2 KRUSKAL'S ALGORITHM


Kruskal's algorithm processes the edges in order of their weight values (smallest to
largest), taking for the MST (coloring black) each edge that does not form a cycle with edges
previously added, stopping after adding V-1 edges. The black edges form a forest of trees that
evolves gradually into a single tree, the MST.

UNIT-IV 1. 35

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.22 KRUSKAL'S algorithm

To implement Kruskal's algorithm, we use a priority queue to consider the edges in order by
weight, a union-find data structure to identify those that cause cycles, and a queue to collect
the MST edges.

4.10.3 PROPOSITION
Kruskal's algorithm computes the MST of any connected edge-weighted graph with E
edges and V vertices using extra space proportional to E and time proportional to E log E (in
the worst case).

4.11 BICONNECTIVITY

A node and all the nodes reachable from it compose a connected component. A graph
is called connected if it has only one connected component.

Since the function visit() of DFS visits every node that is reachable and has not
already been visited, the DFS can easily be modified to print out the connected components
of a graph

UNIT-IV 1. 36

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.23 Two connected components

In actual uses of graphs, such as networks, we need to establish not only that every
node is connected to every other node, but also there are at least two independent paths
between any two nodes. A maximum set of nodes for which there are two different paths is
called biconnected.

Figure 4.24 {H,I,J} and {A,B,C,E,F} are biconnected

Another way to define this concept is that there are no single points of failure, no
nodes that when deleted along with any adjoining arcs, would split the graph into two or
more separate connected components. Such a node is called an articulation point.

UNIT-IV 1. 37

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

If a graph contains no articulation points, then it is biconnected. If a graph does


contain articulation points, then it is useful to split the graph into the pieces where each piece
is a maximal biconnected subgraph called a biconnected component.

UNIT-IV 1. 38

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

Figure 4.25 Three biconnected components

UNIT-IV 1. 39

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

DFS can be used to find the articulation points of a graph, and hence the biconnected
components.

Figure 4.26 DFS Tree

In DFS tree, if a node has no back arcs, then it is an articulation point.

UNIT-IV 1. 40

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

4.12 EULER CIRCUITS

 An Euler path is a path that uses every edge of a graph exactly once.
 An Euler path starts and ends at different vertices.
 An Euler circuit is a circuit that uses every edge of a graph exactly once.
 An Euler circuit starts and ends at the same vertex.
A sequence x0, x1, x2, …, xt of vertices is called an euler circuit in a graph G if:
1. x0 = xt;
2. For every i = 0, 1, 2, …, t-1, xi xi+1 is an edge of G; and
3. For every edge e of G, there is a unique i with 0 ≤ i < t so that e = xi xi+1.

Figure 4.27 Euler Circuit -Graph

Here is an euler circuit for this graph:


(1,8,3,6,8,7,2,4,5,6,2,3,1)

4.12.1 EULER’S THEOREM


A graph G has an euler circuit if and only if it is connected and every vertex has even
degree.

4.12.2 ALGORITHM FOR EULER CIRCUITS

 Choose a root vertex r and start with the trivial partial circuit (r).
 Given a partial circuit (r = x0,x1,…,xt = r) that traverses some but not all of the
edges of G containing r, remove these edges from G. Let i be the least integer

UNIT-IV 1. 41

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

for which xi is incident with one of the remaining edges. Form a greedy partial
circuit among the remaining edges of the form (xi = y0,y1,…,ys = xi).
 Expand the original circuit:
 r =(x0,x1,…, xi-1, xi = y0,y1,…,ys = xi, xi+1,…, xt=r)

4.12.3 AN EXAMPLE

Figure 4.28 Euler Circuit -Example

Start with the trivial circuit (1). Then the greedy algorithm yields the partial circuit
(1,2,4,3,1).

UNIT-IV 1. 42

Downloaded by MAHALAKSHMI ([email protected])


lOMoARcPSD|47448036

Paavai Institutions Department of MCA

4.13 QUESTION BANK

PART A -2 Marks

1.Define Graph?
2. Define adjacent nodes?
3. What is a directed graph?
4. What is a undirected graph?
5. What is a loop?
6.What is a simple graph?
7. What is a weighted graph?
8. Define out degree of a graph?
9. Define indegree of a graph?
10. Define path in a graph?
11.What is a simple path?
12. What is a cycle or a circuit?
13. What is an acyclic graph?
14. What is meant by strongly connected in a graph?
15. When is a graph said to be weakly connected?
16. Name the different ways of representing a graph?
17. What is an undirected acyclic graph?
18. What are the two traversal strategies used in traversing a graph?
19. What is a minimum spanning tree?
20. What is NP?

PART B -16 Marks

1.Explain the various representation of graph with example in detail?


2. Define topological sort? Explain with an example?
3. Explain Dijkstra's algorithm with an example?
4.Explain Prim's algorithm with an example?
5. Explain Krushal's algorithm with an example?

UNIT-IV 1. 43

Downloaded by MAHALAKSHMI ([email protected])

You might also like