0% found this document useful (0 votes)
79 views13 pages

Assignment On Course Code

This document is an assignment submission on graph representation and breadth-first search (BFS) for a data structures course. It discusses different ways of representing graphs using adjacency matrices and lists. It then provides details on performing BFS traversal on graphs, including pseudocode. It also gives examples and applications of BFS, such as determining the level of each node in a tree.

Uploaded by

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

Assignment On Course Code

This document is an assignment submission on graph representation and breadth-first search (BFS) for a data structures course. It discusses different ways of representing graphs using adjacency matrices and lists. It then provides details on performing BFS traversal on graphs, including pseudocode. It also gives examples and applications of BFS, such as determining the level of each node in a tree.

Uploaded by

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

Assignment on: Graph Representation & BFS

Course Name: Data Structure


Course Code: CSE - 203

Submitted to:
DR. MD. SELIM AL MAMUN
Associate Professor,
Department of Computer Science & Engineering
Jatiya Kabi Kazi Nazrul Islam University

Submitted By:
Mithu Chandra Debnath
Roll: 18102030
Session: 2017-18
Dept. of Computer Science & Engineering
Jatiya Kabi Kazi Nazrul Islam University

Submission date:26-09-2021
Graph Representations
In graph theory, a graph representation is a technique to store graph into the memory of
computer.

To represent a graph, we just need the set of vertices, and for each vertex the neighbors of the
vertex (vertices which is directly connected to it by an edge). If it is a weighted graph, then the
weight will be associated with each edge.

There are different ways to optimally represent a graph, depending on the density of its edges,
type of operations to be performed and ease of use.

1. Adjacency Matrix
o Adjacency matrix is a sequential representation.
o It is used to represent which nodes are adjacent to each other. i.e. is there any edge
connecting nodes to a graph.
o In this representation, we have to construct a nXn matrix A. If there is any edge from a
vertex i to vertex j, then the corresponding element of A, ai,j = 1, otherwise ai,j= 0.

o If there is any weighted graph then instead of 1s and 0s, we can store the weight of the
edge.

Example
Consider the following undirected graph representation:

Undirected graph representation


Directed graph representation

See the directed graph representation:

In the above examples, 1 represents an edge from row vertex to column vertex, and 0 represents
no edge from row vertex to column vertex.

Undirected weighted graph representation


Directed weighted graph representation

A B C D E
A 0 7 8 5 0
B 0 0 0 10 6
C 0 0 0 9 0
D 0 0 0 0 15
E 0 0 0 0 0
Breadth First Search
Breadth First Search (BFS)

There are many ways to traverse graphs. BFS is the most commonly used approach.

BFS is a traversing algorithm where you should start traversing from a selected node (source or starting node)
and traverse the graph layer wise thus exploring the neighbor nodes (nodes which are directly connected to
source node). You must then move towards the next-level neighbors’ nodes.

As the name BFS suggests, you are required to traverse the graph breadthwise as follows:

1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer

Consider the following diagram. 


The distance between the nodes in layer 1 is comparatively lesser than the distance between the
nodes in layer 2. Therefore, in BFS, you must traverse all the nodes in layer 1 before you move
to the nodes in layer 2.

Traversing child nodes


A graph can contain cycles, which may bring you to the same node again while traversing the
graph. To avoid processing of same node again, use a Boolean array which marks the node after
it is processed. While visiting the nodes in the layer of a graph, store them in a manner such that
you can traverse the corresponding child nodes in a similar order.

In the earlier diagram, start traversing from 0 and visit its child nodes 1, 2, and 3. Store them in
the order in which they are visited. This will allow you to visit the child nodes of 1 first (i.e. 4
and 5), then of 2 (i.e. 6 and 7), and then of 3 (i.e. 7) etc.

To make this process easy, use a queue to store the node and mark it as 'visited' until all its
neighbors (vertices that are directly connected to it) are marked. The queue follows the First In
First Out (FIFO) queuing method, and therefore, the neighbors of the node will be visited in the
order in which they were inserted in the node i.e. the node that was inserted first will be visited
first, and so on.

Pseudocode

BFS (G, s) //Where G is the graph and s is the source node


let Q be queue.
Q.enqueue( s ) //Inserting s in queue until all its neighbor vertices are marked.

mark s as visited.
while ( Q is not empty)
//Removing that vertex from queue, whose neighbor will be visited now
v = Q.dequeue()

//processing all the neighbors of v


for all neighbours w of v in Graph G
if w is not visited
Q.enqueue(w) //Stores w in Q to further visit its neighbor
mark w as visited.

Traversing process 
The traversing will start from the source node and push s in queue. s will be marked as 'visited'.

First iteration

● s will be popped from the queue


● Neighbors of s i.e. 1 and 2 will be traversed
● 1 and 2, which have not been traversed earlier, are traversed. They will be:
o Pushed in the queue
o 1 and 2 will be marked as visited

Second iteration

● 1 is popped from the queue


● Neighbors of 1 i.e. s and 3 are traversed
● s is ignored because it is marked as 'visited'
● 3, which has not been traversed earlier, is traversed. It is:
o Pushed in the queue
o Marked as visited

Third iteration

● 2 is popped from the queue


● Neighbors of 2 i.e. s, 3, and 4 are traversed
● 3 and s are ignored because they are marked as 'visited'
● 4, which has not been traversed earlier, is traversed. It is:
o Pushed in the queue
o Marked as visited

Fourth iteration

● 3 is popped from the queue


● Neighbors of 3 i.e. 1, 2, and 5 are traversed
● 1 and 2 are ignored because they are marked as 'visited'
● 5, which has not been traversed earlier, is traversed. It is:
o Pushed in the queue
o Marked as visited

Fifth iteration

● 4 will be popped from the queue


● Neighbors of 4 i.e. 2 is traversed
● 2 is ignored because it is already marked as 'visited'

Sixth iteration

● 5 is popped from the queue


● Neighbors of 5 i.e. 3 is traversed
● 3 is ignored because it is already marked as 'visited'

The queue is empty and it comes out of the loop. All the nodes have been traversed by using
BFS.

If all the edges in a graph are of the same weight, then BFS can also be used to find the minimum
distance between the nodes in a graph.

Example:
As in this diagram, start from the source node, to find the distance between the source node and
node 1. If you do not follow the BFS algorithm, you can go from the source node to node 2 and
then to node 1. This approach will calculate the distance between the source node and node 1 as
2, whereas, the minimum distance is actually 1. The minimum distance can be calculated
correctly by using the BFS algorithm.

Complexity
The time complexity of BFS is O(V + E), where V is the number of nodes and E is the number
of edges.

Applications
1. How to determine the level of each node in the given tree?
As you know in BFS, you traverse level wise. You can also use BFS to determine the level of
each node.

Program Code

#include<stdio.h>

int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;

void bfs(int v) {
for(i = 1; i <= n; i++)
if(a[v][i] && !visited[i])
q[++r] = i;
if(f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
}
}

void main() {
int v;
printf("\n Enter the number of vertices:");
scanf("%d", &n);

for(i=1; i <= n; i++) {


q[i] = 0;
visited[i] = 0;
}

printf("\n Enter graph data in matrix form:\n");


for(i=1; i<=n; i++) {
for(j=1;j<=n;j++) {
scanf("%d", &a[i][j]);
}
}
printf("\n Enter the starting vertex:");
scanf("%d", &v);
bfs(v);
printf("\n The node which are reachable are:\n");

for(i=1; i <= n; i++) {


if(visited[i])
printf("%d\t", i);
else {
printf("\n Bfs is not possible. Not all nodes are
reachable");
break;
}
}
}

In this code, while you visit each node, the level of that node is set with an increment in the level
of its parent node. This is how the level of each node is determined.
node level [node]

s (source node) 0
1 1
2 1
3 2
4 2
5 2
6 2
7 3

You might also like