二分木の幅優先探索を再帰的に実装したいとしましょう。それについてどう思いますか?
補助記憶域として呼び出しスタックのみを使用することは可能ですか?
二分木の幅優先探索を再帰的に実装したいとしましょう。それについてどう思いますか?
補助記憶域として呼び出しスタックのみを使用することは可能ですか?
回答:
(私はこれが一種の思考演習、またはトリックの宿題/インタビューの質問であることを想定していますが、何らかの理由でヒープ領域が許可されない奇妙なシナリオを想像できると思いますメモリマネージャー?いくつかの奇妙なランタイム/ OSの問題?]まだスタックにアクセスできます...)
幅優先トラバーサルは、伝統的にスタックではなくキューを使用します。キューとスタックの性質はかなり逆なので、補助スタック(キュー)としてコールスタック(スタックなので、名前)を使用しようとすると、失敗しない限りかなり失敗する運命にありますあなたがすべきではないコールスタックで愚かにばかげたこと。
同様に、実装しようとする末尾以外の再帰の性質は、基本的にアルゴリズムにスタックを追加することです。これにより、バイナリツリーでの最初の検索の幅が狭くなりなくなり、ランタイムや従来のBFS以外のものが完全に適用されなくなります。もちろん、いつでも簡単にループを再帰呼び出しに変えることができますが、これは意味のある再帰ではありません。
ただし、他の人が示しているように、BFSのセマンティクスに従うものをある程度のコストで実装する方法はいくつかあります。比較のコストは高いがノードトラバーサルは安い場合は、@ Simon Buchanが行ったように、葉のみを処理する反復深さ優先検索を実行できます。これは、ヒープに格納されている成長するキューがなく、ローカルの深さ変数であり、ツリーが何度もトラバースされるときに、コールスタック上にスタックが何度も構築されることを意味します。また、@ Patrickが指摘したように、配列に基づくバイナリツリーは通常、最初に幅優先トラバーサル順序で格納されるため、これに対する幅優先検索は簡単であり、補助キューも必要ありません。
配列を使用して二分木を後退させる場合、次のノードを代数的に決定できます。場合iノードで、その子供たちはで見つけることができます2i + 1(左ノード用)と2i + 2(右ノードのため)。ノードの次のネイバーは、で与えられi + 1ない限りi、2
これは、配列に基づくバイナリ検索ツリーでの幅優先検索の非常に単純な実装の疑似コードです。これは、固定サイズの配列、したがって固定深さツリーを想定しています。親のないノードを調べて、管理できないほど大きなスタックを作成する可能性があります。
bintree-bfs(bintree, elt, i)
    if (i == LENGTH)
        return false
    else if (bintree[i] == elt)
        return true
    else 
        return bintree-bfs(bintree, elt, i+1)        
私はそれを完全に再帰的にする方法を見つけることができませんでした(補助的なデータ構造なしで)。ただし、キューQが参照渡しされる場合、次の愚かな末尾再帰関数を使用できます。
BFS(Q)
{
  if (|Q| > 0)
     v <- Dequeue(Q)
     Traverse(v)
     foreach w in children(v)
        Enqueue(Q, w)    
     BFS(Q)
}
次の方法では、DFSアルゴリズムを使用して、特定の深度のすべてのノードを取得しました。これは、そのレベルでBFSを実行するのと同じです。ツリーの深さを確認し、すべてのレベルでこれを行うと、結果はBFSと同じになります。
public void PrintLevelNodes(Tree root, int level) {
    if (root != null) {
        if (level == 0) {
            Console.Write(root.Data);
            return;
        }
        PrintLevelNodes(root.Left, level - 1);
        PrintLevelNodes(root.Right, level - 1);
    }
}
for (int i = 0; i < depth; i++) {
    PrintLevelNodes(root, i);
}
木の深さを見つけることは簡単です:
public int MaxDepth(Tree root) {
    if (root == null) {
        return 0;
    } else {
        return Math.Max(MaxDepth(root.Left), MaxDepth(root.Right)) + 1;
    }
}
levelはゼロになるまで返らないからです。
                    Javaでの単純なBFSおよびDFS再帰:
スタック/キュー内のツリーのルートノードをプッシュ/提供し、これらの関数を呼び出すだけです。
public static void breadthFirstSearch(Queue queue) {
    if (queue.isEmpty())
        return;
    Node node = (Node) queue.poll();
    System.out.println(node + " ");
    if (node.right != null)
        queue.offer(node.right);
    if (node.left != null)
        queue.offer(node.left);
    breadthFirstSearch(queue);
}
public static void depthFirstSearch(Stack stack) {
    if (stack.isEmpty())
        return;
    Node node = (Node) stack.pop();
    System.out.println(node + " ");
    if (node.right != null)
        stack.push(node.right);
    if (node.left != null)
        stack.push(node.left);
    depthFirstSearch(stack);
}私は非常に美しい再帰的な(機能的な)幅優先トラバーサル関連アルゴリズムを見つけました。私の考えではありませんが、このトピックで言及する必要があると思います。
Chris Okasakiは、http: //okasaki.blogspot.de/2008/07/breadth-first-numbering-algorithm-in.htmlでICFP 2000の幅優先の番号付けアルゴリズムを非常に明確に3つの写真で説明しています。
私がhttp://debasishg.blogspot.de/2008/09/breadth-first-numbering-okasakis.htmlで見つけたDebasish GhoshのScala実装は、次のとおりです。
trait Tree[+T]
case class Node[+T](data: T, left: Tree[T], right: Tree[T]) extends Tree[T]
case object E extends Tree[Nothing]
def bfsNumForest[T](i: Int, trees: Queue[Tree[T]]): Queue[Tree[Int]] = {
  if (trees.isEmpty) Queue.Empty
  else {
    trees.dequeue match {
      case (E, ts) =>
        bfsNumForest(i, ts).enqueue[Tree[Int]](E)
      case (Node(d, l, r), ts) =>
        val q = ts.enqueue(l, r)
        val qq = bfsNumForest(i+1, q)
        val (bb, qqq) = qq.dequeue
        val (aa, tss) = qqq.dequeue
        tss.enqueue[org.dg.collection.BFSNumber.Tree[Int]](Node(i, aa, bb))
    }
  }
}
def bfsNumTree[T](t: Tree[T]): Tree[Int] = {
  val q = Queue.Empty.enqueue[Tree[T]](t)
  val qq = bfsNumForest(1, q)
  qq.dequeue._1
}
ばかげた方法:
template<typename T>
struct Node { Node* left; Node* right; T value; };
template<typename T, typename P>
bool searchNodeDepth(Node<T>* node, Node<T>** result, int depth, P pred) {
    if (!node) return false;
    if (!depth) {
        if (pred(node->value)) {
            *result = node;
        }
        return true;
    }
    --depth;
    searchNodeDepth(node->left, result, depth, pred);
    if (!*result)
        searchNodeDepth(node->right, result, depth, pred);
    return true;
}
template<typename T, typename P>
Node<T>* searchNode(Node<T>* node, P pred) {
    Node<T>* result = NULL;
    int depth = 0;
    while (searchNodeDepth(node, &result, depth, pred) && !result)
        ++depth;
    return result;
}
int main()
{
    // a c   f
    //  b   e
    //    d
    Node<char*>
        a = { NULL, NULL, "A" },
        c = { NULL, NULL, "C" },
        b = { &a, &c, "B" },
        f = { NULL, NULL, "F" },
        e = { NULL, &f, "E" },
        d = { &b, &e, "D" };
    Node<char*>* found = searchNode(&d, [](char* value) -> bool {
        printf("%s\n", value);
        return !strcmp((char*)value, "F");
    });
    printf("found: %s\n", found->value);
    return 0;
}
以下は短いScalaソリューションです:
  def bfs(nodes: List[Node]): List[Node] = {
    if (nodes.nonEmpty) {
      nodes ++ bfs(nodes.flatMap(_.children))
    } else {
      List.empty
    }
  }
アキュムレータとして戻り値を使用するという考えは非常に適しています。他の言語でも同様の方法で実装できます。再帰関数がノードのリストを処理することを確認してください。
テストコードリスト(@marcoテストツリーを使用):
import org.scalatest.FlatSpec
import scala.collection.mutable
class Node(val value: Int) {
  private val _children: mutable.ArrayBuffer[Node] = mutable.ArrayBuffer.empty
  def add(child: Node): Unit = _children += child
  def children = _children.toList
  override def toString: String = s"$value"
}
class BfsTestScala extends FlatSpec {
  //            1
  //          / | \
  //        2   3   4
  //      / |       | \
  //    5   6       7  8
  //  / |           | \
  // 9  10         11  12
  def tree(): Node = {
    val root = new Node(1)
    root.add(new Node(2))
    root.add(new Node(3))
    root.add(new Node(4))
    root.children(0).add(new Node(5))
    root.children(0).add(new Node(6))
    root.children(2).add(new Node(7))
    root.children(2).add(new Node(8))
    root.children(0).children(0).add(new Node(9))
    root.children(0).children(0).add(new Node(10))
    root.children(2).children(0).add(new Node(11))
    root.children(2).children(0).add(new Node(12))
    root
  }
  def bfs(nodes: List[Node]): List[Node] = {
    if (nodes.nonEmpty) {
      nodes ++ bfs(nodes.flatMap(_.children))
    } else {
      List.empty
    }
  }
  "BFS" should "work" in {
    println(bfs(List(tree())))
  }
}
出力:
List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
これがPythonの実装です:
graph = {'A': ['B', 'C'],
         'B': ['C', 'D'],
         'C': ['D'],
         'D': ['C'],
         'E': ['F'],
         'F': ['C']}
def bfs(paths, goal):
    if not paths:
        raise StopIteration
    new_paths = []
    for path in paths:
        if path[-1] == goal:
            yield path
        last = path[-1]
        for neighbor in graph[last]:
            if neighbor not in path:
                new_paths.append(path + [neighbor])
    yield from bfs(new_paths, goal)
for path in bfs([['A']], 'D'):
    print(path)
以下は、再帰的なBFSのScala 2.11.4実装です。簡潔にするために末尾呼び出しの最適化を犠牲にしましたが、TCOdバージョンは非常に似ています。@snvの投稿も参照してください。
import scala.collection.immutable.Queue
object RecursiveBfs {
  def bfs[A](tree: Tree[A], target: A): Boolean = {
    bfs(Queue(tree), target)
  }
  private def bfs[A](forest: Queue[Tree[A]], target: A): Boolean = {
    forest.dequeueOption exists {
      case (E, tail) => bfs(tail, target)
      case (Node(value, _, _), _) if value == target => true
      case (Node(_, l, r), tail) => bfs(tail.enqueue(List(l, r)), target)
    }
  }
  sealed trait Tree[+A]
  case class Node[+A](data: A, left: Tree[A], right: Tree[A]) extends Tree[A]
  case object E extends Tree[Nothing]
}
以下は、Haskellを使用して、私にはかなり自然に思えます。ツリーのレベルを再帰的に反復します(ここでは、ツリーを通るパスを示すために、名前を大きな順序付けされた文字列に収集します)。
data Node = Node {name :: String, children :: [Node]}
aTree = Node "r" [Node "c1" [Node "gc1" [Node "ggc1" []], Node "gc2" []] , Node "c2" [Node "gc3" []], Node "c3" [] ]
breadthFirstOrder x = levelRecurser [x]
    where levelRecurser level = if length level == 0
                                then ""
                                else concat [name node ++ " " | node <- level] ++ levelRecurser (concat [children node | node <- level])
これは、BFS再帰トラバーサルPython実装であり、サイクルのないグラフで機能します。
def bfs_recursive(level):
    '''
     @params level: List<Node> containing the node for a specific level.
    '''
    next_level = []
    for node in level:
        print(node.value)
        for child_node in node.adjency_list:
            next_level.append(child_node)
    if len(next_level) != 0:
        bfs_recursive(next_level)
class Node:
    def __init__(self, value):
        self.value = value
        self.adjency_list = []
言語がジェネレーターのようなものをサポートする場合、bfsは再帰的に実行できるという点で、私のセントをトップの回答に追加したいと思います。
まず、@ Tanzelaxの答えは次のとおりです。
幅優先トラバーサルは、従来、スタックではなくキューを使用します。キューとスタックの性質はかなり逆なので、補助記憶装置(キュー)としてコールスタック(スタックなので、名前)を使用しようとすると、かなり失敗する運命にあります
実際、通常の関数呼び出しのスタックは、通常のスタックのようには動作しません。しかし、ジェネレーター関数は関数の実行を一時停止するため、ノードのより深い子孫を掘り下げることなく、ノードの子の次のレベルを生成する機会が得られます。
次のコードは、Pythonの再帰 bfsです。
def bfs(root):
  yield root
  for n in bfs(root):
    for c in n.children:
      yield cここでの直感は:
BFSオーダーで出力するヒープトラバーサルを実装する必要がありました。実際にはBFSではありませんが、同じタスクを実行します。
private void getNodeValue(Node node, int index, int[] array) {
    array[index] = node.value;
    index = (index*2)+1;
    Node left = node.leftNode;
    if (left!=null) getNodeValue(left,index,array);
    Node right = node.rightNode;
    if (right!=null) getNodeValue(right,index+1,array);
}
public int[] getHeap() {
    int[] nodes = new int[size];
    getNodeValue(root,0,nodes);
    return nodes;
}
vを開始頂点とする
問題のグラフをGとする
以下は、キューを使用しない疑似コードです
Initially label v as visited as you start from v
BFS(G,v)
    for all adjacent vertices w of v in G:
        if vertex w is not visited:
            label w as visited
    for all adjacent vertices w of v in G:
        recursively call BFS(G,w)
バイナリ(またはn-ary)ツリーのBFSは、次のように(ここではJavaで)キューなしで再帰的に実行できます。
public class BreathFirst {
    static class Node {
        Node(int value) {
            this(value, 0);
        }
        Node(int value, int nChildren) {
            this.value = value;
            this.children = new Node[nChildren];
        }
        int value;
        Node[] children;
    }
    static void breathFirst(Node root, Consumer<? super Node> printer) {
        boolean keepGoing = true;
        for (int level = 0; keepGoing; level++) {
            keepGoing = breathFirst(root, printer, level);
        }
    }
    static boolean breathFirst(Node node, Consumer<? super Node> printer, int depth) {
        if (depth < 0 || node == null) return false;
        if (depth == 0) {
            printer.accept(node);
            return true;
        }
        boolean any = false;
        for (final Node child : node.children) {
            any |= breathFirst(child, printer, depth - 1);
        }
        return any;
    }
}
トラバース探索の例は、昇順で番号1〜12を出力します。
public static void main(String... args) {
    //            1
    //          / | \
    //        2   3   4
    //      / |       | \
    //    5   6       7  8
    //  / |           | \
    // 9  10         11  12
    Node root = new Node(1, 3);
    root.children[0] = new Node(2, 2);
    root.children[1] = new Node(3);
    root.children[2] = new Node(4, 2);
    root.children[0].children[0] = new Node(5, 2);
    root.children[0].children[1] = new Node(6);
    root.children[2].children[0] = new Node(7, 2);
    root.children[2].children[1] = new Node(8);
    root.children[0].children[0].children[0] = new Node(9);
    root.children[0].children[0].children[1] = new Node(10);
    root.children[2].children[0].children[0] = new Node(11);
    root.children[2].children[0].children[1] = new Node(12);
    breathFirst(root, n -> System.out.println(n.value));
}
#include <bits/stdc++.h>
using namespace std;
#define Max 1000
vector <int> adj[Max];
bool visited[Max];
void bfs_recursion_utils(queue<int>& Q) {
    while(!Q.empty()) {
        int u = Q.front();
        visited[u] = true;
        cout << u << endl;
        Q.pop();
        for(int i = 0; i < (int)adj[u].size(); ++i) {
            int v = adj[u][i];
            if(!visited[v])
                Q.push(v), visited[v] = true;
        }
        bfs_recursion_utils(Q);
    }
}
void bfs_recursion(int source, queue <int>& Q) {
    memset(visited, false, sizeof visited);
    Q.push(source);
    bfs_recursion_utils(Q);
}
int main(void) {
    queue <int> Q;
    adj[1].push_back(2);
    adj[1].push_back(3);
    adj[1].push_back(4);
    adj[2].push_back(5);
    adj[2].push_back(6);
    adj[3].push_back(7);
    bfs_recursion(1, Q);
    return 0;
}
以下は、深さ優先の再帰で幅優先トラバーサルを偽装するJavaScript実装です。ハッシュ内の配列内の各深度でノード値を格納しています。レベルが既に存在する場合(衝突がある場合)、そのレベルの配列にプッシュするだけです。レベルは数値であり、配列インデックスとして機能できるため、JavaScriptオブジェクトの代わりに配列を使用することもできます。ノード、値を返したり、リンクリストに変換したり、好きなものを何でもできます。単純化するために、単に値を返しています。
BinarySearchTree.prototype.breadthFirstRec = function() {
    var levels = {};
    var traverse = function(current, depth) {
        if (!current) return null;
        if (!levels[depth]) levels[depth] = [current.value];
        else levels[depth].push(current.value);
        traverse(current.left, depth + 1);
        traverse(current.right, depth + 1);
    };
    traverse(this.root, 0);
    return levels;
};
var bst = new BinarySearchTree();
bst.add(20, 22, 8, 4, 12, 10, 14, 24);
console.log('Recursive Breadth First: ', bst.breadthFirstRec());
/*Recursive Breadth First:  
{ '0': [ 20 ],
  '1': [ 8, 22 ],
  '2': [ 4, 12, 24 ],
  '3': [ 10, 14 ] } */
以下は、反復アプローチを使用した実際の幅優先トラバーサルの例です。
BinarySearchTree.prototype.breadthFirst = function() {
    var result = '',
        queue = [],
        current = this.root;
    if (!current) return null;
    queue.push(current);
    while (current = queue.shift()) {
        result += current.value + ' ';
        current.left && queue.push(current.left);
        current.right && queue.push(current.right);
    }
    return result;
};
console.log('Breadth First: ', bst.breadthFirst());
//Breadth First:  20 8 22 4 12 24 10 14
以下は、ループとキューを使用せずに双方向グラフの幅優先探索を完全に再帰的に実装するための私のコードです。
public class Graph
{
    public int V;   
    public LinkedList<Integer> adj[];
    Graph(int v)
    {
        V = v;
        adj = new LinkedList[v];
        for (int i=0; i<v; ++i)
            adj[i] = new LinkedList<>();
    }
    void addEdge(int v,int w)
    {
        adj[v].add(w);
        adj[w].add(v);
    }
    public LinkedList<Integer> getAdjVerted(int vertex)
    {
        return adj[vertex];
    }
    public String toString()
    {
        String s = "";
        for (int i=0;i<adj.length;i++)
        {
            s = s +"\n"+i +"-->"+ adj[i] ;
        }
        return s;
    }
}
//BFS IMPLEMENTATION
public static void recursiveBFS(Graph graph, int vertex,boolean visited[], boolean isAdjPrinted[])
    {
        if (!visited[vertex])
        {
            System.out.print(vertex +" ");
            visited[vertex] = true;
        }
        if(!isAdjPrinted[vertex])
        {
            isAdjPrinted[vertex] = true;
            List<Integer> adjList = graph.getAdjVerted(vertex);
            printAdjecent(graph, adjList, visited, 0,isAdjPrinted);
        }
    }
    public static void recursiveBFS(Graph graph, List<Integer> vertexList, boolean visited[], int i, boolean isAdjPrinted[])
    {
        if (i < vertexList.size())
        {
            recursiveBFS(graph, vertexList.get(i), visited, isAdjPrinted);
            recursiveBFS(graph, vertexList, visited, i+1, isAdjPrinted);
        }
    }
    public static void printAdjecent(Graph graph, List<Integer> list, boolean visited[], int i, boolean isAdjPrinted[])
    {
        if (i < list.size())
        {
            if (!visited[list.get(i)])
            {
                System.out.print(list.get(i)+" ");
                visited[list.get(i)] = true;
            }
            printAdjecent(graph, list, visited, i+1, isAdjPrinted);
        }
        else
        {
            recursiveBFS(graph, list, visited, 0, isAdjPrinted);
        }
    }
バイナリツリーの再帰的な幅優先検索アルゴリズムのC#実装。
IDictionary<string, string[]> graph = new Dictionary<string, string[]> {
    {"A", new [] {"B", "C"}},
    {"B", new [] {"D", "E"}},
    {"C", new [] {"F", "G"}},
    {"E", new [] {"H"}}
};
void Main()
{
    var pathFound = BreadthFirstSearch("A", "H", new string[0]);
    Console.WriteLine(pathFound); // [A, B, E, H]
    var pathNotFound = BreadthFirstSearch("A", "Z", new string[0]);
    Console.WriteLine(pathNotFound); // []
}
IEnumerable<string> BreadthFirstSearch(string start, string end, IEnumerable<string> path)
{
    if (start == end)
    {
        return path.Concat(new[] { end });
    }
    if (!graph.ContainsKey(start)) { return new string[0]; }    
    return graph[start].SelectMany(letter => BreadthFirstSearch(letter, end, path.Concat(new[] { start })));
}
アルゴリズムをバイナリツリーだけでなく、別の同じノードを指す2つ以上のノードを持つグラフで機能させる場合は、既にアクセスしたノードのリストを保持して、自己循環を回避する必要があります。実装は次のようになります。
IDictionary<string, string[]> graph = new Dictionary<string, string[]> {
    {"A", new [] {"B", "C"}},
    {"B", new [] {"D", "E"}},
    {"C", new [] {"F", "G", "E"}},
    {"E", new [] {"H"}}
};
void Main()
{
    var pathFound = BreadthFirstSearch("A", "H", new string[0], new List<string>());
    Console.WriteLine(pathFound); // [A, B, E, H]
    var pathNotFound = BreadthFirstSearch("A", "Z", new string[0], new List<string>());
    Console.WriteLine(pathNotFound); // []
}
IEnumerable<string> BreadthFirstSearch(string start, string end, IEnumerable<string> path, IList<string> visited)
{
    if (start == end)
    {
        return path.Concat(new[] { end });
    }
    if (!graph.ContainsKey(start)) { return new string[0]; }
    return graph[start].Aggregate(new string[0], (acc, letter) =>
    {
        if (visited.Contains(letter))
        {
            return acc;
        }
        visited.Add(letter);
        var result = BreadthFirstSearch(letter, end, path.Concat(new[] { start }), visited);
        return acc.Concat(result).ToArray();
    });
}
c ++を使用して、結合グラフと非結合グラフでも動作するプログラムを作成しました。
    #include <queue>
#include "iostream"
#include "vector"
#include "queue"
using namespace std;
struct Edge {
    int source,destination;
};
class Graph{
    int V;
    vector<vector<int>> adjList;
public:
    Graph(vector<Edge> edges,int V){
        this->V = V;
        adjList.resize(V);
        for(auto i : edges){
            adjList[i.source].push_back(i.destination);
            //     adjList[i.destination].push_back(i.source);
        }
    }
    void BFSRecursivelyJoinandDisjointtGraphUtil(vector<bool> &discovered, queue<int> &q);
    void BFSRecursivelyJointandDisjointGraph(int s);
    void printGraph();
};
void Graph :: printGraph()
{
    for (int i = 0; i < this->adjList.size(); i++)
    {
        cout << i << " -- ";
        for (int v : this->adjList[i])
            cout <<"->"<< v << " ";
        cout << endl;
    }
}
void Graph ::BFSRecursivelyJoinandDisjointtGraphUtil(vector<bool> &discovered, queue<int> &q) {
    if (q.empty())
        return;
    int v = q.front();
    q.pop();
    cout << v <<" ";
    for (int u : this->adjList[v])
    {
        if (!discovered[u])
        {
            discovered[u] = true;
            q.push(u);
        }
    }
    BFSRecursivelyJoinandDisjointtGraphUtil(discovered, q);
}
void Graph ::BFSRecursivelyJointandDisjointGraph(int s) {
    vector<bool> discovered(V, false);
    queue<int> q;
    for (int i = s; i < V; i++) {
        if (discovered[i] == false)
        {
            discovered[i] = true;
            q.push(i);
            BFSRecursivelyJoinandDisjointtGraphUtil(discovered, q);
        }
    }
}
int main()
{
    vector<Edge> edges =
            {
                    {0, 1}, {0, 2}, {1, 2}, {2, 0}, {2,3},{3,3}
            };
    int V = 4;
    Graph graph(edges, V);
 //   graph.printGraph();
    graph.BFSRecursivelyJointandDisjointGraph(2);
    cout << "\n";
    edges = {
            {0,4},{1,2},{1,3},{1,4},{2,3},{3,4}
    };
    Graph graph2(edges,5);
    graph2.BFSRecursivelyJointandDisjointGraph(0);
    return 0;
}