日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

發布時間:2025/4/5 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode从零单排】No133. clon graph (BFS广度优先搜索) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景

(以下背景資料轉載自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Search)
顧名思義,就是深度搜索,一條路走到黑,再選新的路。
記得上Algorithm的時候,教授舉得例子就是說,DFS很像好奇的小孩,你給這個小孩幾個盒子套盒子,好奇的小孩肯定會一個盒子打開后繼續再在這個盒子里面搜索。
等把這一套盒子都打開完,再打開第二套的。
Wikipedia上的講解是:“Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures.
One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible
along each branch before backtracking.”
通常來說簡便的DFS寫法是用遞歸,如果非遞歸的話就是棧套迭代,思想是一樣的。
遞歸寫法的DFS偽代碼如下:
Input: A graph G and a root v of G
1???procedure?DFS(G,v):
2???????label?v?as?discovered
3???????for?all?edges?from?v?to?w?in?G.adjacentEdges(v)?do
4???????????if?vertex?w?is?not?labeled?as?discovered?then
5???????????????recursively?call?DFS(G,w)非遞歸寫法的DFS偽代碼如下:
Input: A graph G and a root v of G
1???procedure?DFS-iterative(G,v):
2???????let?S?be?a?stack
3???????S.push(v)
4???????while?S?is?not?empty
5?????????????v?←?S.pop()?
6?????????????if?v?is?not?labeled?as?discovered:
7?????????????????label?v?as?discovered
8?????????????????for?all?edges?from?v?to?w?in?G.adjacentEdges(v)?do
9?????????????????????S.push(w)


BFS(Breadth-first Search)
這個就是相對于BFS的另外一種對圖的遍歷方法,對于一個節點來說先把所有neighbors都檢查一遍,再從第一個neighbor開始,循環往復。
由于BFS的這個特質,BFS可以幫助尋找最短路徑。
Wikipedia上面對BFS的定義是:
“In graph theory, breadth-first search (BFS) is a strategy for searching in a graphwhen search is limited to essentially two operations: (a) visit and inspect a node of a graph; (b) gain access to visit the nodes that neighbor the currently visited node. The BFS begins at a root node and inspects all the neighboring nodes. Then for each of those neighbor nodes in turn, it inspects their neighbor nodes which were unvisited, and so on. Compare BFS with the equivalent, but more memory-efficient
Iterative deepening depth-first search and contrast with depth-first search.”


題目

Clone an undirected graph. Each node in the graph contains a?label?and a list of its?neighbors.


OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use?#?as a separator for each node, and?,?as a separator for node label and each neighbor of the node.

As an example, consider the serialized graph?{0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by?#.

  • First node is labeled as?0. Connect node?0?to both nodes?1?and?2.
  • Second node is labeled as?1. Connect node?1?to node?2.
  • Third node is labeled as?2. Connect node?2?to node?2?(itself), thus forming a self-cycle.
  • Visually, the graph looks like the following:

    1/ \/ \0 --- 2/ \\_/
    總之就是遍歷搜有的路徑,然后再一個一個節點添加進去。

    代碼

    /*** Definition for undirected graph.* class UndirectedGraphNode {* int label;* List<UndirectedGraphNode> neighbors;* UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }* };*/ public class Solution {public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {if (node==null) return null;HashMap<UndirectedGraphNode,UndirectedGraphNode> hm=new HashMap<UndirectedGraphNode,UndirectedGraphNode>();LinkedList<UndirectedGraphNode> queue=new LinkedList<UndirectedGraphNode>();UndirectedGraphNode head = new UndirectedGraphNode(node.label);hm.put(node, head);queue.add(node);while(!queue.isEmpty()){UndirectedGraphNode current=queue.poll();for(UndirectedGraphNode neighbor:current.neighbors){if(!hm.containsKey(neighbor)){queue.add(neighbor);UndirectedGraphNode temp=new UndirectedGraphNode(neighbor.label);hm.put(neighbor,temp);}hm.get(current).neighbors.add(hm.get(neighbor));}}return head;} }

    代碼下載:https://github.com/jimenbian/GarvinLeetCode


    /********************************

    * 本文來自博客 ?“李博Garvin“

    * 轉載請標明出處:http://blog.csdn.net/buptgshengod

    ******************************************/




    總結

    以上是生活随笔為你收集整理的【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。