여기 그래프를 탐색하려고하지만 탐색 기능이 잘못되었는지는 확실하지 않습니다. 재귀가 제대로 작동하지 않는 것 같습니다. 노드 0의 이웃을 탐색하면서 0, 1, 2를 탐색 한 다음 3, 4, 5를 탐색하기 위해 다시 돌아 오지 않습니다. 왜 이렇게이다?JavaScript 그래프 탐색 알고리즘의 재귀 호출
당신은 재귀 함수에서 전역 변수를 설정하고 당신의 발가락에 스테핑하고 VAR을 떠나하여explored=[]
//class definition
function graph(){
this.graph=new Array();
this .graph[0] = [1,0,1,1,0,1]
this .graph[1] = [0,1,1,1,0,0]
this .graph[2] = [1,1,1,1,0,0]
this .graph[3] = [1,1,1,1,1,0]
this .graph[4] = [0,0,0,1,1,0]
this .graph[5] = [1,0,0,0,0,0]
this.explore = explore
}
function explore(node,depth){
explored[node]=1
document.write('<br>')
for(x=0;x<depth;x++)
document.write('-')
document.write(node+'<br>')
neighbours=this.graph[node]
document.write('exploring '+node +' neighbours' + neighbours +'explored = '+explored)
for (i=0;i<neighbours.length;i++){
document.write('checking'+i+' node is ='+node)
if(neighbours[i] ==1 && explored[i]!=1)
this.explore(i,++depth)
}
}
g = new graph()
g.explore(0,0)
var를 사용하지 않으면 x 및 i가 전역 변수로 설정됩니다. – generalhenry
@generalhenry 당신은 답을해야합니다, 그래서 그는 그것을 받아 들일 수 있습니다 .. 그렇지 않다면, 나는 그것을 할 것입니다 .. mohowhah ... mwhahahaha! – Frode
또한 이웃 – generalhenry