Sample Code-Backtracking, Stacks and the Subset Sum Problem | CS 2402, Study notes of Data Structures and Algorithms

Material Type: Notes; Professor: Fuentes; Class: Data Structures; Subject: Computer Science; University: University of Texas - El Paso; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-p3z
koofers-user-p3z 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2402
Spring 2009
Sample Code – Backtracking, Stacks and the Subset Sum Problem
public class subSetSum{
// Solving Subset sum using a stack
// Programmed by Fuentes, Feb. 9, 2009
// Subset sum consists of finding a subset of mySet whose elements add up to goal
// It is a well-known NP-complete problem
public static void main(String[] args) {
myStack S = new myStack();
int [] mySet ={2,5,8,9,12,21,33};
int lastItem =mySet.length-1;
int goal = 61;
int x, newItem;
S.push(0);
int sumStack = mySet[0];
boolean done = false;
System.out.println("The Goal is : "+goal);
while (!done) {
// Uncomment to debug, show stack contents and sum of stack elements
S.printSet(mySet);
System.out.println("Sum Stack "+sumStack);
if (sumStack == goal){
done = true;
System.out.println("The Solution is: ");
S.printSet(mySet);
}
else
if ((sumStack>goal)|(S.peek()==lastItem)){ //Backtrack
System.out.println("Backtracking ");
x = S.pop();
sumStack -= mySet[x];
if (x==lastItem){ // No more elements to add
if(S.isEmpty()){
done = true;
System.out.println("No Solution");
}
else{
x = S.pop();
sumStack -= mySet[x];
}
}
if (!done){
S.push(x+1);
sumStack += mySet[x+1];
}
}
else
if (S.peek()<lastItem){ // Add more elements to stack
System.out.println("Adding more elements ");
pf2

Partial preview of the text

Download Sample Code-Backtracking, Stacks and the Subset Sum Problem | CS 2402 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 2402

Spring 2009

Sample Code – Backtracking, Stacks and the Subset Sum Problem

public class subSetSum{ // Solving Subset sum using a stack // Programmed by Fuentes, Feb. 9, 2009 // Subset sum consists of finding a subset of mySet whose elements add up to goal // It is a well-known NP-complete problem public static void main(String[] args) { myStack S = new myStack(); int [] mySet ={2,5,8,9,12,21,33}; int lastItem =mySet.length-1; int goal = 61; int x, newItem; S.push(0); int sumStack = mySet[0]; boolean done = false; System.out.println("The Goal is : "+goal); while (!done) { // Uncomment to debug, show stack contents and sum of stack elements S.printSet(mySet); System.out.println("Sum Stack "+sumStack); if (sumStack == goal){ done = true; System.out.println("The Solution is: "); S.printSet(mySet); } else if ((sumStack>goal)|(S.peek()==lastItem)){ //Backtrack System.out.println("Backtracking "); x = S.pop(); sumStack -= mySet[x]; if (x==lastItem){ // No more elements to add if(S.isEmpty()){ done = true; System.out.println("No Solution"); } else{ x = S.pop(); sumStack -= mySet[x]; } } if (!done){ S.push(x+1); sumStack += mySet[x+1]; } } else if (S.peek()<lastItem){ // Add more elements to stack System.out.println("Adding more elements ");

newItem = S.peek()+1; S.push(newItem); sumStack += mySet[newItem]; } else{ // No more elements to add done = true; System.out.println("No Solution"); } } } } //Stack class to be used with subsetSum // Programmed by Fuentes, Feb. 9 2009 public class myStack{ private int [] items; private int top; public myStack(){ items = new int[20]; top=-1; } public void push(int n){ top++; items[top]=n; } public int pop(){ top--; return items[top+1]; } public int peek(){ return items[top]; } public boolean isEmpty(){ return top==-1; } public void printStack(){ for (int i= 0;i <=top;i++) System.out.print(items[i]+" "); System.out.println(); } public void printSet(int [] S){ System.out.print("{"); for (int i= 0;i <=top;i++) System.out.print(S[items[i]]+" "); System.out.println("}"); } }