Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
32 views7 pages

Graph Representation & BFS Tutorial

Uploaded by

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

Graph Representation & BFS Tutorial

Uploaded by

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

Graph and its representations

A Graph is a non-linear data structure consisting of


vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs that
connect any two nodes in the graph. More formally a Graph
is composed of a set of vertices( V ) and a set of edges(
E ). The graph is denoted by G(V, E).

Representations of Graph

Here are the two most common ways to represent a graph :


For simplicity, we are going to consider only unweighted
graphs in this post.

1. Adjacency Matrix
2. Adjacency List

Adjacency Matrix Representation

An adjacency matrix is a way of representing a graph as a


matrix of boolean (0’s and 1’s)

Let’s assume there are n vertices in the graph So, create


a 2D matrix adjMat[n][n] having dimension n x n.

● If there is an edge from vertex i to j, mark


adjMat[i][j] as 1.
● If there is no edge from vertex i to j, mark
adjMat[i][j] as 0.

Representation of Undirected Graph as Adjacency Matrix:

The below figure shows an undirected graph. Initially,


the entire Matrix is ​initialized to 0. If there is an
edge from source to destination, we insert 1 to both
cases (adjMat[destination] and adjMat[destination])
because we can go either way.

Undirected Graph to Adjacency Matrix

#include<stdio.h>

#define V 4

void addEdge(int mat[V][V], int i, int j) {


mat[i][j] = 1;
mat[j][i] = 1; // Since the graph is undirected
}

void displayMatrix(int mat[V][V]) {


for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++)
printf("%d ", mat[i][j]);
printf("\n");
}
}

int main() {
// Create a graph with 4 vertices and no edges
// Note that all values are initialized as 0
int mat[V][V] = {0};

// Now add edges one by one


addEdge(mat, 0, 1);
addEdge(mat, 0, 2);
addEdge(mat, 1, 2);
addEdge(mat, 2, 3);

/* Alternatively, we can also create using the below


code if we know all edges in advance

int mat[V][V] = {
{0, 1, 0, 0},
{1, 0, 1, 0},
{0, 1, 0, 1},
{0, 0, 1, 0}
}; */

printf("Adjacency Matrix Representation\n");


displayMatrix(mat);

return 0;
}

Adjacency Matrix Representation


0 1 1 0
1 0 1 0
1 1 0 1
0 0 1 0

Adjacency List Representation


An array of Lists is used to store edges between two vertices. The size of
array is equal to the number of vertices (i.e, n). Each index in this array
represents a specific vertex in the graph. The entry at the index i of the array
contains a linked list containing the vertices that are adjacent to vertex i.

Let’s assume there are n vertices in the graph So, create an array of list of
size n as adjList[n].

● adjList[0] will have all the nodes which are connected (neighbour) to

vertex 0.

● adjList[1] will have all the nodes which are connected (neighbour) to

vertex 1 and so on.

Representation of Undirected Graph as Adjacency list:

The below undirected graph has 3 vertices. So, an array of list will be created
of size 3, where each indices represent the vertices. Now, vertex 0 has two
neighbours (i.e, 1 and 2). So, insert vertex 1 and 2 at indices 0 of array.
Similarly, For vertex 1, it has two neighbour (i.e, 2 and 0) So, insert vertices 2
and 0 at indices 1 of array. Similarly, for vertex 2, insert its neighbours in
array of list.
Breadth First Search or BFS for a Graph
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 100

// BFS from given source s


void bfs(int adj[MAX][MAX], int V, int s) {

// Create a queue for BFS


int q[MAX], front = 0, rear = 0;

// Initially mark all the vertices as not visited


// When we push a vertex into the q, we mark it as
// visited
bool visited[MAX] = { false };

// Mark the source node as visited and enqueue it


visited[s] = true;
q[rear++] = s;

// Iterate over the queue


while (front < rear) {

// Dequeue a vertex and print it


int curr = q[front++];
printf("%d ", curr);

// Get all adjacent vertices of the dequeued vertex


// If an adjacent has not been visited, mark it visited and
enqueue it
for (int i = 0; i < V; i++) {
if (adj[curr][i] == 1 && !visited[i]) {
visited[i] = true;
q[rear++] = i;
}
}
}
}

// Function to add an edge to the graph


void addEdge(int adj[MAX][MAX], int u, int v) {
adj[u][v] = 1;
adj[v][u] = 1; // Undirected graph
}

int main() {
// Number of vertices in the graph
int V = 5;

// Adjacency matrix representation of the graph


int adj[MAX][MAX] = {0};

// Add edges to the graph


addEdge(adj, 0, 1);
addEdge(adj, 0, 2);
addEdge(adj, 1, 3);
addEdge(adj, 1, 4);
addEdge(adj, 2, 4);

// Perform BFS traversal starting from vertex 0


printf("BFS starting from 0:\n");
bfs(adj, V, 0);

return 0;
}

Output
BFS starting from 0 :
0 1 2 3 4

You might also like