Add to Favorites    Make Home Page 10092 Online  
 Language Categories  
 Our Services  

Home » Java Home » Beginners / Lab Assignments Home » Shortest Path

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Shortest Path
Author Shanawas P M
Author Email james_1983_bond [at] yahoo.co.uk
Description To find the shortest path using Breadth First Search
Algorithm
Category Java » Beginners / Lab Assignments
Hits 24987
Code Select and Copy the Code
import java.io.*; import java.util.*; class shortpath { public static int x1,x2,y1,y2,n,m; public static int a[][]=new int[10][10],parent[][]=new int[10][10],dist[][]=new int[10][10],i,j; public static void main(String s[]) throws IOException { int x,y,num; LinkedList l=new LinkedList(); LinkedList path=new LinkedList(); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value for n & m 1st matrix"); BufferedReader bf=new BufferedReader(new InputStreamReader(System.in)); n=Integer.parseInt(bf.readLine()); m=Integer.parseInt(bf.readLine()); System.out.println("Enter the elements for 1st matrix(0 or 1)"); for(i=0;i<n;i++) for(j=0;j<m;j++) { a[i][j]=Integer.parseInt(bf.readLine()); parent[i][j]=-1; dist[i][j]=n*m; } System.out.println("Enter the position of source(x,y)"); x1=Integer.parseInt(bf.readLine()); y1=Integer.parseInt(bf.readLine()); System.out.println("Enter the position of Destination(x,y)"); x2=Integer.parseInt(bf.readLine()); y2=Integer.parseInt(bf.readLine()); l.add(getnum(x1,y1)); dist[x1][y1]=0; while(l.size()!=0) { num=Integer.parseInt(l.removeLast().toString()); x=num/m; y=num%m; if(x>0&&a[x-1][y]!=1&&(dist[x-1][y]>(dist[x][y])+1)) { l.add(getnum(x-1,y)); dist[x-1][y]=dist[x][y]+1; parent[x-1][y]=num; } if(y>0&&a[x][y-1]!=1&&(dist[x][y-1]>(dist[x][y])+1)) { l.add(getnum(x,y-1)); dist[x][y-1]=dist[x][y]+1; parent[x][y-1]=num; } if(x<n-1&&a[x+1][y]!=1&&(dist[x+1][y]>(dist[x][y])+1)) { l.add(getnum(x+1,y)); dist[x+1][y]=dist[x][y]+1; parent[x+1][y]=num; } if(y<m-1&&a[x][y+1]!=1&&(dist[x][y+1]>(dist[x][y])+1)) { l.add(getnum(x,y+1)); dist[x][y+1]=dist[x][y]+1; parent[x][y+1]=num; } } path.add("("+x2+","+y2+")"); x=x2;y=y2; do{ num=parent[x][y]; x=num/m; y=num%m; path.add("("+x+","+y+")"); }while(!(x==x1&&y==y1)); while(path.size()!=0) System.out.println(path.removeLast()); } public static Integer getnum(int x,int y) { return new Integer(x*m+y); } }

Related Source Codes

Script Name Author
Sending mail Using JavaMail to Yahoo and Gmail accounts sai prasad
Simple Program in Java to Implement Multithreading Satish.K
Simple Calculator in Java Using Remote Method Invocation Satish.K
Guest Book Application Using Servlets Satish.K
String Manipulation Using Stringification Satish.K
String Manipulation Using Stringification Satish.K
Moving Ball Application Using Java Beans Satish.K
Rapid Roll game subrahmanyeswararao
student mgm arpan
Sourav Datta
Download Manager Sagar
Address Book in Java Rahul Chouhan
address book using java database connectivity(jdbc-msaccess) shekhar bansal
sun Steganography B.Rajavel
Connecting Java with MS-Access - Inserting data in Aseem

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2017 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1099


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review