PDA

View Full Version : Java: String Permutation, Breadth/Depth Searching



Jeff Wheeler
November 6th, 2006, 02:41 PM
Ok, it's not often I post a question here, but I'm stumped by this easy problem.

I need to write a Java app which can find the shortest path between two words, changing only one letter at a time, using a given dictionary.

My teacher's stuff says to use recursion, which would be a depth-based algorithm; I think I need to use a breadth-based search algorithm instead, however, because it's looking for the shortest path.


package Lab09;

import java.io.*;
import java.util.*;

class Permutation {
HashMap<String, HashSet<String>> dict; // Contain set of similar words
TreeSet<String> path; // Order matters!

Permutation ( ) {
dict = new HashMap<String, HashSet<String>>();
path = new TreeSet<String>();
}

public void addToDict ( String word ) {
dict.put(word, new HashSet<String>());
for ( String potentiallySimilar: dict.keySet() ) {
// Add both direction connections
if ( isSimilar(word, potentiallySimilar)
&& !word.equals(potentiallySimilar) ) {
dict.get(word).add(potentiallySimilar);
if ( dict.containsKey(potentiallySimilar) ) {
dict.get(potentiallySimilar).add(word);
}
}
}
}

public void permutation ( String start, String target ) {
TreeSet<String> path = new TreeSet<String>();
path.add(start);
permutation(target, path);
}

/*
public void permutation ( String target, ) {
// Validate we're not already there
for ()
}
*/

public boolean isSimilar ( String first, String second ) {
if ( first.length() == second.length() ) {
int difference = 0;
for ( int i = 0; i < first.length(); i++ ) {
if ( first.charAt(i) != second.charAt(i) ) {
difference++;
}
}
return (difference <= 1);
} else {
return false;
}
}

public String toString ( ) {
String output = "Dict:\n";
for ( String word: dict.keySet() ) {
output += word + "\n";
for ( String connected: dict.get(word) ) {
output += "\t" + connected + "\n";
}
}
output += "\n\nPath:\n";
for ( String word: path ) {
output += word + "\n";
}
return output;
}
}

public class Lab09f {
public static void main ( String[] args ) throws IOException {
// Create new permutation object
Permutation permutation = new Permutation();
Scanner file = new Scanner(new File("Files/lab09f.dat"));
file.nextLine(); // Who cares about the number of following lines?
while ( file.hasNext() ) {
permutation.addToDict(file.next());
}
permutation.permutation("care", "wire");
System.out.println(permutation);
}
}

lab09f.dat contains,


12
care
cart
cure
dare
dart
hare
here
hire
sire
sure
were
wire


What am I missing?

simplistik
November 6th, 2006, 04:48 PM
OMG you're not omniscient? :D Just giving you a hard time. I'm actually really posting cause I was curious as to what this thing is supposed to do. Not that I can actually help, but it sounds interesting, and I need to plug a few holes in my brain. haha you said dict

Jeff Wheeler
November 6th, 2006, 06:51 PM
Yeah, I figured nobody would, for two reasons: 1) This isn't a Java forum (we don't even have a forum to put it in), and 2) I didn't explain it well.

I had to sneak it online without the teacher noticing. :P

nobody
November 6th, 2006, 06:56 PM
12
care
cart
cure
dare
dart
hare
here
hire
sire
sure
were
wire

Some of those change more than one letter. I don't get it.

Also I am no help because I don't know Java, but cheah.

Jeff Wheeler
November 6th, 2006, 06:58 PM
That's the dictionary of all possible words your path can take. You have to find the shortest path between any two of those words… if they all differed by one letter, it'd always be one step. :P

nobody
November 6th, 2006, 07:02 PM
Oh gotcha, I thought that was the progression. Don't mind me.

bwh2
November 9th, 2006, 12:44 AM
have you tried looking into the documentation for levenshtein distance: http://en.wikipedia.org/wiki/Levenshtein_distance

there's some java in there that might help.