Latest Updates ☛
  • Graphs: Introduction of graphs and types of graphs
  • SPOJ : Geeky solves geeky solution
  • Solution Code : Pass the time problem on SPOJ.
  • Find the minimum shortest path solution in c language ! !

Tuesday, August 13, 2013

C++ Program to calculate the sum of subsets using dynamic programming.. ! !

C++ : Sum Of Subset using Dynamic Programming

In this problem code we use the concept of dynamic programming to capture the subset of a given number set whose sum to be equal to the provided input,here it takes the combination of the numbers from the set of numbers whose sum is  provided and simultaneously checking the condition  that is it promising to include the number into the includeable array in order to receive desired value of sum if condition is true then it includes inti include able array else move forward and checks for next ,similarly it checks for various combination eliminating which are already used.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//C++ program to calculate sum of subset
//by Code Author


#include<stdio.h>
#include<conio.h>

//declaring arrays for weight i.e which contains number set
int w[10],include[10];
int n,sum;

//function prototyping
int promising(int i,int weight,int total);
void sofsubset(int i,int weight,int total);

main()
{
int i,total=0; //initialization of total

//providing total number of numbers in the set
printf("Enter the no. of numbers : ");
scanf("%d",&n);

//printing numbers in increasing order
//caution:numbers must be given in ascending order
printf("Enter the numbers in Increasing order : ");
for(i=0;i<n;i++)
{
scanf("%d",&w[i]);
//here total is automatically calculated inorder to supply to the sum of the subset
total = total + w[i];
}


printf("Enter the required sum for which subset should be given out : ");
scanf("%d",&sum);
sofsubset(-1,0,total);
getch();
}

//function to calculate sum of subset for given sum value
void sofsubset(int i,int weight,int total)
{
int j;
if(promising(i,weight,total)) //calling the checking method
{
if(weight==sum)
{
printf(" The subsets are : ");
for(j=0;j<=i;j++)
{
if(include[j]==1)
printf(" %d ",w[j]);
}
printf("\n");
return;
}
include[i+1]=1;
sofsubset(i+1,weight+w[i+1],total-w[i+1]);
include[i+1]=0;
sofsubset(i+1,weight,total-w[i+1]);

}

}

//function to check the promising condition of the sum of the subset and return accordingly
int promising(int i,int weight,int total)
{

if((weight+total>=sum)&&(weight==sum||weight+w[i+1]<=sum))
return 1;
return 0;
}




The output for the given code is ------


Enter the no. of numbers : 8
Enter the numbers in Increasing order : 2 5 8 9 12 15 17 20
Enter the required sum for which subset should be given out : 17
The subsets are : 2 15
The subsets are : 5 12
The subsets are : 8 9
The subsets are : 17

No comments :

Post a Comment

Privacy Policy | Disclaimer | Terms of Service

Copyright © 2013, Code Author. Powered by Blogger.