You are on page 1of 86

Practical No.

Aim: Write a program to accept the elements in 2D array and perform all the
matrix operations i.e. addition ,multiplication ,transpose etc.

Code:
#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a[3][3],b[3][3],c[3][3],d[3][3],e[3][3],f[3][3],i,j;

cout<<"\n enter the 1st no.";

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

//cout<<"\n Enter the 1st no.";

cin>>a[i][j];

cout<<"\nno.";

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

cout<<a[i][j]<<endl;

}
}

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

cout<<"\n enter the 2nd no.";

cin>>b[i][j];

cout<<"\n Add";

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

c[i][j]=a[i][j]+b[i][j];

cout<<"\t"<<c[i][j];

cout<<"\n";

cout<<"\n sub";

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

d[i][j]=a[i][j]-b[i][j];

cout<<"\t"<<d[i][j];
}

cout<<"\n";

cout<<"\n mul";

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

e[i][j]=a[i][j]*b[i][j];

cout<<"\t"<<e[i][j];

cout<<"\n";

cout<<"\n div";

for(i=1;i<=3;i++)

for(j=1;j<=3;j++)

f[i][j]=a[i][j]/b[i][j];

cout<<"\t"<<f[i][j];

cout<<"\n";

getch();

return 0;

}
Output:
Practical No.2

Aim:. Explain following Techniques.

 Bubble sort
 Insertion sort
 Radix sort

Ans: Sorting Techniques: Sorting is generally understood to be the process of re-arranging a


given set of objects in a specific order.

Bubble Sort: - Bubble Sort is an algorithm which is used to sort N elements that are given in a
memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one
and sort them based on their values.

It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the
first place, just like a water bubble rises up to the water surface.

Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent
data items and swapping each pair that is out of order.

Example. Array element are 5, 1,6,2,4,3.

Ans.: k=6-1=5

We need to do 5 pass to sort the array element

Pass 1: 5 1 6 2 4 3

1 5 6 2 4 3

1 5 6 2 4 3

1 5 2 6 4 3

1 5 2 4 6 3

1 5 2 4 3 6

Pass 2: 1,5,2,4,3,6.

1 5 2 4 3 6

1 5 2 4 3 6

1 2 5 4 3 6
1 2 4 5 3 6

1 2 4 3 5 6

1 2 4 3 5 6

Pass 3 : 1,2,4,3,5,6.

1 2 4 3 5 6

1 2 4 3 5 6

1 2 4 3 5 6

1 2 3 4 5 6

Final Sorted array element 1,2,3,4,5,6

We found sorted array in 3rd pass , so we do not need to do further passes .

Insertion Sort : Insertion sort algorithm sorts a set of values by inserting values into an existing
sorted file. Compare the second element with first ,if the first element is greater than second, place it
before the first one . Otherwise place is just after the first one. Compare the third value with second. If
the third value is greater than the second value then place it just after the second. Otherwise place the
second value to the third place. And compare third value with the first value. If the third value is greater
than the first value place the third value to the second place, otherwise place the first value to second
place. And place the third value to first place and so on.

For example : array element 5,1,6,2,4,3.

Ans.: 5,1,6,2,4,3.

K= variable.

Pass 1:

5 1 6 2 4 3

1 5 6 2 4 3

1 5 2 6 4 3

1 2 5 6 4 3
1 2 4 5 6 3

1 2 3 4 5 6

Sorted array element 1,2,3,4,5,6 .

Radix Sort : Radix sort is based on the value of the actual digits in the positional
representation of the number being sorted. In this we perform the following action on the list for
each digit, beginning with the most significant digit.

These actions are repeated for each digit , starting with the least significant digit and ending with
the most significant digit. Thus when all the number in the list are sorted on a most signification
digit , number that have the same signification position but different digits in the less significant
position are already sorted on the less signification position.

For example: array element 2,14,16,5,8,,22,20

Ans.: pass 1.

Queue

[0] 20

[1]

[2] 2 22

[3]

[4] 14

[5] 5

[6] 16

[7]

[8] 8

First pass =20, 2,22,14,,5,16,8


Pass 2.:

Queue

[0] 2 5 8

[1] 14 16

[2] 20 22

[3]

[4]

[5]

[6]

[7]

[8]

Pass 2: 2,5,8,14,16,20,22,

Final sorted pass = 2,5,8,14,16,20,22


Practical No.3
Aim: Suppose an array contains n elements. Given a number x that may occur
several time in the array . Write a program to find
i) The Number of occurrences of x in the array .
ii) The position of first occurrence of x in the array.
Code:

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a[]={1,3,5,3,7,10,11,3};

int x=3;

int first=0;

int count=0;

int o=0;

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

if(x==a[i])

if(o==0)
{

first=i;

count++;

o++;

cout<<"\n the first occurance is at position"<<first;

cout<<"\n no.of occurance "<<count;

getch();

return 0;

Output:
Practical No.4
Aim: Write a program in c++ to delete particular element from an array of 10
integers.
Code:

#include<iostream.h>

#include<conio.h>

void main()

int i,a[10],no,pos;

clrscr();

cout<<"\n Enter no.";

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

cin>>a[i];

cout<<"\n\n enter position of element remove :";

cin>>pos;

if(pos>10)

cout<<"\n Value out of range ";

}
else

for(i=pos;i<=9;i++)

a[i]=a[i+1];

cout<<"\n New array \n";

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

cout<<"\n"<<a[i];

getch();

Output:
Practical No.5

Aim: Consider two single dimensional array of size 20 and 3 respectively. Write a
program in c++ to display all the element which are common in both arrays.

Code:

#include<iostream.h>

#include<conio.h>

int main()

clrscr();

int a[]={1,3,5,3,7,10,11,3};

int x=3;

int first=0;

int count=0;

int o=0;

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

if(x==a[i])

if(o==0)

first=i;
}

count++;

o++;

cout<<"\n the first occurance is at position"<<first;

cout<<"\n no.of occurance "<<count;

getch();

return 0;

Output:
Practical No.6
Aim: Write a program to build a sparse matrix as an array. Write functions to
check if the sparse matrix is a square , diagonal, lower triangular , upper triangular
or tridiagonal matrix.
Code:
Lower Triangular:
Code:

#include <stdio.h>

#include<iostream.h>

#include<conio.h>

int main()

int A[3][3];

int row, col, isLower;

/*

* Reads elements in matrix from user

*/

printf("Enter elements in matrix of size 3x3: \n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

{
scanf("%d", &A[row][col]);

/*

* Checks whether the matrix is lower triangular matrix */

isLower = 1;

for(row=0; row<3; row++)

for(col=0; col<3; col++)

/*

* If elements above main diagonal(col>row)

* is not equal to zero(A[row][col]!=0)

*/

if(col>row && A[row][col]!=0)

isLower = 0;

}
/*

* If the matrix is lower triangular matrix

*/

if(isLower == 1)

printf("Matrix is Lower triangular matrix: \n");

/*

* Prints elements of lower triangular matrix

*/

for(row=0; row<3; row++)

for(col=0; col<3; col++)

if(A[row][col]!=0)

printf("%d ", A[row][col]);

printf("\n");

}
}

else

printf("Matrix is not a Lower triangular matrix");

return 0;

Output:
Upper Triangular Matrix:
Code:

#include <stdio.h>

#include<iostream.h>

#include<conio.h>

int main()

int A[3][3];

int row, col, isUpper;

/*

* Reads elements in matrix from user

*/

printf("Enter elements in matrix of size 3x3: \n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

scanf("%d", &A[row][col]);

}
/*

* Checks whether the matrix is Upper triangular

*/

isUpper = 1;

for(row=0; row<3; row++)

for(col=0; col<3; col++)

/*

* If elements below the main diagonal (col<row)

* is not equal to zero then it is not upper triangular matrix

*/

if(col<row && A[row][col]!=0)

isUpper = 0;

/*

* If it is upper triangular matrix */


if(isUpper==1)

printf("This is a Upper triangular matrix.\n");

for(row=0; row<3; row++)

for(col=0; col<3; col++)

if(A[row][col] != 0)

printf("%d ", A[row][col]);

printf("\n");

else

printf("This is Not a Upper triangular matrix.");

}
return 0;

Output:
Diagonal Matrix:
Code:

#include<iostream.h>

#include<conio.h>

void diagnol(int a[3][3]);

void main()

clrscr();

int a[3][3],i,j;

cout<<"\n\n";

cout<<"enter elements";

cout<<"\n\n";

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

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

cout<<"\n";

cin>>a[i][j];

}
diagnol(a);

getch();

void diagnol(int a[3][3])

int i,j;

cout<<"\n\n";

cout<<"diagnol 1";

cout<<"\n\n";

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

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

if(i==j)

cout<<a[i][j]<<"\t";

cout<<"\n\n";
cout<<"diagnol 2";

cout<<"\n\n";

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

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

if(i+j==3-1)

cout<<a[i][j]<<"\t";

Output:
Tridiagonal Matrix:
Code

#include<stdio.h>

#include<iostream.h>

#include<conio.h>

void main()

int i,j,k;

clrscr();

int mat[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};

printf("Entered matrix is:\n\n");

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

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

printf("%d\t",mat[i][j]);

printf("\n");

}
printf("\nTridiagnal matrix:\n\n");

printf("%d\t%d\n",mat[0][0],mat[0][1]);

for(i=1;i<5;i++)

for(k=1;k<i;k++)

printf("\t");

if(i==4)

printf("%d\t%d\n",mat[4][3],mat[4][4]);

else

for(j=i;j<5;j++)

if(i==j)

printf("%d\t%d\t%d",mat[i][j-1],mat[i][j],mat[i][j+1]);

printf("\n");

}
getch();

Output:
Practical No.7
Aim: Write a menu driven program for stack contain following function
 PUSH
 POP
 DISPLAY
 PEEK

Code:

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

class node

public:

int data;

node *next;

};

class stack

public:

node *top;

};
void display(stack s)

node *p;

p=s.top;

while(p!=NULL)

cout<<"\n"<<p->data;

p=p->next;

void push(stack *s,int n)

node *p;

p=new node;

p->data=n;

p->next=s->top;

s->top=p;

int pop(stack *s)

{
int n;

node *p;

if(s->top==NULL)

cout<<"\n stack is empty";

return -1;

else

p=s->top;

n=p->data;

s->top=p->next;

delete p;

return n;

void main()

stack s1;

int n,c;
s1.top=NULL;

do

clrscr();

cout<<"MENU \n";

cout<<"1. PUSH \n";

cout<<"2. POP\n";

cout<<"3. DISPLAY\n";

cout<<"4. PEEK\n";

cout<<"\n Enter your choice :";

cin>>c;

switch (c)

case 1:

cout<<"\n enter element to be pushed :";

cin>>n;

push(&s1, n);

cout<<"\n stack element :";

display(s1);

getch();
break;

case 2:

n=pop(&s1);

if(n!=-1)

cout<<"\n element popped:"<<n;

cout<<"\n Now stack Element :";

display(s1);

getch();

break;

case 3:

display(s1);

getch();

break;

while(c!=4);

getch();

}
Output:
Practical No.8
Aim:Transform the following infix expressions into their equivalent prefix
expressions.
(A-B)*(D/E)
(A+B^D)/(E-F)+G
A*(B+D)/E-F*(G+H/K)

Code:

i) (A-B)*(D/E)

INFIX: (LVR)

. -C-
C
. /
C

A
.
B
.
D
. . E
PREFIX :(VLR)

. *
. -C-
C
. /
C

.A .
B D
. . E
2- (A+B^D)/(E-F)+G
INFIX: (LVR)
PREFIX: (LVR)

. +

. /
.
G

. B
. -

. +
. .
^ E F
.
. A D
3. A*(B+D)/E-F*(G+H/K

INFIX: (LVR)
PREFIX: (VLR)

.
Practical No.9
Aim: Write a program in c++ to implement queue using Array .
Code:

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

#include<stdlib.h>

class node

int data;

node *next;

public:

void display();

void insert();

void delet();

};

node *temp=NULL, *ptr=NULL,*front=NULL;

int t,count=0;

void node::display()

if(front==NULL)
cout<<"\n\n\t\t There are no elements in the queue \n\t\t press enter tp return to main menu:";

else

cout<<"\n\t Queue elements";

cout<<"\n --------------------------------------\n";

temp=front;

while(temp!=NULL)

cout<<"\n\t "<<temp->data;

temp=temp->next;

void main()

int ch,y=1;

node n;

clrscr();

while(1)

{
cout<<"\n\n\n\t\t MAIN MENU\n";

cout<<"\n\t**********";

cout<<"\n\t 1. Insert an element \n";

cout<<"\n\t 2. Display the queue.\n";

cout<<"\n\t 3. Delete an element.\n";

cout<<"\n \t 4. Exit.\n";

cout<<"\n\t****************\n";

cout<<"\n enter your choice :";

cin>>ch;

switch(ch)

case 1:

n.insert();

n.display();

break;

case 2:

n.display();

getch();

break;

case 3:
n.delet();

n.display();

break;

case 4:

exit(0);

break;

default:

cout<<"\n\n\t\t Invalid choice entered, \n press any key to return ";

getch();

void node::insert()

temp=new node;

cout<<"\n Enter a value :";

cin>>temp->data;

temp->next=NULL ;

if(front==NULL)

front=temp;
else

ptr=front;

while(ptr->next!=NULL)

ptr=ptr->next;

ptr->next=temp;

void node::delet()

if(front==NULL)

cout<<"\n\n There are no element in link list \n press enter to return to main menu";

else

ptr=front;

front=front->next;

delete ptr;

}
Output:
Practical No.10
Aim:Consider the single Linked list contains following elements:
Rollno int ,sname char(20),city char(20),course char(3)
Write a program in c++ to represent linked list with the above elements.
Code:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<string.h>

#include<alloc.h>

struct stud {

int roll, mtot;

char name[25];

struct stud *next;

} *head, *tail;

void creat();

void display();

void addbeg();

void addend();

void addany();

void delet();
void rev();

void main()

clrscr();

creat();

display();

printf("\n ********** Add node at begining of the list **********");

addbeg();

display();

printf("\n ********** Add node at end of the list **********");

addend();

display();

printf("\n ********** Add node at any position of the list **********");

addany();

display();

printf("\n ********** Deletion of node from the list **********");

delet();

display();

getch();

printf("\n ********** Revers the list **********");


rev();

display();

getch();

void creat()

int i, n, k = 0, rl, mt;

char nm[25];

struct stud *p;

printf("\nn Enter the number of nodes::");

scanf("%d\n", &n);

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

printf("\nn the node number::%d", i + 1);

printf("\n n Enter student roll number::");

scanf("%d\n", &rl);

fflush(stdin);

printf("\n n Enter student name::");

gets(nm);

printf("\n n Enter student total marks::");


scanf("%d\n", &mt);

fflush(stdin);

p = (struct stud *) malloc(sizeof(struct stud));

if (k == 0) {

head = p;

head->roll = rl;

strcpy(head->name, nm);

head->mtot = mt;

head->next = NULL;

k = 1;

tail = head;

} else {

head->next = p;

head = head->next;

head->roll = rl;

strcpy(head->name, nm);

head->mtot = mt;

head->next = NULL;

}
}

void display()

struct stud *p1;

p1 = tail;

while (p1 != NULL) {

printf("\n n student roll number::%4d", p1->roll);

printf("\n n student name ::%s", p1->name);

printf("\n n student total marks::%4d", p1->mtot);

printf("\n ----------------------------------------------");

p1 = p1->next;

void addbeg()

char nm[25];

struct stud *p;

int mt, rl;

printf("\nn Enter student roll number::");

scanf("%d\n", &rl);
fflush(stdin);

printf("\n Enter student name::");

gets(nm);

printf("\n Enter student total marks::");

scanf("%d\n", &mt);

fflush(stdin);

p = (struct stud *) malloc(sizeof(struct stud));

p->roll = rl;

strcpy(p->name, nm);

p->mtot = mt;

p->next = tail;

tail = p;

void addend()

char nm[25];

struct stud *p;

int mt, rl;

printf("\n Enter student roll number::");

scanf("%d\n", &rl);
fflush(stdin);

printf("\n Enter student name::");

gets(nm);

printf("\n Enter student total marks::");

scanf("%d\n", &mt);

fflush(stdin);

p = (struct stud *) malloc(sizeof(struct stud));

p->roll = rl;

strcpy(p->name, nm);

p->mtot = mt;

p->next = NULL;

head->next = p;

head = p;

void addany()

char nm[25];

struct stud *p, *p1;

int mt, rl, n, k = 0, c =0;

printf("\n Enter the position where you want to add the node::");
scanf("%d\n", &n);

fflush(stdin);

if (n <= 0) {

printf("\n wrong entry::");

getch();

exit(1);

p = (struct stud *) malloc(sizeof(struct stud));

printf("\n Enter student roll number::");

scanf("%d\n", &rl);

fflush(stdin);

printf("\n Enter student name::");

gets(nm);

printf("\n Enter student total marks::");

scanf("%d\n", &mt);

fflush(stdin);

p->roll = rl;

strcpy(p->name, nm);

p->mtot = mt;

p1 = tail;
n = n - 1;

while (p1 != NULL) {

c++;

if (c == n) {

p->next = p1->next;

p1->next = p;

k = 1;

break;

p1 = p1->next;

if (k == 0)

printf("\n sorry position not found::");

void delet()

char nm[25];

struct stud *p, *p1;

int mt, rl, k = 0;

printf("\n Enter student's roll number which is to be deleted::");


scanf("%d\n", &rl);

fflush(stdin);

p1 = tail;

// p=head1;

while (p1 != NULL) {

k++;

if (p1->roll == rl) {

if (k == 1)

tail = p1->next;

else

p->next = p1->next;

free(p1);

break;

p = p1;

p1 = p1->next;

if (k == 0)

printf("\n node position not found::");

}
void rev()

char nm[25];

struct stud *p, *p1, *temp = NULL;

int mt, rl;

p1 = tail;

while (p1 != NULL) {

p = (struct stud *) malloc(sizeof(struct stud));

p->roll = p1->roll;

strcpy(p->name, p1->name);

p->mtot = p1->mtot;

p->next = temp;

temp = p;

p1 = p1->next;

free(tail);

tail = p1;

tail = temp;

}
Output:
Practical No.11
Aim: Write menu driven program which create and display the circular linked list .

Code:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

class node

public:

int data;

node *next;

};

class cirqueue

public:

node *rear;

};

void addq(cirqueue *q, int n)

node *ptr , *p,*prev;


ptr=new node;

ptr->data=n;

ptr->data=n;

if(q->rear==NULL)

q->rear=ptr;

ptr->next=ptr;

else

ptr->next=q->rear->next;

q->rear->next=ptr;

q->rear=ptr;

void display(cirqueue q)

node *p,*r;

r=p=q.rear->next;

if(p==NULL)
cout<<"\n Circular queue is empty ";

else

do

cout<<"\n "<<p->data;

p=p->next;

while(p!=r);

void main()

cirqueue q1;

int n,c;

q1.rear=NULL;

do

clrscr();

cout<<"\n 1.create ";


cout<<"\n 2.Display ";

cout<<"\n 3. Exit ";

cout<<"\n Enter your choice :";

cin>>c;

switch(c)

case 1:

cout<<"\n enter the element to be inserted";

cin>>n;

addq(&q1, n);

cout<<"\n circular queue after addition ";

display(q1);

getch();

break;

case 2:

cout<<"\n Queue is";

display(q1);

getch();

break;

default:
exit(1);

while(c!=4) ;

Output:
Practical No.12
Aim:Create binary search tree 15,2,25,45,35,23,100,5.
Ans:

Step 1: node= 15

Step 2:

Step 3:
Practical No.13
Aim:.Given two binary tree, write a program that finds whether
-The two binary trees are similar.
-The two binary trees are mirror images of each other.
a. The two binary trees are similar

Code: #include <stdio.h>

#include <stdlib.h>

#include<iostream.h>

#include<conio.h>

struct node

int data;

struct node* left;

struct node* right;

};

struct node* newnode(int data)

struct node* node = (struct node*)

malloc(sizeof(struct node));

node->data = data;

node->left = NULL;
node->right = NULL;

return(node);

int identicaltrees(struct node* a, struct node* b)

if (a==NULL && b==NULL)

return 1;

if (a!=NULL && b!=NULL)

return

a->data == b->data &&

identicaltrees(a->left, b->left) &&

identicaltrees(a->right, b->right)

);

return 0;

int main()

{
struct node *root1 = newnode(1);

struct node *root2 = newnode(1);

root1->left = newnode(2);

root1->right = newnode(3);

root1->left->left = newnode(4);

root1->left->right = newnode(5);

root2->left = newnode(2);

root2->right = newnode(3);

root2->left->left = newnode(4);

root2->left->right = newnode(5);

if(identicaltrees(root1, root2))

printf("both tree are identical.");

else

printf("trees are not identical.");

getchar();

return 0;}output:
Practical No.13.2
B. The two binary trees are mirror images of each other
Code:

#include<conio.h>

#include<iostream.h>

#include<stdlib.h>

#include<stdio.h>

#define true 1

#define false 0

struct Node

int data;

Node* left, *right;

};

int areMirror(Node* a, Node* b)

if (a==NULL && b==NULL)

return true;

if (a==NULL || b == NULL)

return false;

return a->data == b->data &&


areMirror(a->left, b->right) &&

areMirror(a->right, b->left);

Node* newNode(int data)

Node* node = new Node;

node->data = data;

node->left = node->right = NULL;

return(node);

void main()

clrscr();

Node *a = newNode(1);

Node *b = newNode(1);

a->left = newNode(2);

a->right = newNode(3);

a->left->left = newNode(4);

a->left->right = newNode(5);
b->left = newNode(3);

b->right = newNode(2);

b->right->left = newNode(5);

b->right->right = newNode(4);

areMirror(a, b)? cout << "Yes" : cout << "No";

Output:
Practical No.14
Aim: Write a program to traverse the graph using BFS method .

Code:

#include<iostream.h>

#include<conio.h>

#include<iomanip.h>

int adj[10][10],n;

int visited[10];

void bfs(int v)

int q[10],front=-1,rear=-1,i;

visited[v]=1;

q[++rear]=v;

cout<<"\n Visiting order\n";

while(front!=rear)

v=q[++front];

cout<<v;

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

if(!visited[i]&&adj[v][i])
{

visited[i]=1;

q[++rear]=i;

void main()

int i,j,m,a,b,v;

char c;

clrscr();

cout<<"\n Enter number of nodes and number of edges:";

cin>>n>>m;

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

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

adj[i][j]=0;

for(i=1;i<=m;i++)

cout<<"\n enter an edges :";


cin>>a>>b;

adj[a][b]=1;

adj[b][a]=1;

do{

clrscr();

cout<<"\nAdjacency Matrix \n";

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

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

cout<<setw(4)<<adj[i][j];

cout<<"\n";

cout<<"\n Enter initial node";

cin>>v;

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

visited[i]=0;

bfs(v);

cout<<"\n Do you wish to continue (Y/N):" ;

cin>>c;
}

while(c!='N');

getch();

Output:
Practical No.15
Aim: Write a program to traverse the graph using DFS method .

Code.:

#include<iostream.h>

#include<conio.h>

void graph(int adj[][10],int n,int m)

int i,j,a,b;

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

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

adj[i][j]=0;

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

cout<<"\n enter an edge :";

cin>>a>>b;

adj[a][b]=1;

adj[b][a]=1;

int visited[10]={0};
void dfs(int adj[][10],int n ,int v)

int i;

visited[v]=1;

cout<<v;

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

if(adj[v][i]&&!visited[i])

dfs(adj,n,i);

void main()

int adj[10][10];

int n,m,i,j,v;

clrscr();

cout<<"\n enter number of nodes and number of edges :";

cin>>n>>m;

graph(adj,n,m);

cout<<"\n enter the initial node: ";

cin>>v;

dfs(adj,n,v);
getch();

Output:

You might also like