Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > A* algorithm
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 25362 of 27317
Post > Topic >>

A* algorithm

by sulekhasweety@[EMAIL PROTECTED] Apr 4, 2008 at 09:11 AM

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;

 }
 




 11 Posts in Topic:
A* algorithm
sulekhasweety@[EMAIL PROT  2008-04-04 09:11:58 
Re: A* algorithm
Barry Schwarz <schwarz  2008-04-05 01:45:41 
Re: A* algorithm
"Joachim Schmitz&quo  2008-04-05 12:06:33 
Re: A* algorithm
Barry Schwarz <schwarz  2008-04-06 00:04:12 
Re: A* algorithm
"Joachim Schmitz&quo  2008-04-06 09:17:42 
Re: A* algorithm
Barry Schwarz <schwarz  2008-04-06 09:50:35 
Re: A* algorithm
sulekhasweety@[EMAIL PROT  2008-04-05 09:00:39 
Re: A* algorithm
Barry Schwarz <schwarz  2008-04-06 00:04:13 
Re: A* algorithm
"Joachim Schmitz&quo  2008-04-06 09:20:44 
Re: A* algorithm
sulekhasweety@[EMAIL PROT  2008-04-06 04:46:27 
Re: A* algorithm
Barry Schwarz <schwarz  2008-04-06 09:50:35 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:50:53 CDT 2008.