SHARE
TWEET

Untitled

a guest Mar 24th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //package Artificial_Intelligence.IDS;
  2.  
  3. import java.util.InputMismatchException;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class IterativeDeepening {
  8.  
  9.     private Stack stack;
  10.     private int numberOfNodes;
  11.     private int depth;
  12.     private int maxDepth;
  13.     private boolean goalFound = false;
  14.  
  15.     public IterativeDeepening()
  16.     {
  17.         stack = new Stack();
  18.     }
  19.  
  20.     public void iterativeDeeping(int adjacencyMatrix[][], int destination)
  21.     {
  22.         numberOfNodes = adjacencyMatrix[1].length - 1;
  23.         while (!goalFound)
  24.         {
  25.             depthLimitedSearch(adjacencyMatrix, 1, destination);
  26.             maxDepth++;
  27.         }
  28.         System.out.println("\nGoal Found at depth " + depth);
  29.     }
  30.  
  31.     private void depthLimitedSearch(int adjacencyMatrix[][], int source, int goal)
  32.     {
  33.         int element, destination = 1;
  34.         int[] visited = new int[numberOfNodes + 1];
  35.         stack.push(source);
  36.         depth = 0;
  37.         System.out.println("\nAt Depth " + maxDepth);
  38.         System.out.print(source + "\t");
  39.  
  40.         while (!stack.isEmpty())
  41.         {
  42.             element = (int) stack.peek();
  43.             while (destination <= numberOfNodes)
  44.             {
  45.                 if (depth < maxDepth)
  46.                 {
  47.                     if (adjacencyMatrix[element][destination] == 1)
  48.                     {
  49.                         stack.push(destination);
  50.                         visited[destination] = 1;
  51.                         System.out.print(destination + "\t");
  52.                         depth++;
  53.                         if (goal == destination)
  54.                         {
  55.                             goalFound = true;
  56.                             return;
  57.                         }
  58.                         element = destination;
  59.                         destination = 1;
  60.                         continue;
  61.                     }
  62.                 } else
  63.                 {
  64.                     break;
  65.                 }
  66.                 destination++;
  67.             }
  68.             destination = (int) stack.pop() + 1;
  69.             depth--;
  70.         }
  71.     }
  72.  
  73.     public static void main(String... arg)
  74.     {
  75.         int number_of_nodes, destination;
  76.         Scanner scanner = null;
  77.         try
  78.         {
  79.             System.out.println("Enter the number of nodes in the graph");
  80.             scanner = new Scanner(System.in);
  81.             number_of_nodes = scanner.nextInt();
  82.  
  83.             int adjacency_matrix[][] = new int[number_of_nodes + 1][number_of_nodes + 1];
  84.             System.out.println("Enter the adjacency matrix");
  85.             for (int i = 1; i <= number_of_nodes; i++)
  86.                 for (int j = 1; j <= number_of_nodes; j++)
  87.                     adjacency_matrix[i][j] = scanner.nextInt();
  88.  
  89.             System.out.println("Enter the destination for the graph");
  90.             destination = scanner.nextInt();
  91.  
  92.             IterativeDeepening iterativeDeepening = new IterativeDeepening();
  93.             iterativeDeepening.iterativeDeeping(adjacency_matrix, destination);
  94.         }catch (InputMismatchException inputMismatch)
  95.         {
  96.             System.out.println("Wrong Input format");
  97.         }
  98.         scanner.close();
  99.     }
  100. }
  101.  
  102.  
  103. /*
  104. *
  105.  
  106. Enter the number of nodes in the graph
  107. 7
  108. Enter the adjacency matrix
  109. 0 1 1 0 0 0 0
  110. 0 0 0 1 1 0 0
  111. 0 0 0 0 0 1 1
  112. 0 0 0 0 0 0 0
  113. 0 0 0 0 0 0 0
  114. 0 0 0 0 0 0 0
  115. 0 0 0 0 0 0 0
  116. Enter the destination for the graph
  117. 7
  118.  
  119. At Depth 0
  120. 1
  121. At Depth 1
  122. 1   2   3
  123. At Depth 2
  124. 1   2   4   5   3   6   7
  125. Goal Found at depth 2
  126.  
  127. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top