Program: #10: Write A Program For Knapsack Problem' Using Greedy Approach
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 main ()
{
int i, n, val [20], wt [20], W;
#include<stdio.h>
#include<math.h>
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;
}
#include<stdio.h>
#define MAX 30
typedef struct edge
{
int u, v, w;
}edge;
edgelist elist;
int G [MAX] [MAX], n;
edgelist spanlist;
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]);
void kruskal ()
{
int belongs [MAX], i, j, cno1, cno2;
elist.n=0;
sort ();
spanlist.n=0;
if (cno1!=cno2)
{
spanlist.data [spanlist.n]=elist.data [i];
spanlist.n=spanlist.n+1;
union1 (belongs, cno1, cno2);
}
}
void sort ()
{
int i, j;
edge temp;
void print ()
{
int i, cost=0;
2 0 1
5 3 2
1 0 3
4 1 3
5 2 4
#include<stdio.h>
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:");
if (ncity==999)
{
ncity=0;
printf ("%d", ncity+1);
cost+=ary [city] [ncity];
return;
}
mincost (ncity);
}
if (min!=999)
cost+=kmin;
return nc;
}
int main ()
{
takeInput ();
printf ("\n\nThe Path is:\n");
mincost (0);
return 0;
}
Minimum cost is 7
#include <stdio.h>
#include <stdlib.h>
struct bucket
{
int count;
int* value;
};
n=i;
printf ("Before Sorting\n");
for (j = 0; j<i; j++)
{
printf ("%d ", array [j]);
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
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
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);
if (start==NULL)
start=temp;
else
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);
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 (large!=0)
{
ndigit++;
large = large/10;