Aoa
Aoa
--------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n, source, edges, s, d;
printf("Enter number of vertices: ");
scanf("%d", &n);
int dist[n], pred[n], w[n][n];
printf("Enter source vertex: ");
scanf("%d", &source);
dist[0] = 0;
pred[0] = 0;
int check = 1;
printArray(dist,"Distance", n);
printArray(pred, "Predecessor", n);
return 0;
}
-----------------------------------------------------------------------------
KMP:
-------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
int kmp(char *str1, char *str2, int *prefix2, int a_len, int b_len)
{
int i = 0, j = 0;
int main()
{
char str1[20], str2[20];
if(ans == -1)
printf("No match found\n");
else
printf("There was a match found at index %d\n", ans);
return 0;
}
-----------------------------------------------------------------------------
LCS:
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20], str2[20];
int a_len, b_len;
printf("Enter string A: ");
scanf("%s", str1);
fflush(stdin);
printf("Enter string B: ");
scanf("%s", str2);
a_len = strlen(str1);
b_len = strlen(str2);
printf("\n");
for(int i = 0; i <= a_len; i++)
{
for(int j = 0; j <= b_len; j++)
{
printf("%d ", s[i][j]);
}
printf("\n");
}
return 0;
}
-----------------------------------------------------------------------------------
---
SUM OF SUBSETS:
-----------------------------------------------------------------------------------
-----
#include<stdio.h>
if(wsf == sum)
{
printf("The solution vector is as follows\n");
for(int i = 0; i < numbers; i++)
{
printf("%d ", sol_vec[i]);
}
printf("\n");
}
else
{
if(promising(l, wsf, tpl, w))
{
sol_vec[l+1] = 1;
sum_of_subsets(l + 1, wsf + w[l+1], tpl - w[l+1], sol_vec, w);
sol_vec[l+1] = 0;
sum_of_subsets(l + 1, wsf, tpl - w[l+1], sol_vec, w);
}
}
}
int main()
{
int sum_array = 0;
printf("Enter the number of numbers: ");
scanf("%d", &numbers);
bubble_sort(numbers_arr);
return 0;
}
---------------------------------------------------------------------------------
GRAPH COLOURING:
----------------------------------------------------------------------------------
#include<stdio.h>
if(node == nodes - 1)
{
for(int i = 0; i < nodes; i++)
{
printf("Node %d is coloured with colour no %d\n", i,
sol_vec[i]);
}
printf("\n");
}
else
mcolor(node + 1, sol_vec, adj);
}
}
}
int main()
{
int c, edges, start, end, path;
int adj[nodes][nodes];
int sol_vec[nodes];
adj[start][end] = 1;
adj[end][start] = 1;
}
return 0;
}
------------------------------------------------------------------------------
N QUEEN:
-------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int queens;
int main()
{
int c;
printf("Enter number of queens: ");
scanf("%d", &queens);
c = queens;
int sol_vec[queens];
nQueen(0, sol_vec);
return 0;
}
----------------------------------------------------------------------------------
FLOYD MARSHALL:
----------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int nodes;
int main()
{
int edges, start, end, weight;
printf("Enter number of nodes: ");
scanf("%d", &nodes);
// Weights for only those vertices which have are adjacent to each other
for(int i = 0; i < edges; i++)
{
printf("Enter start vertex: ");
scanf("%d", &start);
printf("Enter ending vertex: ");
scanf("%d", &end);
printf("Enter weight: ");
scanf("%d", &weight);
cost[start][end] = weight;
dist[start][end] = weight;
cost[end][start] = weight;
dist[end][start] = weight;
}
print2D(dist);
}
return 0;
}
-----------------------------------------------------------------------------------
-
PRIMS:
-----------------------------------------------------------------------------------
-
#include<stdio.h>
int V;
}
}
printMST(pred, graph);
// for(int i = 1; i < V; i++)
// {
// printf("%d - %d | %d\n", pred[i], i, graph[i][pred[i]]);
// }
}
int main()
{
int edges, source, destination, weight;
printf("Enter number of vertices: ");
scanf("%d", &V);
int graph[V][V];
for(int i = 0; i < V; i++)
{
for(int j = 0; j < V; j++)
{
graph[i][j] = 0;
}
}
printf("Enter number of edges: ");
scanf("%d", &edges);
for(int i = 0; i < edges ; i++)
{
printf("Enter source: ");
scanf("%d", &source);
printf("Enter destination: ");
scanf("%d", &destination);
printf("Enter weight: ");
scanf("%d", &weight);
graph[source][destination] = weight;
graph[destination][source] = weight;
}
prims(graph);
return 0;
}
-------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
int n;
return index;
}
pred[source] = -1;
dist[source] = 0;
int main()
{
int edges, source, destination, weight, i;
printf("Enter number of vertices: ");
scanf("%d", &n);
int graph[n][n];
graph[source][destination] = weight;
graph[destination][source] = weight;
}
djikstra(graph);
return 0;
}
-----------------------------------------------------------------------------------
----
0/1 KNAPSACK:
-----------------------------------------------------------------------------------
----
#include<stdio.h>
#include<stdlib.h>
int capacity;
int main()
{
int items;
printf("Enter number of items: ");
scanf("%d", &items);
weight[0] = 0;
profit[0] = 0;
int i = items;
int w = capacity;
----------------------------------------------------------------------------------