UNIT 3 Some
UNIT 3 Some
UNIT-III
GRAPHS
UNIT-IV 1. 1
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.
The path in a graph is the route taken to reach terminal node from a starting node.
UNIT-IV 1. 3
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.
UNIT-IV 1. 4
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
LECTURE NOTES
4.1 DEFINITIONS
UNIT-IV 1. 6
4.1.6 TREE
A connected graph T without any cycles is called a tree graph or free tree or, simply, a
tree.
UNIT-IV 1. 7
(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:
UNIT-IV 1. 8
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.
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.10 EXAMPLE
UNIT-IV 1. 9
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.
UNIT-IV 1. 10
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
and
P=1011
1011
1011
1011
UNIT-IV 1. 13
(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
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:
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.
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
UNIT-IV 1. 16
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.
UNIT-IV 1. 17
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.
UNIT-IV 1. 18
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.
UNIT-IV 1. 19
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.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.
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
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.
UNIT-IV 1. 22
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.
for k = 0 to N-1
for i = local_i_start to local_i_end
for j = 0
to N-1
UNIT-IV 1. 23
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.
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
UNIT-IV 1. 25
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.
UNIT-IV 1. 26
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.
UNIT-IV 1. 27
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.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
UNIT-IV 1. 29
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.
UNIT-IV 1. 30
The either() and other() methods are useful for accessing the edge's vertices; the compareTo()
method compares edges by weight.
UNIT-IV 1. 31
We allow parallel edges and self-loops. EdgeWeightedGraph.java implements the API using
the adjacency-lists representation.
UNIT-IV 1. 32
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
UNIT-IV 1. 34
UNIT-IV 1. 35
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
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.
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
UNIT-IV 1. 38
UNIT-IV 1. 39
DFS can be used to find the articulation points of a graph, and hence the biconnected
components.
UNIT-IV 1. 40
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.
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
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
Start with the trivial circuit (1). Then the greedy algorithm yields the partial circuit
(1,2,4,3,1).
UNIT-IV 1. 42
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?
UNIT-IV 1. 43