<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 廣度優先搜索示例 > 原文: [https://javatutorial.net/breadth-first-search-example-java](https://javatutorial.net/breadth-first-search-example-java) 當涉及從給定數據結構訪問數據時,搜索或遍歷非常重要。 在這些數據結構中,例如[圖](https://javatutorial.net/graphs-java-example)和樹,有多種遍歷/搜索元素的方法。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) 廣度優先搜索是這些方法的一個示例。 BFS 是一種遍歷樹或圖形的算法,它從樹的根(樹中的最高節點)開始或僅從頂部開始,并在當前深度掃描所有相鄰節點,然后再繼續移動到節點或元素。 下一個深度級別。 簡而言之,BFS 必須完成一層,然后繼續進行下一層直到沒有剩余的任何層。 BFS 使用與深度優先搜索完全相反的工作流程,反之亦然。 在 BFS 和 DFS 之間的實現方面,一個很大的不同是 BFS 使用隊列,而 DFS 使用棧。 ![Workflow of BFS](https://img.kancloud.cn/d1/b1/d1b17382cbdb8d0bf72d9b41efbcd198_509x278.jpg) BFS 的工作流程 ## BFS 的實現 實現 BFS 時要遵循兩個簡單的規則: 1. 訪問給定層上的每個元素 2. 移至下一層 一個例子: ![Layers example](https://img.kancloud.cn/e6/06/e6068ee2d1bfa0f1221b26a3d3eb1db8_587x266.jpg) 在繼續進行第 2 層之前,必須先通過第 1 層。 ![Layers BFS](https://img.kancloud.cn/46/ff/46ff43d0a3be7a51754a6755f2113324_686x380.jpg) 在那之后,這是應該怎么做的: ![Layers BFS](https://img.kancloud.cn/17/8b/178b34bca6fe360f3baeab9c866ddd5e_605x278.jpg) **偽代碼** ```java public void breadthFirstSearch(Graph G, Object S) // G => Graph ; S => Source node { define a queue named as Queue; insert the source node in the Q mark s as visited perform a while loop that keeps looping until the queue is not empty removing the current element from the queue as it will be visited now perform a for loop that goes through all neighbours of the current element if condition that checks if the current element/node/vertex is not visited if it has not been visited, enqueue it and mark it as visited } ``` **實際代碼實現** ```java public void BFS(int s, int l) { // create an array that holds boolean values that will have length 'l' boolean visited[] = new boolean[l]; // create a queue LinkedList<Integer> q = new LinkedList<Integer>(); // mark the current node as visited and add it to the queue visited[s]=true; q.add(s); while (q.size() != 0) { // dequeuing a vertex from queue s = q.poll(); // get all adjacent vertices of the dequeued vertex if a adjacent has not // been visited, then mark it visited and enqueue it Iterator<Integer> k = adj[s].listIterator(); while (k.hasNext()) { int j = k.next(); if (!visited[j]) { visited[j] = true; q.add(j); } } } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看