您的位置:首页 > 其它

4.2---找图的两个节点是否有路径

2015-12-22 16:06 204 查看
//这道题AC了,但是并不确定是否完全正确。此外要注意因为是有向图,所以既要检查a到b,还要检查b到a
public class Path {

public boolean checkPath(UndirectedGraphNode a, UndirectedGraphNode b) {
return checkPath2(a,b) || checkPath2(b,a);
}
public boolean checkPath2(UndirectedGraphNode a, UndirectedGraphNode b) {

boolean res = false;
if(a.label == b.label) return true;
if(a.neighbors == null) return false;
for(UndirectedGraphNode temp : a.neighbors){
if(temp.label == -1){
return false;
}
else{
temp.label = -1;
}
if(checkPath(temp,b)){
return true;
}
}
return res;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: