Last active
March 3, 2024 07:54
-
-
Save KodeSeeker/841e146633b0a77ee5b3 to your computer and use it in GitHub Desktop.
Breadth first search with BackTracking.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class BFS{ | |
| public static Node[] prev; | |
| public static Graph G; | |
| public static void BFSWithBackTracking( ) | |
| { | |
| if(G==null) | |
| return; | |
| //classic BFS with an array to store the predecessor of each node | |
| Node[] prev = new Node[G.size()]; | |
| //Queue for BFS | |
| Queue<Node> q= new LinkedList<Node>(); | |
| Node root = G.root; | |
| if(root!=null) | |
| { | |
| q.push(root); | |
| prev[root]=-1; | |
| root.visited=true; | |
| } | |
| while(!q.isEmpty()) | |
| { | |
| Node child = q.poll(); | |
| for(Node n:child.getNeighbors()) | |
| { | |
| if(!n.isVisited()) | |
| { | |
| n.visited=true; | |
| prev[n]=child; | |
| q.push(n); | |
| } | |
| } | |
| } | |
| } | |
| //Method to find shortest path | |
| //Assume pred[] is populated with the right node information. | |
| public int shortestPath(Node v)// v is the destination vertex | |
| { | |
| if(pred.length==0) | |
| return -1; // pred hasnt been populated correctly. | |
| int dist=0; | |
| if(G.root==v) | |
| return 0; | |
| while(pred[v]!=-1) | |
| { | |
| dist++; | |
| v=pred[v]; | |
| } | |
| return dist; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment