This is my bfs algorithim. I want to store the number of edges i traversed in the field edges, but I can't figure out where to place the variable to add one for each edge. I keep getting answers that are too long, so i think this is harder than simply incrementing edge.
It should be noted that this is supposed to calculate the edges along the true path only, not the extra edges.
public int distance(Vertex x, Vertex y){
Queue<Vertex> search = new LinkedList<Vertex>();
search.add(x);
x.visited = true;
while(!search.isEmpty()){
Vertex t = search.poll();
if(t == y){
return edges;
}
for(Vertex n: t.neighbours){
if(!n.visited){
n.visited = true;
search.add(n);
}
}
System.out.println(search + " " + t);
}
return edges;
}
Any and all help is appreciated. if you require more classes/methods let me know
EDIT
import java.util.ArrayList;
public class Vertex {
public static char currentID = 'a';
protected ArrayList<Vertex> neighbours;
protected char id;
protected boolean visited = false;
protected Vertex cameFrom = null;
public Vertex(){
neighbours = new ArrayList<Vertex>();
id = currentID;
currentID++;
Graph.all.add(this);
}
public void addNeighbour(Vertex x){
int a;
while(x == this){
a = (int) (Math.random()*(Graph.all.size()));
x = Graph.all.get(a);
}
if(!(neighbours.contains(x))){
neighbours.add(x);
x.addNeighbour(this);
//System.out.println(this + " Linking to " + x);
}
}
public void printNeighbours(){
System.out.println("The neighbours of: " + id + " are: " + neighbours);
}
public String toString(){
return id + "";
}
}
In your Vertex
class, create a Vertex cameFrom
field which you set to point to the Vertex
you came from when that node was visited. You could even replace your boolean visited
field with this (if it is null
the Vertex
hasn't been visited yet).
Then, when you find the Vertex y
just follow the pointers back to Vertex x
counting how many steps it takes as you go.
If you don't want to change your Vertex
class, then just keep a Map<Vertex,Vertex>
during your search which stores the mappings from the vertex you're visiting to the vertex you came from. When you get to the end you can follow the path to the beginning in the same way.
Something along these lines perhaps:
public int distance(Vertex x, Vertex y){
Queue<Vertex> search = new LinkedList<Vertex>();
search.add(x);
while(!search.isEmpty()){
Vertex t = search.poll();
if(t == y){
return pathLength( t );
}
for(Vertex n: t.neighbours){
if(n.cameFrom == null || n != x){
n.cameFrom = t;
search.add(n);
}
}
System.out.println(search + " " + t);
}
return -1;
}
public int pathLength( Vertex v )
{
int path = 0;
while ( v.cameFrom != null )
{
v = v.cameFrom;
path++;
}
return path;
}
In this example, the number of edges is simply the size of the search
. queue.
EDIT:
One possible solution is to do it layer by layer. Lets say you asked for the distance between Vertex A, F
and the Graph looks like:
A
|\
B C
|
D
|\
E F
First calculate the distance between A and B,C (which is easy because B and C are immediate neighbors of A. Then calculate the distance between A and D (which is easy because D is an immediate neighbor of B, then A and E, F. Store the distance in the A vertex node. Now after you've run the BFS and determined the search result, you can simply ask for the distance. Look at this visual diagram.
来源:https://stackoverflow.com/questions/10060144/counting-the-number-of-edges-traversed-in-a-breadth-first-search