DAA Unit-III
DAA Unit-III
GRAPHS
1. Introduction to Graphs
Why graphs are useful?
Graphs are widely used to model any situation where entities or things are related to each other in
pairs; for example, the following information can be represented by graphs:
Family trees in which the member nodes have an edge from parent to each of their children.
Transportation networks in which nodes are airports, intersections, ports, etc. The edges can be
airline flights, one-way roads, shipping routes, etc.
What is a graph?
Graph: A graph, G, is a non-linear data structure represented as G = (V, E) consists of two sets, where
V is a finite, nonempty set of vertices (also called nodes).
E is a set of pairs of vertices; these pairs are called edges (or Arcs or links). Each edge ‘e’ in E is
identified with a unique pair (a, b) of nodes in V, denoted by e = (a, b).
V (G) and E (G) will present the sets of vertices and edges, respectively, of G.
Example – 1:
Figure 1
Consider a graph, G in above Fig-1. Then the vertex V and edge E can be represented as:
V = {v1, v2, v3, v4, v5, v6} and E = {e1, e2, e3, e4, e5, e6} i.e., E = {(v1, v2) (v2, v3) (v1, v3) (v3, v4),
(v3, v5) (v5, v6)}. There are six edges and vertices in the graph.
Example –2:
The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Null graph (or Empty Graph): A graph G = (V, E) where E = , is called as a null or empty graph.
Trivial graph: A graph with one vertex and no edges is called a trivial graph
2. Graph Terminology
Vertex: A individual data element of a graph is called as Vertex. Vertex is also known as node. In above
example graph, A, B, C, D & E are known as vertices.
Edge: An edge is a connecting link between two vertices. Edge is also known as Arc. An edge is
represented as (startingVertex, endingVertex). For example, in above graph, the link between vertices A
and B is represented as (A, B). In above example graph, there are 7 edges (i.e., (A,B), (A,C), (A,D),
(B,D), (B,E), (C,D), (D,E)).
Types of graphs:
In general, graphs are classified as two types.
i) Undirected graph
ii) Directed graph (or digraph).
iii) Weighted graph
i. undirected graph:
In an undirected graph, the pair of vertices representing any edge is unordered (i.e., the edges of a graph
have no directions). Thus, the pairs (u, v) and (v, u) represent the same edge.
Undirected graph
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.
Two vertices are said to be adjacent if they are joined by an edge. If (u, v) is an edge in
E(G), then we say that the vertices u and v are adjacent and that the edge (u, v) is incident on vertices u
and v.
ii. directed graph:
In a directed graph, each edge is represented by a directed pair <u, v> (i.e., the edges of a graph have
directions); u is the tail and v the head of the edge. Therefore, <v, u> and <u, v> represent two different
edges.
G1 G2
Directed graph
This graph G can be defined as G1 = ( V , E )
Where V = {A,B,C,D,E} and E = {<A,B>,<A,D>, <B,D>, <C,B>,<D,E>,<E,C>}.
G2 = ( V , E ) where V = {a, b, c, d} and E = {<a, b>, <a, d>, <d, b>, <d, d>, <c, c>}
If edge (u, v) is a directed edge, then vertex u is adjacent to v, and v is adjacent from u.
The edge <u, v> is incident to u and v.
V(G1) = {0, 1, 2, 3}; E(G1) = {(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)}
V(G2) = {0, 1, 2, 3, 4, 5, 6}; E(G2) = {(0, 1), (0, 2), (1, 3), (1, 4), (2, 5),(2, 6)}
V(G3) = {0, 1, 2 }; E(G3) = {<0, 1>, <1, 0>, <1, 2>}
Weighted graph:
Labeled graph or weighted graph: A graph is said to be labeled if every edge in the graph is assigned
some data.
A graph G is said to be weighted graph if every edge and/or vertices in the graph is
assigned with some weight or value. Weight of the edge, denoted by w(e) is a positive value which
indicates the cost of traversing the edge.
A weighted graph can be defined as G = (V, E, We, Wv) where V is the set of vertices, E is
the set at edges and We is a weights of the edges whose domain is E and Wv is a weight to the vertices
whose domain is V.
Weighted graphs
Consider a graph, which shows the distance in km between four metropolitan cities in India.
Here V = {N, K, M, C,} E = {(N, K), (N, M,), (M, K), (M, C), (K, C)} We = {55,47, 39, 27, 113}
and Wv = {N, K, M, C} The weight at the vertices is not necessary to maintain have become the set W v
and V are same. A graph with weighted edges is called a network.
Complete graph:
A graph G is said to be a complete, if all its nodes are fully connected, that is, there is a path from one
node to every other node in the graph.
An n vertex, undirected graph with exactly edges is said to be complete graph.
Let a and b are two vertices in the directed graph, then it is a complete graph if there is a path
from a to b as well as a path from b to a. An n vertex, directed graph with exactly n(n – 1) edges is said
to be complete digraph.
In the case of a digraph with n vertices, the maximum number of edges is given by nC2 = n(n–1).
Such a graph with exactly n(n – 1 ) edges is said to be a complete digraph.
Simple (directed) graph: A directed graph G is said to be a simple (directed graph) if and only if it has
no parallel edges and loops. However, a simple directed graph may contain cycle with an exception that it
cannot have more than one loop at a given node.
A graph (digraph) that do not contain any cycle is called acyclic graph.
A directed graph that do not contain any cycles is called as directed acyclic graph (DAG).
Subgraph: A subgraph G’ = (V’,E’) of a graph G = (V, E) is such that V’ ⊆ V and E’ ⊆ E.
Path: A path between two vertices a and b in a graph is a sequence of vertices a, v1, v2,..., vn, b such that
all of the edges (a, v1), (v2, v3), ..., (vn, b) exist in the graph. In other words, a path is just what it sounds
like: a way to get from a to b possibly going through other vertices. The length of the path is the number
of edges on it.
A simple path is a path in which all the vertices except possibly the first and last vertices are distinct.
A cycle is a simple path in which the first and last vertices are the same. A cycle is also known as a
circuit, elementary cycle, circular path or polygon. A closed simple path with length 3 or more is known
as a cycle. A cycle of length k is called a k – cycle.
Two vertices vi, vj in a graph G are said to be connected only if there is a path in G between vi and vj
Acyclic graph and cyclic graph: If a graph does not contain any cycles, then the graph is called an
acyclic graph and if it contains cycle then the graph is cyclic.
An undirected graph is said to be a connected graph if every pair of distinct vertices vi , vj are
connected. That is to say that there are no isolated nodes in a connected graph.
In the case of an undirected graph which is not connected, the maximal connected subgraph is called as a
connected component or simply a component.
A directed graph is said to be strongly connected if every pair of distinct vertices vi, vj are connected (by
means of a directed path). That is, if u and v are two distinct vertices in V(G), there is a directed path
from u to v and also from v to u.
A strongly connected component is a maximal subgraph that is strongly connected.
If G is a directed graph:
The in-degree of a vertex is the number of edges for which vertex is head (That is, the number of edges
coming towards vertex v).
And, the out-degree of a vertex is the number of edges for which the vertex is the tail (That is, the
number of edges leaving vertex v).
Degree of directed vertex(v) = in-degree(v) + out-degree(v)
For undirected graphs, there is no in-degree and out-degree; there is just the degree
If di is the degree of the vertex i in a graph with n vertices and e edges, then the number of edges is
e= / 2.
A vertex with degree one is called as a pendant vertex or end vertex (that is in-degree(v) = 1 and out-
degree(v) = 0).
Example
In the linked-adjacency-list representation of a weighted graph, each node contains two data
fields.
o Vertex and weight
struct node
{ Vertex Label Weight
char vertex;
int weight;
struct node*next;
};
Examples for Graph Representation:
4. Adjacency Multi-list Representation: Graphs can also be represented using multi-lists which can be
said to be modified version of adjacency lists.
Adjacency multi-list is an edge-based rather than a vertex-based representation of graphs.
A multi-list representation basically consists of two parts—a directory of nodes’ information and a
set of linked lists storing information about edges.
While there is a single entry for each node in the node directory, every node, on the other hand,
appears in two adjacency lists (one for the node at each end of the edge).
For example, the directory entry for node i points to the adjacency list for node i. This means that
the nodes are shared among several lists.
In a multi-list representation, the information about an edge (vi, vj) of an undirected graph can
be stored using the following attributes:
M: A single bit field to indicate whether the edge has been examined or not.
vi: A vertex in the graph that is connected to vertex vj by an edge.
vj: A vertex in the graph that is connected to vertex vi by an edge.
Link i for vi: A link that points to another node that has an edge incident on v i.
Link j for vj: A link that points to another node that has an edge incident on v j.
Example – 1: Consider the undirected graph given in below. The adjacency multi-list for the graph can
be given below. Using the adjacency multi-list given above, the adjacency list for vertices can be
constructed as shown below figure.
It is slow to add/delete a node; a complex It is fast to add/delete a node; easier than the
operation O(n^2). matrix representation
It is fast to add a new edge O(1). It fast to add a new
edge O(1).
4. Operations on graphs
1. Creation of graph
2. Insert vertex
3. Delete vertex
4. Add edge
5. Delete edge
6. Find a vertex
7. Traversal of a graph (visit all the vertices)
1. Creation of a graph: The algorithm for creation of graph using adjacency matrix will be as follows:
1. Declare an array of A[n][n] which will store graph, where n is the number of vertices in G.
2. Enter how many nodes you want in a graph
3. Enter the edges of the graph by two vertices each, say vi, vj indicates some edge.
4. If the graph is directed then set A[i][j] = 1
5. If the graph is undirected then set A[i][j] = A[j][i] = 1
6. When all edges of the desired graph is entered, then print the graph A.
2. Insert Vertex: Insert vertex adds a new vertex to a graph. When the vertex is inserted, it must be
connected.
3. Delete Vertex: Delete vertex removes a vertex from the graph. When a vertex is deleted, all
connecting edges are also removed.
If we delete vertex v4, then the neighboring vertices are v1, v2, v3 set to 0. That is, A[v4][v1] =A[v1][v4]
= 0 ; A[v4][v2] =A[v2][v4] = 0; A[v4][v3] =A[v3][v4] = 0.
4. Add edge: We can insert an edge in the graph. The edge is always represented by two vertices. So, to
add an edge, two vertices must be specified.
Example:
5. Delete Edge: Delete edge removes one edge from a graph. An edge can be removed by giving its two
vertices V1 and V2.
6. Find vertex: Find vertex traverses a graph, looking for a specified vertex. If the vertex is found, its
data are returned. If it is not found, an error is indicated.
1. Represent the following graph using adjacency matrix, adjacency list and incidence matrix.
Step – 6: There is no new vertex to be visited from D. So use back track. Pop D
on to the stack.
Step – 9: There is no new vertex to be visited from G. So use back track. Pop G
on to the stack.
Step – 10: There is no new vertex to be visited from F. So use back track. Pop F
on to the stack.
Step – 11: There is no new vertex to be visited from E. So use back track. Pop E
on to the stack.
Step – 12: There is no new vertex to be visited from C. So use back track. Pop C
on to the stack.
Step – 13: There is no new vertex to be visited from B. So use back track. Pop B
on to the stack.
Step – 14: There is no new vertex to be visited from A. So use back track. Pop A
on to the stack.
Stack became empty. So, stop DFS traversal. Final result of DFS is
A – B – C – E – D – F – G.
Blog: anilkumarprathipati.wordpress.com Page 17
Graph Algorithms UNIT-3
Example – 2:
Example – 3:
Consider the graph G, which is represented by its adjacency lists as shown in Figure. If a depth-first
search is initiated from vertex 0, then the vertices of G are visited in the following order: 0 1 3 7 4 5 2 6.
Since DFS(0) visits all vertices that can be reached from 0 the vertices visited, together with all edges in
G incident to these vertices form a connected component of G.
The depth first search order: v0, v1, v3, v7, v4, v5, v2, v6.
Analysis or DFS:
When G is represented by its adjacency lists, the vertices w adjacent to v can be determined by
following a chain of links.
Since DFS examines each node (v) in the adjacency lists at most once and there are 2e list nodes
the time to complete the search is O(v+e).
If G is represented by its adjacency matrix then the time to determine all
vertices adjacent to v is O(n). Since at most n vertices are visited the total time is O( n2).
Step – 2: Visit all adjacent vertices of A which are not visited (B, D, E).
Insert newly visited vertices into the queue and delete A from queue.
Step – 3: Visit all adjacent vertices of D which are not visited (there is no vertex). Delete D from
queue.
Step – 4: Visit all adjacent vertices of E which are not visited (C, F).
Insert newly visited vertices into the queue and delete E from queue.
Step – 5: Visit all adjacent vertices of B which are not visited (there is no vertex). Delete B from
queue..
Step – 6: Visit all adjacent vertices of C which are not visited (G).
Insert newly visited vertices into the queue and delete C from queue.
Step – 7: Visit all adjacent vertices of F which are not visited (there is no vertex). Delete F from
queue.
Blog: anilkumarprathipati.wordpress.com Page 21
Graph Algorithms UNIT-3
Step – 8: Visit all adjacent vertices of G which are not visited (there is no vertex). Delete G from
queue.
Queue became empty. So, stop BFS traversal. Final result of BFS is
A – D – E – B – C – F – G.
Example – 2:
The procedure terminates, and the vertices 1 through 9 marked as visited. So, the BFS is 1, 2, 3, 4,
5, 6, 7, 8, 9.
Example – 3: Consider the graph of below Figure. If a breadth-first search is performed beginning at
vertex 0, then we first visit vertex 0. Next, vertices 1 and 2 are visited. Then vertices 3, 4, 5, and 6, and
finally 7, are visited.
The breadth first search order: v0, v1, v2, v3, v4, v5, v6, v7.
Analysis of BFS:
Each visited vertex enters the queue exactly once.
So, the while loop is iterated at most n times.
If an adjacency matrix is used the loop takes O(n) time for each vertex visited. The total time is
therefore, O(n2).
If adjacency lists are used, the loop has a total cost of deg(0) + deg (1) + … + deg(n) = O(e) where
deg is the degree of vertex i.
BFS vs DFS
BFS (Breadth First Search) DFS (Depth First Search)
It uses Queue data structure for finding the It uses Stack data structure for finding the
shortest path shortest path
In BFS, we can be find single source shortest In DFS, we might traverse through more edges
path in an un-weighted graph, because in to reach a destination vertex from a source.
BFS, we reach a vertex with minimum
number of edges from a source vertex.
It is more suitable for searching vertices It is more suitable when there are solutions
which are closer to the given source. away from source.
Applications: Spanning Trees, Connected Applications: Spanning Trees, Connected
components (paths), Cycles, and shortest components (paths), Cycles, and Bi-connected
Paths. (bipartite) Components.
The Time complexity of BFS and DFS is O(V + E) when Adjacency List is used and O(V^2)
when Adjacency Matrix is used, where V stands for vertices and E stands for edges.
3 1
C D
2
Graph G
Fig: Graph
The feasible solutions are the spanning tree of the graph G. Those are
2 2 2
A B A B A B A B
3 1 3 1 3 1
C D C D C D C D
2 2 2
1 .Total Weights=6 2 .Total Weights=6 3 .Total Weights=7 4 .Total Weights=5
Fig: Spanning trees for the above graph
From the above spanning tree the figure 4 gives the optimal solution, because it is the spanning
tree with the minimum cost i.e. it is a minimum spanning tree of the graph G.
The greedy technique suggests constructing a solution to an optimization problem through a
sequence of steps, each expanding a partially constructed solution obtained so far until a complete
solution to the problem is reached to each step, the choice made must be feasible, locally optimal and
irrecoverable.
Feasible: The choice which is made has to be satisfying the problems constraints.
Locally optimal: The choice has to be the best local choice among all feasible choices available on
that step.
Irrecoverable: The choice once made cannot be changed on sub-sequent steps of the algorithm
(Greedy method).
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible
number of edges. Hence, a spanning tree does not have cycles and it cannot be disconnected.
Note: Every connected and undirected Graph G has at least one spanning tree. A disconnected
graph does not have any spanning tree.
i. Kruskal's Algorithm
It is used to find the minimum cost spanning tree uses the greedy approach. This algorithm treats
the graph as a forest and every node it has as an individual tree. A tree connects to another only and only
if, it has the least cost among all available options and does not violate MST properties.
To understand Kruskal's algorithm let us consider the following example:
In case of parallel edges, keep the one which has the least cost associated and remove all others. Only
thick line represents the edge between vertices/nodes.
The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them does not
violate spanning tree properties, so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them again −
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph. −
We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
We observe that edges with cost 5 and 6 also create circuits. We ignore them and move on.
Now we are left with only one node to be added. Between the two least cost edges available 7 and 8, we
shall add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we now have minimum cost
spanning tree.
Example -2: Consider the below graph to find the MST
Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one
which has the least cost associated and remove all others.
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with weight 7 and 8,
respectively. We choose the edge S,A as it is lesser than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We
select the one which has the lowest cost and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will check all the
edges again. However, we will choose only the least cost edge. In this case, C-3-D is the new edge,
which is less than other edges' cost 8, 6, 4, etc.
We may find that the output spanning tree of the same graph using two different algorithms is
same.
Example:
Algorithm:
Initially, S(1) = 0; S(2) = 0; …, S(5) = 0. Dist(1) is cost (V0 ,V1) = 50. Dist(2) is cost of (V0,V2)
that is 10. Dist(3) is cost (V0,V3) is α (that means there is no path). Dist(4) is 45. Dist(5) is α. Let S(V0)
is 1 Dist (V0) is 0; For num is 2 to 4 do Dist(2) = min{Dist(1)…Dist(5)} = 10.
Some of Dist(3), … Dist(5) may change. New Dist(3) = min (old Dist(3) = α, Dist(2)+cost(2,3)) =
min(α,10+15) = 25.
New Dist(4) is min (old Dist(4) = 45, Dist(2)+cost(2,4) is min(45,10+α) = 45 new Dist(5) is min
(old Dist(5) = α, Dist(2)+cost(2,5)) = min(α,10+ α) = α. Now choose u such that dist (u)= min{Dist (3),
Dist (4), Dist (5)))} i.e min { 25, 45, α } = 25, Therefore U is 3 S(3) is 1. The process is continued. The
time taken by the algorithm is 0(n2 ).where n is the number of vertices.
The edges on the shortest paths from a vertex v to all remaining vertices in a connected undirected
graph G form a spanning tree of G called shortest path spanning tree. • Shortest path spanning tree for the
above graph.
C[i, j] =
D1[i,j] =
D2[i,j] = andD3[i,j] =
As no of nodes in the given graph are 3, So D3 [i,j] gives the shortest distance from every vertex i to every
other vertex j.
Algorithm AllPaths (cost, D, n)
{
for i = 1 to n do
for j = 1 to n do
D[i, j] = cost[i, j];
for k= 1 to n do
for i = 1 to n do
for j = 1 to n do
D[i, j] = min { D[i,j], D[i,k]+ D[k,j] };
}
Time Complexity:-The time needed by All Paths algorithm is especially easy to determine because the
loop is independent of the data in the matrix D. The D[i, j] is obtained after the statement is iterated n 3
times. So the time complexity of All Paths algorithm is Ө(n3).
1.Many decisions and sequences areguaranteed 1.Divide the given problem into many subproblems.
and all the overlapping subinstancesare consid Find the individual solutions andcombine them to get
ered. the solution for themain problem
3.Split the input at every possible points rather 3.Split the input only at specific points (midpoint),
each problem is independent.
than at a particular point
4. Subproblems are dependent on the main 4. Subproblems are independent on the main
Problem Problem
5. Time taken by this approach is not that 5. Time taken by this approach efficient when
much efficient when compared with DAC. compared with GA.
6.Space requirement is less when compared 6.Space requirement is very much high when
DAC approach. compared GA approach.