[LeetCode from zero to single row] No133. clon graph (BFS breadth-first search), leetcodebfs
Background (the following background information reproduced from: http://www.cnblogs.com/springfor/p/3874591.html? Utm_source = tuicool)
DFS (Dpeth-first Search)
As its name implies, it is a deep search, where one path goes to the dark and another path is selected.
I remember when I went to Algorithm, the professor gave an example, that is, DFS is like a curious child. You gave the child a few boxes, curious children will certainly open a box and then search for it again.
After opening the box, open the second one.
Wikipedia explained: "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 has es as far as possible
Along each branch before backtracking ."
Generally, the simple Syntax of DFS is recursion. If it is not recursion, It is stack iteration. The idea is the same.
The DFS pseudocode for Recursive writing is as follows:
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)
The DFS pseudocode for non-recursive writing is as follows:
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 ipvs. 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)
This is another method of traversing the graph relative to BFS. For a node, check all neighbors first, and then start from the first neighbor.
BFS can help find the shortest path because of this feature.
The definition of BFS on Wikipedia is as follows:
"In graph theory, breadth-first search (BFS) is a strategy for searching in a graph when search is limited to essential two operations: () 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 ."
Question
Clone an undirected graph. Each node in the graph containslabel
And a list of itsneighbors
.
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#
.
- First node is labeled
0
. Connect node0
To both nodes1
And2
.
- Second node is labeled
1
. Connect node1
To node2
.
- Third node is labeled
2
. Connect node2
To node2
(Itself), thus forming a self-cycle.
Visually, the graph looks like the following:
1 / \ / \ 0 --- 2 / \ \_/
In short, it is to traverse the searched path and then add one node to it.
Code
/** * 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; }}
Code download: https://github.com/jimenbian/GarvinLeetCode
/********************************
* This article is from the blog "Li bogarvin"
* Reprinted please indicate the source: http://blog.csdn.net/buptgshengod
**************************************** **/