摘要:開始看這道題目的時(shí)候,沒有看懂和的作用。然后對(duì)這個(gè)放入的結(jié)點(diǎn)開始操作遍歷的所有,當(dāng)前遍歷到的的叫做。當(dāng)完成,則中沒有新的結(jié)點(diǎn)了,退出循環(huán)。返回在中更新過(guò)的,結(jié)束。
Problem
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.
We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
return a deep copied graph.
ExampleHow we serialize an undirected graph.
Nodes are labeled uniquely.
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 / \_/Note
開始看這道題目的時(shí)候,沒有看懂queue和hashmap的作用。
復(fù)制一個(gè)無(wú)向圖,先分析圖的結(jié)構(gòu):label相當(dāng)于圖結(jié)點(diǎn)的value,而neighbors相當(dāng)于圖結(jié)點(diǎn)的所有next。然后考慮用什么方式復(fù)制:用queue用來(lái)標(biāo)記當(dāng)前正在復(fù)制的或已經(jīng)復(fù)制過(guò)的結(jié)點(diǎn)cur,而hashmap用來(lái)進(jìn)行對(duì)新建無(wú)向圖結(jié)點(diǎn)root進(jìn)行復(fù)制label和neighbors的操作。首先,從node結(jié)點(diǎn)開始,放入queue。然后對(duì)這個(gè)放入queue的結(jié)點(diǎn)cur開始操作:遍歷cur的所有neighbors,當(dāng)前遍歷到的的neighbors叫做next。若map中不存在這個(gè)點(diǎn),即沒有被復(fù)制過(guò),就在map中復(fù)制這個(gè)結(jié)點(diǎn)next的label,同時(shí)存入queue以便在下次循環(huán)取出并復(fù)制其neighbors;無(wú)論map中包不包含這個(gè)neighbors結(jié)點(diǎn)next,都要將next加入map中對(duì)應(yīng)新建結(jié)點(diǎn)root的neighbors。當(dāng)BFS完成,則queue中沒有新的結(jié)點(diǎn)了,退出while循環(huán)。返回在map中更新過(guò)的root,結(jié)束。
map.get(cur).neighbors.add(map.get(next));
這一句可以理解為,在for循環(huán)對(duì)cur的neighbors的遍歷中,先在HashMap里的root中建立新結(jié)點(diǎn)next,再將next放進(jìn)root的結(jié)點(diǎn)cur的neighbors里。
// Definition for undirected graph. class UndirectedGraphNode { int label;ArrayListneighbors; UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList (); } }
public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) return null; UndirectedGraphNode root = new UndirectedGraphNode(node.label);//復(fù)制根節(jié)點(diǎn) QueueUpdate 2018-9 BFSqueue = new LinkedList<>(); Map map = new HashMap<>(); queue.offer(node);//queue放入根結(jié)點(diǎn) map.put(node, root);//map放入根結(jié)點(diǎn)和它的復(fù)制結(jié)點(diǎn) while (!queue.isEmpty()) { UndirectedGraphNode cur = queue.poll();//取出queue中(同一層)的結(jié)點(diǎn)進(jìn)行BFS for (UndirectedGraphNode n: cur.neighbors) { //對(duì)沒有復(fù)制過(guò)的結(jié)點(diǎn)進(jìn)行復(fù)制,并將這個(gè)結(jié)點(diǎn)放入queue if (!map.containsKey(n)) { map.put(n, new UndirectedGraphNode(n.label)); queue.offer(n); } //連接復(fù)制結(jié)點(diǎn)和復(fù)制neighbor結(jié)點(diǎn) map.get(cur).neighbors.add(map.get(n)); } } return root; } }
public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { if (node == null) return node; Mapmap = new HashMap<>(); map.put(node, new UndirectedGraphNode(node.label)); // : <原結(jié)點(diǎn), 復(fù)制結(jié)點(diǎn)> Queue queue = new LinkedList<>(); queue.offer(node); //copy過(guò)的結(jié)點(diǎn)存入queue,以繼續(xù)遍歷其neighbor結(jié)點(diǎn) while (!queue.isEmpty()) { UndirectedGraphNode cur = queue.poll(); //取出copy過(guò)的結(jié)點(diǎn),繼續(xù)復(fù)制其所有neighbor結(jié)點(diǎn) for (UndirectedGraphNode neighbor: cur.neighbors) { if (!map.containsKey(neighbor)) { //若結(jié)點(diǎn)未復(fù)制過(guò),copy后存入queue map.put(neighbor, new UndirectedGraphNode(neighbor.label)); queue.offer(neighbor); } map.get(cur).neighbors.add(map.get(neighbor)); //將每個(gè)copied neighbor存入copied parent node } } return map.get(node); //返回copied根節(jié)點(diǎn) } }
DFS
public class Solution { public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) { Mapmap = new HashMap<>(); return cloneNode(node, map); } private UndirectedGraphNode cloneNode(UndirectedGraphNode node, Map map) { if (node == null) return node; if (map.containsKey(node)) { return map.get(node); } else { UndirectedGraphNode nodeCopy = new UndirectedGraphNode(node.label); map.put(node, nodeCopy); for (UndirectedGraphNode child : node.neighbors) { nodeCopy.neighbors.add(cloneNode(child, map)); } return nodeCopy; } } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/65729.html
摘要:解題思路涉及到圖的遍歷無(wú)非就是深度優(yōu)先搜索廣度優(yōu)先搜索,可以先看前幾日的這篇文章就需要借助隊(duì)列實(shí)現(xiàn),可以借助棧也可以直接用遞歸實(shí)現(xiàn)。 題目: 給定無(wú)向連通圖中一個(gè)節(jié)點(diǎn)的引用,返回該圖的深拷貝(克?。?。圖中的每個(gè)節(jié)點(diǎn)都包含它的值 val(Int) 和其鄰居的列表(list[Node])。 Given a reference of a node in a connected undirec...
1. 說(shuō)明 本文所有的算法嚴(yán)格按照《算法導(dǎo)論》,本文將詳細(xì)的對(duì)BFS和DFS進(jìn)行分析,并提供算法的 js 實(shí)現(xiàn),同時(shí)會(huì)對(duì)創(chuàng)建鏈表的方式進(jìn)行優(yōu)化 2. 圖的表示 圖的表示分為對(duì)頂點(diǎn)集 V 的表示和對(duì)邊集 E 的表示,這里的重點(diǎn)是如何表示邊,邊的表示分為鄰接矩陣和鄰接鏈表這兩種表示方法,鄰接矩陣適合表示邊稠密的圖,其消耗空間為|V|*|V|,如果是無(wú)向圖,則可以用上三角矩陣或者下三角矩陣來(lái)表示,是空間...
摘要:但是一個(gè)偏序關(guān)系,如果我們默認(rèn),先出現(xiàn)的排在前面,那么我們就能比較,的關(guān)系了。對(duì)于算法的每個(gè)節(jié)點(diǎn),我們都有一個(gè)發(fā)現(xiàn)時(shí)間,一個(gè)訪問(wèn)時(shí)間,當(dāng)運(yùn)行完成時(shí),對(duì)于圖中的任意一條邊,都有所以拓?fù)渑判虻拇涡蚺c頂點(diǎn)的完成時(shí)間恰好相反。 1. 偏序和全序的概念 1.1. 偏序 設(shè)R是集合A上的一個(gè)二元關(guān)系,若R滿足下列三個(gè)性質(zhì)則稱R為A上的偏序關(guān)系自反性:對(duì)任意x∈A,有∈R反對(duì)稱性:對(duì)任意的x,y∈A...
Problem There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it wont stop rolling until hitting a wall. When the ball sto...
摘要:生成樹和最小生成樹的概念設(shè)圖連通,則生成樹包含圖中的所有節(jié)點(diǎn),及條邊的連通圖,一個(gè)圖的生成樹可以有多顆最小生成樹最小權(quán)重生成樹,在生成樹的概念上加一個(gè)限制條件,即生成樹的所有邊的權(quán)值總和最小的樹,最小生成樹也可以有多顆求解最小生成樹的通用 1. 生成樹和最小生成樹的概念 設(shè)圖G(V,E)連通,則生成樹:包含圖G(V,E)中的所有節(jié)點(diǎn),及|V|-1條邊的連通圖,一個(gè)圖的生成樹可以有多顆最...
閱讀 1937·2023-04-25 23:28
閱讀 671·2023-04-25 22:49
閱讀 2415·2021-09-27 13:34
閱讀 5413·2021-09-22 15:09
閱讀 3671·2019-08-30 12:52
閱讀 2805·2019-08-29 15:26
閱讀 711·2019-08-29 11:12
閱讀 2256·2019-08-26 12:24