0% found this document useful (0 votes)
18 views4 pages

Ada

The document contains multiple C programming labs focused on different sorting and graph algorithms, including Selection Sort, Merge Sort, Quick Sort, Topological Sorting, Floyd's Algorithm, Warshall's Algorithm, Prim's Algorithm, and Dijkstra's Algorithm. Each lab includes code snippets that demonstrate the implementation of these algorithms along with performance measurement. The labs are structured to allow user input for array sizes and adjacency matrices, and they output the results of the sorting or pathfinding operations.

Uploaded by

pajjubhau7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Ada

The document contains multiple C programming labs focused on different sorting and graph algorithms, including Selection Sort, Merge Sort, Quick Sort, Topological Sorting, Floyd's Algorithm, Warshall's Algorithm, Prim's Algorithm, and Dijkstra's Algorithm. Each lab includes code snippets that demonstrate the implementation of these algorithms along with performance measurement. The labs are structured to allow user input for array sizes and adjacency matrices, and they output the results of the sorting or pathfinding operations.

Uploaded by

pajjubhau7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 1 selction sort

#include<stdio.h>#include<time.h>#include<stdlib.h>#define MAXSIZE 30000#define NTIMES


10000
void selectionsort(int A[],int n)
{int i,j,min,temp;for(i=0;i<=n-2;i++){min=i;for(j=i+1;j<=n-1;j++){
if(A[j]<A[min])
min=j; }temp=A[i];A[i]=A[min];A[min]=temp;}} void main(){int A[MAXSIZE],i,n,k;
clock_t start,end; double runtime=0; printf("Enter the size of Array\n"); scanf("%d",&n);
for(k=0;k<NTIMES;k++) { srand(1); for(i=0;i<n;i++) A[i]=rand(); start=clock(); selectionsort(A,n);
end=clock(); runtime+=(double)(end-start)/CLK_TCK; } runtime=runtime/NTIMES;
printf("The Time for sorting %d Elements =%lf sec\n",n,runtime);}

lab 2 divide and concure , merge sort


#include <stdio.h>#include <stdlib.h>#include <time.h>#include <conio.h>
#define MAXSIZE 30000#define NTIMES 10000
void Merge(int A[], int low, int mid, int high) {
int i = low, j = mid + 1, k = low, p;
int B[MAXSIZE];
while (i <= mid && j <= high) {
if (A[i] <= A[j])
B[k++] = A[i++];
else
B[k++] = A[j++]; }
while (i <= mid) { // Left part remaining
B[k++] = A[i++];
}
while (j <= high) { // Right part remaining
B[k++] = A[j++];
}
for (i = low; i <= high; i++) {
A[i] = B[i]; }}
void Mergesort(int A[], int low, int high) {
int mid;
if (low < high) {
mid = (low + high) / 2;
Mergesort(A, low, mid);
Mergesort(A, mid + 1, high);
Merge(A, low, mid, high); }}
void main() {
int A[MAXSIZE], i, n;
clock_t start, end;
double runtime = 0;
clrscr();
printf("Enter The Size Of Array:\n");
scanf("%d", &n);
for (int t = 0; t < NTIMES; t++) {
srand(0); // Seed with constant to regenerate same input each time
for (i = 0; i < n; i++) {
A[i] = rand() % n; }
start = clock();
Mergesort(A, 0, n - 1);
end = clock();
runtime += (double)(end - start) / CLOCKS_PER_SEC; }
runtime = runtime / NTIMES;
printf("Time For Sorting %d Elements = %lf Sec\n", n, runtime); getch();}

lab 3 quick sort


#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>#define MAXSIZE 30000
#define NTIMES 10000
int Partition(int A[], int low, int high){
int p=A[low],i=low+1,j=high,temp;do{
while(i<=high && A[i]<=p) i++;
while(j>=low && A[j]>p) j--;
if(i<j){temp=A[i];A[i]=A[j];A[j]=temp;}
}while(i<j);temp=A[low];A[low]=A[j];A[j]=temp;return j;}void QuickSort(int A[], int low, int high){
int s;if(low<high){
s=Partition(A,low,high);QuickSort(A,low,s-1);
QuickSort(A,s+1,high);
}}void main(){
int A[MAXSIZE],i,n;clock_t start,end;double runtime=0;clrscr();
printf("Enter The Size Of Array:\n");scanf("%d",&n);srand(1);
for(int t=0;t<NTIMES;t++){
for(i=0;i<n;i++) A[i]=rand()%n;
start=clock();QuickSort(A,0,n-1);end=clock();runtime+=(double)(end-start)/CLOCKS_PER_SEC;}
runtime=runtime/NTIMES;
printf("Time For Sorting %d Elements = %lf Sec\n",n,runtime);getch();}

lab4 topologicl

#include <stdio.h>const int MAX = 10;void fnTopological(int a[MAX][MAX], int n);int main(void){
int a[MAX][MAX],n; int i,j;
printf("Topological Sorting Algorithm -\n");
printf("\nEnter the number of vertices : ");
scanf("%d",&n); printf("Enter the adjacency matrix -\n");
for (i=0; i<n; i++)
for (j=0; j<n; j++)scanf("%d",&a[i][j]);
fnTopological(a,n);printf("\n");return 0;}
void fnTopological(int a[MAX][MAX], int n) { int in[MAX], out[MAX], stack[MAX], top=-1;
int i,j,k=0; for (i=0;i<n;i++){ in[i] = 0;for (j=0; j<n; j++) if (a[j][i] == 1)
in[i]++; } while(1) {for (i=0;i<n;i++) { if (in[i] == 0) {stack[++top] = i; in[i] = -1; }}
if (top == -1) break; out[k] = stack[top--];for (i=0;i<n;i++) { if (a[out[k]][i] == 1) in[i]--; }
k++; }printf("Topological Sorting (JOB SEQUENCE) is:- \n"); for (i=0;i<k;i++)
printf("%d ",out[i] + 1);}
lab5 floyd algo
#include<stdio.h>#include<conio.h>#define MAX 10
int D[MAX][MAX];int Min(int a, int b) { if(a<b) return a; else return b;}
void Floyd(int W[MAX][MAX], int n){ int i,j,k; for(i=1;i<=n;i++) for(j=1;j<=n;j++)
D[i][j]=W[i][j]; for(k=1;k<=n;k++) for(i=1;i<=n;i++) for(j=1;j<=n;j++)
D[i][j]=Min(D[i][j], D[i][k]+D[k][j]); } void main() {
int W[MAX][MAX], n, i, j; printf("Enter the number of vertices in a graph\n");
scanf("%d",&n);printf("enter the weight matrix of graph with no negative length cycle\n");
printf("enter 1000 For(i,j) If No Such edge And 0 For(i,i)\n"); for(i=1;i<=n;i++)
for(j=1;j<=n;j++) scanf("%d",&W[i][j]); Floyd(W,n);
printf("the all-pairs-shortest-path matrix of a graph\n"); for(i=1;i<=n;i++) {
for(j=1;j<=n;j++){ printf("%d\t",D[i][j]); } printf("\n"); } getch();}

lab 6 warshalles
#include<stdio.h>#include<conio.h>#define MAX 10
int R[MAX][MAX]; void Warshall(int A[MAX][MAX], int n) { int i,j,k; for(i=1;i<=n;i++)
for(j=1;j<=n;j++) R[i][j]=A[i][j]; for(k=1;k<=n;k++) for(i=1;i<=n;i++)
for(j=1;j<=n;j++) R[i][j]=R[i][j]||(R[i][k]&&R[k][j]); }
void main() { int A[MAX][MAX], n, i, j;
printf("enter the number of vertices in a digraph\n"); scanf("%d",&n);
printf("enter the adjacent matrix of a digraph\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++)
scanf("%d",&A[i][j]); Warshall(A, n);
printf("the transitive closure or path matrix of a digraph\n");
for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf("%d\t",R[i][j]); } printf("\n"); }
getch(); }

lab 8 prims algo

#include <stdio.h> #define INFINITY 1000 #define MAX 10


int prim(int cost[MAX][MAX], int n, int t[MAX-1][2]) {
int i, j, u, k, min, mincost = 0; int nearest[MAX]; nearest[1] = 0;
for (i = 2; i <= n; i++) nearest[i] = 1; for (i = 1; i <= n - 1; i++) { min = INFINITY;
for (u = 2; u <= n; u++) { if (nearest[u] != 0 && cost[u][nearest[u]] < min) {
min = cost[u][nearest[u]]; j = u; } } t[i][1] = j; t[i][2] = nearest[j];
mincost += cost[j][nearest[j]]; nearest[j] = 0; for (k = 2; k <= n; k++) {
if (nearest[k] != 0 && cost[k][nearest[k]] > cost[k][j]) nearest[k] = j; } return mincost;
} void main() { int cost[MAX][MAX], n, i, j, t[MAX][2], mincost;
printf("Enter the number of vertices in an undirected graph:\n"); scanf("%d", &n);
printf("Enter the cost adjacency matrix of an undirected graph:\n");
printf("enter 1000 for(i,j) if no such edge and 0 for(i,i):\n"); for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++) scanf("%d", &cost[i][j]); mincost = prim(cost, n, t);
printf("Edges included in the order as follows:\n"); for (i = 1; i <= n - 1; i++)
printf("(%d, %d)\n", t[i][1], t[i][2]); printf(" cost of Minimum Spanning Tree: %d\n", mincost);
}
lab 9 dijastra algo

#include <stdio.h>#define INFINITY 1000#define MAX 10


void Dijkstra(int cost[MAX][MAX], int n, int v, int dist[MAX], int p[MAX]) {
int i, j, u, min; int Visited[MAX]; for (i = 0; i <= n; i++) { Visited[i] = 0; dist[i] = cost[v][i];
p[i] = v; } Visited[v] = 1; dist[v] = 0; for (i = 2; i <= n; i++) { min = INFINITY;
for (j = 1; j <=n; j++) { if ( dist[j] < min &&!Visited[j]) { min = dist[j]; u = j;
} } Visited[u] = 1; for (j = 1; j <=n; j++) { if (!Visited[j]) {
if (dist[j]>dist[u] + cost[u][j] ) { dist[j] = dist[u] + cost[u][j]; p[j] = u;
}}}}} void main() { int cost[MAX][MAX], n, v, dist[MAX], p[MAX], i, j;
printf("Enter The Number Of Vertices In A Weighted Connected Graph\n");
scanf("%d", &n); printf("Enter The Cost Adjacency Matrix For The Connected Graph\n");
printf("Enter 1000 For i, j If No Such Edge And 0 For i, i\n"); for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) { scanf("%d", &cost[i][j]); } }
printf("Enter The Source Vertex\n"); scanf("%d", &v); Dijkstra(cost, n, v, dist, p);
printf("The Shortest Path With Lengths From Source To Other Vertices Are\n");
for (i = 1; i <= n; i++) { if (i != v) printf("%d --> %d ---> %d of length %d\n", v, p[i],i, dist[i]); }}

You might also like