《leetcode-php》无向图的深度复制

本文探讨了如何通过深度优先搜索(DFS)和广度优先搜索(BFS)克隆无向图,提供了PHP实现代码,展示了如何创建图节点、进行图的遍历和克隆。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值