ここでグラフを探索しようとしていますが、私は探索機能に何が問題なのかよく分かりません。再帰は正しく機能していないようです。ノード0の近傍を探索している間に、0,1,2を探索し、その後3,4,5を探索することは決して戻らない。どうしてこんなことに?あなたは再帰関数でグローバル変数を設定し、あなたのつま先を踏んでいるVARを除外することによりJavaScriptグラフ探索アルゴリズムの再帰
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