Skip to content

Instantly share code, notes, and snippets.

@adispen
Last active August 29, 2015 14:00
Show Gist options
  • Save adispen/11356335 to your computer and use it in GitHub Desktop.
Save adispen/11356335 to your computer and use it in GitHub Desktop.
public Stack<Vertex> bfs(String name, int i, Vertex[] friendGraph){
Stack<Vertex> friendStack = new Stack<Vertex>();
Queue<Neighbor> neighborQueue = new LinkedList<Neighbor>();
Neighbor l = null;
friendGraph[i].prev = -1;
do{
for(Neighbor j = friendGraph[i].neighborList; j != null; j = j.next){
if(friendGraph[j.vertexNum].prev == -2){
neighborQueue.offer(j);
friendGraph[j.vertexNum].prev = i;
}
}
if(friendGraph[i].name == name){
for(int k = i; i != -1; k = friendGraph[k].prev){
friendStack.push(friendGraph[k]);
}
return friendStack;
}
l = neighborQueue.remove();
}while(l != null);
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment