I need to ask for your help, as I've been struggling with my problem for many days and didn't find any suitable solution. I want to find weight of subgrah that contains node (going to be a parameter of my method) and will end with central node 0. I know it sounds stupid but image will be a great help here (http://img542.imageshack.us/img542/5400/zrzutekranu20130418o205.png). For example getWeight(8) will return 21, the same if we run getWeight(7) (9,10). getWeight(2) = 7. I wrote such method however sometimes I'm getting Stack Overflow Exception :(
private void getWeight(Object node, Object source) {
Collection nodeCollection = graph.getNeighbors(node);
for (Object n : nodeCollection) {
if ((Integer) n == 0) {
weight += ((MyEdge) (graph.findEdge(startNode, node))).getW();
} else {
if (n != source) {
weight += ((MyEdge) (graph.findEdge(node, n))).getW();
getWeight(n, node);
} else {
}
}
}
return weight;
}
I'm using jung2 lib.
Please help, you are my last hope!
@Zim-Zam O'Pootertoot: Like this?
ArrayList<Boolean> visited = new ArrayList<Boolean>();
public void getWeight2(Object i) {
visited.set((Integer) i, true);
for (Object v : getGraph().getNeighbors(i)) {
if (!visited.get((Integer) v)) {
if (getGraph().getNeighborCount(v) > 1 & !v.equals(startNode)) {
weight += ((MyEdge) getGraph().findEdge(i, v)).getW();
getWeight2(v);
} else {
weight += ((MyEdge) getGraph().findEdge(i, v)).getW();
}
}
}
}
Still SOExp ;(