Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please modify the code below to solve this problem using Iterative deepening search. The input should be letters. The code import java.util.InputMismatchException; import java.util.Scanner; import
Please modify the code below to solve this problem using Iterative deepening search.
The input should be letters.
The code
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
public class IterativeDeepening
{
private Stack stack;
private int numberOfNodes;
private int depth;
private int maxDepth;
private boolean goalFound = false;
public IterativeDeepening()
{
stack = new Stack();
}
public void iterativeDeeping(int adjacencyMatrix[][], int destination)
{
numberOfNodes = adjacencyMatrix[1].length - 1;
while (!goalFound)
{
depthLimitedSearch(adjacencyMatrix, 1, destination);
maxDepth++;
}
System.out.println(" Goal Found at depth " + depth);
}
private void depthLimitedSearch(int adjacencyMatrix[][], int source, int goal)
{
int element, destination = 1;
int[] visited = new int[numberOfNodes + 1];
stack.push(source);
depth = 0;
System.out.println(" At Depth " + maxDepth);
System.out.print(source + "\t");
while (!stack.isEmpty())
{
element = stack.peek();
while (destination
{
if (depth
Problem Statement The example we will use is the case of a genetics professor searching for a name for her newborn baby boy - of course, it must only contain the letters D, N and A. The states in this search are strings of letters (but only Ds, Ns and As), and the initial state is an empty string. The goal state is "DAN" string. Also, the actions available are: (i) add a 'D' to an existing string (ii) add an 'N' to an existing string (iii) add an 'A' to an existing string. Think of the search graphically: by making each state a node in a tree and each action a branch, we can think of the search progressing as movement from node to node along branches in the tree and we say that a node in a search space has been expanded if the state that node represents has been visited and searched from add N add add A add D add N DD DN DA NDNNNA ADAN)AA DDD (DDN (DDA DND DNN DNA DAD {
if (adjacencyMatrix[element][destination] == 1)
{
stack.push(destination);
visited[destination] = 1;
System.out.print(destination + "\t");
depth++;
if (goal == destination)
{
goalFound = true;
return;
}
element = destination;
destination = 1;
continue;
}
} else
{
break;
}
destination++;
}
destination = stack.pop() + 1;
depth--;
}
}
public static void main(String... arg)
{
int number_of_nodes, destination;
Scanner scanner = null;
try
{
System.out.println("Enter the number of nodes in the graph");
scanner = new Scanner(System.in);
number_of_nodes = scanner.nextInt();
int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
System.out.println("Enter the adjacency matrix");
for (int i = 1; i
for (int j = 1; j
adjacency_matrix[i][j] = scanner.nextInt();
System.out.println("Enter the destination for the graph");
destination = scanner.nextInt();
IterativeDeepening iterativeDeepening = new IterativeDeepening();
iterativeDeepening.iterativeDeeping(adjacency_matrix, destination);
}catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input format");
}
scanner.close();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started