HardwareHeaven.com

HardwareHeaven.com

Looking for the skin chooser?
 
 
  • Home

  • Hardware reviews

  • Articles

  • News

  • Tools

  • Gaming at HardwareHeaven

  • Forums

 

Go Back   HardwareHeaven.com > Forums > Software / Tools > Programming, Coding, (Web)Design


Programming, Coding, (Web)Design Discuss all your programming or design needs with likeminded people.

Reply
 
Thread Tools
Old Mar 1, 2005, 05:40 AM   #1
DriverHeaven Newbie
 
Join Date: Nov 2004
Posts: 8
Rep Power: 0
lime is on a distinguished road

java backtracking problem

i'm working on a program that takes in courses and it's prerequisites and outputs the total number of solutions a student could take, like the number of legal sequences...

i'm having a little trouble in my backtracking method....basically i have a hashmap with the course as the key and the prerequisites as a linked list for the value. now in this class i have another hashmap that's the courses and initially they have a value of false (Boolean) i also made an array of the course numbers for indexing ease.... if you input just 3 courses with no prerequisites it works fine, i.e. 6 legal sequences. if i input "100" and "200 100" it works fine, 1 legal sequence.

i have a test set of data that ends up with 711 outcomes instead of the 3 that it should:
350 200
100
200 100
500 350 400
300 200
400 300

so here's my code...i was wondering if anyone could point anything out....

Code:
import java.util.*;
/**
 * @author Eric Isley
 *
 */
public class Courses {
	private int n,solutionCount,key,temp,course,x;
	private boolean current;
	private HashMap theMap,taken;
	private LinkedList tempList;
	private int[] courses;
	private Object[] tempArray;
	
	public Courses(int nIn, HashMap mapIn) {
		n = nIn;
		theMap = mapIn;
		taken = new HashMap(n);
		courses = new int[n];
		solutionCount = 0;
		findSolutions();
	}
	
	private void findSolutions() {
		// set all taken values to false to begin
		Set tempSet = theMap.keySet();
		Iterator iter = tempSet.iterator();
		while(iter.hasNext()){
			taken.put(iter.next(),new Boolean(false));
		}	 
		// put course numbers into an array
		tempArray = tempSet.toArray();
		solve(1);
	}
	
	private void solve(int k) {
		for(int i=k;i<=n;i++){
			x = ((Integer)tempArray[i-1]).intValue();
			if(checkPos(x)){
				taken.remove(new Integer(x));
			    taken.put(new Integer(x),new Boolean(true));
				if(k == n-1)
				    solutionCount++;
				else
					solve(k+1);
				taken.remove(new Integer(x));
			    taken.put(new Integer(x),new Boolean(false));			    
			} // end if
		} // end for
	} // end solve()
	
	private boolean checkPos(int x) {
		tempList = (LinkedList)theMap.get(new Integer(x));
		for(int i=0;i<tempList.size();i++) {
			temp = ((Integer)tempList.remove()).intValue();
		    current = ((Boolean)taken.get(new Integer (temp))).booleanValue();
			if(current == false)
				return false;
		} // end for
		return true;
	} // end checkPos

	// returns the count
	public int getCount(){
		return solutionCount;
	}
}
lime is offline   Reply With Quote


Reply

Thread Tools