スタックと訪問済みリストを維持し、while ループを使用することでそれを行うことができると思います: Visited は bool[] であり、すべての位置で false を保持するように初期化されています。どういうわけかブール値を返し、ノードからネイバーへのエッジがあるかどうかを伝えます。(1 との暗黙の比較、または単純に隣接行列にブール値を保持させる)
スタックは、ノードのインデックスを保持します。
dfs(G,i){
Initialize Stack
Current = i
PossibleEdge = 0
Visited[Current] = true //You have visited the starting node
Do {
While (PossibleEdge < count) {
if (G[Current,PossibleEdge] && NOT Visited[PossibleEdge]){
Stack.Push(Current) //Save state
Current = PossibleEdge //Continue with the child node
Visited[Current] = true
PossibleEdge = -1 //So that it will be incremented to 0
}
PossibleEdge++
}
PossibleEdge = Current //Continue to next row of "parent node"
Current = Stack.Pop() //Get parent node back
} While (Stack contains nodes)
}
最適化できると確信しています (そして、私が疲れ果てているのを見ると、いくつかの論理エラーがある可能性があります) が、基本的な手順が理にかなっている場合は、それが始まりです!
編集:このヒントを明確にして追加します:再帰はおそらく簡単です;)