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.
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.PriorityQueue;
import java.util.Scanner;
public class BestFirstSearch
{
private PriorityQueue<Vertex> priorityQueue;
private int heuristicvalues[];
private int numberOfNodes;
public static final int MAX_VALUE = 999;
public BestFirstSearch(int numberOfNodes)
{
this.numberOfNodes = numberOfNodes;
this.priorityQueue = new PriorityQueue<Vertex>(this.numberOfNodes,
new Vertex());
}
public void bestFirstSearch(int adjacencyMatrix[][], int[] heuristicvalues,int source)
{
int evaluationNode;
int destinationNode;
int visited[] = new int [numberOfNodes + 1];
this.heuristicvalues = heuristicvalues;
priorityQueue.add(new Vertex(source, this.heuristicvalues));
visited = 1;
while (!priorityQueue.isEmpty())
{
evaluationNode = getNodeWithMinimumHeuristicValue();
destinationNode = 1;
System.out.print(evaluationNode + "\t");
while (destinationNode <= numberOfNodes)
{
Vertex vertex = new Vertex(destinationNode,this.heuristicvalues[destinationNode]);
if ((adjacencyMatrix[evaluationNode][destinationNode] != MAX_VALUE
&& evaluationNode != destinationNode)&& visited[destinationNode] == 0)
{
priorityQueue.add(vertex);
visited[destinationNode] = 1;
}
destinationNode++;
}
}
}
private int getNodeWithMinimumHeuristicValue()
{
Vertex vertex = priorityQueue.remove();
return vertex.node;
}
public static void main(String... arg)
{
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
int heuristicvalues[];
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
adjacency_matrix = new int[number_of_vertices + 1][number_of_vertices + 1];
heuristicvalues = new int[number_of_vertices + 1];
System.out.println("Enter the Weighted Matrix for the graph");
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
adjacency_matrix[i][j] = scan.nextInt();
if (i == j)
{
adjacency_matrix[i][j] = 0;
continue;
}
if (adjacency_matrix[i][j] == 0)
{
adjacency_matrix[i][j] = MAX_VALUE;
}
}
}
for (int i = 1; i <= number_of_vertices; i++)
{
for (int j = 1; j <= number_of_vertices; j++)
{
if (adjacency_matrix[i][j] == 1 && adjacency_matrix[j][i] == 0)
{
adjacency_matrix[j][i] = 1;
}
}
}
System.out.println("Enter the heuristic values of the nodes");
for (int vertex = 1; vertex <= number_of_vertices; vertex++)
{
System.out.print(vertex + ".");
heuristicvalues[vertex] = scan.nextInt();
System.out.println();
}
System.out.println("Enter the source ");
source = scan.nextInt();
System.out.println("The graph is explored as follows");
BestFirstSearch bestFirstSearch = new BestFirstSearch(number_of_vertices);
bestFirstSearch.bestFirstSearch(adjacency_matrix, heuristicvalues,source);
} catch (InputMismatchException inputMismatch)
{
System.out.println("Wrong Input Format");
}
scan.close();
}
}
class Vertex implements Comparator<Vertex>
{
public int heuristicvalue;
public int node;
public Vertex(int node, int heuristicvalue)
{
this.heuristicvalue = heuristicvalue;
this.node = node;
}
public Vertex()
{
}
@Override
public int compare(Vertex vertex1, Vertex vertex2)
{
if (vertex1.heuristicvalue < vertex2.heuristicvalue)
return -1;
if (vertex1.heuristicvalue > vertex2.heuristicvalue)
return 1;
return 0;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Vertex)
{
Vertex node = (Vertex) obj;
if (this.node == node.node)
{
return true;
}
}
return false;
}
}
$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
Related posts:
Câu lệnh điều khiển vòng lặp trong Java (break, continue)
Một số nguyên tắc, định luật trong lập trình
Hướng dẫn Java Design Pattern – Proxy
Spring REST API + OAuth2 + Angular
Java Program to Implement Hash Tables with Linear Probing
Configure a RestTemplate with RestTemplateBuilder
HTTP Authentification and CGI/Servlet
HttpClient 4 – Send Custom Cookie
Java IO vs NIO
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Implement K Way Merge Algorithm
Java Program to Create a Random Graph Using Random Edge Generation
Spring Data JPA and Null Parameters
Java Program to Implement Warshall Algorithm
Java Program to Generate Random Numbers Using Multiply with Carry Method
Các kiểu dữ liệu trong java
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Phương thức forEach() trong java 8
Sử dụng JDBC API thực thi câu lệnh truy vấn dữ liệu
Java Program to Find kth Largest Element in a Sequence
Send an email with an attachment
Adding Parameters to HttpClient Requests
ExecutorService – Waiting for Threads to Finish
Lập trình hướng đối tượng (OOPs) trong java
Spring Boot - Apache Kafka
Java Program to Implement Sorted List
Hashing a Password in Java
Spring Data Java 8 Support
Java Program to implement Associate Array
Examine the internal DNS cache
Hướng dẫn Java Design Pattern – Visitor
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search