Java Program to Implement Iterative Deepening

This Java program,Implements Iterative Deepening.Iterative deepening depth-first search(IDDFS) is a state space search strategy in which a depth-limited search is run repeatedly, increasing the depth limit with each iteration until it reaches , the depth of the shallowest goal state. IDDFS is equivalent to breadth-first search, but uses much less memory; on each iteration, it visits the nodes in the search tree in the same order as depth-first search, but the cumulative order in which nodes are first visited is effectively breadth-first.

Here is the source code of the Java program implements iterative deepening. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;
 
public class IterativeDeepening
{
    private Stack<Integer> stack;
    private int numberOfNodes;
    private int depth;
    private int maxDepth;
    private boolean goalFound = false;
 
    public IterativeDeepening()
    {
        stack = new Stack<Integer>();
    }
 
    public void iterativeDeeping(int adjacencyMatrix[][], int destination)
    {
        numberOfNodes = adjacencyMatrix[1].length - 1;
        while (!goalFound)
        {
            depthLimitedSearch(adjacencyMatrix, 1, destination);
            maxDepth++;
        }
        System.out.println("\nGoal 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("\nAt Depth " + maxDepth);
        System.out.print(source + "\t");
 
        while (!stack.isEmpty())
        {
            element = stack.peek();
            while (destination <= numberOfNodes)
            {
                if (depth < maxDepth)
                {
                    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 <= number_of_nodes; i++)
                for (int j = 1; j <= number_of_nodes; 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();
    }
}
$javac IterativeDeepening.java
$java IterativeDeepening
Enter the number of nodes in the graph
7
Enter the adjacency matrix
0 1 1 0 0 0 0 
0 0 0 1 1 0 0
0 0 0 0 0 1 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Enter the destination for the graph
7
 
At Depth 0
1	
At Depth 1
1	2	3	
At Depth 2
1	2	4	5	3	6	7	
Goal Found at depth 2

Related posts:

Returning Image/Media Data with Spring MVC
Java Program to Check if an UnDirected Graph is a Tree or Not Using DFS
HashSet trong Java hoạt động như thế nào?
Java Program to Implement Euclid GCD Algorithm
Java Program to Implement the Program Used in grep/egrep/fgrep
Overflow and Underflow in Java
Spring REST API + OAuth2 + Angular
Xử lý ngoại lệ đối với trường hợp ghi đè phương thức trong java
Tránh lỗi NullPointerException trong Java như thế nào?
SOAP Web service: Authentication trong JAX-WS
Guide to Spring @Autowired
Java Program to Implement Shell Sort
Java Program to Implement Depth-limited Search
Removing all Nulls from a List in Java
A Guide to LinkedHashMap in Java
Làm thế nào tạo instance của một class mà không gọi từ khóa new?
Convert Hex to ASCII in Java
Giới thiệu Java Service Provider Interface (SPI) – Tạo các ứng dụng Java dễ mở rộng
Java Program to Implement an Algorithm to Find the Global min Cut in a Graph
Java Program to Implement the Hill Cypher
Java Program to Find Nearest Neighbor Using Linear Search
Spring’s RequestBody and ResponseBody Annotations
Merging Two Maps with Java 8
Java Program to Implement ConcurrentHashMap API
Guide to the Fork/Join Framework in Java
Spring Boot Configuration with Jasypt
Recommended Package Structure of a Spring Boot Project
Deploy a Spring Boot App to Azure
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
@Order in Spring
Java Program to Perform Quick Sort on Large Number of Elements
Java Program to Implement Caesar Cypher