0% found this document useful (0 votes)
58 views

Program: #10: Write A Program For Knapsack Problem' Using Greedy Approach

The document describes a program that implements the bucket sort algorithm. It defines a bucket struct with a count and value array. It then sorts an input array by distributing elements into buckets based on their value range, sorts each bucket individually, and combines the sorted buckets back into the output array. The program tests bucket sort on a sample integer array, printing the array before and after sorting to demonstrate the algorithm.

Uploaded by

Harshit Varshney
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)
58 views

Program: #10: Write A Program For Knapsack Problem' Using Greedy Approach

The document describes a program that implements the bucket sort algorithm. It defines a bucket struct with a count and value array. It then sorts an input array by distributing elements into buckets based on their value range, sorts each bucket individually, and combines the sorted buckets back into the output array. The program tests bucket sort on a sample integer array, printing the array before and after sorting to demonstrate the algorithm.

Uploaded by

Harshit Varshney
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/ 24

Program: #10

Write a program for ‘Knapsack Problem’ using greedy approach.

#include<stdio.h>
int max (int a, int b)
{
return (a > b)? a : b;
}

int knapsack (int W, int wt [], int val [], int n)


{
int i, w;
int K [n+1] [W+1];

for (i = 0; i <= n; i++)


{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K [i] [w] = 0;
else if (wt [i-1] <= w)
K [i] [w] = max (val [i-1] + K [i-1] [w-wt [i-1]], K [i-1] [w]);
else
K [i] [w] = K [i-1] [w];
}
}

return K [n] [W];


}

int main ()
{
int i, n, val [20], wt [20], W;

printf ("Enter number of items:");


scanf ("%d", &n);

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


printf ("Enter value and weight of items:\n");
for (i = 0; i < n; ++i)
{
scanf ("%d%d", &val [i], &wt [i]);
}

printf ("Enter size of knapsack:");


scanf ("%d", &W);

printf ("%d", knapsack (W, wt, val, n));


return 0;
}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Output:

Enter number of items:3


Enter value and weight of items:
100 20
50 10
150 30
Enter size of knapsack:50
250

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Program: #13

Write a program for ‘Knapsack Problem’ using greedy approach.

#include<stdio.h>
#include<math.h>

int board [20], count;

int place (int row, int column)


{
int i;
for (i=1; i<=row-1; ++i)
{
if (board [i]==column)
return 0;
else
if (abs (board [i] - column) ==abs (i-row))
return 0;
}
return 1;
}

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)
{
if (board [i]==j)
printf ("\tQ");
else

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


printf ("\t-");
}
}
}

void queen (int row, int n)


{
int column;
for (column=1; column<=n; ++column)
{
if (place (row, column))
{
board [row]=column;
if (row==n)
print (n);
else
queen (row+1, n);
}
}
}

int main ()
{
int n, i, j;
printf (" - N Queens Problem Using Backtracking -");
printf ("\n\nEnter number of Queens:");
scanf ("%d", &n);
queen (1, n);
return 0;
}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Output:

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Program: #12

Write a program to find Minimum Spanning Tree using Kruskal’s Algorithm.

#include<stdio.h>
#define MAX 30
typedef struct edge
{
int u, v, w;
}edge;

typedef struct edgelist


{
edge data [MAX];
int n;
}edgelist;

edgelist elist;
int G [MAX] [MAX], n;
edgelist spanlist;

void kruskal ();


int find (int belongs [], int vertexno);
void union1(int belongs [], int c1, int c2);
void sort ();
void print ();

void main ()
{
int i, j, total_cost;
printf ("\nEnter number of vertices:");
scanf ("%d", &n);
printf ("\nEnter the adjacency matrix:\n");
for (i=0; i<n; i++)
for (j=0; j<n; j++)
scanf ("%d", &G [i] [j]);

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


kruskal ();
print ();
}

void kruskal ()
{
int belongs [MAX], i, j, cno1, cno2;
elist.n=0;

for (i=1; i<n; i++)


for (j=0; j<i; j++)
{
if (G [i] [j]!=0)
{
elist.data [elist.n].u=i;
elist.data [elist.n].v=j;
elist.data [elist.n].w=G [i] [j];
elist.n++;
}
}

sort ();

for (i=0; i<n; i++)


belongs [i]=i;

spanlist.n=0;

for (i=0; i<elist.n; i++)


{
cno1=find (belongs, elist.data [i].u);
cno2=find (belongs, elist.data [i].v);

if (cno1!=cno2)
{
spanlist.data [spanlist.n]=elist.data [i];
spanlist.n=spanlist.n+1;
union1 (belongs, cno1, cno2);
}
}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


}

int find (int belongs [], int vertexno)


{
return (belongs [vertexno]);
}

void union1 (int belongs [], int c1, int c2)


{
int i;

for (i=0; i<n; i++)


if (belongs [i]==c2)
belongs [i]=c1;
}

void sort ()
{
int i, j;
edge temp;

for (i=1; i<elist.n; i++)


for (j=0; j<elist.n-1; j++)
if (elist.data [j].w>elist.data [j+1].w)
{
temp=elist.data [j];
elist.data [j]=elist.data [j+1];
elist.data [j+1]=temp;
}
}

void print ()
{
int i, cost=0;

for (i=0; i<spanlist.n; i++)


{
printf ("\n%d\t%d\t%d", spanlist.data [i].u, spanlist.data [i].v, spanlist.data
[i].w);
cost=cost+spanlist.data [i].w;

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


}

printf ("\n\nCost of the spanning tree=%d", cost);


}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Output:

Enter number of vertices:6

Enter the adjacency matrix:


031600
305030
150564
605002
036006
004260

2 0 1
5 3 2
1 0 3
4 1 3
5 2 4

Cost of the spanning tree:13

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Program: #11

Write a program to perform ‘Travelling Salesman Problem’.

#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;
completed [city]=1;

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


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 ()
{
takeInput ();
printf ("\n\nThe Path is:\n");
mincost (0);

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


printf ("\n\nMinimum cost is %d\n ", cost);

return 0;
}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Output:

Enter the number of villages: 4

Enter the Cost Matrix

Enter Elements of Row: 1


0413

Enter Elements of Row: 2


4021

Enter Elements of Row: 3


1205

Enter Elements of Row: 4


3150
The cost list is:
0413
4021
1205
3150

The Path is:


1—>3—>2—>4—>1

Minimum cost is 7

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Program: #8

Write a program to implement ‘Bucket Sort’.

#include <stdio.h>
#include <stdlib.h>

struct bucket
{
int count;
int* value;
};

int compareIntegers (const void* first, const void* second)


{
int x = *((int*)first), y = *((int*)second);
if (x == y)
{
return 0;
}
else if (x < y)
{
return -1;
}
else
{
return 1;
}
}

void bucketSort (int array [], int n)


{
struct bucket buckets [3];
int i, j, k;
for (i = 0; i < 3; i++)
{
buckets [i].count = 0;

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


buckets [i].value = (int*)malloc(sizeof(int) * n);
}

for (i = 0; i < n; i++)


{
if (array [i] < 0)
{
buckets [0].value [buckets [0].count++] = array [i];
}
else if (array [i] > 10)
{
buckets [2].value [buckets [2].count++] = array [i];
}
else
{
buckets [1].value [buckets [1].count++] = array [i];
}
}
for (k = 0, i = 0; i < 3; i++)
{
qsort (buckets [i].value, buckets [i].count, sizeof (int), &compareIntegers);
for (j = 0; j < buckets [i].count; j++)
{
array [k + j] = buckets [i].value [j];
}
k += buckets [i].count;
free (buckets [i].value);
}
}

int main (char *arg []) {

int array [100] = { 5, -34, 10, 1, -42, 123, 2, 395, 5, 4, 1234, 7 };


int i = 12, j, k, n;

n=i;
printf ("Before Sorting\n");
for (j = 0; j<i; j++)
{
printf ("%d ", array [j]);

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


}

bucketSort (array, n);


printf ("\n After Sorting\n");
for (k = 0; k<i; k++)
printf ("%d ", array [k]);
return 0;
}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Output:

Before Sorting
0.235 0.101 0.476 0.7645 0.15 0.142
After Sorting
0.101 0.142 0.15 0.235 0.476 0.7645

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Program: #9

Write a program to implement ‘Radix Sort’.

#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}*start=NULL;

void radix_sorting ();


int larger_dig ();
int digit (int num, int k);

void main ()
{
struct node *temp, *q;
int count, n, num_item;

printf ("Enter the Number of Elements to be Inserted into the List : ");
scanf ("%d", &n);

for (count=0; count<n; count++)


{
printf ("Enter element %d : ", count+1);
scanf ("%d", &num_item);

temp= malloc (sizeof (struct node));


temp->data=num_item;
temp->next=NULL;

if (start==NULL)
start=temp;
else

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


{
q=start;
while (q->next!=NULL)
q=q->next;
q->next=temp;
}
}

radix_sorting ();
printf ("Sorted List is :\n");

q=start;
while (q!=NULL)
{
printf ("%d ", q->data);
q = q->next;
}
printf ("\n");

void radix_sorting ()
{
int count, k, dig, least_sig, most_sig;
struct node *p, *rear [10], *front [10];

least_sig=1;
most_sig=larger_dig (start);

for (k=least_sig; k<=most_sig; k++)


{
for (count=0; count<=9; count++)
{
rear [count] = NULL;
front [count] = NULL;
}

for (p=start; p!=NULL; p=p->next )


{
dig = digit (p->data, k);

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


if (front [dig] == NULL)
front [dig] = p;
else
rear [dig]->next = p;
rear [dig] = p;
}

count=0;
while (front [count] == NULL)
count++;
start = front [count];
while (count<9)
{
if (rear [count+1]!=NULL)
rear [count]->next=front [count+1];
else
rear [count+1]=rear [count];
count++;
}
rear [9]->next=NULL;
}
}

int larger_dig ()
{
struct node *p=start;
int large=0, ndigit=0;

while (p!= NULL)


{
if (p ->data > large)
large = p->data;
p = p->next;
}

while (large!=0)
{
ndigit++;
large = large/10;

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


}
return (ndigit);
}

int digit (int num, int k)


{
int digit, count;
for (count=1; count<=k; count++)
{
digit = num % 10;
num = num /10;
}
return (digit);
}

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)


Output:

Enter the Number of Elements to be Inserted into the List : 5


Enter element 1 : 345
Enter element 2 : 123
Enter element 3 : 976
Enter element 4 : 23
Enter element 5 : 1
Sorted List is :
1 23 123 345 976

DESIGN & ANALYSIS OF ALGORITHM LAB (RCS-552)

You might also like