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

Lab Manual

Uploaded by

Anju
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)
5 views

Lab Manual

Uploaded by

Anju
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/ 22

IMPLEMENTATION OF BFS FOR GRAPH REPRESENTATIONS

Aim

To develop a C program to implement BFS for graph representations.

Algorithm

Step1: Start

Step 2: Enter the no: of vertices and adjacency matrix of the graph

Step 3: Call the BFS function, it works as follows;

1. Set STATUS[i] = 1 for all vertices i.

2. enqueue(node)

3. STATUS[node] = 2

4. Repeat step 5 to 8 while QUEUE is not empty

5. node = dequeue() and process it

6. Repeat step 7 for all the adjacent vertices q of node

7. If STATUS[q] == 1, then enqueue(q) and STATUS[q] = 2

8. Set STATUS[node] = 3

Step 4: Stop

Program
#include<stdio.h>

#define MAX 20

int G[10][10],visited[10],n,rear,front,queue[MAX];

void breadthFirstSearch(int a[10][10], int, int);

rear=-1;front=-1;

void enqueue(int vertex)

if(rear == MAX-1)

printf("Queue Overflow\n");

else

if(front == -1)

front = 0;

rear = rear+1;

queue[rear] = vertex ;
}

int dequeue()

int delete_item;

if(front == -1 || front > rear)

printf("Queue Underflow\n");

exit(1);

delete_item = queue[front];

front = front+1;

return delete_item;

void main()

int i,j,n;

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

scanf("%d",&n);

printf("%d",n);

printf("\nEnter adjecency matrix of the graph:");

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

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

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

breadthFirstSearch(G,n,0);

void breadthFirstSearch(int a[10][10], int n, int pnode)

int i,status[n];

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

status[i]=1;

enqueue(pnode);

status[pnode] =2;

while(front!=-1)

{
pnode = dequeue();

printf("%d ", pnode);

status[pnode] =3;

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

if(a[pnode][i] == 1 && status[i] == 1)

enqueue(i);

status[i] = 2;

Output:

Enter number of vertices:6

Enter adjacency matrix of the graph:

010100

001001

000011

001010

000000

000000

013254

Queue Underflow

IMPLEMENTATION OF DFS FOR GRAPH REPRESENTATIONS

Aim
To develop a C program to implement DFS for graph representations.
Algorithm
Step1: Start
Step 2: Enter the no: of vertices and adjacency matrix of the graph
Step 3: Call the DFS function, it works as follows
1. Set STATUS[i] = 1 for all vertices i.
2. push(node) and STATUS[node] = 2
3. Repeat step 5 to 8 while STACK is not empty
4. pnode = pop()
5. If STATUS[node] == 3, then go to step 3
6. Process the node
7. Repeat step 8 for all the adjacent vertices q of node
8. If STATUS[q] != 3, then push(q) and STATUS[q] = 2
9. Set STATUS[node] = 3
Step 4. Stop

Program

#include<stdio.h>
int G[10][10],visited[10],n,top=-1,stack[20];
void depthFirstSearch(int a[10][10],int n,int pnode);
void push(int item)
{
stack[++top] = item;
}
int pop()
{
return stack[top--];
}
void main()
{
int i,j,n;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnteradjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);

depthFirstSearch(G,n,0);
}
void depthFirstSearch(int a[10][10],int n1,int pnode)
{
int i, status[n1];
for(i=0;i<n1;i++)
status[i]=1;
push(pnode);
status[pnode] =2;
while(top!=-1)
{
pnode = pop();
printf("%d ", pnode);
status[pnode] =3;
for(i=0;i<n1;i++)
{
if(a[pnode][i] == 1 && status[i] == 1)
{
push(i);
status[i] = 2;
}
}
}
}

Output:
Enter number of vertices:6
Enter adjacency matrix of the graph:
010000
001001
000111
000010
000000
000000
015243
IMPLEMENTATION OF SORTING ALGORITHMS – BUBBLE SORT

Aim

To develop a C program to implement sorting algorithms – bubble sort.

Algorithm

Step 1: Start

Step 2: Start at index zero, compare the element with the next one (a[0] & a[1]

Step 3: Swap if a[0] > a[1].

Step 4: Now compare a[1] & a[2] and swap if a[1] > a[2].

Step 5: Repeat this process until the end of the array.

Step 6: Repeat this process n-1 times.

Step 7: Stop

Program
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);

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


scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}

Output

Enter the number of Elements:


5
Enter 5 integers :
45
21
89
78
99
Sorted list in ascending order:
21 45 78 89 99

Result

Program to implement bubble sort algorithm has been successfully executed


and output is verified.

IMPLEMENTATION OF SORTING ALGORITHMS – INSERTION SORT

Aim
To develop a C program to implement sorting algorithms – insertion sort.
Algorithm
Step 1: Start
Step 2: Insert elements and Repeat step 3 to 8 for i =1 to n-1
Step 3: Set temp = a[i]
Step 4: Set j = i-1
Step 5: Repeat step 6 and 7 while temp <a[j] and j>=0
Step 6: a[j] = a[j-1]
Step 7: j = j-1
Step 8: a[j] =temp
Step 9: Display the sorted array
Step 10: Stop

Program

#include <stdio.h>
int main()
{
int a[50],n,i,temp,min,j;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the %delements of the array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
printf("\nDisplaying the sorted elements:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Output

Enter the limit: 5


Enter the 5 elements of the array:
30
10
40
20
50
Displaying the sorted elements:10 20 30 40 50

Result

Program to implement insertion sort algorithm has been successfully executed


and output is verified.

IMPLEMENTATION OF SORTING ALGORITHMS – SELECTION SORT

Aim

To develop a C program to implement sorting algorithms – selection sort.

Algorithm

Step 1: Start

Step 2: Insert the elements

Step 3: Repeat step 4 to 7 for i = 0 to n-1

Step 4: Set min = i

Step 5: Repeat step 6 for j = i+1 to n-1

Step 6: If a[min] > a[j], then min = j

Step 7: Swap a[min] and a[i]

Step 8: Display the sorted array

Step 9: Exit

Program
#include <stdio.h>
int main()
{
int a[50],n,i,temp,min,j;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the %delements of the array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("\n Displaying the sorted elements:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}

Output:

Enter the limit:5

Enter the 5 elements of the array:

5
7

Displaying the sorted elements:1 2 4 5 7

Result

Program to implement selection sort algorithm has been successfully executed


and output is verified.

IMPLEMENTATION OF SORTING ALGORITHMS – QUICK SORT

Aim
To develop a C program to implement sorting algorithms – quick sort.
Algorithm
Step 1: Insert elements to be sorted into an array A
Step 2: The quick sort algorithm works as follows:
1. Select an element pivot from the array elements.
2. Rearrange the elements in the array in such a way that all elements that are less
than the pivot appear before the pivot and all elements greater than the pivot element
come after it (equal values can go either way). After such a partitioning, the pivot is
placed in its final position. This is called the partition operation.
3. Recursively sort the two sub-arrays thus obtained. One with sub-list of values
smaller than that of the pivot element and the other having higher value elements.
Step 3: If the last element is considered as the pivot (END) and the partition is done as follows.
PARTITION (A, BEG, END)
{
X=A[END] i=BEG-1
for(j=BEG TO END-1)
{
if(A[j]<=X)
{
i=i+1;
Exchange A[i] With A[j]
}
}
Exchange A[i+1] with A[END] return i+1
}
Step 4: Display the sorted elements
Step 5: Stop

Program

#include <stdio.h>

#define size 100

int PARTITION(int A[], int BEG, int END);

void QUICKSORT(int A[], int BEG, int END);

int main() {

int n, i;

int a[size];

printf("Enter the no. of elements:");

scanf("%d", &n);

printf("Enter the elements:");

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

scanf("%d", &a[i]);

QUICKSORT(a, 0, n - 1);

printf("After sorting the elements are:");

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


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

return 0;

int PARTITION(int A[], int BEG, int END) {

int X, t, i, j;

X = A[END];

i = BEG - 1;

for (j = BEG; j <= END - 1; j++) {

if (A[j] <= X) {

i = i + 1;

t = A[i];

A[i] = A[j];

A[j] = t;

t = A[i + 1];

A[i + 1] = A[END];

A[END] = t;

return i + 1;

void QUICKSORT(int A[], int BEG, int END) {

int Q;

if (BEG < END) {

Q = PARTITION(A, BEG, END);

QUICKSORT(A, BEG, Q - 1);

QUICKSORT(A, Q + 1, END);
}

Output:

Enter the no. of elements:5 Enter the elements:45

56

34

32

22

After sorting the elements are: 22 32 34 45 56

Result

Program to implement quick sort algorithm has been successfully executed and
output is verified.

IMPLEMENTATION OF SORTING ALGORITHMS – MERGE SORT

Aim
To develop a C program to implement sorting algorithms – merge sort.
Algorithm
Step 1: Start
Step2: Insert elements to be sorted into an array Step 3: The Merge sort algorithm works as
follows:
1: If it is only one element in the list it is already sorted, return.
2: Divide the list recursively into two halves until it can no more be divided.
3: Merge the smaller lists into new list in sorted order.
Step 4:
MERGE_SORT(ARR, BEG, MID)
1. If BEG < END then do step 2 to 5
2. Set MID = (BEG + END)/2
3. Call MERGE_SORT(ARR, BEG, MID)
4. Call MERGE_SORT(ARR, MID+1, END)
5. MERGE(ARR, BEG, MID, END)
6. END
Step 5: Display the sorted elements
Step 6: Stop

Program

#include <stdio.h>
void mergesort(int A[],int lb,int nb)
{
int mid;
if(lb<nb)
{
mid=(lb+ub)/2;
mergesort(A,lb,mid);
mergesort(A,mid+1,ub);
merge(A,lb,mid,ub);
}
}
void merge(int a[],int lb,int mid,int ub)
{
int i,j,k,b[10];
i=lb;
j=mid+1;
k=lb;
while(i<=mid&&j<=ub)
{
if(a[i]<=a[j])
{
b[k]=a[i];
i++;
k++;
}
else
{
b[k]=a[j];
j++;
k++;
}
}
if(i>mid)
{
while(j<=ub)
{
b[k]=a[j];
j++;
k++;
}
}
else
{
while(i<=mid)
{
b[k]=a[i];
i++;
k++;
}
}
for(k=lb;k<=ub;k++)
{
a[k]=b[k];
}
}
int main()
{
int n,i; int a[10];
printf("Enter the number of elements");
scanf("%d",&n);
printf("\nThe elements are:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("\nAfter sorting the elements are");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

Output:

Enter the number of elements: 7

The elements are: 6 9 45 32 12 56 88

After sorting the elements are: 6 9 12 32 45 56 88

Result

Program to implement merge sort algorithm has been successfully executed


and output is verified.

Experiment No.20

IMPLEMENTATION OF SORTING ALGORITHMS – HEAP SORT

Aim

To develop a C program to implement sorting algorithms – heap sort.

Algorithm

Step 1: Start

Step 2: Construct a Binary Tree with given list of Elements.

Step 3: Transform the Binary Tree into Min/Max Heap.

Step 4: Delete the root element from Min/Max Heap using Heapify method.

Step 5: Put the deleted element into the Sorted list.

Step 6: Repeat the same until Min/Max Heap becomes empty.

Step 7: Display the sorted list.


Step 8: Stop

Program

#include <stdio.h>
int temp;
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
temp = arr[i];
arr[i]= arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--)
{
temp = arr[0];
arr[0]= arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
printf("%d ",arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
}

Output:

Sorted array is 5 6 7 11 12 1

Result

Program to implement heap sort algorithm has been successfully executed and
output is verified.

HASH TABLE USING LINEAR PROBING

AIM: Implementation of hash table using linear probing and searching in hash table

ALGORITHM:
Step1: Start
Step2: Enter the no: of elements to be inserted, n
Step3: Input the n elements in array A
Step4: Enter the size of hash table, m
Step5: Initialize all the entry to the hash table(H) as -1
Step6: Hash:
6.1. initialize j=0 and h=-1 evaluate the expression, h=((A[i]%m)+j)%m;
6.2.if (h<m) and if H(h) ==-1 insert the element A[i] at H[h]
else increment j and goto Hash
Step 7: Repeat the step 6 until all elements of the array is inserted into the hash table.
Step 8: Display the elements in the array
Step 9: Enter the element to be searched, item
Step10: if item is there in the array print item found else not found
Step 11: Stop

PROGRAM:
#include<stdio.h>
void main()
{
int i,h,H[100],A[50],m,n,j,e,c=0;
printf("\nEnter the number of elements to be inserted:");
scanf("%d",&n);
printf("\nEnter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\nEnter size of Hash table:");
scanf("%d",&m);

for(i=0;i<m;i++)
H[i]=-1;

for(i=0;i<n;i++)
{
j=0;
h=-1;
hash:
h=((A[i]%m)+j)%m;
if(h<m)
{
if(H[h]==-1)
H[h]=A[i];
else
{ j++;
goto hash;
}
}

}
printf("\nHash Table
display:\n");
for(i=0;i<m;i++)
{
if(H[i]==-1)
printf("
- "); else
printf("%d ",H[i]);
}
printf("\nEnter element to be searched:");
scanf("%d",&e);
for(i=0;i<m;i++)
if(H[i]==e)
{
printf("\nElement %d found in hash table \nposition :%d \nindex:%d\
n",e,i+1,i); c++;
}
if(c==0)
printf("\nElement %d not found in hash table\n",e);
}

Output:
Enter the number of
elements to be inserted:6

Enter 6 elements:
26
37
59
76
65
86

Enter size of Hash table:11

Hash Table display:


65 - - - 26 37 59 - - 86 76
Enter element to be searched:86

Element 86 found in hash table

position :10
index:9

Result: The program to implement hash table using linear probing and searching is
successfully implemented and executed.

You might also like