0% found this document useful (0 votes)
48 views

Pune Institute of Computer Technology PUNE - 411043 Department of Electronics & Telecommunication

The document describes a student lab experiment on implementing breadth-first search (BFS) and depth-first search (DFS) algorithms on a graph represented using an adjacency matrix. It includes the prerequisites, objectives, theory, algorithm, flowchart, output, and assessment sections for the lab. The student is asked to create a graph, input the vertices and edges, and implement BFS and DFS to traverse the graph from a given starting vertex.

Uploaded by

Nakul Sharma
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)
48 views

Pune Institute of Computer Technology PUNE - 411043 Department of Electronics & Telecommunication

The document describes a student lab experiment on implementing breadth-first search (BFS) and depth-first search (DFS) algorithms on a graph represented using an adjacency matrix. It includes the prerequisites, objectives, theory, algorithm, flowchart, output, and assessment sections for the lab. The student is asked to create a graph, input the vertices and edges, and implement BFS and DFS to traverse the graph from a given starting vertex.

Uploaded by

Nakul Sharma
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/ 12

PUNE INSTITUTE OF COMPUTER TECHNOLOGY

PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENT YEAR: 2019-2020 CLASS : SE
SUBJECT: DATA STRUCTURES and ALGORITHMS
EXPT No: LAB Ref: SE/2019/ Starting date:
Roll No:22324 Submission date:
Title: Creation of Graph

Prerequisites: Basic Knowledge of C and Adjacency Matrix


Information about Breadth First Search and Depth First Search

Objectives: To learn the concepts of Graph

Theory:
/*BFS and DFS on a graph represented using adjacency matrix 22324*/

#include<conio.h>

#include<stdio.h>

#define MAX 10

typedef struct Q

int R,F;

int data[MAX];

}Q;

int empty(Q *P);

int full(Q *P);

void enqueue(Q *P,int x);

int dequeue(Q *P);

void BFS(int);

P:f-LTL-UG/03/R1 Page 1 of 12
void DFS(int);

int G[MAX][MAX];

int n=0;

int visited[MAX];

void main()

int i,j,v,op,nedges;

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

scanf("%d",&n);

printf("\nEnter No. of edges : ");

scanf("%d",&nedges);

for(i=0;i<n;i++)

for(j=0;j<n;j++)

G[i][j]=0;

printf("\nEnter the Graph as list of edges(Starting vertex No. is 0): ");

for(v=0;v<nedges;v++)

printf("\nEnter the next edge(Start vertex, end vertex) : ");

scanf("%d%d",&i,&j);

G[i][j]=G[j][i]=1;//Mark the edge

do{

printf("\n\n1)DFS\n2)BFS\n3)Display the Graph\n4)QUIT");

printf("\nEnter Your choice : ");

P:f-LTL-UG/03/R1 Page 2 of 12
scanf("%d",&op);

switch(op)

case 1:printf("\nEnter the starting vertex for DFS : ");

scanf("%d",&v);

for(i=0;i<n;i++)

visited[i]=0;

DFS(v);break;

case 2:printf("\nEnter the starting vertex for BFS : ");

scanf("%d",&v);

BFS(v);break;

case 3:printf("\nAdjacency Matrix : \n");

for(i=0;i<n;i++)

printf("\n");

for(j=0;j<n;j++)

printf("%d ",G[i][j]);

break;

}while(op!=4);

void BFS(int v)

int visited[MAX],i;

Q q;

P:f-LTL-UG/03/R1 Page 3 of 12
q.R=q.F=-1;

for(i=0;i<n;i++)

visited[i]=0;

enqueue(&q,v);

printf("\n visit:\n%d",v);

visited[v]=1;

while(!empty(&q))

v=dequeue(&q);

for(i=0;i<n;i++)

if(visited[i]==0 && G[v][i]!=0)

enqueue(&q,i);

visited[i]=1;

printf("\n%d",i);

int empty(Q *P)

if(P->R==-1)

return(1);

return(0);

int full(Q *P)

P:f-LTL-UG/03/R1 Page 4 of 12
if(P->R==MAX-1)

return(1);

return(0);

void enqueue(Q *P,int x)

if(P->R==-1)

P->R=P->F=0;

P->data[P->R]=x;

else

P->R=P->R+1;

P->data[P->R]=x;

int dequeue(Q *P)

int x;

x=P->data[P->F];

if(P->R==P->F)

P->R=-1;

P->F=-1;

else

P:f-LTL-UG/03/R1 Page 5 of 12
P->F=P->F+1;

return(x);

void DFS(int i)

int j;

printf("\n%d",i);

visited[i]=1;

for(j=0;j<n;j++)

if(!visited[j] && G[i][j]==1)

DFS(j);

P:f-LTL-UG/03/R1 Page 6 of 12
FLOW CHART (Extra sheet may be attached)

P:f-LTL-UG/03/R1 Page 7 of 12
ALGORITHM (Extra sheet may be attached)

P:f-LTL-UG/03/R1 Page 8 of 12
ERROR (if any)

REMEDY (if any)

CONCLUSION :

REFERENCES :

Continuous Assessment Assessed By

RPP (5) ARR (5) Total (10) Signature:

Date:

P:f-LTL-UG/03/R1 Page 9 of 12
PUNE INSTITUTE OF COMPUTER TECHNOLOGY
PUNE - 411043
Department of Electronics & Telecommunication
ASSESMENTYEAR: 2019-2020 CLASS : SE
SUBJECT: DATA STRUCTURES and ALGORITHMS
EXPTNo: LAB Ref:SE/2019/ Starting date:
Roll No:22324 Submission date:
Title:

Output:

ROLL NO. 22324


Enter no of vertices : 5

Enter No. of edges : 8

Enter the Graph as list of edges(Starting vertex No. is 0):


Enter the next edge(Start vertex, end vertex) : 1 2

Enter the next edge(Start vertex, end vertex) : 1 4

Enter the next edge(Start vertex, end vertex) : 1 3

Enter the next edge(Start vertex, end vertex) : 2 5

Enter the next edge(Start vertex, end vertex) : 2 3

Enter the next edge(Start vertex, end vertex) : 4 3

Enter the next edge(Start vertex, end vertex) : 4 5

Enter the next edge(Start vertex, end vertex) : 3 5

1)DFS
2)BFS
3)Display the Graph
4)QUIT
Enter Your choice : 1

Enter the starting vertex for DFS : 2

2
1
3
4

1)DFS
2)BFS

P:f-LTL-UG/03/R1 Page 10 of 12
3)Display the Graph
4)QUIT
Enter Your choice : 2

Enter the starting vertex for BFS : 2

visit:
2
1
3
4

1)DFS
2)BFS
3)Display the Graph
4)QUIT
Enter Your choice : 3

Adjacency Matrix :

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

1)DFS
2)BFS
3)Display the Graph
4)QUIT
Enter Your choice :

P:f-LTL-UG/03/R1 Page 11 of 12
P:f-LTL-UG/03/R1 Page 12 of 12

You might also like