ADA ALGORITHMS: PROGRAM 1://Merge sort - Code #include <stdio.h> // function to sort the subsection a[i .. j] of the array a[] void merge_sort(int i, int j, int a[], int aux[]) { if (j <= i) { return; // the subsection is empty or a single element } int mid = (i + j) / 2; // left sub-array is a[i .. mid] // right sub-array is a[mid + 1 .. j] merge_sort(i, mid, a, aux); recursively // sort the left sub-array merge_sort(mid + 1, j, a, aux); recursively int pointer_left = i; of the left sub-array // sort the right sub-array // pointer_left points to the beginning int pointer_right = mid + 1; beginning of the right sub-array // pointer_right points to the int k; // k is the loop counter // we loop from i to j to fill each element of the final merged array for (k = i; k <= j; k++) { if (pointer_left == mid + 1) { the limit // left pointer has reached aux[k] = a[pointer_right]; pointer_right++; } else if (pointer_right == j + 1) { reached the limit // right pointer has aux[k] = a[pointer_left]; pointer_left++; } else if (a[pointer_left] < a[pointer_right]) { left points to smaller element // pointer aux[k] = a[pointer_left]; pointer_left++; } else { // pointer right points to smaller element aux[k] = a[pointer_right]; pointer_right++; } } for (k = i; k <= j; k++) { a[] // copy the elements from aux[] to a[k] = aux[k]; } } int main() { int a[100], aux[100], n, i, d, swap; printf("Enter number of elements in the array:\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for (i = 0; i < n; i++) scanf("%d", &a[i]); merge_sort(0, n - 1, a, aux); printf("Printing the sorted array:\n"); for (i = 0; i < n; i++) printf("%d\n", a[i]); return 0; } /* 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> void quicksort(int number[25],int first,int last) { int i, j, pivot, temp; 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() { int i, count, number[25]; printf("Enter some elements (Max. - 25): "); scanf("%d",&count); printf("Enter %d elements: ", count); for(i=0;i<count;i++) scanf("%d",&number[i]); quicksort(number,0,count-1); printf("The Sorted Order is: "); 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: /* PROGRAM TO IMPLEMENT THE BST*/ #include<stdio.h> #include<stdlib.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements:\n"); scanf("%d",&n); printf("Enter %d Elements:\n", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter the value is to be Searched:\n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d is present at index %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d is not present in the list.\n", search); return 0; } /* 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. Enter number of elements: 6 Enter 6 Elements: 22 22 33 44 55 66 Enter the value is to be Searched: 66 66 is present at index 6. */ 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: Enter number of vertices:3 Enter the adjacency matrix: 1 2 3 4 5 6 7 8 9 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: Enter number of nodes : 8 Enter edge 1( -3 -3 to quit ) : 0 2 Enter edge 2( -3 -3 to quit ) : 0 4 Enter edge 3( -3 -3 to quit ) : 1 1 Enter edge 4( -3 -3 to quit ) : 1 2 Enter edge 5( -3 -3 to quit ) : 1 5 Enter edge 6( -3 -3 to quit ) : 2 5 Enter edge 7( -3 -3 to quit ) : 2 7 Enter edge 8( -3 -3 to quit ) : 3 6 Enter edge 9( -3 -3 to quit ) : 4 8 Invalid Edge/ Node! Enter edge 9( -3 -3 to quit ) : -3 -3 Enter start node for DFS : 0 02574 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 MAX 100 #define initial 1 #define visited 2 #define finished 3 int n; /*Number of vertices in the graph */ int adj[MAX][MAX]; void create_graph( ); 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; for(v=0; v<n; v++) state[v] = initial; DFS(0);/*start DFS from vertex 0*/ for(v=0; v<n; v++) { if(state[v]==initial) DFS(v); } printf("\nGraph is Acyclic\n"); }/*End of DF_Traversal( )*/ void DFS(int v) { int i; state[v] = visited; for(i=0; i<n; i++) { if(adj[v][i]==1) { if(state[i]==initial) DFS(i); else if(state[i]==visited) { printf("\nBack edge (%d,%d) found\n", v, i); printf("\nGraph is cyclic\n"); exit(1); } } } state[v] = finished; }/*End of DFS()*/ void create_graph() { int i,max_edges,origin,destin; printf("\nEnter number of vertices : "); scanf("%d",&n); max_edges = n*(n-1); 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: Enter number of vertices : 6 Enter edge 1( -1 -1 to quit ) : 0 1 Enter edge 2( -1 -1 to quit ) : 0 2 Enter edge 3( -1 -1 to quit ) : 0 3 Enter edge 4( -1 -1 to quit ) : 1 3 Enter edge 5( -1 -1 to quit ) : 3 4 Enter edge 6( -1 -1 to quit ) : 2 5 Enter edge 7( -1 -1 to quit ) : 5 4 Enter edge 8( -1 -1 to quit ) : -1 -1 Graph is Acyclic */ /* C Program to implement BFS Algorithm for Connected Graph */ #include<stdio.h> #include<stdlib.h> #define MAX 100 #define initial 1 #define waiting 2 #define visited 3 int n; /*Number of vertices in the graph*/ int adj[MAX][MAX]; /*Adjacency Matrix*/ int state[MAX]; /*can be initial, waiting or visited*/ void create_graph(); void BF_Traversal(); void BFS(int v); int queue[MAX], front = -1,rear = -1; void insert_queue(int vertex); int delete_queue(); int isEmpty_queue(); 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; printf("\nEnter starting vertex for Breadth First Search : "); scanf("%d", &v); BFS(v); }/*End of BF_Traversal()*/ 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()*/ void insert_queue(int vertex) { if(rear == MAX-1) printf("\nQueue Overflow\n"); else { if(front == -1) /*If queue is initially empty */ front = 0; rear = rear+1; queue[rear] = vertex ; } }/*End of insert_queue()*/ 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; printf("\nEnter number of vertices : "); scanf("%d",&n); max_edges = n*(n-1); 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; } } } /*End of for*/ /*End of create_graph()*/ /* SAMPLE INPUT AND OUTPUT: Enter number of vertices : 5 Enter edge 1( -1 -1 to quit ) : 0 1 Enter edge 2( -1 -1 to quit ) : 0 2 Enter edge 3( -1 -1 to quit ) : 0 3 Enter edge 4( -1 -1 to quit ) : 1 3 Enter edge 5( -1 -1 to quit ) : 3 2 Enter edge 6( -1 -1 to quit ) : 4 4 Enter edge 7( -1 -1 to quit ) : -1 -1 Enter starting vertex for Breadth First Search : 0 0123 */ /*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]; /* For first smallest edge */ temp = cost[0][0]; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (temp > cost[i][j]) { temp = cost[i][j]; startVertex = i; endVertex = j; } } } /* Now we have fist smallest edge in graph */ tree[0][0] = startVertex; tree[0][1] = endVertex; tree[0][2] = temp; minimumCost = temp; /* Now we have to find min dis of each vertex from either startVertex or endVertex by initialising nr[] array */ for (i = 0; i < n; i++) { if (cost[i][startVertex] < cost[i][endVertex]) nr[i] = startVertex; else nr[i] = endVertex; } /* To indicate visited vertex initialise nr[] for them to 100 */ nr[startVertex] = 100; nr[endVertex] = 100; /* Now find out remaining n-2 edges */ temp = 99; for (i = 1; i < n - 1; i++) { for (j = 0; j < n; j++) { if (nr[j] != 100 && cost[j][nr[j]] < temp) { temp = cost[j][nr[j]]; k = j; } } /* Now i have got next vertex */ tree[i][0] = k; tree[i][1] = nr[k]; tree[i][2] = cost[k][nr[k]]; minimumCost = minimumCost + cost[k][nr[k]]; nr[k] = 100; /* Now find if k is nearest to any vertex than its previous near value */ for (j = 0; j < n; j++) { if (nr[j] != 100 && cost[j][nr[j]] > cost[j][k]) nr[j] = k; } temp = 99; } /* Now i have the answer, just going to print it */ printf("\nThe min spanning tree is:-\n "); for (i = 0; i < n - 1; i++) { for (j = 0; j < 3; j++) printf("%d\t", tree[i][j]); printf("\n"); } printf("\nMin cost : %d", minimumCost); } void main() { int i, j; printf("\nEnter the no. of vertices :"); scanf("%d", &n); printf("\nEnter the costs of edges in matrix form :"); for (i = 0; i < n; i++) for (j = 0; j < n; j++) { scanf("%d", &cost[i][j]); } printf("\nThe matrix is :\n "); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d\t", cost[i][j]); } printf("\n"); } prim(); } /* SAMPLE INPUT AND OUTPUT:Enter the no. of vertices:- 3 Enter the costs of edges in matrix form:99 2 3 2 99 5 3 5 99 The matrix is:- 99 2 3 2 99 5 3 5 99 The min spanning tree is:0 1 2 2 0 3 Min cost:- 5 */ /*Implementation of Kruskal's Algorithm*/ #include <stdio.h> #include <conio.h> #include <stdlib.h> int i,j,k,a,b,u,v,n,ne=1; int min,mincost=0,cost[9][9],parent[9]; int find(int); int uni(int,int); int main() { printf("\n\tImplementation of Kruskal's Algorithm\n"); printf("\nEnter the no. of vertices:"); scanf("%d",&n); printf("\nEnter the cost adjacency matrix:\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&cost[i][j]); if(cost[i][j]==0) cost[i][j]=999; } } printf("The edges of Minimum Cost Spanning Tree are\n"); while(ne < n) { for(i=1,min=999;i<=n;i++) { for(j=1;j <= n;j++) { if(cost[i][j] < min) { min=cost[i][j]; a=u=i; b=v=j; } } } u=find(u); v=find(v); if(uni(u,v)) { printf("%d edge (%d,%d) =%d\n",ne++,a,b,min); mincost +=min; } cost[a][b]=cost[b][a]=999; } printf("\n\tMinimum cost = %d\n",mincost); return 0; } int find(int i) { while(parent[i]) i=parent[i]; return i; } int uni(int i,int j) { if(i!=j) { parent[j]=i; return 1; } return 0; } /* SAMPLE INPUT AND OUTPUT: Implementation of Kruskal's Algorithm Enter the no. of vertices:3 Enter the cost adjacency matrix: 9 8 7 6 5 4 3 2 3 The edges of Minimum Cost Spanning Tree are 1 edge (3,2) =2 2 edge (3,1) =3 Minimum cost = 5 */