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

Ads La7 - Graph Traversal Using Bfs and Dfs

The document provides an overview of graph traversal techniques, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). It includes C code implementations for both algorithms, detailing how each method visits vertices in a graph without looping. The BFS algorithm explores nodes layer by layer, while the DFS algorithm explores as far as possible along each branch before backtracking.

Uploaded by

OML series
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)
17 views7 pages

Ads La7 - Graph Traversal Using Bfs and Dfs

The document provides an overview of graph traversal techniques, specifically Breadth-First Search (BFS) and Depth-First Search (DFS). It includes C code implementations for both algorithms, detailing how each method visits vertices in a graph without looping. The BFS algorithm explores nodes layer by layer, while the DFS algorithm explores as far as possible along each branch before backtracking.

Uploaded by

OML series
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/ 7

GRAPH TRAVERSAL USING BFS

AND DFS
Name: Om Lohade
Class: IT – C
PRN: 12320123
Roll no.: SEDA 3

C program for graph traversals using BFS and DFS

Graph traversal is a search technique to find a vertex in a graph. In the search


process, graph traversal is also used to determine the order in which it visits
vertices. Without producing loops, a graph traversal finds the edges to be
employed in the search process. That is, utilizing graph traversal, you can visit
all the graph's vertices without going through a looping path.
There are two methods for traversing graphs, which are as follows:
Breadth-First Search or BFS Algorithm
Depth- First Search or DFS Algorithm

BFS: BFS is a graph traversal approach in which you start at a source node and
layer by layer through the graph, analyzing the nodes directly related to the
source node. Then, in BFS traversal, you must move on to the next-level
neighbour nodes.

DFS: Initially all vertices are marked unvisited in DFS algorithm.


The traversal starts at a vertex u in the graph. By starting at vertex u it
considers the edges from u to other vertices.
If the edge leads to an already visited vertex, then backtrack to current vertex
u.
If an edge leads to an unvisited vertex, then go to that vertex and start
processing from that vertex. That means the new vertex becomes the current
root for traversal.
Follow this process until all vertices are marked visited.

BFS Code:
#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#define MAX 50

typedef struct graph

int v;

bool adj[MAX][MAX];

}Graph;

Graph *creategraph(int k)

Graph *g = malloc(sizeof(Graph));

g->v=k;

for(int i=0;i<k;i++)

for(int j=0;j<k;j++)

g->adj[i][j]=false;

}
return g;

void createEdge(Graph *g,int s, int d)

g->adj[s][d] = true;

void destroy(Graph *g)

free(g);

void BFS(Graph *g, int s)

bool visited[g->v];

for (int j=0;j<g->v;j++)

visited[j]=false;

int queue[g->v];

int front=0,rear=0;

queue[rear++]=s;

visited[s]=true;

while(front!=rear)

s = queue[front++];

printf("%d ", s);


for(int adj=0;adj<g->v;adj++)

if(g->adj[s][adj] && !visited[adj])

visited[adj]=true;

queue[rear++]=adj;

int main()

// Create a graph

Graph* g = creategraph(4);

createEdge(g, 0, 1);

createEdge(g, 0, 2);

createEdge(g, 1, 2);

createEdge(g, 2, 0);

createEdge(g, 2, 3);

createEdge(g, 3, 3);

printf("Following is Breadth First Traversal "

"(starting from vertex 1) \n");

BFS(g, 1);

destroy(g);

return 0;

}
DFS Code:
#include <stdio.h>

#include <stdlib.h>

int vis[100];

struct graph

int V;

int E;

int **adj;

};

struct graph* adj()

struct graph *g = (struct graph*)malloc(sizeof(struct graph));

g->V=7;

g->E=7;

g->adj = (int**)malloc((g->V)*sizeof(int*));

for(int i=0;i<g->V;i++)

{
g->adj[i] = (int*)malloc((g->V)*sizeof(int));

for(int j=0;j<g->V;j++)

for(int k=0;k<g->V;k++)

g->adj[j][k]=0;

g->adj[0][1] = g->adj[1][0] = 1;

g->adj[0][2] = g->adj[2][0] = 1;

g->adj[1][3] = g->adj[3][1] = 1;

g->adj[1][4] = g->adj[4][1] = 1;

g->adj[1][5] = g->adj[5][1] = 1;

g->adj[1][6] = g->adj[6][1] = 1;

g->adj[6][2] = g->adj[2][6] = 1;

return g;

void DFS(struct graph *g, int u)

vis[u]=1;

printf("%d ", u);

for(int v=0; v<g->V;v++)

if(!vis[v] && g->adj[u][v])

DFS(g,v);

void DFStraversal(struct graph *g)

{
for(int p=0;p<100;p++)

vis[p]=0;

for(int q=0;q<g->V;q++)

if(!vis[q])

DFS(g,q);

void main()

struct Graph* G;

G = adj();

DFStraversal(G);

You might also like