top of page

Graphs: Data Structure Class Notes

Updated: Oct 18, 2022

Mobiprep has created last-minute notes for all topics of Graphs to help you with the revision of concepts for your university examinations. So let’s get started with the lecture notes on Graphs.

  1. Data Structure

  2. Stacks

  3. Queues

  4. Linked Lists

  5. Sorting

  6. Tree

  7. Graph

  8. Hashing and Searching

Our team has curated a list of the most important questions asked in universities such as DU, DTU, VIT, SRM, IP, Pune University, Manipal University, and many more. The questions are created from the previous year's question papers of colleges and universities.

  1. What is BFS?

  2. What is DFS?

  3. What is Prims minimum spanning tree?

  4. What is Krushkal's spanning tree?

  5. Explain Floyd's algorithm.


Question 1 - What is BFS?

Answer - BFS is a tree or graph traversal algorithm in which it starts with root node or 'search key' and visits all neighbour nodes of the same depth level before moving to next depth level.

To implement this algorithm, we need to use the Queue . All the adjacent vertices are added into the queue, when all adjacent vertices are completed, one item is removed from the queue and start traversing through that vertex again.


Algorithm BFS(G, key) is
declare Q as queue
label key as discovered
while Q is not empty do
v = Q.dequeue()
if v is the key then
return v
for all edges from v to w in G.adjacentEdges(v) do
if w is not labeled as discovered then
label w as discovered
w.parent= v

C Program to implement BFS

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 5

struct Vertex {
char label;
bool visited;

//queue variables

int queue[MAX];
int rear = -1;
int front = 0;
int queueItemCount = 0;

//graph variables

//array of vertices
struct Vertex* lstVertices[MAX];

//adjacency matrix
int adjMatrix[MAX][MAX];

//vertex count
int vertexCount = 0;

//queue functions

void insert(int data) {
queue[++rear] = data;

int removeData() {
return queue[front++];

bool isQueueEmpty() {
return queueItemCount == 0;

//graph functions

//add vertex to the vertex list
void addVertex(char label) {
struct Vertex* vertex = (struct Vertex*) malloc(sizeof(struct Vertex));
vertex->label = label;
vertex->visited = false;
lstVertices[vertexCount++] = vertex;

//add edge to edge array
void addEdge(int start,int end) {
adjMatrix[start][end] = 1;
adjMatrix[end][start] = 1;

//display the vertex
void displayVertex(int vertexIndex) {
printf("%c ",lstVertices[vertexIndex]->label);

//get the adjacent unvisited vertex
int getAdjUnvisitedVertex(int vertexIndex) {
int i;

for(i = 0; i<vertexCount; i++) {
if(adjMatrix[vertexIndex][i] == 1 && lstVertices[i]->visited == false)
return i;

return -1;

void breadthFirstSearch() {
int i;

//mark first node as visited
lstVertices[0]->visited = true;

//display the vertex

//insert vertex index in queue
int unvisitedVertex;

while(!isQueueEmpty()) {
//get the unvisited vertex of vertex which is at front of the queue
int tempVertex = removeData();

//no adjacent vertex found
while((unvisitedVertex = getAdjUnvisitedVertex(tempVertex)) != -1) {
lstVertices[unvisitedVertex]->visited = true;


//queue is empty, search is complete, reset the visited flag
for(i = 0;i<vertexCount;i++) {
lstVertices[i]->visited = false;

int main() {
int i, j;

for(i = 0; i<MAX; i++) // set adjacency {
for(j = 0; j<MAX; j++) // matrix to 0
adjMatrix[i][j] = 0;

addVertex('S'); // 0
addVertex('A'); // 1
addVertex('B'); // 2
addVertex('C'); // 3
addVertex('D'); // 4

addEdge(0, 1); // S - A
addEdge(0, 2); // S - B
addEdge(0, 3); // S - C
addEdge(1, 4); // A - D
addEdge(2, 4); // B - D
addEdge(3, 4); // C - D

printf("\nBreadth First Search: ");


return 0;


Question 2 - What is DFS?

Answer - DFS is a graph traversal algorithm in which it starts with key node and explore all the nodes along the branch before backtracking.

It starts by putting any one of the graph node on top of a stack and then takes top item of the stack and add it to the visited list. After that it creates a list of vertex's adjacent nodes and add them to the top of the stack if they aren't visited. It keeps repeating previous steps until the stack is empty.

DFS recursive implementation:

DFS(G, u)
u.visited = true
for each v ∈ G.Adj[u]
if v.visited == false

init() {
For each u ∈ G
u.visited = false
For each u ∈ G
DFS(G, u)

C Program to implement DFS:

#include <stdio.h>
#include <stdlib.h>

struct node {
int vertex;
struct node* next;

struct node* createNode(int v);

struct Graph {
int numVertices;
int* visited;

// We need int** to store a two dimensional array.
// Similary, we need struct node** to store an array of Linked lists
struct node** adjLists;

// DFS algo
void DFS(struct Graph* graph, int vertex) {
struct node* adjList = graph->adjLists[vertex];
struct node* temp = adjList;

graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);


Question 3 - What is Prims minimum spanning tree?

Answer - Prims minimum spanning tree algorithm is a greedy algorithm. This algorithm is used to find minimum cost spanning tree by using greedy approach.

It maintains two sets of vertices one contains vertices already included in MST and other one contains vertices that is not included yet.

At each step it selects minimum cost edge and moves to other end of the edge to select another MST.

Steps in Prims Algorithm:

  1. Remove all the loops and parallel edges.(keep the edges with least cost).

  2. Choose any node as a root node and add it to the spanning tree.

  3. Add another vertex which is not in spanning tree and has least weight edge connected with the spanning tree.

  4. Repeat previous step until all the vertices of the graph added to the spanning tree.

  5. Print the cost.

C code to implement Prims Algorithm

int a,b,u,v,n,i,j,ne=1;
int visited[10]= {
void main() {
printf("\n Enter the number of nodes:");
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
while(ne<n) {
for (i=1,min=999;i<=n;i++)
for (j=1;j<=n;j++)
if(visited[i]!=0) {
if(visited[u]==0 || visited[v]==0) {
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
printf("\n Minimun cost=%d",mincost);


Question 4 - What is Krushkal's spanning tree?

Answer - Krushkal's spanning tree is a spanning tree which is formed by using krushkal's algorithm, which is a greedy algorithm.

The idea is to make a greedy choice to select smallest weighted edge that does not form a cycle with already constructed MST.

Steps in Krushkal's Algorithm:

  1. Remove all the loops and parallel edges from the graph.

  2. Arrange all the edges in their increasing order og weight.

  3. Add the edge which has least weightage.

  4. Repeat step 3 till all the vertices of the graph is added without forming any cycle.

C code :


#define MAX 30

typedef struct edge
int u,v,w;

typedef struct edgelist
edge data[MAX];
int n;

edgelist elist;

int G[MAX][MAX],n;
edgelist spanlist;

void kruskal();
int find(int belongs[],int vertexno);
void union1(int belongs[],int c1,int c2);
void sort();
void print();

void main()
int i,j,total_cost;

printf("\nEnter number of vertices:");


printf("\nEnter the adjacency matrix:\n");



void kruskal()
int belongs[MAX],i,j,cno1,cno2;







int find(int belongs[],int vertexno)

void union1(int belongs[],int c1,int c2)
int i;


void sort()
int i,j;
edge temp;


void print()
int i,cost=0;


printf("\n\nCost of the spanning tree=%d",cost);


Question 5 - Explain Floyd's algorithm.

Answer - Floyd's algorithm is a dynamic approach to find the shortest path between all the pairs of vertices in a weighted graph. The graph can be directed or undirected graph.

Pseudo Code :

n = no of vertices
A = matrix of dimension n*n
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
return A

C program:

#include <stdio.h>

// defining the number of vertices
#define nV 4

#define INF 999

void printMatrix(int matrix[][nV]);

// Implementing floyd warshall algorithm
void floydWarshall(int graph[][nV]) {
int matrix[nV][nV], i, j, k;

for (i = 0; i < nV; i++)
for (j = 0; j < nV; j++)
matrix[i][j] = graph[i][j];

// Adding vertices individually
for (k = 0; k < nV; k++) {
for (i = 0; i < nV; i++) {
for (j = 0; j < nV; j++) {
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];

void printMatrix(int matrix[][nV]) {
for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
if (matrix[i][j] == INF)
printf("%4s", "INF");
printf("%4d", matrix[i][j]);

int main() {
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};

26 views0 comments

Recent Posts

See All
bottom of page