DS Unit 4
DS Unit 4
Ans. Graph is a non linear data structure, it contains a set of points known as nodes (or vertices)
and set of linkes known as edges (or Arcs) which connets the vertices.
A graph is defined as follows:
Graph is a collection of vertices and arcs which connects vertices in the graph
or
Graph is a collection of nodes and edges which connects nodes in the graph
A Graph 'G' is a collection of two set V and E where vertices are denoted as v0, v1... vn-1 and
collection of edges e0, e1... en where V(G) is set of vertices, E(G) is set of edges and graph G
can be represented as G(V,E).
Example
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)}.
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)).
Page 1 of 16
Q.2 Explain different types of graph and terminology used in graph ?
S e l f L o o p : If there is an edge whose starting and ending vertices are same i.e
(V 1 , V 1 ) is an edge then it is called Self Loop.
Page 2 of 16
P a r a l l e l E d g e s : I f there are more than 1 edge between the same pair of vertices,
then they are known as Parallel Edges.
Page 3 of 16
C y c l e : A Cycle is a path containing atleast one or more edge which starts from a
vertex and terminates into the same vertex is called Cyclic Graph.
Bridge : If on removing an edge from the graph, the graph becomes disconnected then that edge
is called the Bridge.
Let us consider that we have a tree T. let our tree T is a binary tree that us complete binary tree.
Then there is an efficient way of representing T in the memory called the sequential
representation or array representation of T. This representation uses only a linear array TREE as
follows:
For Example:
Consider the following Tree:
Page 4 of 16
Its sequential representation is as follow:
Representation of a node:
In this representation of binary tree root will contain the location of the root R of T. If any one of
the subtree is empty, then the corresponding pointer will contain the null value if the tree T itself
is empty, the ROOT will contain the null value.
Example
Consider the binary tree T in the figure. A schematic diagram of the linked list representation
of T appears in the following figure. Observe that each node is pictured with its three fields, and
that the empty subtree is pictured by using x for null entries.
Binary Tree
Page 5 of 16
Linked Representation of the Binary Tree
In this representation, graph can be represented using a matrix of size total number of vertices by
total number of vertices. That means if a graph with 4 vertices can be represented using a matrix
of 4X4 class. In this matrix, rows and columns both represents vertices. This matrix is filled with
either 1 or 0. Here, 1 represents there is an edge from row vertex to column vertex and 0
represents there is no edge from row vertex to column vertex.
Page 6 of 16
For example, consider the following undirected graph representation.
2. Adjacency List :
In this representation, every vertex of graph contains list of its adjacent vertices.
For example, consider the following directed graph representation implemented using linked list.
Page 7 of 16
Q. 6 Explain the DFS algorithm with the help of example
Ans.It is the traversal technique which is used whenever it is possible to search the graph deeper.
Given as input graph G = (V, E) and a source vertex S from where the searching start.
First we travel or visit the starting node then we travel through each node along a path
which begins at Stack 'S' i.e we visit a neighbour vertex of S and again a neighbour os S
and so on.
DFS works on both directed and undirected graph.
DFS(vertex i)
1. Initialize a stack 'S' of vertex 'w'
2. Push 'i' in the stack 'S'
3. Repeat step 4 to 5 while(stack S is not empty)
4. i = pop(S)
5. if(!visited[i]) then
a) visited[i] = 1
b) for each w adjacent to i
if(!visited[w]) then push w in the stack S
6. Return
Page 8 of 16
1. Initially, push J onto the stack as follows :
STACK : J
3. Pop top element K and push onto stack all the neighbours of K
STACK : D, E, G
4. Pop top element G and push onto stack all the neighbours of G
STACK : D, E, C
5. Pop top element C and push onto stack all the neighbours of C
STACK : D, E, F
6. Pop top element F and push onto stack all the neighbours of F
STACK : D, E
7. Pop top element E and push onto stack all the neighbours of E
STACK : D
Page 9 of 16
Q.7 Explain BFS algorithm with the help of example?
Ans. It is the technique which uses queue for traversing all the nodes of the graph.
In this, first we take any node as the starting node then we take all the adjacent nodes to
that starting node. Similarly, we take for all other adjacent nodes and so on.
We maintain the status of visited nodes in an array so that no node can be traversed again.
BFS also works on directed and undirected graphs.
BFS(vertex V)
1. Initialize a Queue Q
2. Add V to Queue Q
3. Set visited[V] = 1
4. Repeat step 5 to 6 while(Q is not empty)
5. Delete the element V from Q
6. for all vertices w adjacent from V
a) if(!visited[w]) then
set visited[w] = 1 and add vertex w to the Q
7. Return
Example of BFS
1. Consider the above figure. Initially, add A to Queue and add Null to ORIG array
as follows :
QUEUE : A
ORIG : ∅
Page 10 of 16
ORIG : A
Ans. 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..
Page 11 of 16
By this definition, we can draw a conclusion that every connected and undirected Graph G has at
least one spanning tree. A disconnected graph does not have any spanning tree, as it cannot be
spanned to all its vertices.
We found three spanning trees off one complete graph. A complete undirected graph can have
maximum nn-2 number of spanning trees, where n is the number of nodes. In the above
addressed example, n is 3, hence 33−2 = 3 spanning trees are possible.
Ans. Given a connected weighted Graph 'G', it is often desired to create a spanning tree 'T' for 'G'
such that the sum of the weights of the tree edges in 'T' is as small as possible. Such a tree which
is formed with least possible cost is called Minimum Cost Spanning Tree and connects all the
nodes in G.
Some of the methods for creating a minimum cost spanning tree for a weighted graph is -
Prim's Algorithm
Kruskal's Algorithm
Q.9 Write down the algorithm for Prim’s spanning tree with the help of example.
Ans.
The following are the main 3 steps of the Prim's Algorithm:
1. Begin with any vertex which you think would be suitable and add it to the tree.
Page 12 of 16
2. Find an edge that connects any vertex in the tree to any vertex that is not in the tree. Note
that, we don't have to form cycles.
3. Stop when n - 1 edges have been added to the tree.
Q.10 Write down the algorithm for Kruskal’s spanning tree with the help of example.
Ans Kruskal’s algorithms is one of the types of greedy algorithms which is used to solve a
minimal spanning tree (MST) by choosing the best possible choice at each step and then, this
decision leads to the best over all solution.
1. Arrange the edges by weight: least weight first and heaviest last.
2. Choose the lightest not examined edge from the diagram. Add this chosen edge to the
tree, only if doing so will not make a cycle.
3. Stop the process whenever n - 1 edges have been added to the tree.
For example :
Solution:
The list of edges in order of their weights or sizes would be as follows:
Page 13 of 16
All the vertices have been connected now, hence, the last iteration number 5, gives us the
optimal solution, and the minimum length would be the sum of all weights given to these edges,
as 2 + 3 + 4 + 4 + 5 = 18 is the length of the shortest path by applying Kruskal's Algorithm.
Page 14 of 16
ED 2
AB 3
CD 4
AE 4
EF 5,
with Total weight of tree equal to 18
Q.11 Write down the Dijikstra algorithm for finding out the shortest path in the graph.
Let us consider vertex 1 and 9 as the start and destination vertex respectively. Initially, all the
vertices except the start vertex are marked by ∞ and the start vertex is marked by 0.
Page 15 of 16
Step1 Step2 Step3 Step4 Step5 Step6 Step7 Step8
Vertex Initial
V1 V3 V2 V4 V5 V7 V8 V6
1 0 0 0 0 0 0 0 0 0
2 ∞ 5 4 4 4 4 4 4 4
3 ∞ 2 2 2 2 2 2 2 2
4 ∞ ∞ ∞ 7 7 7 7 7 7
5 ∞ ∞ ∞ 11 9 9 9 9 9
6 ∞ ∞ ∞ ∞ ∞ 17 17 16 16
7 ∞ ∞ 11 11 11 11 11 11 11
8 ∞ ∞ ∞ ∞ ∞ 16 13 13 13
9 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 20
Hence, the minimum distance of vertex 9 from vertex 1 is 20. And the path is
1→ 3→ 7→ 8→ 6→ 9
Page 16 of 16