I need java code that chooses the shortest path between two words with each step only changing one letter. I could only implement code that returns with the shortest path.
Problem Restatement
In this question, the find function (remark: the name find does not seem to
be appropriate. Better use findPath, IMHO) returns a list of words from a
dictionary of words stored in a string list. A sample dictionary text file
for the example below can be found in the question.
The findPath function takes three arguments:
A starting word,
a target word,
a dictionary (a list of words).
The find function tries to 'connect' the starting word to the target
word through a finite 'path' made of different 'steps', i.e. different
words all belonging to the dictionary, but each one differing only by one
letter from the previous word.
For example,
>>> find('hide', 'seek', words)
['hide', 'bide', 'bids', 'beds', 'bees', 'sees', 'seek']
This is a 6-step Path made of 7 words.
The current implementation yields only one path amongst all possible shortest
paths connecting two words, or 'None' if no such path exists. The question is
then:
How should I modify it to yield instead a list of all possible paths of a
certain length, given that no words should be connected only once in
a valid path?
Analysis of the current algorithm
The algorithm will be described by a graph analogy.
The algorithm builds the 'visited' dictionary as a tree that originates with
'None', goes to the 'start' word, then branches from the 'start' word to every
word that can be connected to it.
The algorithm then continues by examining every new single added node ('curr'
string), in the order it was added to the tree. The algorithm keeps track of
the words that have been included in the tree with the 'queue' list, so that
if a word is met again, it will not be considered again.
Once the 'target' word is added to the tree, nothing happens, and this is a
small pity. One must wait that the 'target' word is examined, to
detect that a path has been found.
When the 'target' word is examined, then the path is returned in a quick
and elegant way by simply browsing back the tree to the root
if curr == target:
path = []
while curr:
path.insert(0, curr)
curr = visited[curr]
return path
Current algorithm properties
In the current form and with the sample dictionary, the tree starting with
'None->lead' has the following branches:
bead dead head mead read load lend leud lewd leaf leak leal lean leap lear leas
Please note that the current algorithm returns a path that is a function
of the order in which the words are considered, basically the order of
the words in the dictionary and the order of the given arguments. Therefore,
as stated in the comment, the current algorithm is able to find one
or two shortest path, since we can invert the order of the arguments, e.g.
>>> find("hide", "seek", words))
['hide', 'bide', 'bids', 'beds', 'bees', 'sees', 'seek']
but
>>> find("seek", "hide", words)
['seek', 'meek', 'merk', 'mirk', 'mire', 'hire', 'hide']
so that hide->hire->mire->mirk->merk->meek->seek can be found immediately
as an alternate shortest path.
Problem analysis
There are two levels of innovation in the question you ask. Firstly, the
algorithm should cover all possible shortest paths, not only the first path
being found. Secondly, the algorithm should take an additional number 'n'
and yield back all possible paths of length 'n'.
Covering all possible shortest paths
The shortest possible path, if it exists, has at least so many
number of steps as letters differing in the two words.
Suppose that you want to connect 'lead' to 'heal'. There are two letters
difference and a possible shortest path is 'lead->head->heal'. If 'head' was
missing in the dictionary and 'leal' was present, then an alternate shortest
path would be 'lead->leal->heal'. If both 'head' and 'leal' were missing, the
shortest path would be longer than 2.
In general, if you scramble your dictionary (change the order
of the words), you should be able to obtain a different shortest path
with the same algorithm. But to be sure to obtain all possible paths,
you should perform an extremely high number of scramble operations
and that looks to me inefficient.
Covering all possible paths
Suppose that you want to connect 'lead' to 'heal'. A possible shortest
path is 'lead->head->heal'.
If you want to consider all possible paths,
you must consider also 'lead->dead->head->heal', 'lead->read->head->heal',
'lead->dead->read->head->heal', etc., and in some cases some paths that
have the same content of words as other paths, but with partly permutated order as for
'lead->read->dead->head->heal'
Conclusions
It is evident that the current tree structure is unfit to describe all of
the possible paths, because in the tree structure a single node is
asymmetrically connected on one side with its parent, and on the other
side with all of his children. This asymmetry prevents any possibility
to consider all possible paths, unless you are ready to duplicate the
tree a very large number of times (but I would not suggest doing so), to cover
all possibly existing trees.
Suggestions
1) Divide your problem in two different steps, the first 'covering all
possible shortest paths', the second 'covering all possible paths of length n'.
Attack the easiest problem and solve it considering the second problem, i.e.
do not accept a solution that would be incompatible with the second problem.
2) Reduce the size of your dictionary to a dozen words that you select such to
offer you the possibility to test quickly your algorithm. e.g.
heal
head
leal
real
seal
lead
read
some
more
word
allows you to test if you can find or not the two possible shortest paths between
'lead' and 'heal'.
3) Change of data structure. Leave behind the visited dictionary and its tree,
and use instead another data structure. I would suggest to use a list of lists
covering all possible shortest paths, i.e. in the example above there could
be one different list for each possible path
[lead, leal]
[lead, head]
[lead, read]
[lead, leal, real]
[lead, leal, seal]
[lead, leal, heal] *solution*
[lead, head, read]
[lead, head, heal] *solution*
4) Build a solid testbench : determine by yourself what is the
expected output for a given problem and work on the code until your test
succeed. Only then, complexify your test and debug your code again.
These are the specs for the program I need to complete. Could someone please help me!! I really need to get this program done! Will take any help/advice I can get! Thank you all so much!
You must create two programs named Board and ConFour that have the
following criteria:
1) Proper Introduction
2) Comments that accurately describe the program features
3) Board should have one attribute; a two dimensional array of
character values representing the Connect Four game board. Be sure
to include a constructor (without parameters) that instantiates the
2D-array with 6 rows and 7 columns
4) Board should contain at least four methods. The first method should
be setBoard() which adds an empty character value to every position in the board. The second method, setPosition(), should place the character representing a player (X or O) in the column of their choosing. The third method named checkWinner() should check the board to see if there are four of the same character (X or O) in a row, column or either diagonal. Lastly, printBoard(), should print the contents of the board.
5) ConFour should represent the game play. Have the user(s) enter
START to start the game (they should be able to continuously play after each game)
6) Start each turn by printing the board followed by asking the user to
enter the column they want (be sure to alternate players). If the user enters an incorrect column number, make them re-enter. First player to get four in a row, column or either diagonal is the winner.
Here is a little bit of my thoughts:
To start off, try to make algorithms that check for 4 Xs/Os in a row, which should be 4 for each player. You could also just make 4 algorithms that require you to input the number you are checking for. The directions you need to check are horizontal (check array[i][x+1], where i is the constant in the for loop and x is the number you find to be X or O), vertical (check array[x+1][i] ), right-facing diagonal (check array[i+1][x+1] ), and left-facing diagonal (check array[i-1][x-1].
To print the board, just use 2 for loops that print the values of the array.
For the intro, use a bunch of System.out.println() statements.
The entering of coins is the weird part. You have to create height variables (Or a height function) that stores/checks the height of the coins, then places it on top/next to the other coin. Then check if anyone wins and move on the next player. Keep repeating it until someone wins. Warning: Do not use a while loop. They can't check more than one boolean at a time (but you could put a bunch of if(check) {
boolean itsalltrue = true;
}s, too.
Well, that's all that I can think of (I deliberately did not write the code because I would like you write your own). Enjoy!
Is this for a class? Did you literally just copy and paste the assignment? Try spending some time looking through the notes provided for you, or search more specific questions here. Here's an example of a similar question with code:
Simple 2d array java game
I´ m trying to implement the A* search algorithm in Java, and I have a question:
How many times I need to run a A* loop, until there is clearly no existing path?
For Example: If i have a for-loop; how long should "i" increase?
When you have explored all nodes that you can reach with A*, without finding your goal, then you can assume that there is no path.
I think you are thinking of A* the wrong way.
You run it until there are no more nodes to search. If you havent reached your target by then there is no path.
Psuedo:
//Our list of still open nodes
List<Node> nodesToSearch = new List<Node>();
while (nodesToSearch.Count > 0)
{
//SearchNode, if its our target we are done
//Add reachable neighbours to the list of nodes to search. So next iteration we will continue on searching those
//Remove current node.. since its searched
}
//If we end up here without a target there is no path.
//That means we have searched all nodes and their neighbours etc etc etc. Without reaching the target.
I have a method "connection(int n)" which gives me all the cells number that have relation with cell number "n" now I want a method which gives me all the routes with a specific length "myLength" that start from cell number "start" and just in one direction (as it's usual) I mean we are not allowed to pass some cells more than one time
thanks in advance for your help
P.S. I can't use map tools, graph tools,... with basic tools please
You are looking for BFS.
Model your problem as a graph G = (V,E) such that V = {1,...,n} [all possible values] and E = { (u,v) | connection(u) returns v } [there is a connection between u and v using your connection() method]
In addition to the standard BFS, you will need to add another stop condition when you reached the limited length.
EDIT:
Note that this solution assumes you are looking for a path up-to length, and not exactly length.
BFS doesn't work here for the counter example of a clique if you want exactly of length.
To get all vertices that have a simple path of exactly length - You will probably need a DFS that avoids loops [can be done by maintaining a set that is modified each iteration], but can explore each vertex more then once.
I'm stuck on my assignment and wanted some pointers on an algorithm.
I am presented with text files that represent different images. blank space is where no pixels are on and '&' represents an on pixel.
The aim is i am given a 100 x 100 image text file to analyse and work out the probability that the object is there and then the co-ordinate of where it is on the file.
I know that i have to use character analysis of some sort but i feel that i have to check for example 10x10 grids at a time, analyse how many pixels are on and work out the certainty that the object is there. (This is because more or less pixels can be on and the object still present)
Thanks for your help.
I think I understand your question correctly. One thing that will change the answer is whether or not you know the object beforehand. If you are looking for an arbitrary pattern, it is somewhat more difficult, but still feasible. To find an object that you know what it will look like will come down to nested for loops and a solid understanding of 2D arrays. You can pull in each line of the text file and look for an '&'. If it finds one, it begins looking for the rest of the pattern based on the location relative to that first '&'.
For example, if you are looking for a diagonal line from top left to bottom right, you would continue along until you came to the first '&'. After that, you would look at the cell one column over and 1 row down. If that is an ampersand as well, you know that you have a diagonal line. If not, just keep going along after the first '&'.
for (int c = 0; c < textArray.length; c++)
{
for (int i = 0; i < textArray[c].length; i++)
{
Look at the character
If it is '&'
Look for the next character and so forth
If the pattern is there
return true
}
}
See if that helps get your algorithm rolling. You will need to make sure to check for legal bounds in your arrays in order to combat out of bounds exceptions.