Professional Documents
Culture Documents
PROGRAM 1:-
//Merge sort - Code
#include <stdio.h>
int main() {
int a[100], aux[100], n, i, d, swap;
merge_sort(0, n - 1, a, aux);
/*
SAMPLE INPUT AND OUTPUT:
Enter number of elements in the array:
6
Enter 6 integers
1
223
23
54
0
2
Printing the sorted array:
0
1
2
23
54
223
*/
PROGRAM 2:
/*PROGRAM TO IMPLEMENT THE QUICK SORT
ALGORITHM*/
#include<stdio.h>
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
int main()
{
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
/*
SAMPLE INPUT AND OUTPUT:
Enter some elements (Max. - 25): 6
Enter 6 elements: 23
4
55
6
77
0
The Sorted Order is: 0 4 6 23 55 77
*/
PROGRAM 3:
/*
SAMPLE INPUT AND OUTPUT:
Enter number of elements:
5
Enter 5 Elements:
23
33
44
55
66
Enter the value is to be Searched:
7
Not found! 7 is not present in the list.
*/
PROGRAM 4:
/*C Program to check whether the given graph is connected or
not*/
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v) {
int i;
reach[v]=1;
for (i=1;i<=n;i++)
if(a[v][i] && !reach[i]) {
printf("\n %d->%d",v,i);
dfs(i);
}
}
int main()
{
int i,j,count=0;
printf("\n Enter number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for (i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
return 0;
}
/*
SAMPLE INPUT AND OUTPUT:
1->2
2->3
Graph is connected
------------------------------
*/
PROGRAM 5:
DFS_2.c
#include<stdio.h>
#include<stdlib.h>
#define MAXVALUE 100
#define initialValue 1
#define visitedValue 2
int node;
int adjacent[MAXVALUE][MAXVALUE];
int state[MAXVALUE];
void DFSTraversal();
void DFS(int vertex);
void createGraph();
int stack[MAXVALUE];
int topValue = -1;
void pushNode(int vertex);
int popNode();
int isEmpty();
main()
{
createGraph();
DFSTraversal();
}
void DFSTraversal()
{
int vertex;
for(vertex=0; vertex<node; vertex++)
state[vertex]=initialValue;
printf("\nEnter start node for DFS : ");
scanf("%d",&vertex);
DFS(vertex);
printf("\n");
}
void DFS(int vertex)
{
int i;
pushNode(vertex);
while(!isEmpty())
{
vertex = popNode();
if(state[vertex]==initialValue)
{
printf("%d ",vertex);
state[vertex]=visitedValue;
}
for(i=node-1; i>=0; i--)
{
if(adjacent[vertex][i]==1 &&
state[i]==initialValue)
pushNode(i);
}
}
}
void pushNode(int vertex)
{
if(topValue == (MAXVALUE-1))
{
printf("\n Error: Stack Overflow\n");
return;
}
topValue=topValue+1;
stack[topValue] = vertex;
}
int popNode()
{
int vertex;
if(topValue == -1)
{
printf("\nStack Underflow\n");
exit(1);
}
else
{
vertex = stack[topValue];
topValue=topValue-1;
return vertex;
}
}
int isEmpty( )
{
if(topValue == -1)
return 1;
else
return 0;
}
void createGraph()
{
int i,maxEdges,originNode,destinNode;
printf("\nEnter number of nodes : ");
scanf("%d",&node);
maxEdges=node*(node-1);
for(i=1;i<=maxEdges;i++)
{
printf("\nEnter edge %d( -3 -3 to quit ) : ",i);
scanf("%d %d",&originNode,&destinNode);
if( (originNode == -3) && (destinNode == -3) )
break;
if( originNode>= node || destinNode>= node ||
originNode<0 || destinNode<0)
{
printf("\nInvalid Edge/ Node!\n");
i--;
}
else
{
adjacent[originNode][destinNode] = 1;
}
}
}
/*
SAMPLE INPUT AND OUTPUT:
2 DFS_01
#include <stdio.h>
#include <stdlib.h>
int sourceV,Vertex,Edge,time,visited[10],Graph[10][10];
void DepthFirstSearch(int i)
{
int j;
visited[i]=1;
printf(" %d->",i++);
for(j=0;j<Vertex;j++)
{
if(Graph[i][j]==1&&visited[j]==0)
DepthFirstSearch(j);
}
}
int main()
{
int i,j,vertex1,vertex2;
printf("\t\t\tGraphs\n");
printf("Enter no. of edges:");
scanf("%d",&Edge);
printf("Enter no. of vertices:");
scanf("%d",&Vertex);
for(i=0;i<Vertex;i++)
{
for(j=0;j<Vertex;j++)
Graph[i][j]=0;
}
for(i=0;i<Edge;i++)
{
printf("Enter the edges in V1 V2 : ");
scanf("%d%d",&vertex1,&vertex2);
Graph[vertex1-1][vertex2-1]=1;
}
for(i=0;i<Vertex;i++)
{
for(j=0;j<Vertex;j++)
printf(" %d ",Graph[i][j]);
printf("\n");
}
printf("Enter source Vertex: ");
scanf("%d",&sourceV);
DepthFirstSearch(sourceV-1);
return 0;
}
/*
SAMPLE INPUT AND OUTPUT:
Graphs
Enter no. of edges:5
Enter no. of vertices:2
Enter the edges in V1 V2 : 1 2
Enter the edges in V1 V2 : 2 3
Enter the edges in V1 V2 : 2 4
Enter the edges in V1 V2 : 2 5
Enter the edges in V1 V2 : 4 5
0 1
0 0
Enter source Vertex: 1 2
0->
*/
/* C Program to find whether a Directed Graph is Cyclic or not
*/
#include<stdio.h>
#include<stdlib.h>
#define initial 1
#define visited 2
#define finished 3
int state[MAX];
void DF_Traversal();
void DFS(int v);
int main()
{
create_graph();
DF_Traversal();
return 0;
}/*End of main()*/
void DF_Traversal()
{
int v;
void create_graph()
{
int i,max_edges,origin,destin;
for(i=1;i<=max_edges;i++)
{
printf("\nEnter edge %d( -1 -1 to quit ) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin == -1) && (destin == -1) )
break;
if( origin >= n || destin >= n || origin<0 || destin<0)
{
printf("\nInvalid edge!\n");
i--;
}
else
{
adj[origin][destin] = 1;
}
}
}
/*
SAMPLE INPUT AND OUTPUT:
*/
#include<stdio.h>
#include<stdlib.h>
#define initial 1
#define waiting 2
#define visited 3
void create_graph();
void BF_Traversal();
void BFS(int v);
int main()
{
create_graph();
BF_Traversal();
return 0;
}/*End of main()*/
void BF_Traversal()
{
int v;
for(v=0; v<n; v++)
state[v] = initial;
void BFS(int v)
{
int i;
insert_queue(v);
state[v] = waiting;
while(!isEmpty_queue())
{
v = delete_queue( );
printf("%d ",v);
state[v] = visited;
for(i=0; i<n; i++)
{
/*Check for adjacent unvisited vertices */
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}
}
printf("\n");
} /*End of BFS()*/
int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
} /*End of isEmpty_queue()*/
int delete_queue()
{
int del_item;
if(front == -1 || front > rear)
{
printf("\nQueue Underflow\n");
exit(1);
}
del_item = queue[front];
front = front+1;
return del_item;
} /*End of delete_queue() */
void create_graph()
{
int i,max_edges,origin,destin;
*/
/*PRIMS ALGO*/
#include<stdio.h>
#include<conio.h>
int n, cost[10][10];
void prim() {
int i, j, startVertex, endVertex;
int k, nr[10], temp, minimumCost = 0, tree[10][3];
void main() {
int i, j;
/*
Min cost:- 5
*/
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
/*
SAMPLE INPUT AND OUTPUT:
Minimum cost = 5
*/