You are on page 1of 14

EXPERIMENT-6

Objective: Write a program to find Minimum Spanning


Tree using Kruskal’s Algorithm.
Program:
#include <stdio.h>
#include <stdlib.h>
// Structure to represent an edge in the graph
struct Edge {
int src, dest, weight;
};
// Structure to represent a subset for union-find
struct Subset {
int parent, rank;
};
// Function to create a graph with V vertices and E edges
struct Edge* createGraph(int V, int E) {
struct Edge* graph = (struct Edge*)malloc(E *
sizeof(struct Edge));
return graph;
}
// Function to find the set of an element i
int find(struct Subset subsets[], int i) {
if (subsets[i].parent != i)
subsets[i].parent = find(subsets, subsets[i].parent);
return subsets[i].parent;
}
// Function that does union of two sets of x and y
void Union(struct Subset subsets[], int x, int y) {
int xroot = find(subsets, x);
int yroot = find(subsets, y);

ANSHIKA SINGH (2100300130026)


if (subsets[xroot].rank < subsets[yroot].rank)
subsets[xroot].parent = yroot;
else if (subsets[xroot].rank > subsets[yroot].rank)
subsets[yroot].parent = xroot;
else {
subsets[yroot].parent = xroot;
subsets[xroot].rank++;
}
}
// Comparison function used by qsort to sort edges based
on their weight
int compare(const void* a, const void* b) {
return ((struct Edge*)a)->weight - ((struct
Edge*)b)->weight;
}
// Function to construct MST using Kruskal's algorithm
void KruskalMST(struct Edge* graph, int V, int E) {
// Allocate memory for creating V subsets
struct Subset* subsets = (struct Subset*)malloc(V *
sizeof(struct Subset));
// Create V subsets with single elements
for (int v = 0; v < V; ++v) {
subsets[v].parent = v;
subsets[v].rank = 0;
}
// Sort all the edges in non-decreasing order of their
weight
qsort(graph, E, sizeof(graph[0]), compare);
// Initialize result
struct Edge* result = (struct Edge*)malloc((V - 1) *
sizeof(struct Edge));
ANSHIKA SINGH (2100300130026)
// Index used to pick the next edge
int i = 0;
// Index used to pick the next result edge
int e = 0;
// Number of edges to be taken is equal to V-1
while (e < V - 1 && i < E) {
// Pick the smallest edge. Increment the index for the next
iteration
struct Edge next_edge = graph[i++];
int x = find(subsets, next_edge.src);
int y = find(subsets, next_edge.dest);
// If including this edge doesn't cause a cycle, include it in
the result and increment the index
if (x != y) {
result[e++] = next_edge;
Union(subsets, x, y);
}
}
// Print the result
printf("Edges in the MST:\n");
for (i = 0; i < e; ++i) {
printf("(%d, %d) -> %d\n", result[i].src,
result[i].dest, result[i].weight);
}
free(subsets);
free(result);
}
int main() {
int V = 4; // Number of vertices
int E = 5; // Number of edges
struct Edge* graph = createGraph(V, E);
ANSHIKA SINGH (2100300130026)
// Add edge 0-1
graph[0].src = 0;
graph[0].dest = 1;
graph[0].weight = 10;
// Add edge 0-2
graph[1].src = 0;
graph[1].dest = 2;
graph[1].weight = 6;
// Add edge 0-3
graph[2].src = 0;
graph[2].dest = 3;
graph[2].weight = 5;
// Add edge 1-3
graph[3].src = 1;
graph[3].dest = 3;
graph[3].weight = 15;
// Add edge 2-3
graph[4].src = 2;
graph[4].dest = 3;
graph[4].weight = 4;
KruskalMST(graph, V, E);
free(graph);
return 0;
}

OUTPUT:

ANSHIKA SINGH (2100300130026)


EXPERIMENT-8
Objective: Write a program to implement Knapsack
Problem using Greedy Solution.
Program:
# include<stdio.h>
void knapsack(int n, float weight[], float profit[], float
capacity)
{
float x[20], tp= 0;
int i, j, u;
u=capacity;
for (i=0;i<n;i++)
x[i]=0.0;
for (i=0;i<n;i++)
{
if(weight[i]>u)
break;
else
{
x[i]=1.0;
tp= tp+profit[i];
u=u-weight[i];
}
}
if(i<n)
x[i]=u/weight[i];
tp= tp + (x[i]*profit[i]);
printf("\nThe result vector is:- ");
for(i=0;i<n;i++)
printf("%f\n",x[i]);

ANSHIKA SINGH (2100300130026)


printf("\nMaximum profit is:- %f", tp);
}
void main()
{
float weight[20], profit[20], capacity;
int n, i ,j;
float ratio[20], temp;
printf ("\nEnter the no. of objects:- ");
scanf ("%d", &n);
printf ("\nEnter the wts and profits of each
object:- ");
for (i=0; i<n; i++)
{
scanf("%f %f", &weight[i], &profit[i]);
}
printf ("\nEnter the capacity of knapsack:- ");
scanf ("%f", &capacity);
for (i=0; i<n; i++)
{
ratio[i]=profit[i]/weight[i];
}
for(i=0; i<n; i++)
{
for(j=i+1;j< n; j++)
{
if(ratio[i]<ratio[j])
{
temp= ratio[j];
ratio[j]= ratio[i];
ratio[i]= temp;
temp= weight[j];
ANSHIKA SINGH (2100300130026)
weight[j]= weight[i];
weight[i]= temp;
temp= profit[j];
profit[j]= profit[i];
profit[i]= temp;
}
}
}
knapsack(n, weight, profit, capacity);
}

OUTPUT:

ANSHIKA SINGH (2100300130026)


EXPERIMENT-9
Objective: Write a program to implement N Queen
Problem using Backtracking.
Program:
#include<stdio.h>
#include<math.h>
int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
printf(" - N Queens Problem Using Backtracking -");
printf("\n\nEnter number of Queens:");
scanf("%d",&n);
queen(1,n);
return 0;
}
//function for printing the solution
void print(int n)
{
int i,j;
printf("\n\nSolution %d:\n\n",++count);
for(i=1;i<=n;++i)
printf("\t%d",i);
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)

ANSHIKA SINGH (2100300130026)


printf("\tQ"); //queen at i,j position
else
printf("\t-"); //empty slot
}
}
}
/*funtion to check conflicts
If no conflict for desired postion returns 1 otherwise
returns 0*/
int place(int row,int column)
{
int i;
for(i=1;i<=row-1;++i)
{
//checking column and digonal conflicts
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1; //no conflicts
}
//function to check for proper positioning of queen
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
ANSHIKA SINGH (2100300130026)
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}

OUTPUT:
- N Queens Problem Using Backtracking –

Enter number of Queens:4

Solution 1:

1 2 3 4

1 - Q - -

2 - - - Q

3 Q - - -

4 - - Q -

ANSHIKA SINGH (2100300130026)


Solution 2:

1 2 3 4

1 - - Q -

2 Q - - -

3 - - - Q

4 - Q - -

ANSHIKA SINGH (2100300130026)


EXPERIMENT-10
Objective: Write a program to perform Travelling
Salesman Problem.
Program:
#include<stdio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
printf("Enter the number of villages: ");
scanf("%d",&n);
printf("\nEnter the Cost Matrix\n");
for(i=0;i < n;i++)
{
printf("\nEnter Elements of Row: %d\n",i+1);
for( j=0;j < n;j++)
scanf("%d",&ary[i][j]);
completed[i]=0;
}
printf("\n\nThe cost list is:");
for( i=0;i < n;i++)
{
printf("\n");
for(j=0;j < n;j++)
printf("\t%d",ary[i][j]);
}
}
void mincost(int city)
{
int i,ncity;

ANSHIKA SINGH (2100300130026)


completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i < n;i++)
{
if((ary[c][i]!=0)&&(completed[i]==0))
if(ary[c][i]+ary[i][c] < min)
{
min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
int main()
ANSHIKA SINGH (2100300130026)
{
takeInput();
printf("\n\nThe Path is:\n");
mincost(0); //passing 0 because starting vertex
printf("\n\nMinimum cost is %d\n ",cost);
return 0;
}

OUTPUT:

ANSHIKA SINGH (2100300130026)

You might also like