Professional Documents
Culture Documents
NAME:
HUSNAIN IQBAL
SECTION:
A
ASSIGNMENT #:
2
ROLL NUM:
UW-18-CS-BS-042
SUBMITTED TO:
MAM HIRA WASEEM
DATE:
11-03-2021
UNIVERSTY OF WAH
Create a program in Java that perform the following search strategies
● Breadth First Search
● Depth first Search
● Uniform Cost Search
● Iterative Deepening
● Depth Limited Search
And
● A* search
● Your program should have a menu that give user freedom to choose among any given option
Note: Your program should be capable of traversing the trees and give the optimal Solution.
CODE:
import java.util.*;
System.out.println("1: BFS");
System.out.println("2: DFS");
System.out.println("3: UCS");
System.out.println("5: DLS");
System.out.println("6: A* search");
while(true)
{
System.out.print("Make your choice: ");
ch = sc.nextInt(); // reading user's choice
switch (ch)
{
case 1:
BFS obj = new BFS();
obj.makeGraph(10);
obj.addEdgeToGraph(0, 1);
obj.addEdgeToGraph(0, 9);
obj.addEdgeToGraph(2, 3);
obj.addEdgeToGraph(2, 4);
obj.addEdgeToGraph(3, 5);
obj.addEdgeToGraph(3, 6);
obj.addEdgeToGraph(4, 7);
obj.addEdgeToGraph(4, 8);
case 2:
node40.addneighbours(node10);
node40.addneighbours(node20);
node10.addneighbours(node30);
node20.addneighbours(node10);
node20.addneighbours(node30);
node20.addneighbours(node60);
node20.addneighbours(node50);
node30.addneighbours(node60);
node60.addneighbours(node70);
node50.addneighbours(node70);
System.out.println();
break;
case 3:
int adjacency_matrix[][];
int number_of_vertices;
int source = 0;
int destination = 0;
int distance;
Scanner scan = new Scanner(System.in);
try
{
System.out.println("Enter the number of vertices");
number_of_vertices = scan.nextInt();
break;
case 4:
IDfS();
break;
case 5:
ASS();
break;
default:
System.out.println("Invalid choice! Please make a valid choice. \n\n");
}
}
}
}
import java.util.*;
import java.util.Queue;
int i;
int k;
while( !q.isEmpty() )
{
k = q.remove();
System.out.print( k +" ");
// we are iterating through adjacency list of vertex k which has to be explored now.
// it will give the adjacent nodes of each vertex.
Iterator<Integer> i = adjList[k].listIterator();
int j;
while( i.hasNext() )
{
j = i.next();
if( visited[j] != true )
{
// if any child found with out visiting then those child will be added to queue.
q.add(j);
visited[j] = true;
}
}
}
}
// BFsearch function will maintain boolean array to know which vertex is explored.
// If in case of disconnected graph it will again call BFtravesal on another component
public void BFsearch(int v)
{
boolean visited[] = new boolean[n];
BFtraversal(v, visited);
for ( int i = 0; i < n; i++ )
{
// after calling BFtraveral it is checking whether any vertex left out with out exploring
if( visited[i] != true )
{
// if found it will call again BFtraversal
BFtraversal(i, visited);
}
}
}
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
Node(int data)
{
this.data=data;
this.neighbours=new ArrayList<>();
}
public void addneighbours(Node neighbourNode)
{
this.neighbours.add(neighbourNode);
}
public List<Node> getNeighbours() {
return neighbours;
}
public void setNeighbours(List<Node> neighbours) {
this.neighbours = neighbours;
}
}
// Recursive DFS
public void dfs(Node node)
{
System.out.print(node.data + " ");
List<Node> neighbours=node.getNeighbours();
node.visited=true;
for (int i = 0; i < neighbours.size(); i++) {
Node n=neighbours.get(i);
if(n!=null && !n.visited)
{
dfs(n);
}
}
}
List<Node> neighbours=element.getNeighbours();
for (int i = 0; i < neighbours.size(); i++) {
Node n=neighbours.get(i);
if(n!=null && !n.visited)
{
stack.add(n);
}
}
}
}
import java.util.*;
while (!priorityQueue.isEmpty())
{
evaluationNode = getNodeWithMinDistanceFromPriorityQueue();
System.out.println("The eval Node is " + evaluationNode);
if (evaluationNode == destination)
{
return distances[destination];
}
settled.add(evaluationNode);
addFrontiersToQueue(evaluationNode);
}
return distances[destination];
}
private void addFrontiersToQueue(int evaluationNode)
{
for (int destination = 1; destination <= numberOfNodes; destination++)
{
if (!settled.contains(destination))
{
if (adjacencyMatrix[evaluationNode][destination] != MAX_VALUE)
{
if (distances[destination] > adjacencyMatrix[evaluationNode][destination]
+ distances[evaluationNode])
{
distances[destination] = adjacencyMatrix[evaluationNode][destination]
+ distances[evaluationNode];
parent[destination] = evaluationNode;
}
public Node()
{
@Override
public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
if (node1.node < node2.node)
return -1;
return 0;
}
@Override
public boolean equals(Object obj)
{
if (obj instanceof Node)
{
Node node = (Node) obj;
if (this.node == node.node)
{
return true;
}
}
return false;
}
}
● Iterative Deepening
import java.util.*;
startNode.costFromStart = 0;
startNode.estimatedCostToGoal =
startNode.getEstimatedCost(goalNode);
startNode.pathParent = null;
openList.add(startNode);
while (!openList.isEmpty()) {
AStarNode node = (AStarNode)openList.removeFirst();
if (node == goalNode) {
return constructPath(goalNode);
}
List neighbors = node.getNeighbors();
for (int i=0; i<neighbors.size(); i++) {
AStarNode neighborNode =
(AStarNode)neighbors.get(i);
boolean isOpen = openList.contains(neighborNode);
boolean isClosed =
closedList.contains(neighborNode);
float costFromStart = node.costFromStart +
node.getCost(neighborNode);
}
● Depth Limited Search
package iterative_deepening_dfs.java.simple;
int depth = 1;
while (!bottomReached) {
bottomReached = true;
if (result != null) {
return result;
}
depth *= 2;
return null;
private static Node iterativeDeepeningDFS(Node node, int target, int currentDepth, int maxDepth) {
if (node.value == target) {
return node;
if (currentDepth == maxDepth) {
if (node.children.length > 0) {
bottomReached = false;
return null;
if (result != null) {
return result;
return null;
}
class Node {
Node[] children;
int value;