0% found this document useful (0 votes)
31 views3 pages

Best First Search and A* Algorithm Implementation

The document provides a Java implementation of the Best First Search and A* algorithms using a priority queue to explore a graph based on heuristic values. It includes methods for inputting the graph's adjacency matrix and heuristic values, as well as for executing the search algorithm. The program handles user input and exceptions, ensuring a structured approach to graph traversal.

Uploaded by

Arshad Gilani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views3 pages

Best First Search and A* Algorithm Implementation

The document provides a Java implementation of the Best First Search and A* algorithms using a priority queue to explore a graph based on heuristic values. It includes methods for inputting the graph's adjacency matrix and heuristic values, as well as for executing the search algorithm. The program handles user input and exceptions, ensuring a structured approach to graph traversal.

Uploaded by

Arshad Gilani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Title:Implement Best First search and A* algorithm

import [Link];
import [Link];
import [Link];
import [Link];
public class BestFirstSearch
{
private PriorityQueue<Vertex> priorityQueue;
private int heuristicvalues[];
private int numberOfNodes;
public static final int MAX_VALUE = 999;
public BestFirstSearch(int numberOfNodes)
{
[Link] = numberOfNodes;
[Link] = new
PriorityQueue<Vertex>([Link], new Vertex());
}
public void bestFirstSearch(int adjacencyMatrix[][], int[] heuristicvalues,int source)
{
int evaluationNode;
int destinationNode;
int visited[] = new int [numberOfNodes + 1];
[Link] = heuristicvalues;
[Link](new Vertex(source, [Link][source]));
visited[source] = 1;while (![Link]())
{
evaluationNode = getNodeWithMinimumHeuristicValue();
destinationNode = 1;
[Link](evaluationNode + "\t");
while (destinationNode <= numberOfNodes)
{
Vertex vertex = new Vertex(destinationNode,[Link][destinationNode]);
if ((adjacencyMatrix[evaluationNode][destinationNode]!= MAX_VALUE && evaluationNode !=
destinationNode)&& visited[destinationNode] == 0)
{
[Link](vertex); visited[destinationNode] = 1;
}
destinationNode++;
}
}
}
private int getNodeWithMinimumHeuristicValue()
{
Vertex vertex = [Link]();
return [Link];
}
public static void main(String... arg)
{
int adjacency_matrix[][];
int number_of_vertices; int source = 0;
int heuristicvalues[];Scanner scan = new Scanner([Link]);
try
{
[Link]("Enter the number of vertices");
number_of_vertices = [Link](); adjacency_matrix = new int[number_of_vertices
+1][number_of_vertices + 1];
heuristicvalues = new int[number_of_vertices + 1];
[Link]("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] = [Link](); 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;}
}
}
[Link]("Enter the heuristic values of the nodes");
for (int vertex = 1; vertex <= number_of_vertices; vertex++)
{
[Link](vertex + ".");
heuristicvalues[vertex] = [Link]();
[Link]();
}
[Link]("Enter the source ");
source = [Link]();
[Link]("The graph is explored as follows");
BestFirstSearch bestFirstSearch = new BestFirstSearch(number_of_vertices);
[Link](adjacency_matrix, heuristicvalues,source);
} catch (InputMismatchException inputMismatch)
{
[Link]("Wrong Input Format");
}
[Link]();
}
}
class Vertex implements Comparator<Vertex>
{
public int heuristicvalue; public int node;public Vertex(int node, int heuristicvalue)
{
[Link] = heuristicvalue;
[Link] = node;
}
public Vertex()
{
}
public int compare(Vertex vertex1, Vertex vertex2)
{
if ([Link] < [Link]) return -1;
if ([Link] > [Link]) return 1;
return 0;
}
public boolean equals(Object obj)
{
if (obj instanceof Vertex)
{
Vertex node = (Vertex) obj;
if ([Link] == [Link])
{
return true;
}
}
return false;
}
}

You might also like