0% found this document useful (0 votes)
8 views6 pages

ada lab

The document contains multiple C programming code snippets implementing various algorithms including Kruskal's, Prim's, Floyd-Warshall, Dijkstra's, and Knapsack. Each section defines functions for graph operations, sorting, and dynamic programming techniques. The code is structured to read graph data, perform calculations, and output results for different algorithmic problems.
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)
8 views6 pages

ada lab

The document contains multiple C programming code snippets implementing various algorithms including Kruskal's, Prim's, Floyd-Warshall, Dijkstra's, and Knapsack. Each section defines functions for graph operations, sorting, and dynamic programming techniques. The code is structured to read graph data, perform calculations, and output results for different algorithmic problems.
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/ 6

1.#include<stdio.

h> for(j=1;j<=n;j++)
#define INF 999 scanf("%d",&c[i][j]);
#define MAX 100 kruskal(n);
int p[MAX],c[MAX][MAX],t[MAX][2]; return 0;
int find(int v){ }
while(p[v])
v=p[v]; 2. #include<stdio.h>
return v; #define INF 999
} int prim(int c[10][10],int n,int s){
void union1(int i,int j){ int v[10],i,j,sum=0,ver[10],d[10],min,u;
p[j]=i; for(i=1;i<=n;i++){
} ver[i]=s;
void kruskal(int n){ d[i]=c[s][i];
int i,j,k,u,v,min,res1,res2,sum=0; v[i]=0;
for(k=1;k<n;k++){ }
min=INF; v[s]=1;
for(i=1;i<n-1;i++){ for(i=1;i<=n-1;i++) {
for(j=1;j<=n;j++){ min=INF;
if(i==j) continue; for(j=1;j<=n;j++)
if(c[i][j]<min){ if(v[j]==0&&d[j]<min) {
u=find(i); min=d[j];
v=find(j); u=j;
if(u!=v){ }
res1=i; v[u]=1;
res2=j; sum=sum+d[u];
min=c[i][j]; printf("\n %d->%d sum=%d",ver[u],u,sum);
} for(j=1;j<=n;j++)
} if(v[j]==0&&c[u][j]<d[j]){
} d[j]=c[u][j];
} ver[j]=u;
union1(res1,find(res2)); }
t[k][1]=res1; }
t[k][2]=res2; return sum;
sum=sum+min; }
} void main(){
printf("\n cost of spanning tree is=%d",sum); int c[10][10],i,j,res,s,n;
printf("\n edges of spanning tree are:\n"); printf("\n enter n value:");
for(i=1;i<n;i++) scanf("%d",&n);
printf("%d > %d\n",t[i][1],t[i][2]); printf("\n enter the graph data:\n");
} for(i=1;i<=n;i++)
int main(){ for(j=1;j<=n;j++)
int i,j,n; scanf("%d",&c[i][j]);
printf("\n enter the n value:"); printf("\n enter the source node:");
scanf("%d",&n); scanf("%d",&s);
for(i=1;i<=n;i++) res=prim(c,n,s);
p[i]=0; printf("\n cost=%d",res);
printf("\n enter the graph data:\n"); }
for(i=1;i<=n;i++)
3a. #include<stdio.h> {
#define INF 999 for(j=1; j<=n; j++)
int min(int a,int b){ printf("%d ",a[i][j]);
return(a<b)?a:b; printf("\n");
} }
void floyd(int p[][10],int n){ return 0;
int i,j,k; }
for(k=1;k<=n;k++)
for(i=1;i<=n;i++) 4. #include<stdio.h>
for(j=1;j<=n;j++) #define INF 999
p[i][j]=min(p[i][j],p[i][k]+p[k][j]); void dijkstra(int c[10][10],int n,int s,int d[10]){
} int v[10],min,u,i,j;
void main(){ for(i=1; i<=n; i++){
int a[10][10],n,i,j; d[i]=c[s][i];
printf("\n Enter the n value:"); v[i]=0;
scanf("%d",&n); }
printf("\n Enter the graph data:\n"); v[s]=1;
for(i=1;i<=n;i++) for(i=1; i<=n; i++){
for(j=1;j<=n;j++) min=INF;
scanf("%d",a[i][j]); for(j=1; j<=n; j++)
floyd(a,n); if(v[j]==0 && d[j]<min){
printf("\n Shortest path matrix\n"); min=d[j];
for(i=1;i<=n;i++){ u=j;
for(j=1;j<=n;j++) }
printf("%d",a[i][j]); v[u]=1;
printf("\n"); for(j=1; j<=n; j++)
} if(v[j]==0 && (d[u]+c[u][j])<d[j])
} d[j]=d[u]+c[u][j];
}
3b. #include<stdio.h> }
void warsh(int p[][10],int n){ int main(){
int i,j,k; int c[10][10],d[10],i,j,s,sum,n;
for(k=1; k<=n; k++) printf("\nEnter n value:");
for(i=1; i<=n; i++) scanf("%d",&n);
for(j=1; j<=n; j++) printf("\nEnter the graph data:\n");
p[i][j]=p[i][j] || p[i][k] && p[k][j]; for(i=1; i<=n; i++)
} for(j=1; j<=n; j++)
int main(){ scanf("%d",&c[i][j]);
int a[10][10],n,i,j; printf("\nEnter the souce node:");
printf("\nEnter the n value:"); scanf("%d",&s);
scanf("%d",&n); dijkstra(c,n,s,d);
printf("\nEnter the graph data:\n"); for(i=1; i<=n; i++)
for(i=1; i<=n; i++) printf("\nShortest distance from %d to %d is
for(j=1; j<=n; j++) %d",s,i,d[i]);
scanf("%d",&a[i][j]); return 0;
warsh(a,n); }
printf("\nResultant path matrix\n");
for(i=1; i<=n; i++)
5.#include<stdio.h> return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
#include<conio.h> }
int temp[10],k=0; int main(){
void sort(int a[][10],int id[],int n){ int m,i,max_profit;
int i,j; printf("\n Enter the no.of objects:");
for(i=1; i<=n; i++){ scanf("%d",&n);
if(id[i]==0){ printf("\n Enter the knapsack capacity:");
id[i]=-1; scanf("%d",&m);
temp[++k]=i; printf("\n Enter the profit followed by
for(j=1; j<=n; j++){ weight:\n");
if(a[i][j]==1 && id[j]!=-1) for(i=1;i<=n;i++)
id[j]--; scanf("%d %d",&p[i],&w[i]);
} max_profit=knap(1,m);
i=0; printf("\n Max profit=%d",max_profit);
} return 0;
} }
}
void main(){ 7. #include<stdio.h>
int a[10][10],id[10],n,i,j; #define MAX 50
printf("\nEnter the n value:"); int p[MAX],w[MAX],x[MAX];
scanf("%d",&n); double maxprofit;
for(i=1; i<=n; i++) int n,m,i;
id[i]=0; void greedyknapsack(int n,int w[],int p[],int m){
printf("\nEnter the graph data:\n"); double ratio[MAX];
for(i=1; i<=n; i++) for(i=0;i<n;i++){
for(j=1; j<=n; j++){ for(int j=i+1;j<n;j++){
scanf("%d",&a[i][j]); if(ratio[i]<ratio[j]){
if(a[i][j]==1) double knap=ratio[i];
id[j]++; ratio[i]=ratio[j];
} ratio[j]=knap;
sort(a,id,n); int temp2=w[i];
if(k!=n) w[i]=w[j];
printf("\nTopological ordering not possible"); w[j]=temp2;
else{
printf("\nTopological ordering is:"); temp2=p[i];
for(i=1; i<=k; i++) p[i]=p[j];
printf("%d ",temp[i]); p[j]=temp2;
} }
getch(); }
} }
int currentWeight=0;
6. #include<stdio.h> maxprofit=0.0;
int w[10],p[10],n; for(i=0;i<n;i++){
int max(int a,int b){ if(currentWeight+w[i]<=m){
return a>b?a:b; x[i]=1;
} currentWeight+=w[i];
int knap(int i,int m){ maxprofit+=p[i];
if(i==n) return w[i]>m?0:p[i]; }
else{ int i,n,sum=0;
x[i]=(m-currentWeight)/(double) w[i]; printf("\nEnter the n value:");
maxprofit+=x[i]=p[i]; scanf("%d",&n);
break; printf("\nEnter the set in increasing order:");
} for(i=1; i<=n; i++)
} scanf("%d",&s[i]);
printf("Optimal solutions for greedy printf("\nEnter the max subset value:");
method:%.1f\n",maxprofit); scanf("%d",&d);
printf("Solution vector for greedy method:"); for(i=1;i<=n;i++)
for(i=0;i<n;i++) sum=sum+s[i];
printf("%d\t",x[i]); if(sum<d||s[1]>d)
} printf("\n No subset possible");
int main(){ else
printf("\n Enter the no.of objects:"); sumofsub(0,1,sum);
scanf("%d",&n); return 0;
printf("\n Enter the objects weights:"); }
for(i=0;i<n;i++)
scanf("%d",&w[i]); 9. #include <stdio.h>
printf("\n Enter the objects profits:"); #include <stdlib.h>
for(i=0;i<n;i++) #include <time.h>
scanf("%d",&p[i]); void swap(int *a, int *b) {
printf("Enter the maximum capacity:"); int temp = *a;
scanf("%d",&m); *a = *b;
greedyknapsack(n,w,p,m); *b = temp;
return 0; }
} void selectionSort(int a[], int n) {
int i, j, min_idx;
8. #include<stdio.h> for (i = 0; i < n - 1; i++) {
#define MAX 10 min_idx = i;
int s[MAX],X[MAX],d; for (j = i + 1; j < n; j++) {
void sumofsub(int p,int k,int r){ if (a[j] < a[min_idx]) {
int i; min_idx = j;
X[k]=1; }
if((p+s[k])==d){ }
for(i=1; i<=k; i++) swap(&a[min_idx], &a[i]);
if(X[i]==1) }
printf("%d",s[i]); }
printf("\n"); void printarray(int a[], int size) {
} for (int i = 0; i < size; i++)
else printf("%d", a[i]);
if(p+s[k]+s[k+1]<=d) printf("\n");
sumofsub(p+s[k], k+1, r-s[k]); }
if((p+r-s[k]>=d)&&(p+s[k+1]<=d)){ int main() {
X[k]=0; int n, i;
sumofsub(p, k+1, r-s[k]); int arr[30000];
} printf("Enter the number of elements: ");
} scanf("%d", &n);
int main(){ for (i = 0; i < n; i++) {
arr[i] = rand() % 100; quicksort(a, low, j - 1);
} quicksort(a, j + 1, high);
printArray(arr, n); }
clock_t start_time = clock(); }
selectionSort(arr, n); int main() {
clock_t end_time = clock(); int n, i;
double time_taken = ((double)(end_time - int arr[10000];
start_time)) / CLOCKS_PER_SEC; printf("Enter the number of elements: ");
printf("Sorted array:\n"); scanf("%d", &n);
printarray(arr, n); srand(time(0));
printf("Time taken for sorting: %0.6f seconds\n", for (i = 0; i < n; i++) {
time_taken); arr[i] = rand() % 100;
return 0; }
} printf("Array Before sorting:\n");
10. #include <stdio.h> printArray(arr, n);
#include <stdlib.h> clock_t start_time = clock();
#include <time.h> quicksort(arr, 0, n - 1);
void swap(int *a, int *b) { clock_t end_time = clock();
int temp = *a; printf("Sorted array:\n");
*a = *b; printArray(arr, n);
*b = temp; double time_taken = ((double)(end_time -
} start_time)) / CLOCKS_PER_SEC;
void printArray(int a[], int size) { printf("Time taken for sorting: %0.6f seconds\n",
for (int i = 0; i < size; i++) time_taken);
printf("%d", a[i]); // No spaces between return 0;
numbers }
printf("\n"); 11. #include <stdio.h>
} #include <stdlib.h>
int partition(int a[], int low, int high) { #include <time.h>
int pivot = a[low]; void Merge(int a[], int low, int mid, int high) {
int i = low + 1; int i, j, k;
int j = high; int b[1000]; // Adjust size if n > 1000
while (1) { i = low;
while (i <= high && a[i] <= pivot) j = mid + 1;
i++; k = low;
while (a[j] > pivot) while (i <= mid && j <= high) {
j--; if (a[i] <= a[j])
if (i < j) b[k++] = a[i++];
swap(&a[i], &a[j]); else
else b[k++] = a[j++];
break; }
} while (i <= mid)
swap(&a[low], &a[j]); b[k++] = a[i++];
return j; while (j <= high)
} b[k++] = a[j++];
void quicksort(int a[], int low, int high) { for (k = low; k <= high; k++)
if (low < high) { a[k] = b[k];
int j = partition(a, low, high); }
void Mergesort(int a[], int low, int high) { char board[MAX][MAX];
if (low >= high) for (int i = 0; i < n; i++)
return; for (int j = 0; j < n; j++)
int mid = (low + high) / 2; board[i][j] = '-';
Mergesort(a, low, mid); for (int i = 0; i < n; i++)
Mergesort(a, mid + 1, high); board[i][c[i]] = 'Q';
Merge(a, low, mid, high); for (int i = 0; i < n; i++) {
} for (int j = 0; j < n; j++)
void printArray(int arr[], int size) { printf("%c", board[i][j]); // No spaces
for (int i = 0; i < size; i++) between cells
printf("%d", arr[i]); // No spaces between printf("\n");
numbers }
printf("\n"); printf("\n\n");
} }
int main() { void n_queens(int n) {
int n, i; int r = 0;
int arr[1000]; int c[MAX];
printf("Enter the number of elements: "); for (int i = 0; i < MAX; i++)
scanf("%d", &n); c[i] = -1;
srand(time(0)); while (r >= 0) {
for (i = 0; i < n; i++) { c[r]++;
arr[i] = rand() % 100; while (c[r] < n && !can_place(c, r))
} c[r]++;
printf("Unsorted array:\n"); if (c[r] < n) {
printArray(arr, n); if (r == n - 1) {
clock_t start_time = clock(); display(c, n); // Print one solution
Mergesort(arr, 0, n - 1); } else {
clock_t end_time = clock(); r++;
printf("Sorted array:\n"); c[r] = -1;
printArray(arr, n); }
double time_taken = ((double)(end_time - } else {
start_time)) / CLOCKS_PER_SEC; r--;
printf("Time taken for sorting: %.6f seconds\n", }
time_taken); }
return 0; }
} int main() {
12. #include <stdio.h> int n;
#include <stdlib.h> printf("Enter the number of queens (max %d): ",
#include <math.h> MAX);
#define MAX 10 scanf("%d", &n);
int can_place(int c[], int r) { if (n > MAX || n < 1) {
for (int i = 0; i < r; i++) { printf("Invalid number of queens. Must be
if (c[i] == c[r] || abs(c[i] - c[r]) == abs(i - r)) between 1 and %d.\n", MAX);
return 0; return 1;
} }
return 1; n_queens(n);
} return 0;
void display(int c[], int n) { }

You might also like