Go Back   DriverHeaven.net > Forums > Software / Tools > Programming, Coding, (Web)Design

Notices

Reply
 
LinkBack Thread Tools Display Modes
Old Mar 1, 2005, 12:40 AM   #1 (permalink)
lime
DriverHeaven Newbie
 
Join Date: Oct 2004
Posts: 8
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

Bookmarks

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump




 

 
Powered by: vBulletin
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0
Artwork by Allan 'Zardon' Campbell, vBulletin implementation by Craig '5320' Humphreys based on original artwork by Ratchet.

All times are GMT -5. The time now is 06:10 PM. Copyright ©2008 DriverHeaven.net