그래프에 bfs를 구현할 수있는 사람이 있습니까? 여기 그래프 구현과 bfs 알고리즘을 그래프에 넣었습니다. 나는 그것을하는 방법에 대한 아이디어가 필요하다.자바의 지시 그래프
public class DiGraph<V> {
public static class Edge<V>{
private V vertex;
private int cost;
public Edge(V v, int c){
vertex = v;
cost = c;
}
@Override
public String toString() {
return "{" + vertex + ", " + cost + "}";
}
}
private Map<V, List<Edge<V>>> inNeighbors = new HashMap<V, List<Edge<V>>>();
private Map<V, List<Edge<V>>> outNeighbors = new HashMap<V, List<Edge<V>>>();
public int nr_vertices;
public int nr_edges;
public String toString() {
StringBuffer s = new StringBuffer();
for (V v: inNeighbors.keySet())
s.append("\n " + v + " -> " + inNeighbors.get(v));
return s.toString();
}
public void addIn (V vertex) {
if (inNeighbors.containsKey(vertex))
return;
inNeighbors.put(vertex, new ArrayList<Edge<V>>());
}
public void addOut(V vertex) {
if (outNeighbors.containsKey(vertex))
return;
outNeighbors.put(vertex, new ArrayList<Edge<V>>());
}
public boolean contains (V vertex) {
return inNeighbors.containsKey(vertex);
}
public void add (V from, V to, int cost) {
this.addIn(from);
this.addIn(to);
this.addOut(to);
this.addOut(from);
inNeighbors.get(from).add(new Edge<V>(to,cost));
outNeighbors.get(to).add(new Edge<V>(from,cost));
}
public int outDegree (V vertex) {
return inNeighbors.get(vertex).size();
}
public int inDegree (V vertex) {
return inboundNeighbors(vertex).size();
}
}
public void bfs()
{
// BFS uses Queue data structure
Queue queue = new LinkedList();
queue.add(this.rootNode);
printNode(this.rootNode);
rootNode.visited = true;
while(!queue.isEmpty()) {
Node node = (Node)queue.remove();
Node child=null;
while((child=getUnvisitedChildNode(node))!=null) {
child.visited=true;
printNode(child);
queue.add(child);
}
}
// Clear visited property of nodes
clearNodes();
}
내가 인터넷에서 그것을 갔던 BFS 알고리즘은, 내가 그것을 이해하지만, 일반적인 경우에, 난 내 그래프이이 문제를 해결하는 방법입니다
'일부 기능이 작동하지 않습니다'는 의미는 정확히 무엇입니까? – home
isEdge()는 항상 YES를 반환합니다. 그리고 inboundNeighbors()는 임의의 nr을 반환합니다. 나는 그것을 이해할 수 없다. – jojuk
디버깅을 통해 한 번에 하나의 오류 만 수정하십시오. 너무 많으면 다시 읽거나 다시 읽거나 다시 작성하는 것도 좋은 생각입니다. 마치 영어 논문을 쓰는 것과 같지만 구문과 의미로 더 자세히 구분됩니다. 컴파일러와 런타임 출력은 일반적으로 매우 구체적이며 작업 할 행 번호를 제공합니다. – clwhisk