[go: up one dir, main page]

0% found this document useful (0 votes)
33 views5 pages

Sathaye College: Practical No: 5

This document presents code for implementing recursive breadth-first search to find the shortest path between two cities. The code takes input of start and goal cities and distances between cities, arranges the cities by distance, and recursively searches levels of cities until reaching the goal city. It outputs the minimum distance and city at each level until finding the goal city.

Uploaded by

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

Sathaye College: Practical No: 5

This document presents code for implementing recursive breadth-first search to find the shortest path between two cities. The code takes input of start and goal cities and distances between cities, arranges the cities by distance, and recursively searches levels of cities until reaching the goal city. It outputs the minimum distance and city at each level until finding the goal city.

Uploaded by

Bhupendra Mish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SATHAYE COLLEGE

Intelligent Systems M.Sc.IT Part II SEM IV

Practical No: 5

Aim: To Implement Recursive Breadth First Search

CODE:

import java.util.*;

import java.io.*;

public class RecursiveBestFirst

public static void main(String args[])throws Exception

System.out.println("Performed by Bhupendra Mishra");

String cities[]=new String[10];

String seleccity[]=new String[10];

int distance[]=new int[10];

int g[]=new int[10];

int h[]=new int[10];

int i=0,p=0,min;

String mincity="",cities1="",mincity1="";

System.out.println("enter the start city");

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String startcity=br.readLine();

System.out.println("enter the goal state");

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));

String goalcity=br1.readLine();

System.out.println("Enter the distance between "+startcity +" and " +goalcity);

BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));

int totdist=Integer.parseInt(br2.readLine());

do

System.out.println("How many states from the selected state in the next level");

int n=Integer.parseInt(br.readLine());

for(i=0;i<n;i++)

int t=i+1;

System.out.println("Enter city"+t);

BufferedReader br3=new BufferedReader(new InputStreamReader(System.in));

cities[i]=br3.readLine();

for(i=0;i<n;i++)

if(goalcity.equalsIgnoreCase(cities[i]))

System.out.println("GOAL REACHED");

System.exit(0);

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

for(i=0;i<n;i++)

System.out.println("Enter the distance from "+goalcity+ " to "+cities[i]);

BufferedReader br3=new BufferedReader(new InputStreamReader(System.in));

distance[i]=Integer.parseInt(br3.readLine());

//arranging the cities in ascending order

int min1=0;

for(i=0;i<n;i++)

if(min1<distance[i] && min1!=0)

distance[i]=min1;

mincity=cities[i];

System.out.println("THEREFORE THE CITIES IN ASCENDING ORDER OF DISTANCE ARE ");

for(i=0;i<(n-1);i++)

for(int j=i+1;j<n;j++)

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

if(distance[i]>distance[j])

int d=distance[i];

cities1=cities[i];

distance[i]=distance[j];

cities[i]=cities[j];

distance[j]=d;

cities[j]=cities1;

}}}

min=distance[0];

min1=distance[1];

mincity=cities[0];

mincity1=cities[1];

System.out.println("min= "+min+" mincity= "+mincity);

System.out.println("min1= "+min1+" mincity1= "+mincity1);

while(!(mincity.equalsIgnoreCase(goalcity)));

System.out.println("\nGOAL REACHED\n");

}}

BHUPENDRA MISHRA
SATHAYE COLLEGE
Intelligent Systems M.Sc.IT Part II SEM IV

OUTPUT:

BHUPENDRA MISHRA

You might also like