Java Program to Implement Best First Search

This Java program,implements Best-First Search.Best-first search is a search algorithm which explores a graph by expanding the most promising node chosen according to a specified rule.
Judea Pearl described best-first search as estimating the promise of node n by a “heuristic evaluation function which, in general, may depend on the description of n, the description of the goal, the information gathered by the search up to that point, and most important, on any extra knowledge about the problem domain.

Here is the source code of the Java program to implements Best-First Search. The Java program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. import java.util.Comparator;
  2. import java.util.InputMismatchException;
  3. import java.util.PriorityQueue;
  4. import java.util.Scanner;
  5.  
  6. public class BestFirstSearch
  7. {
  8.     private PriorityQueue<Vertex> priorityQueue;
  9.     private int heuristicvalues[];
  10.     private int numberOfNodes;
  11.  
  12.     public static final int MAX_VALUE = 999;
  13.  
  14.     public BestFirstSearch(int numberOfNodes)
  15.     {
  16.         this.numberOfNodes = numberOfNodes;
  17.         this.priorityQueue = new PriorityQueue<Vertex>(this.numberOfNodes,
  18.         new Vertex());
  19.     }
  20.  
  21.     public void bestFirstSearch(int adjacencyMatrix[][], int[] heuristicvalues,int source)
  22.     {
  23.         int evaluationNode;
  24.         int destinationNode;
  25.         int visited[] = new int [numberOfNodes + 1];
  26.         this.heuristicvalues = heuristicvalues;
  27.  
  28.         priorityQueue.add(new Vertex(source, this.heuristicvalues[source]));
  29.         visited[source] = 1;
  30.  
  31.         while (!priorityQueue.isEmpty())
  32.         {
  33.             evaluationNode = getNodeWithMinimumHeuristicValue();
  34.             destinationNode = 1;
  35.  
  36.             System.out.print(evaluationNode + "\t");			
  37.             while (destinationNode <= numberOfNodes)
  38.             {
  39.                 Vertex vertex = new Vertex(destinationNode,this.heuristicvalues[destinationNode]);
  40.                 if ((adjacencyMatrix[evaluationNode][destinationNode] != MAX_VALUE 			                                           
  41.                       && evaluationNode != destinationNode)&& visited[destinationNode] == 0)
  42.                 {
  43.                     priorityQueue.add(vertex);
  44.                     visited[destinationNode] = 1;
  45.                 }
  46.                 destinationNode++;
  47.             }
  48.         }
  49.     }
  50.  
  51.     private int getNodeWithMinimumHeuristicValue()
  52.     {
  53.         Vertex vertex = priorityQueue.remove();
  54.         return vertex.node;
  55.     }
  56.  
  57.     public static void main(String... arg)
  58.     {
  59.         int adjacency_matrix[][];
  60.         int number_of_vertices;
  61.         int source = 0;
  62.         int heuristicvalues[];
  63.  
  64.         Scanner scan = new Scanner(System.in);
  65.         try
  66.         {
  67.             System.out.println("Enter the number of vertices");
  68.             number_of_vertices = scan.nextInt();
  69.             adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
  70.             heuristicvalues = new int[number_of_vertices + 1];
  71.  
  72.             System.out.println("Enter the Weighted Matrix for the graph");
  73.             for (int i = 1; i <= number_of_vertices; i++)
  74.             {
  75.                 for (int j = 1; j <= number_of_vertices; j++)
  76.                 {
  77.                     adjacency_matrix[i][j] = scan.nextInt();
  78.                     if (i == j)
  79.                     {
  80.                         adjacency_matrix[i][j] = 0;
  81.                         continue;
  82.                     }
  83.                     if (adjacency_matrix[i][j] == 0)
  84.                     {
  85.                         adjacency_matrix[i][j] = MAX_VALUE;
  86.                     }
  87.                 }
  88.             }
  89.             for (int i = 1; i <= number_of_vertices; i++)
  90.             {
  91.                 for (int j = 1; j <= number_of_vertices; j++)
  92.                 {
  93.                     if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
  94.                     {
  95.                         adjacency_matrix[j][i] = 1;
  96.                     }
  97.                 }
  98.             }
  99.  
  100.             System.out.println("Enter the heuristic values of the nodes");
  101.             for (int vertex = 1; vertex <= number_of_vertices; vertex++)
  102.             {
  103.                 System.out.print(vertex + ".");
  104.                 heuristicvalues[vertex] = scan.nextInt();
  105.                 System.out.println();
  106.             }
  107.  
  108.             System.out.println("Enter the source ");
  109.             source = scan.nextInt();
  110.  
  111.             System.out.println("The graph is explored as follows");
  112.             BestFirstSearch bestFirstSearch = new BestFirstSearch(number_of_vertices);
  113.             bestFirstSearch.bestFirstSearch(adjacency_matrix, heuristicvalues,source);
  114.  
  115.        } catch (InputMismatchException inputMismatch)
  116.        {
  117.            System.out.println("Wrong Input Format");
  118.        }
  119.        scan.close();
  120.    }
  121. }
  122.  
  123. class Vertex implements Comparator<Vertex>
  124. {
  125.     public int heuristicvalue;
  126.     public int node;
  127.  
  128.     public Vertex(int node, int heuristicvalue)
  129.     {
  130.         this.heuristicvalue = heuristicvalue;
  131.         this.node = node;
  132.     }
  133.  
  134.     public Vertex()
  135.     {
  136.  
  137.     } 
  138.  
  139.     @Override 
  140.     public int compare(Vertex vertex1, Vertex vertex2)
  141.     {
  142.         if (vertex1.heuristicvalue < vertex2.heuristicvalue)
  143.             return -1;
  144.         if (vertex1.heuristicvalue > vertex2.heuristicvalue)
  145.             return 1;
  146.         return 0;
  147.     }
  148.  
  149.     @Override
  150.     public boolean equals(Object obj)
  151.     {
  152.         if (obj instanceof Vertex)
  153.         {
  154.             Vertex node = (Vertex) obj;
  155.             if (this.node == node.node)
  156.             {
  157.                 return true;
  158.             }
  159.         }
  160.         return false;
  161.     }
  162. }


$javac BestFirstSearch.java
$java BestFirstSearch
Enter the number of vertices
6
Enter the Weighted Matrix for the graph
0 0 1 1 0 1 
0 0 0 1 1 1 
1 0 0 1 0 0
1 1 1 0 1 0
0 1 0 1 0 0
1 1 0 0 0 0
Enter the heuristic values of the nodes
1.2
 
2.3
 
3.1
 
4.4
 
5.0
 
6.10
 
Enter the source 
6
The graph is explored as follows
6	1	3	2	5	4

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.