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

Graphs

The document provides an introduction to graphs, defining them as non-linear data structures consisting of vertices (nodes) and edges (links). It covers various graph terminologies, types of edges, and representations, including adjacency matrices, incidence matrices, and adjacency lists. Additionally, it discusses graph traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS), as well as concepts like spanning trees and algorithms for finding minimum spanning trees.

Uploaded by

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

Graphs

The document provides an introduction to graphs, defining them as non-linear data structures consisting of vertices (nodes) and edges (links). It covers various graph terminologies, types of edges, and representations, including adjacency matrices, incidence matrices, and adjacency lists. Additionally, it discusses graph traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS), as well as concepts like spanning trees and algorithms for finding minimum spanning trees.

Uploaded by

vidit6163
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 122

Graphs

Introduction to Graphs
Graph is a non-linear data structure. It contains a set of points
known as nodes (or vertices) and a set of links known as edges (or
Arcs). Here edges are used to connect the vertices. A graph is
defined as follows...

Graph is a collection of vertices and arcs in which vertices are


connected with arcs
or
Graph is a collection of nodes and edges in which nodes are
connected with edges

Generally, a graph G is represented as G = ( V, E), where V is set


of vertices and E is set of edges.

05/23/2025 1
Graphs
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)}.

05/23/2025 2
Graphs

Graph Terminology
We use the following terms in graph data structure...
Vertex
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)).

05/23/2025 3
Graphs

Edges are three types.

1.Undirected Edge - An undirected egde is a bidirectional edge.


If there is undirected edge between vertices A and B then edge
(A , B) is equal to edge (B , A).
2.Directed Edge - A directed egde is a unidirectional edge. If
there is directed edge between vertices A and B then edge (A ,
B) is not equal to edge (B , A).
3.Weighted Edge - A weighted egde is a edge with value (cost)
on it.

05/23/2025 4
Graphs

Undirected Graph
A graph with only undirected edges is said to be undirected graph.
Directed Graph
A graph with only directed edges is said to be directed graph.

Mixed Graph
A graph with both undirected and directed edges is said to be mixed
graph.

05/23/2025 5
Graphs

Origin
If a edge is directed, its first endpoint is said to be the origin of it.
Destination
If a edge is directed, its first endpoint is said to be the origin of it
and the other endpoint is said to be the destination of that edge.
Adjacent
If there is an edge between vertices A and B then both A and B are
said to be adjacent. In other words, vertices A and B are said to be
adjacent if there is an edge between them.

05/23/2025 6
Graphs

Incident
Edge is said to be incident on a vertex if the vertex is one of the
endpoints of that edge.
Outgoing Edge
A directed edge is said to be outgoing edge on its origin vertex.
Incoming Edge
A directed edge is said to be incoming edge on its destination
vertex.
Degree
Total number of edges connected to a vertex is said to be degree of
that vertex.

05/23/2025 7
Graphs

Indegree
Total number of incoming edges connected to a vertex is said to be
indegree of that vertex.
Outdegree
Total number of outgoing edges connected to a vertex is said to be
outdegree of that vertex.
Parallel edges or Multiple edges
If there are two undirected edges with same end vertices and two
directed edges with same origin and destination, such edges are
called parallel edges or multiple edges.

05/23/2025 8
Graphs

Self-loop
Edge (undirected or directed) is a self-loop if its two endpoints
coincide with each other.
Simple Graph
A graph is said to be simple if there are no parallel and self-loop
edges.
Path
A path is a sequence of alternate vertices and edges that starts at a
vertex and ends at other vertex such that each edge is incident to
its predecessor and successor vertex.

05/23/2025 9
Graphs

Graph Representations

Graph data structure is represented using following representations...

1.Adjacency Matrix
2.Incidence Matrix
3.Adjacency List

05/23/2025 10
Graphs

Adjacency Matrix
In this representation, the graph is represented using a matrix of size
total number of vertices by a total number of vertices.

That means a graph with 4 vertices is represented using a matrix of


size 4X4.

In this matrix, both rows and columns represent vertices.

This matrix is filled with either 1 or 0.

Here, 1 represents that there is an edge from row vertex to column


vertex and 0 represents that there is no edge from row vertex to
column vertex.
05/23/2025 11
Graphs

Undirected graph representation...

Directed graph representation...

05/23/2025 12
Graphs

Incidence Matrix

In this representation, the graph is represented using a matrix of


size total number of vertices by a total number of edges.

That means graph with 4 vertices and 6 edges is represented using


a matrix of size 4X6.

In this matrix, rows represent vertices and columns represents


edges. This matrix is filled with 0 or 1 or -1.

05/23/2025 13
Graphs
Incidence Matrix

Here, 0 represents that the row edge is not connected to column


vertex

1 represents that the row edge is connected as the outgoing edge to


column vertex and -1 represents that the row edge is connected as
the incoming edge to column vertex.

For example, consider the following directed graph representation...

05/23/2025 14
Graphs

Adjacency List
In this representation, every vertex of a graph contains list of its
adjacent vertices.

For example, consider the following directed graph representation


implemented using linked list...

This representation can also be implemented using an array as follows..

05/23/2025 15
Graphs
Graph Traversal – BFS

Graph traversal is a technique used for searching a vertex in a


graph. The graph traversal is also used to decide the order of
vertices is visited in the search process.

A graph traversal finds the edges to be used in the search process


without creating loops. That means using graph traversal we visit
all the vertices of the graph without getting into looping path.

There are two graph traversal techniques and they are as


follows...
1.DFS (Depth First Search)
2.BFS (Breadth First Search)

05/23/2025 16
Graphs

DFS (Depth First Search)

DFS traversal of a graph produces a spanning tree as final


result. Spanning Tree is a graph without loops.

We use Stack data structure with maximum size of total


number of vertices in the graph to implement DFS traversal.

05/23/2025 17
Graphs
DFS (Depth First Search)
We use the following steps to implement DFS traversal...
 Step 1 - Define a Stack of size total number of vertices in the
graph.

 Step 2 - Select any vertex as starting point for traversal. Visit


that vertex and push it on to the Stack.

 Step 3 - Visitany one of the non-visited adjacent vertices


of a vertex which is at the top of stack and push it on to the
stack.

05/23/2025 18
Graphs

 Step 4 - Repeat step 3 until there is no new vertex to be visited


from the vertex which is at the top of the stack.

 Step 5 - When there is no new vertex to visit then use back


tracking and pop one vertex from the stack.

 Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.

 Step 7 - When stack becomes Empty, then produce final


spanning tree by removing unused edges from the graph

05/23/2025 19
Graphs

Example

05/23/2025 20
Graphs

05/23/2025 21
Graphs

05/23/2025 22
Graphs

05/23/2025 23
Graphs

05/23/2025 24
Graphs

05/23/2025 25
Graphs

05/23/2025 26
Graphs

05/23/2025 27
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 28
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 29
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 30
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 31
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 32
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 33
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 34
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 35
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 36
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 37
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 38
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 39
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 40
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 41
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 42
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 43
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 44
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 45
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 46
Graph Traversal "Dr. Kunwar Pal,
CSED, NIT Jalandhar " 47
Graphs

BFS (Breadth First Search)

BFS traversal of a graph produces a spanning tree as final result.


Spanning Tree is a graph without loops. We use Queue data
structure with maximum size of total number of vertices in the
graph to implement BFS traversal.

We use the following steps to implement BFS traversal...


 Step 1 - Define a Queue of size total number of vertices in the
graph.
 Step 2 - Select any vertex as starting point for traversal. Visit
that vertex and insert it into the Queue.

05/23/2025 48
Graphs

 Step 3 - Visit all the non-visited adjacent vertices of the


vertex which is at front of the Queue and insert them into the
Queue.

 Step 4 - When there is no new vertex to be visited from the


vertex which is at front of the Queue then delete that vertex.

 Step 5 - Repeat steps 3 and 4 until queue becomes empty.

 Step 6 - When queue becomes empty, then produce final


spanning tree by removing unused edges from the graph.

05/23/2025 49
Graphs

05/23/2025 50
Graphs

05/23/2025 51
Graphs

05/23/2025 52
Graphs

Queue became Empty, So, stop the BFS process.

Final result of BFS is a Spanning tree as shown below

05/23/2025 53
Graph Traversal "Dr. Kunwar Pal, CSED, N 54
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 55
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 56
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 57
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 58
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 59
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 60
IT Jalandhar "
Graph Traversal "Dr. Kunwar Pal, CSED, N 61
IT Jalandhar "
Graphs
Spanning Tree

Spanning tree can be defined as a sub-graph of connected,


undirected graph G that is a tree produced by removing the
desired number of edges from a graph.
In other words, Spanning tree is a non-cyclic sub-graph of a
connected and undirected graph G that connects all the vertices
together.
A graph G can have multiple spanning trees.

05/23/2025 62
Graphs

Spanning Tree

Minimum Spanning Tree

There can be weights assigned to every edge in a weighted graph.


However, A minimum spanning tree is a spanning tree which has
minimal total weight.
In other words, minimum spanning tree is the one which contains
the least weight among all other spanning tree of some particular
graph.

05/23/2025 63
Graphs

In this section, we will discuss the algorithms to calculate the shortest


path between two nodes in a graph.
There are two algorithms which are being used for this purpose.
o Prim's Algorithm
o Kruskal's Algorithm

05/23/2025 64
Graphs

Prim's Algorithm

Prim's Algorithm is used to find the minimum spanning tree from a


graph. Prim's algorithm finds the subset of edges that includes every
vertex of the graph such that the sum of the weights of the edges can
be minimized.

Prim's algorithm starts with the single node and explore all the adjacent
nodes with all the connecting edges at every step. The edges with the
minimal weights causing no cycles in the graph got selected.

05/23/2025 65
Graphs

Prim’s Approach

o Step 1: Select a starting vertex


o Step 2: Repeat Steps 3 and 4 until there are fringe vertices
o Step 3: Select an edge e connecting the tree vertex and fringe
vertex that has minimum weight
o Step 4: Add the selected edge and the vertex to the
minimum spanning tree T
[END OF LOOP]
o Step 5: EXIT

05/23/2025 66
Graphs
Example :
Construct a minimum spanning tree of the graph given in the
following figure by using prim's algorithm.

Solution
o Step 1 : Choose a starting vertex B.
o Step 2: Add the vertices that are adjacent to B. the edges that
connecting the vertices are shown by dotted lines.

05/23/2025 67
Graphs

o Step 3: Choose the edge with the minimum weight among all. i.e.
BD and add it to MST. Add the adjacent vertices of D i.e. C and E.
o Step 3: Choose the edge with the minimum weight among all. In this
case, the edges DE and CD are such edges. Add them to MST and
explore the adjacent of C i.e. E and A.
o Step 4: Choose the edge with the minimum weight i.e. CA. We
can't choose CE as it would cause cycle in the graph.

The graph produces in the step 4 is the minimum spanning tree of the
graph shown in the above figure.
The cost of MST will be calculated as;
cost(MST) = 4 + 2 + 1 + 3 = 10 units.

05/23/2025 68
Graphs

05/23/2025 69
05/23/2025 70
Graphs
Kruskal's Algorithm

Kruskal's Algorithm is used to find the minimum spanning tree for


a connected weighted graph.

The main target of the algorithm is to find the subset of edges by


using which, we can traverse every vertex of the graph.

Kruskal's algorithm follows greedy approach which finds an


optimum solution at every stage instead of focusing on a global
optimum.

05/23/2025 71
Graphs

The Kruskal's algorithm is given as follows.

Solution
o Step 1: Create a forest in such a way that each graph is a
separate tree.

o Step 2: Create a priority queue Q that contains all the edges


of the graph.

o Step 3: Repeat Steps 4 and 5 while Q is NOT EMPTY

05/23/2025 72
Graphs

o Step 4: Remove an edge from Q

o Step 5: IF the edge obtained in Step 4 connects two different


trees, then Add it to the forest (for combining two trees into one
tree).

ELSE
Discard the edge

Step 6: END

05/23/2025 73
Graphs

Example :
Apply the Kruskal's algorithm on the graph given as follows.

Solution:
the weight of the edges given as:

Edge AE AD AC AB BC CD DE
Weight 5 10 7 1 3 4 2
05/23/2025 74
Graphs

Sort the edges according to their weights.

Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10

Start constructing the tree; Add AB to the MST;

Add DE to the MST;

05/23/2025 75
Graphs

Add BC to the MST;

05/23/2025 76
Graphs

The next step is to add AE, but we can't add that as it will cause a
cycle.
The next edge to be added is AC, but it can't be added as it will
cause a cycle.
The next edge to be added is AD, but it can't be added as it
will contain a cycle.
Hence, the final MST is the one which is shown in
the step 4. the cost of MST = 1 + 2 + 3 + 4 = 10.

05/23/2025 77
05/23/2025 78
Shortest Path Problems

What is shortest path ?


 shortest length between two vertices for an unweighted graph:
 smallest cost between two vertices for a weighted graph:

B 210 B

A A
450
60 190

C unweighted C weighted
graph graph
200 130
D D
E E

05/23/2025 79
Shortest Path Problems

• How can we find the shortest route between two points on a map?
• Model the problem as a graph problem:
– Road map is a weighted graph:
vertices = cities
edges = road segments between cities
edge weights = road distances
– Goal: find a shortest path between two vertices (cities)

05/23/2025 80
Variants of Shortest Paths
• Single-source shortest path
– G = (V, E)  find a shortest path from a given source vertex s to
each vertex v  V
• Single-destination shortest path
– Find a shortest path to a given destination vertex t from each
vertex v
– Reverse the direction of each edge  single-source
• Single-pair shortest path
– Find a shortest path from u to v for given vertices u and v
– Solve the single-source problem
• All-pairs shortest-paths
– Find a shortest path from u to v for every pair of vertices u and
v
05/23/2025 81
Shortest Path Algorithm
Find the single source shortest path from the vertex A.

Priority queue  A B C D E F
0∞ ∞ ∞ ∞∞
Solution set (S)= ϕ
Extract min(Queue) = A
S = S U {A}
= {A}

05/23/2025 82
Shortest Path Algorithm

Priority queue  B C D E F
2 ∞ 8 ∞∞
Solution set (S)= {A}
Extract min(Queue) = B
S = S U {B}
= {A,B}

Priority queue  C D E F
5 7 ∞∞
Solution set (S)= {A,B}
Extract min(Queue) = C
S = S U {C}
= {A,B,C}

05/23/2025 83
Shortest Path Algorithm
Priority queue  D E F
6 12 11
Solution set (S)= {A,B,C}
Extract min(Queue) = D
S = S U {D}
= {A,B,C,D}

Priority queue  E F
12 11
Solution set (S)= {A,B,C,D}
Extract min(Queue) = F
S = S U {F}
= {A,B,C,D,F}

05/23/2025 84
Shortest Path Algorithm

Priority queue  E
12
Solution set (S)= {A,B,C,D,F}
Extract min(Queue) = E
S = S U {E}
= {A,B,C,D,F,E}

Source Distance From Source


A A B C D E F
0 2 5 6 12 11

05/23/2025 85
Dijkstra’s Algorithm
t 1 x
 
Find the single source shortest path from the 10 9
vertex S. s 0
2 3 4 6

5 7
 
2
y z

05/23/2025 86
Dijkstra’s Algorithm

• Find shortest path from s to t.

24
2 3
9

s
18
14
2 6
6
30 4 19
11
15 5
5
6
20 16

t
7 44

05/23/2025 87
05/23/2025 88
Applications of Dijkstra's Algorithm

- Traffic Information Systems are most prominent use


- Mapping (Map Quest, Google Maps)
- Routing Systems

05/23/2025 89
Applications of Dijkstra's Algorithm

 One particularly relevant this week:


epidemiology

 Prof. Lauren Meyers (MIT, Biology


Dept.) uses networks to model the spread
of infectious diseases and design
prevention and response strategies.

05/23/2025 90
Applications of Dijkstra's Algorithm

 Vertices represent individuals, and


edges their possible contacts. It is useful
to calculate how a particular individual is
connected to others.

 Knowing the shortest path lengths to


other individuals can be a relevant
indicator of the potential of a particular
individual to infect others.

05/23/2025 91
Floyd Warshall Algorithm-

•Floyd Warshall Algorithm is a famous algorithm.

•It is used to solve All Pairs Shortest Path Problem.

•It computes the shortest path between every pair of vertices of the
given graph.

•Floyd Warshall Algorithm is an example of dynamic programming


approach.

05/23/2025 92
Floyd Warshall Algorithm-

Advantages-
Floyd Warshall Algorithm has the following main advantages-

•It is extremely simple.


•It is easy to implement.

05/23/2025 93
Floyd Warshall Algorithm-

Floyd Warshall Algorithm is as shown below-

Create a |V| x |V| matrix // It represents the distance between


every pair of vertices as given
For each cell (i,j) in M do-
if i = = j
M[ i ][ j ] = 0 // For all diagonal elements, value = 0
if (i , j) is an edge in E
M[ i ][ j ] = weight(i,j) // If there exists a direct edge between
the vertices, value = weight of edge
else
M[ i ][ j ] = infinity // If there is no direct edge between the
vertices, value = ∞

05/23/2025 94
Floyd Warshall Algorithm-

for k from 1 to |V|

for i from 1 to |V|

for j from 1 to |V|

if M[ i ][ j ] > M[ i ][ k ] + M[ k ][ j ]
M[ i ][ j ] = M[ i ][ k ] + M[ k ][ j ]

05/23/2025 95
Floyd Warshall Algorithm-

Time Complexity-

•Floyd Warshall Algorithm consists of three loops over all the nodes.

•The inner most loop consists of only constant complexity operations.

•Hence, the asymptotic complexity of Floyd Warshall algorithm is O(n 3).

•Here, n is the number of nodes in the given graph.

05/23/2025 96
Floyd Warshall Algorithm-

When Floyd Warshall Algorithm Is Used?


•Floyd Warshall Algorithm is best suited for dense graphs.

•This is because its complexity depends only on the number of vertices


in the given graph.

•For sparse graphs, Johnson’s Algorithm is more suitable.

05/23/2025 97
Floyd Warshall Problem

Problem-

Consider the following directed weighted graph-

Using Floyd Warshall Algorithm, find the shortest path distance between
every pair of vertices.

05/23/2025 98
Floyd Warshall Algorithm-
Solution-

Step-01:

•Remove all the self loops and parallel edges (keeping the lowest weight
edge) from the graph.
•In the given graph, there are neither self edges nor parallel edges.

Step-02:

•Write the initial distance matrix.


•It represents the distance between every pair of vertices in the form of
given weights.

05/23/2025 99
Floyd Warshall Algorithm-

Step-02: (Continue)

•For diagonal elements (representing self-loops), distance value = 0.


•For vertices having a direct edge between them, distance value =
weight of that edge.
•For vertices having no direct edge between them, distance value = ∞.

Initial distance matrix for the given graph is-

05/23/2025 100
Floyd Warshall Algorithm-

Step-03:

Using Floyd Warshall Algorithm, write the following 4 matrices-

05/23/2025 101
Floyd Warshall Algorithm-

Step-03:

The last matrix D4 represents the


shortest path distance between every
pair of vertices.

05/23/2025 102
Floyd Warshall Algorithm-

05/23/2025 103
Floyd Warshall Algorithm-

05/23/2025 104
Floyd Warshall Algorithm-

05/23/2025 105
Floyd Warshall Algorithm-

Remember-
•In the above problem, there are 4 vertices in the given graph.
•So, there will be total 4 matrices of order 4 x 4 in the solution
excluding the initial distance matrix.
•Diagonal elements of each matrix will always be 0.

05/23/2025 106
Transitive Closure of a Graph

Transitive Closure of a Graph

Given a directed graph, find out if a vertex j is reachable from another vertex i for all vertex
pairs (i, j) in the given graph. Here reachable mean that there is a path from vertex i to j.

The reach-ability matrix is called the Transitive Closure of a Graph.

Transitive closure of graphs is

1111
1111
1111
0001

05/23/2025 107
Faculty Video Links, Youtube & NPTEL
Video Links and Online Courses Details

• Self Made Video Link:

• YouTube/other Video Links

https://www.youtube.com/watch?v=eujc7_0FhCI

https://www.youtube.com/watch?v=gXgEDyodOJU

05/23/2025 108
Daily Quiz

1. Prove that, if there exists negative edge, dijkstra’s shortest path


algorithm may fail to find the shortest path
2. How you can find number of shortest paths from source to destination
using Dijkstra’s algorithm?
3. Suppose you are given a graph where each edge represents the path
cost and each vertex has also a cost which represents that, if you
select a path using this node, the cost will be added with the path cost.
How can it be solved using Dijkstra’s algorithm?
4. Write the algorithm of Floyd Warshal all pair shortest path.
5. Write the algorithm for prims and Kruskal.
6. What do you mean by minimum spanning tree.
7. Define adjacency list and adjacency matrix.
8. Write the applications of graph.

05/23/2025 109
Weekly Assignment

Q.1 Draw the complete undirected graphs on one, two, three, four and five
vertices. Prove that the number of edges in an n vertex complete graph is
n ( n -1)/2.

Q.2 What are the different ways of representing a graph? Represent the
following
graph using those ways.

Q.3 What are the different ways of representing a graph? Represent the
following
graph using those ways.

05/23/2025 110
Weekly Assignment

Q.4 Write an algorithm which does depth first search through an un-
weighted
connected graph. In an un-weighted graph, would breadth first search
or depth
first search or neither find a shortest path tree from some node? Why?

Q.5 Which are the two standard ways of traversing a graph? Explain
them with an example of each.

Q.6 Explain Dijkstra’s algorithm for finding the shortest path in a


given graph.

Q.7 Write the short note on Tree Traversal.

05/23/2025 111
MCQ s
1. Which of the following algorithms can be used to most efficiently
determine the presence of a cycle in a given graph ?
a. Depth First Search
b. Breadth First Search
c. Prim's Minimum Spanning Tree Algorithm
d. Kruskal' Minimum Spanning Tree Algorithm
2. Traversal of a graph is different from tree because
a. There can be a loop in graph so we must maintain a visited
flag for every vertex
b. DFS of a graph uses stack, but in-order traversal of a tree is
recursive
c. BFS of a graph uses queue, but a time efficient BFS of a tree
is recursive.
d. All of above
05/23/2025 112
MCQ s
3. What are the appropriate data structures for following algorithms?
a. Breadth First Search
b. Depth First Search
c. Prim's Minimum Spanning Tree
d. Kruskal' Minimum Spanning Tree

4. The Breadth First Search algorithm has been implemented using the
queue data structure. One possible order of visiting the nodes of the
following graph is

05/23/2025 113
MCQ s
5. Consider the following graph,
Among the following sequences:
(I) a b e g h f
(II) a b f e h g
(III) a b f h g e
(IV) a f g h b e
Which are depth first traversals of the above graph?

6. Given two vertices in a graph s and t, which of the two traversals


(BFS and DFS) can be used to find if there is path from s to t?

a) Only BFS
b) Only DFS
c) Both BFS and DFS
d) Neither BFS nor DFS

05/23/2025 114
MCQ s
7. Which of the following is the most commonly used data structure for
implementing Dijkstra's Algorithm?
a. Max priority queue
b. Stack
c. Circular queue
d. Min priority queue

8. What is the time complexity of Dijikstra's algorithm?


a. O(N)
b. O(N3)
c. O(N2)
d. O(logN)

05/23/2025 115
MCQ s

9. Which of the following condition is sufficient to detect cycle in a


directed graph?
a. There is an edge from currently being visited node to an already
visited node.
b. There is an edge from currently being visited node to an ancestor of
currently visited node in DFS forest.
c. Every node is seen twice in DFS.
d. None of the above

10. Dijkstra's Algorithm is used to solve ..... problems.


a. All pair shortest path
b. Single source shortest path
c. Network flow
d. Sorting
05/23/2025 116
Expected Questions for University Exam

1. Define graph. How a graph is different from a tree?


2. Write Dijkastra algorithm for finding the shortest path from a source
vertex.
3. Write the algorithm of floyd warshall. Also explain it with example.
4. Define connected graph and complete graph with example.
5. How the graph can be represented in memory? Explain with suitable
example.
6. Explain Depth First Search Traversal in Graph with the help of an
example.

05/23/2025 117
Expected Questions for University Exam

7. Define Floyd Warshall Algorithm for all pair shortest path and apply the
same on following graph:

8. Considering ‘S’ as source vertex, Apply the Dijkstra’s shortest path


algorithm on the following graph:

05/23/2025 118
Old Question Papers

https://drive.google.com/open?id=15fAkaWQ5c
cZRZPzwlP4PBh1LxcPp4VAd

05/23/2025 119
Summary

Graph theory is an exceptionally rich area for programmers and


designers.

Graphs can be used to solve some very complex problems, such as least
cost routing, mapping, program analysis, and so on.

Network devices, such as routers and switches, use graphs to calculate


optimal routing for traffic.

05/23/2025 120
References

[1] Aaron M. Tenenbaum, Yedidyah Langsam and Moshe J. Augenstein,


“Data Structures Using C and C++”, PHI Learning Private Limited,
Delhi India
[2] Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia
Publications Pvt Ltd Delhi India.
[3] Lipschutz, “Data Structures” Schaum’s Outline Series, Tata
McGraw-hill Education (India) Pvt. Ltd.
[4] Thareja, “Data Structure Using C” Oxford Higher Education.
[5] AK Sharma, “Data Structure Using C”, Pearson Education India.

05/23/2025 121
Thank you

05/23/2025 122

You might also like