"Leetcode from zero single row" No133. Clon graph (BFs breadth First search)

Source: Internet
Author: User

Background (The following background information is reproduced from: Http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool)
DFS (Dpeth-first Search)
As the name implies, is the deep search, a road to the black, and then choose a new road.
Remember when algorithm, the professor cited the example is that Dfs is like a curious child, you give the child a box of boxes, curious children will certainly be a box open and continue to search in this box.
After you have opened this set of boxes, open the second set.
Wikipedia's explanation is: "Depth-first Search(DFS) is a 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. "
In general, a simple DFS notation is recursive, and if it is not recursive, it is the stack iteration, the idea is the same.
The recursive code for DFS pseudocode is as follows:
Input: A graphGand a rootvof G
1Procedure DFS (G,V):
2Label V as discovered
3 forAll edges from V to W in G.adjacentedges (v) Do
4 ifVertex W isn't labeled as discovered then
5Recursively call DFS (G,W)
The DFS pseudo-code for non-recursive notation is as follows:
Input G v of G
1Procedure Dfs-iterative (G,V):
2Let S be a stack
3S.push (v)
4 whileS is not empty
5V←s.pop ()
6ifV is not labeled as discovered:
7Label V as discovered
8 forAll edges from V to W in G.adjacentedges (v) Do
9S.push (W)



BFS (Breadth-first Search)
This is the relative to the bfs of the other way of traversing the graph, for a node to first check all neighbors, and then start from the first neighbor, the cycle.
Because of this characteristic of BFS, BFS can help find the shortest path.
Wikipedia's definition of BFS above is:
"In graph theory,Breadth-first Search(BFS) is a strategy for searching in a graph when search was limited to essentially, operations: (a) visit and inspect a nod E 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 the 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. "


Topic

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


OJ ' s undirected graph serialization:

Nodes is labeled uniquely.

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

{0 , 1,2# 1 , * 2 , 2} .

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

  1. First node is labeled as0. Connect node0to both nodes1and2.
  2. Second node is labeled as1. Connect node1to Node2.
  3. Third node is labeled as2. Connect node2to Node2(itself), thus forming a self-cycle.

Visually, the graph looks like the following:

       1      /      /       0---2         /          \_/

in short, traverse the search path, and then add one more node.

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) Retu        RN 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 from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

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




"Leetcode from zero single row" No133. Clon graph (BFs breadth First search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.