0

I am writing a program that is supposed to read a file of words and then find the shortest from one word to another by going from the startword to an adjacent word that will differ with one letter, simple example: Startword: dog endword: for could be: " dog -> fog -> for".

My idea is to do a BFS in a adjacent list. Everything is working in my code except for my Graph (adjacent list). The problem is that I want to hash the words to an index in the graph, but for that the endsize of the list needs to be known.

I start by adding a vector which will contain the entire wordlist, I use the size of this vector to set size of arraylist and then I iterate the vector to hash the words into the arraylist.

My adjacent list is of type:

    ArrayList<LinkedList<String>> adj_list;

Here's how I use it:

    public void adda(Vector<String> v){
    M = v.size();
    adj_list = new ArrayList<LinkedList<String>>(M);
    for (int i = 0; i < M; i++){
        add(v.get(i));
    }

    void add (String word){
    int z = hash(word);
    for (int i = z; i < M; i++){
        if(adj_list.get(i)== null){
            z=i;
            adj_list.add(new LinkedList<String>());
            break;

Hashfunction:

   public static int hash(String key) {
        int i;
        int v = 0;
        for (i = 0; i < key.length(); i++) {
            v += key.charAt(i);
        }
        return v % M;

I have a strong feeling that just the idea of adding a vector to take the elements from it and add to an arraylist is stupid enough. I cannot come up with any better, working, ideas though (and the one I have right now isn't working either). Should I use an adjacency list at all? Is it a good idea to hash the words or are there any better ways? Does anyone sort of understand what type of program I am trying to create and have any idea that might be helpful?

zkalman
  • 17
  • 6
  • why don't you just create a node like : class Node { String key } and then the adjacency list is List> adjList = new ArrayList<>() , btw how are you constructing the adjList of a given word? You have all the words that differ in one letter to that given word ? But then there are so many words that might differ in one letter e.g. the word "bog" is nearest to cog, dog, log etc. and also nearest to blog, box, big etc. Do you add all those words as adjacent? – SomeDude Oct 09 '18 at 23:11
  • Nodes is a better idea for sure, thanks. I take the start and endword. Then create new words by changing a character from the startword, if this new word exists in the wordlist I add it as adjacent to the startword, then I do the same procedure, starting from the adjacent word, and so on until I have found a chain of words that ends up with the endword. Blog wouldn't be a possible word in that string since the length of the words must be the same. Don't think this is the best way.. for example I would like to hash the words, which wont work with this datastructure as far as I can see.@SomeDude – zkalman Oct 10 '18 at 10:24

0 Answers0