Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Memoization (dyanamic programming) for edit distance recursion

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Memoization (dyanamic programming) for edit distance recursion

    Hey everyone, I am new here. Had a question about my code. I am trying to implement memoization into this program that calculates the levenshtein edit distance of two words. I have the program working correctly without the memozation; however, am otherwise having trouble. Anyone out there spy something I am missing here?

    import java.util.ArrayList;
     
     
    public class EditDistance {
    	public static int CALLS;
        public static boolean MEMOIZING;
        public static ArrayList<String> foundCombination = new ArrayList<String>();
        public static ArrayList<Integer> foundValue = new ArrayList<Integer>();
     
        public static int distance(String s1, String s2){
     
        	CALLS++;
        	//System.out.println(CALLS);
        	//System.out.println(s1 + ", " + s2);
        	//System.out.println(index1 + ", " + index2);
     
        	int[] minCheck = new int[3];
        	int equalChars = 0;
        	int findMin = 0;
        	int tempIndex = 0;
     
     
     
     
    		if((s1.length() != 0) && (s2.length() != 0)){
     
    			if(MEMOIZING == true && foundCombination.contains(Character.toString(s1.charAt(s1.length()-1)) + Character.toString(s2.charAt(s2.length()-1)))){
    				tempIndex = foundCombination.indexOf(Character.toString(s1.charAt(s1.length()-1)) + Character.toString(s2.charAt(s2.length()-1)));
    				return foundValue.get(tempIndex);
    			}
     
    			if(!(s1.substring(s1.length()-1, s1.length()).equals(s2.substring(s2.length()-1, s2.length())))){
    				equalChars = 1;
    			}
     
    			minCheck[0] = distance(s1, s2.substring(0, s2.length()-1)) + 1;
    			if(MEMOIZING == true){
    				foundCombination.add(Character.toString(s1.charAt(s1.length()-1)) + Character.toString(s2.charAt(s2.length()-1)));
    				foundValue.add(minCheck[0]);
    			}
    			minCheck[1] = distance(s1.substring(0, s1.length()-1), s2) + 1;
    			if(MEMOIZING == true){
    				foundCombination.add(Character.toString(s1.charAt(s1.length()-1)) + Character.toString(s2.charAt(s2.length()-1)));
    				foundValue.add(minCheck[1]);
    			}
    			minCheck[2] = distance(s1.substring(0, s1.length()-1), s2.substring(0, s2.length()-1)) + equalChars;
    			if(MEMOIZING == true){
    				foundCombination.add(Character.toString(s1.charAt(s1.length()-1)) +Character.toString(s2.charAt(s2.length()-1)));
    				foundValue.add(minCheck[2]);
    			}
    			findMin = minCheck[0];
     
    			for(int i = 1; i < minCheck.length; i++){
    				if(minCheck[i] < findMin){
    					findMin = minCheck[i];
    				}
    			}
     
    			return findMin;
     
    		}
    		else{//returning the bigger string length
     
    			if(s1.length() > s2.length()){
    				return s1.length();
    			}else{return s2.length();}
     
    		}
     
     
     
        }
    }



    Here is the Driver for the program

    public class tester {
     
    	public static void main(String[] args){
    	     System.out.println( "--------------------------------------------------") ;
    	     EditDistance.MEMOIZING = false ;
    	     EditDistance.CALLS = 0 ;
    	     System.out.println( EditDistance.distance("socks", "shoes") ) ;
    	     System.out.println( EditDistance.CALLS ) ;
    	     System.out.println( "--------------------------------------------------") ;
    	     EditDistance.MEMOIZING = true ;
    	     EditDistance.CALLS = 0 ;
    	     System.out.println( EditDistance.distance("socks","shoes") ) ;
    	     System.out.println( EditDistance.CALLS ) ;
    	     System.out.println(EditDistance.foundCombination);
    	     System.out.println(EditDistance.foundValue);
    	}
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Memoization (dyanamic programming) for edit distance recursion

    am otherwise having trouble
    What exactly is the trouble? Different output than you expect (please provide expected vs. obtained output)?

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Memoization (dyanamic programming) for edit distance recursion

    If you run the tester class, you see the edit distance for the two words is three and done in roughly 2500 calls. When memorization is flipped to true, you should get the same edit distance value, just done in significantly fewer recursive calls

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Memoization (dyanamic programming) for edit distance recursion

    Quote Originally Posted by rph12 View Post
    If you run the tester class, you see the edit distance for the two words is three and done in roughly 2500 calls. When memorization is flipped to true, you should get the same edit distance value, just done in significantly fewer recursive calls
    So what's the trouble then? Is this not what you expect?

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Memoization (dyanamic programming) for edit distance recursion

    You should expect the same edit distance with fewer calls. You get the fewer calls, just not the correct edit distance.

Similar Threads

  1. JFrame Edit
    By n00b123 in forum AWT / Java Swing
    Replies: 1
    Last Post: May 19th, 2010, 08:23 PM
  2. Loading Images and Edit it
    By shadihrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 25th, 2010, 08:39 PM
  3. Eclipse: How to use/edit shorcut keys.
    By Truffy in forum Java JDK & IDE Tutorials
    Replies: 3
    Last Post: December 3rd, 2009, 12:51 PM
  4. Edit ArrayList Object
    By frankycool in forum Collections and Generics
    Replies: 12
    Last Post: November 16th, 2009, 12:31 AM
  5. Struts combobox edit
    By kalees in forum Web Frameworks
    Replies: 2
    Last Post: November 1st, 2009, 12:27 AM