Assignment On Course Code
Assignment On Course Code
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:
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.
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
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
mark s as visited.
while ( Q is not empty)
//Removing that vertex from queue, whose neighbor will be visited now
v = Q.dequeue()
Traversing process
The traversing will start from the source node and push s in queue. s will be marked as 'visited'.
First iteration
Second iteration
Third iteration
Fourth iteration
Fifth iteration
Sixth iteration
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>
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);
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