Reputation: 37
I am trying to write a boggle solve problem in JavaScript using a depth first search algorithm implementing a trie. When building the trie, I receive no errors; however when the trie reaches the dfs algorithm it gives me "Type:Error, cannot use 'in' operator to search for 'd' in undefined.
I cannot figure out whether the problem is in my generate_trie function or my depth_first_search algorithm. You can find all the code for the program below.
// Sample Boggle Dictionary
var boggle_dxctionary = ['apple', 'pickle', 'side',
'sick', 'mood', 'cat',
'cats', 'man', 'super',
'antman', 'godzilla', 'dog',
'dot', 'sine', 'cos',
'signal', 'bitcoin', 'cool',
'kick', 'zapper'
];
// Sample Boggle Board
var boggle_board = [
['c', 'n', 't', ],
['d', 'a', 't', ],
['o', 'o', 'm', ],
];
var column_length = boggle_board[0].length;
var row_length = boggle_board.length;
var trie_node = {
'valid': false,
'next': {}
};
var neighbors_delta = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
function generate_trie(word, node) {
if (!(word)) {
return;
}
if ((word[0] in node) == false) {
node[word[0]] = {
'valid': (word.length == 1),
'next': {}
};
}
generate_trie(word.slice(1, ), node[word[0]]);
}
function build_trie(boggle_dxct, trie) {
for (word = 0; word < boggle_dxct.length; word++) {
generate_trie(boggle_dxct[word], trie);
}
return trie;
}
function get_neighbors(row, column) {
var neighbors = [];
for (neighbor = 0; neighbor < neighbors_delta.length; neighbor++) {
new_row = row + neighbors_delta[neighbor][0];
new_column = column + neighbors_delta[neighbor][1];
if (new_row >= row_length || new_column >= column_length || new_row < 0 || new_column < 0) {
continue;
}
neighbors.push([new_row, new_column]);
}
return neighbors;
}
function depth_first_search(row, column, visited, trie, current_word, found_words, board) {
var row_column_pair = [row, column];
console.log(row_column_pair);
if (row_column_pair in visited) {
return;
}
letter = board[row][column];
visited.push(row_column_pair);
console.log("Up to here is good3");
console.log(letter);
if (letter in trie) {
current_word = current_word + letter;
console.log("Up to here is good4");
if (trie[letter]['valid']) {
found_words.push(current_word);
}
neighbors = get_neighbors(row, column);
for (n = 0; n < neighbors.length; n++) {
console.log("Up to here is good5");
depth_first_search(neighbors[n][0], neighbors[n][1], visited.slice(0, visited.length), trie[letter], current_word, found_words, board);
console.log("Up to here is good6");
}
}
}
function main(trie_node, board) {
trie_node = build_trie(boggle_dxctionary, trie_node);
found_words = [];
for (r = 0; r < row_length; r++) {
console.log("Up to here is good1");
for (c = 0; c < column_length; c++) {
var visited = [];
var current_word = '';
depth_first_search(r, c, visited, trie_node, current_word, found_words, board);
console.log("Up to here is good2");
}
}
console.log(found_words);
}
main(trie_node,boggle_board);
Upvotes: 0
Views: 284
Reputation: 781130
The problem is that you didn't declare letter
as a local variable in depth_first_search
. So when it calls itself in the for
loop, the recursive call changes the value of letter
, and trie[letter]
doesn't find anything.
Always use local variables unless you have a specific reason not to. I've added var
declarations throughout the code below.
// Sample Boggle Dictionary
var boggle_dxctionary = ['apple', 'pickle', 'side',
'sick', 'mood', 'cat',
'cats', 'man', 'super',
'antman', 'godzilla', 'dog',
'dot', 'sine', 'cos',
'signal', 'bitcoin', 'cool',
'kick', 'zapper'
];
// Sample Boggle Board
var boggle_board = [
['c', 'n', 't', ],
['d', 'a', 't', ],
['o', 'o', 'm', ],
];
var column_length = boggle_board[0].length;
var row_length = boggle_board.length;
var trie_node = {
'valid': false,
'next': {}
};
var neighbors_delta = [
[-1, -1],
[-1, 0],
[-1, 1],
[0, -1],
[0, 1],
[1, -1],
[1, 0],
[1, 1],
];
function generate_trie(word, node) {
if (!(word)) {
return;
}
if ((word[0] in node) == false) {
node[word[0]] = {
'valid': (word.length == 1),
'next': {}
};
}
generate_trie(word.slice(1, ), node[word[0]]);
}
function build_trie(boggle_dxct, trie) {
for (var word = 0; word < boggle_dxct.length; word++) {
generate_trie(boggle_dxct[word], trie);
}
return trie;
}
function get_neighbors(row, column) {
var neighbors = [];
for (var neighbor = 0; neighbor < neighbors_delta.length; neighbor++) {
var new_row = row + neighbors_delta[neighbor][0];
var new_column = column + neighbors_delta[neighbor][1];
if (new_row >= row_length || new_column >= column_length || new_row < 0 || new_column < 0) {
continue;
}
neighbors.push([new_row, new_column]);
}
return neighbors;
}
function depth_first_search(row, column, visited, trie, current_word, found_words, board) {
var row_column_pair = [row, column];
console.log(row_column_pair);
if (row_column_pair in visited) {
return;
}
var letter = board[row][column];
visited.push(row_column_pair);
console.log("Up to here is good3");
console.log(letter);
if (letter in trie) {
current_word = current_word + letter;
console.log("Up to here is good4");
if (trie[letter]['valid']) {
found_words.push(current_word);
}
var neighbors = get_neighbors(row, column);
for (n = 0; n < neighbors.length; n++) {
console.log("Up to here is good5");
depth_first_search(neighbors[n][0], neighbors[n][1], visited.slice(0, visited.length), trie[letter], current_word, found_words, board);
console.log("Up to here is good6");
}
}
}
function main(trie_node, board) {
trie_node = build_trie(boggle_dxctionary, trie_node);
var found_words = [];
for (r = 0; r < row_length; r++) {
console.log("Up to here is good1");
for (c = 0; c < column_length; c++) {
var visited = [];
var current_word = '';
depth_first_search(r, c, visited, trie_node, current_word, found_words, board);
console.log("Up to here is good2");
}
}
console.log(found_words);
}
main(trie_node, boggle_board);
Upvotes: 1