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 node1to 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
/ \
\_/
克隆一个无向图,该图每个节点包含一个标签属性和一列邻居。
OJ的无向图序列化:
节点的标签是唯一的。
我们使用#作为每一个节点的分割符,用,作为节点标签和他的邻居的分隔符。
例如:无向图序列{0,1,2# 1,2# 2,2},这个图总共有3个节点,因此包含被#分割的三个部分。
第一个节点:标签是0,0的邻居是1和2.
第二个节点:标签是1,1的邻居是2.
第三个节点:标签是2,2的邻居是他自己
这个题考查了图的遍历:BFS(广度优先搜索)和DFS(深度优先搜索)
<?php
class UndirectedGraphNode {
public $label;
public $arrNeighbor;
public function __construct($label) {
$this->label = $label;
}
}
//深度优先搜索,递归方法
function cloneGraph($node) {
$arrCloneNode = array();
return dfsClone($node, $arrCloneNode);
}
function dfsClone($node, &$arrCloneNode) {
if (!$node) {
return null;
}
if ($arrCloneNode[$node->label]) {return $arrCloneNode[$node->label];}
$cloneNode = new UndirectedGraphNode($node->label);
$arrCloneNode[$node->label] = $cloneNode;
$arrNeighbor = array();
foreach ($node->arrNeighbor as $neighbor) {
$ret = dfsClone($neighbor, $arrCloneNode);
$arrNeighbor[] = $ret;
}
$cloneNode->arrNeighbor = $arrNeighbor;
return $cloneNode;
}
function dfsSearch($node, &$isVisited) {
$isVisited[$node->label] = 1;
print $node->label."\n";
foreach ($node->arrNeighbor as $neighbor) {
print "邻居是:".$neighbor->label."\n";
if ($isVisited[$neighbor->label] == 0) {
dfsSearch($neighbor, $isVisited);
}
}
}
$a = new UndirectedGraphNode(0);
$b = new UndirectedGraphNode(1);
$c = new UndirectedGraphNode(2);
$a->arrNeighbor = array($b, $c);
$b->arrNeighbor = array($c);
$c->arrNeighbor = array($c);
$ret = cloneGraph($a);
dfsSearch($ret, $isVisited = array());
<?php
class UndirectedGraphNode {
public $label;
public $arrNeighbor;
public function __construct($label) {
$this->label = $label;
}
}
//广度优先搜索,非递归方法
function cloneGraph($node) {
return dfsClone($node);
}
function dfsClone($node) {
if (!$node) {
return null;
}
$head = new UndirectedGraphNode($node->label);
$arrNode = array();
$arrCloneNode = array();
$arrCloneNode[$node->label] = $head;
array_push($arrNode, $node);
while (!empty($arrNode)) {
$node = array_pop($arrNode);
$arrNeighbor = array();
foreach ($node->arrNeighbor as $neighbor) {
if (isset($arrCloneNode[$neighbor->label])) {
$arrNeighbor[] = $arrCloneNode[$neighbor->label];
} else {
$cloneNode = new UndirectedGraphNode($neighbor->label);
$arrCloneNode[$neighbor->label] = $cloneNode;
$arrNeighbor[] = $cloneNode;
array_push($arrNode, $neighbor);
}
}
$arrCloneNode[$node->label]->arrNeighbor = $arrNeighbor;
}
return $head;
}
function dfsSearch($node, &$isVisited) {
$isVisited[$node->label] = 1;
print $node->label."\n";
foreach ($node->arrNeighbor as $neighbor) {
print "邻居是:".$neighbor->label."\n";
if ($isVisited[$neighbor->label] == 0) {
dfsSearch($neighbor, $isVisited);
}
}
}
$a = new UndirectedGraphNode(0);
$b = new UndirectedGraphNode(1);
$c = new UndirectedGraphNode(2);
$a->arrNeighbor = array($b, $c);
$b->arrNeighbor = array($c);
$c->arrNeighbor = array($c);
$ret = cloneGraph($a);
dfsSearch($ret, $isVisited = array());