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:

Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
Introduction to Spring Method Security
Spring Boot - Zuul Proxy Server and Routing
How to Add a Single Element to a Stream
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Implement Find all Forward Edges in a Graph
Java 8 Collectors toMap
Map Serialization and Deserialization with Jackson
Java Program to Search for an Element in a Binary Search Tree
Java Program to Implement Coppersmith Freivald’s Algorithm
Java Program to Implement Jarvis Algorithm
Uploading MultipartFile with Spring RestTemplate
Java Program to Implement Multi-Threaded Version of Binary Search Tree
HttpClient 4 – Send Custom Cookie
Tạo chương trình Java đầu tiên sử dụng Eclipse
Một số ký tự đặc biệt trong Java
Spring Boot - Admin Server
Java Program to Implement Weight Balanced Tree
Java Program to Generate Random Numbers Using Multiply with Carry Method
Java Program to Implement Vector API
Spring Boot - Servlet Filter
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Stream Filter with Lambda Expression
Java Program to Implement Segment Tree
Java Program to Implement Sorted List
Java Program to Perform Arithmetic Operations on Numbers of Size
The Difference Between map() and flatMap()
Spring Data JPA @Modifying Annotation
Hướng dẫn Java Design Pattern – Strategy
Spring Security Authentication Provider