GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Nov-2008, 05:42
AmaScatman AmaScatman is offline
New Member
 
Join Date: Nov 2008
Posts: 3
AmaScatman is on a distinguished road

Backtrack algorithm


hi all..
i need help in solving a backtrack algorithm using c++ .

the program asks the user for a number , and the output will be all the sums of a certain number.. for example:

if the user entered 10.. the output should be:
1+2+3+4
1+2+7
1+3+6
1+4+5
1+9
2+3+5
2+8
3+7
4+6
10

example 2.. if the user entered 6.. the output should be:
1+2+3
1+5
2+4
6
  #2  
Old 06-Nov-2008, 06:38
ocicat ocicat is offline
Regular Member
 
Join Date: May 2008
Posts: 586
ocicat is a jewel in the roughocicat is a jewel in the rough

Re: need help in backtrack algorithm


Quote:
Originally Posted by AmaScatman
the program asks the user for a number , and the output will be all the sums of a certain number.. for example:

if the user entered 10.. the output should be:
1+2+3+4
1+2+7
1+3+6
1+4+5
1+9
2+3+5
2+8
3+7
4+6
10
These are obviously examples in recursion.

The goal of this site is not to write code for posters. Our goal is to help clarify concepts & questions so you can complete the assignment yourself. By posting code, we will be able to see what you do & do not understand so this will help focus discussion with specific questions.

When posting code, please place a [cpp] tag before any code posted & a [/cpp] tag afterwards. This will preserve formatting & color code keywords & identifiers.
  #3  
Old 06-Nov-2008, 15:06
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Backtrack algorithm


I will try to give you some ideas about where to begin your program. First you might want to factorize your input and store all the factors in a set. then get all permutations of these factors that add up to the input. doesnt sound like a an efficient way to do it, but it will work. I will post something better later.
  #4  
Old 06-Nov-2008, 15:41
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Backtrack algorithm


Factoring the input won't accomplish anything useful. In the description of the problem, he does not state that the sums need to be made up of factors.

A recursive solution is on the right track. Given a number n, print all the sums that add up to n - 1 plus a 1. Then print all the sums that add up to n - 2 plus a 2, etc. If you want to make sure you don't repeat sums, always pick the numbers from smallest to largest. Your algorithm will need to take a second input then, which represents the smallest number you want to include as part of the sum.

You'll need to write a function getSums(i, n) that returns all the sums that add up to n using distinct integers from i through n. The function will need a loop where another variable j goes from i to n. On each iteration, you'll find the sums that start with j. You can do that by calling getSums(j+1,n-j) and appending j onto the beginning of each one.

You'll also want to write a wrapper function that takes n as a parameter and calls getSums(1,n) to start the recursion.

Finally, recursion always requires a base case, so don't forget about that. To figure out the base case, note that each recursive call uses a smaller value of n.

I hope I'm explaining that well. I wrote a quick implementation of it in Matlab, and it works. It only uses 13 lines of code.
__________________
www.blake-foster.com
  #5  
Old 06-Nov-2008, 15:51
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Backtrack algorithm


Quote:
Originally Posted by Blake
Factoring the input won't accomplish anything useful. In the description of the problem, he does not state that the sums need to be made up of factors.

A re.

thats right, i was reading something about factors and suddenly saw this question and assumed something that was not given. apologies..
  #6  
Old 06-Nov-2008, 16:13
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Backtrack algorithm


Perhaps you were thinking of perfect numbers (numbers that are the sum of their factors)?
__________________
www.blake-foster.com
  #7  
Old 08-Nov-2008, 01:16
n00pster n00pster is offline
Junior Member
 
Join Date: Sep 2008
Location: Miami
Posts: 40
n00pster will become famous soon enough

Re: Backtrack algorithm


Quote:
Originally Posted by Blake
Perhaps you were thinking of perfect numbers (numbers that are the sum of their factors)?
Well actually I was reading about the Euclidean algorithm to determine the GCD which also mentions how it does not require factorization. was just looking through a book about cryptography with Coding Theory.
  #8  
Old 08-Nov-2008, 07:01
AmaScatman AmaScatman is offline
New Member
 
Join Date: Nov 2008
Posts: 3
AmaScatman is on a distinguished road

Re: Backtrack algorithm


CPP / C++ / C Code:
#include <iostream.h>
#include <conio.h>

int val=0,start=0;

void sumAlgo(int n){

if(start==n){ cout<<n; return ;}

val=0;
start++;

for(int i=0 ; i<n ; i++){
 val=start+i;
 if(n == val && i>start) cout<<start<<"+"<<i<<endl;
}

sumAlgo(n);
}


main(){
 int numb;
 cout<<"enter a number :";
 cin>>numb;

sumAlgo(numb);
getch();
}

well thats my code so far..... it only displays 2 sums--- for example:
if the user eneter 10 the output will be:
1+9
2+8
3+7
4+6
10

so i need help in the "sumAlgo" function.. i think i should use a backtrack algorithm.... but i don't know how!!
any suggestions??
Last edited by admin : 08-Nov-2008 at 10:31. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #9  
Old 08-Nov-2008, 10:22
Blake's Avatar
Blake Blake is offline
Regular Member
 
Join Date: Nov 2005
Posts: 330
Blake is a jewel in the roughBlake is a jewel in the roughBlake is a jewel in the rough

Re: Backtrack algorithm


You'll need to make the recursive calls inside the loop, and n should not be a global variable (since every recursive call will change its value). Here's the Matlab code that does the job. It should point you in the right direction, without giving away the answer. Look closely at where the loop starts and ends.

Code:
function getSums(n) getSumsHelper(1,n,[]); end function getSumsHelper(i,n,s) if n == 0 s else for j=i:n getSumsHelper(j+1,n-j,[s j]); end end end

It uses an array to store the values in the sum (the third parameter to getSumsHelper), and prints the whole array once all the values in the sum are found.
__________________
www.blake-foster.com
  #10  
Old 09-Nov-2008, 04:24
AmaScatman AmaScatman is offline
New Member
 
Join Date: Nov 2008
Posts: 3
AmaScatman is on a distinguished road

Re: Backtrack algorithm


gr8!!! thx Blake...
i've finally done it!!!
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Backtrack in C programming, need help please selvamd5 C++ Forum 2 08-Nov-2006 00:11
Algorithm Help Please! daking_09 C++ Forum 0 24-May-2006 19:12
help.... SLR * algorithm tay C Programming Language 4 10-Sep-2004 11:48
Simple Encryption Algorithm aaroncohn C Programming Language 17 03-Mar-2004 15:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:13.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.