473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

A* algorithm

Hi ,

this is the program showing implementation of a* algorithm, given n
integers and a sum m ,write a program to find the set of integers
summing to m using a* algorithm.

but i am not getting the o/p correct , i am getting only one set of
integers, can any one point the errors/corrections required ?

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

typedef enum {FALSE,TRUE}bln;
int subsetsum(int*,int,int,bln* ,int);
int compare(void*,void*);
void printanswer(int*,int,bln*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(int);
int i;

bln *selected = calloc(size,sizeof(bln));

qsort(a,size,sizeof(int),compare);

for(i=0;i < size;i++)
{
printf("\n i = %d",i);

if( subsetsum(a,size,sum,selected,i))
printanswer(a,size,selected);

memset(selected,FALSE,size);
}

return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,bln *selected ,int start)
{

int i=start;

if(sum == 0)
return TRUE;

if(selected[i] == FALSE && a[i] <= sum)
{
selected[i] = TRUE;
if(subsetsum(a,n,sum - a[i],selected,i+1))
return TRUE;

selected[i] = FALSE;
}

return FALSE;

}

void printanswer(int* a,int n,bln* selected)
{
int i;

for(i=0;i<n;i++)
if(selected[i])
printf("\t%d",a[i]);

puts("");
}
int compare(void* e1,void* e2)
{
return *(int*)e2 - *(int*)e1;

}
Apr 4 '08 #1
10 2182
On Fri, 4 Apr 2008 09:11:58 -0700 (PDT), su***********@gmail.com
wrote:
>Hi ,

this is the program showing implementation of a* algorithm, given n
integers and a sum m ,write a program to find the set of integers
summing to m using a* algorithm.

but i am not getting the o/p correct , i am getting only one set of
integers, can any one point the errors/corrections required ?

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

typedef enum {FALSE,TRUE}bln;
int subsetsum(int*,int,int,bln* ,int);
int compare(void*,void*);
void printanswer(int*,int,bln*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(int);
You would be better off using sizeof(*a) for the divisor.
int i;

bln *selected = calloc(size,sizeof(bln));

qsort(a,size,sizeof(int),compare);
Your compiler should have reported a problem. Your compare function
does not have the correct prototype to be used by qsort.

Also sizeof(*a) here for the third argument.
>
for(i=0;i < size;i++)
{
printf("\n i = %d",i);

if( subsetsum(a,size,sum,selected,i))
printanswer(a,size,selected);

memset(selected,FALSE,size);
This sets the first six bytes pointed to by selected to zero.
Unfortunately, you have no idea what type of integer selected points
to. Since you only want to store 0 and 1 in each of the integers, you
could make bln a typedef for char. Or you could change the third
argument to size * sizeof(*selected) which would reinitialize the
entire allocated array. As it stands now, if bln is not the same as
char, you are not resetting the entire array for the next set of
tests.
}

return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,bln *selected ,int start)
{

int i=start;

if(sum == 0)
return TRUE;

if(selected[i] == FALSE && a[i] <= sum)
{
selected[i] = TRUE;
if(subsetsum(a,n,sum - a[i],selected,i+1))
It is possible to call subsetsum with start set to size-1. This
recursive call would then call subsetsum with start set to size and i
is then set to start and both selected[i] and a[i] attempt to evaluate
non-existent objects. This is called undefined behavior.
return TRUE;

selected[i] = FALSE;
}

return FALSE;

}

void printanswer(int* a,int n,bln* selected)
{
int i;

for(i=0;i<n;i++)
if(selected[i])
printf("\t%d",a[i]);

puts("");
}
int compare(void* e1,void* e2)
{
return *(int*)e2 - *(int*)e1;

}

Remove del for email
Apr 5 '08 #2
Barry Schwarz wrote:
On Fri, 4 Apr 2008 09:11:58 -0700 (PDT), su***********@gmail.com
wrote:
<snip>
>int compare(void*,void*);
<snip>
> qsort(a,size,sizeof(int),compare);

Your compiler should have reported a problem. Your compare function
does not have the correct prototype to be used by qsort.
What's wrong with it? Besides the missing const?

Bye, Jojo
Apr 5 '08 #3
On Apr 5, 1:45*pm, Barry Schwarz <schwa...@dqel.comwrote:
On Fri, 4 Apr 2008 09:11:58 -0700 (PDT), sulekhaswe...@gmail.com
wrote:


Hi ,
this is the program showing implementation of a* algorithm, given n
integers and a sum m ,write a program to find the set of integers
summing to m using a* algorithm.
but i am not getting the o/p correct , i am getting only one set of
integers, can any one point the errors/corrections required ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef enum {FALSE,TRUE}bln;
int subsetsum(int*,int,int,bln* ,int);
int compare(void*,void*);
void printanswer(int*,int,bln*);
int main(void)
{
* int a[] *= {5,3,4,8,9,6};
* int sum *= 15;
* int size = sizeof(a)/sizeof(int);

You would be better off using sizeof(*a) for the divisor.
* int i;
* bln *selected = calloc(size,sizeof(bln));
* qsort(a,size,sizeof(int),compare);

Your compiler should have reported a problem. *Your compare function
does not have the correct prototype to be used by qsort.

Also sizeof(*a) here for the third argument.
* for(i=0;i < size;i++)
* {
* * printf("\n i = %d",i);
* * if( subsetsum(a,size,sum,selected,i))
* * *printanswer(a,size,selected);
* * memset(selected,FALSE,size);

This sets the first six bytes pointed to by selected to zero.
Unfortunately, you have no idea what type of integer selected points
to. *Since you only want to store 0 and 1 in each of the integers, you
could make bln a typedef for char. *Or you could change the third
argument to size * sizeof(*selected) which would reinitialize the
entire allocated array. *As it stands now, if bln is not the same as
char, you are not resetting the entire array for the next set of
tests.


* }
* return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,bln *selected ,int start)
{
* int i=start;
* if(sum == 0)
* * return TRUE;
* if(selected[i] == FALSE && a[i] <= sum)
* {
* * *selected[i] = TRUE;
* * *if(subsetsum(a,n,sum - a[i],selected,i+1))

It is possible to call subsetsum with start set to size-1. *This
recursive call would then call subsetsum with start set to size and *i
is then set to start and both selected[i] and a[i] attempt to evaluate
non-existent objects. *This is called undefined behavior.


* * *return TRUE;
* * *selected[i] = FALSE;
* }
* return FALSE;
}
void printanswer(int* a,int n,bln* selected)
{
* *int i;
* *for(i=0;i<n;i++)
* * if(selected[i])
* * printf("\t%d",a[i]);
* *puts("");
}
int compare(void* e1,void* e2)
{
* *return *(int*)e2 - *(int*)e1;
}

I tried what u have said as follows , but still i am not get correct o/
p
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*typedef enum {FALSE,TRUE}*/
char bln;
int subsetsum(int*,int,int,char* ,int);
int compare(void*,void*);
void printanswer(int*,int,char*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(*a);
int i;

char *selected = calloc(size,sizeof(char));

qsort(a,size,sizeof(int),compare);

for(i=0;i < size;i++)
{
printf("\n i = %d",i);

if( subsetsum(a,size,sum,selected,i))
printanswer(a,size,selected);

memset(selected,0,size * sizeof(*selected));
}

return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,char *selected ,int start)
{

int i=start;

if(sum == 0)
return 1;

if(selected[i] == 0 && a[i] <= sum)
{
selected[i] = 1;
if(subsetsum(a,n,sum - a[i],selected,i+1))
return 1;

selected[i] = 0;
}

return 0;

}

void printanswer(int* a,int n,char* selected)
{
int i;

for(i=0;i<n;i++)
if(selected[i])
printf("\t%d",a[i]);

puts("");
}
int compare(void* e1,void* e2)
{
return *(int*)e1 - *(int*)e2;

}

I am getting o/p as follows:-

i = 0
i = 1 4 5 6

i = 2
i = 3
i = 4
i = 5
Apr 5 '08 #4
On Sat, 5 Apr 2008 12:06:33 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote:
>Barry Schwarz wrote:
>On Fri, 4 Apr 2008 09:11:58 -0700 (PDT), su***********@gmail.com
wrote:
<snip>
>>int compare(void*,void*);
<snip>
>> qsort(a,size,sizeof(int),compare);

Your compiler should have reported a problem. Your compare function
does not have the correct prototype to be used by qsort.
What's wrong with it? Besides the missing const?
Isn't that enough?
Remove del for email
Apr 6 '08 #5
On Sat, 5 Apr 2008 09:00:39 -0700 (PDT), su***********@gmail.com
wrote:
snip 120+ lines of obsolete code

Please trim your posts when replying
>
I tried what u have said as follows , but still i am not get correct o/
p
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*typedef enum {FALSE,TRUE}*/
char bln;
int subsetsum(int*,int,int,char* ,int);
int compare(void*,void*);
void printanswer(int*,int,char*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(*a);
int i;

char *selected = calloc(size,sizeof(char));

qsort(a,size,sizeof(int),compare);
I said your compare function was wrong. It is still wrong. If you
don't care why should I?
>
for(i=0;i < size;i++)
{
printf("\n i = %d",i);

if( subsetsum(a,size,sum,selected,i))
printanswer(a,size,selected);

memset(selected,0,size * sizeof(*selected));
}

return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,char *selected ,int start)
{

int i=start;

if(sum == 0)
return 1;

if(selected[i] == 0 && a[i] <= sum)
{
selected[i] = 1;
if(subsetsum(a,n,sum - a[i],selected,i+1))
This will still invoke undefined behavior when i in main is size-1.
You haven't addressed that issue at all.
return 1;

selected[i] = 0;
}
There is an error in the logic of this if block. It needs to be a
loop. The net effect of the error is that subsetsum can only detect a
solution when using sequential elements of a. That is why it catches
4-5-6 when i is 1 in main but does not catch 3-4-8 when i is 0 or 6-9
when i is 3. Take a sheet of paper and "play computer" to see it
happen.
>
return 0;

}

void printanswer(int* a,int n,char* selected)
{
int i;

for(i=0;i<n;i++)
if(selected[i])
printf("\t%d",a[i]);

puts("");
}
int compare(void* e1,void* e2)
{
return *(int*)e1 - *(int*)e2;

}

I am getting o/p as follows:-

i = 0
i = 1 4 5 6

i = 2
i = 3
i = 4
i = 5

Remove del for email
Apr 6 '08 #6
Barry Schwarz wrote:
On Sat, 5 Apr 2008 12:06:33 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote:
>Barry Schwarz wrote:
>>On Fri, 4 Apr 2008 09:11:58 -0700 (PDT), su***********@gmail.com
wrote:
<snip>
>>>int compare(void*,void*);
<snip>
>>> qsort(a,size,sizeof(int),compare);

Your compiler should have reported a problem. Your compare function
does not have the correct prototype to be used by qsort.
What's wrong with it? Besides the missing const?

Isn't that enough?
Maybe, but why didn't you say so rather than let us guess?

Bye, Jojo
Apr 6 '08 #7
Barry Schwarz wrote:
On Sat, 5 Apr 2008 09:00:39 -0700 (PDT), su***********@gmail.com
wrote:
snip 120+ lines of obsolete code

Please trim your posts when replying
>>
I tried what u have said as follows , but still i am not get correct
o/ p
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

/*typedef enum {FALSE,TRUE}*/
char bln;
int subsetsum(int*,int,int,char* ,int);
int compare(void*,void*);
void printanswer(int*,int,char*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(*a);
int i;

char *selected = calloc(size,sizeof(char));

qsort(a,size,sizeof(int),compare);

I said your compare function was wrong. It is still wrong. If you
don't care why should I?
You hadn't said where it was wrong and still don't bother to.
To the OP:
int compare(const void*, const void*);

Bye, Jojo
Apr 6 '08 #8
Here is the corrected program

#include< stdio.h >
#include< stdlib.h >
#include< string.h >
char bln;
int subsetsum(int*,int,int,char* ,int);
int compare(const void*,const void*);
void printanswer(int*,int,char*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(*a);
int i;
char *selected = calloc(size,sizeof(*selected));

qsort(a,size,sizeof(int),compare);

for(i=0;i < (size-1);i++)
{
printf("\n i = %d",i);

if( subsetsum(a,size,sum,selected,i))
printanswer(a,size,selected);

memset(selected,0,size * sizeof(*selected));
}

return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,char *selected ,int start)
{
int i;

if(sum == 0)
return 1;

for(i=start;i <= (n-1);i++)
if(selected[i] == 0 && a[i] <= sum)
{
selected[i] = 1;
if(subsetsum(a,n,sum - a[i],selected,i+1))
return 1;

selected[i] = 0;
}

return 0;
}

void printanswer(int* a,int n,char* selected)
{
int i;

for(i=0;i< n;i++)
if(selected[i])
printf("\t%d",a[i]);

puts("");
}
int compare(const void* e1,const void* e2)
{
return *(int*)e2 - *(int*)e1;
}
Apr 6 '08 #9
On Sun, 6 Apr 2008 09:17:42 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote:
>Barry Schwarz wrote:
>On Sat, 5 Apr 2008 12:06:33 +0200, "Joachim Schmitz"
<no*********@schmitz-digital.dewrote:
>>Barry Schwarz wrote:
On Fri, 4 Apr 2008 09:11:58 -0700 (PDT), su***********@gmail.com
wrote:
<snip>
int compare(void*,void*);
<snip>
qsort(a,size,sizeof(int),compare);

Your compiler should have reported a problem. Your compare function
does not have the correct prototype to be used by qsort.
What's wrong with it? Besides the missing const?

Isn't that enough?
Maybe, but why didn't you say so rather than let us guess?
Because spoon feeding is not conducive to learning. He didn't ask why
he got a diagnostic message he didn't understand. He asked why his
code wasn't producing the expected results. Two reasonable first
steps are to remove the undefined behavior and generate a clean
compile.

If he looked up qsort and checked the prototype, the lesson would stay
with him a lot longer. Or at least he will improve his skill in
looking it up. And maybe also figure out how to get his compiler to
warn him if he makes a similar mistake again.
Remove del for email
Apr 6 '08 #10
On Sun, 6 Apr 2008 04:46:27 -0700 (PDT), su***********@gmail.com
wrote:
>Here is the corrected program
It looks much better.
>#include< stdio.h >
#include< stdlib.h >
#include< string.h >
char bln;
int subsetsum(int*,int,int,char* ,int);
int compare(const void*,const void*);
void printanswer(int*,int,char*);
int main(void)
{

int a[] = {5,3,4,8,9,6};
int sum = 15;
int size = sizeof(a)/sizeof(*a);
int i;
char *selected = calloc(size,sizeof(*selected));

qsort(a,size,sizeof(int),compare);

for(i=0;i < (size-1);i++)
{
printf("\n i = %d",i);

if( subsetsum(a,size,sum,selected,i))
printanswer(a,size,selected);

memset(selected,0,size * sizeof(*selected));
}
A style suggestion which will save you a lot of aggravation later
(when you write larger programs). Learn to indent consistently. I
prefer 3 or 4 spaces (not tabs if you post to Usenet) but the
consistency is more important than the value (within reason). This
block should look something like
for
{
printf
if
printanswer
memset
}
It lets you quickly recognize the range of loops and if statements.
>
return EXIT_SUCCESS;
}
int subsetsum(int *a,int n,int sum,char *selected ,int start)
{
int i;

if(sum == 0)
return 1;

for(i=start;i <= (n-1);i++)
Using size-1 in main and n-1 here eliminates the undefined behavior at
the cost of not handling boundary conditions (also known as extreme
cases or corner conditions). Try the same program with sum set 3.

You should also try with sum set to 4 or 7 and decide how you want to
clean up the output. (Hint: one solution would be a simple if in
front of the call to printanswer.)
>if(selected[i] == 0 && a[i] <= sum)
{
selected[i] = 1;
if(subsetsum(a,n,sum - a[i],selected,i+1))
return 1;

selected[i] = 0;
}

return 0;
}

void printanswer(int* a,int n,char* selected)
{
int i;

for(i=0;i< n;i++)
if(selected[i])
printf("\t%d",a[i]);
for
if
printf
>
puts("");
}
int compare(const void* e1,const void* e2)
{
return *(int*)e2 - *(int*)e1;
Many lint type programs will complain that you are casting away the
const. Chang the two casts to (const int*), even though technically
unnecessary (especially in this case with such a simple function),
would eliminate that.

Just for your info, the above is not completely safe in that it could
result in overflow. Since your real purpose is the algorithm and not
the sort, I wouldn't change it but the usual recommendation is

const int *i2 = e2;
const int *i1 = e1;
return (*i1 *i2) ? (-1) : (*i1 < *i2);
(parentheses just for clarity)

I wonder why you changed from an ascending sort to a descending one.
>}

Remove del for email
Apr 6 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jack Smith | last post by:
Hello, any help appreciated with following problem. I figured out the algorithm (I think), just having trouble proving it is optimal. Suppose we are given n tasks each of which takes 1 unit...
16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
10
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement,...
32
by: Cmorriskuerten | last post by:
HI, is this is this solution to test if a number is a prime number or not: /* * Is n a prime number? * Return TRUE (1): n is a prime number * Return FALSE (0): n is a *not* a prime number...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Julio C. Hernandez Castro | last post by:
Dear all, We have just developped a new block cipher called Raiden, following a Feistel Network structure by means of genetic programming. Our intention now consists on getting as much feedback...
0
by: aruna | last post by:
hey guys i earlier had very valuable responses from you all for base64 encoding algorithm.so thank for that. so now i need your assistance to do a float encoding algorithm. you may wonder why i'm...
1
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.