LeetCode Clone Graph

本文介绍了一种无向图的深拷贝方法,通过DFS和BFS两种方式实现图的复制,确保新图与原图完全独立且结构相同。

摘要生成于 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 #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. 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
         / \
         \_/
题意:

给一个图,将这个图复制给另一个图,其中图描述是这样的,如果有节点重复,那么就不考虑了,正如上面题目中写的那样,比如上面的那个图,就是这样复制的,那么我们在复制的时候,考虑用DFS或者是BFS来做,当然在实际操作的时候,用map来存储相关的节点信息,用map来排除已经复制过的节点信息。其实复制过程采用两个遍历来做,首先是用一个map来保存图中的所有节点,其中key和value分别表示的是旧的图每个节点和新的图每个节点,那么首先如果用DFS来做,那么就是用栈来做;如果换成是BFS,那么就是采用队列来保存。每一次外循环的时候,我们保存的是图中的所有的节点,而内层循环保存的是新的节点的邻居节点。但是如果碰到有环的情况,那么我们就可以将这个节点直接加到刚才的那个节点中去。

public class Solution 
{
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node)
	{
		if(node == null)
			return null;
		Stack<UndirectedGraphNode> stack = new Stack<UndirectedGraphNode>();
		HashMap<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
		UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
		stack.push(node);
		map.put(node, newHead);
		while(!stack.isEmpty())
		{
			UndirectedGraphNode curr = stack.pop();
			List<UndirectedGraphNode> currneighbors = curr.neighbors;
			for(UndirectedGraphNode aneighbor : currneighbors)
			{
				if(!map.containsKey(aneighbor))
				{
					UndirectedGraphNode copy = new UndirectedGraphNode(aneighbor.label);
					map.put(aneighbor, copy);
					map.get(curr).neighbors.add(copy);
					stack.add(aneighbor);
				}
				else
				{
					map.get(curr).neighbors.add(map.get(aneighbor));
				}
			}
		}
		
		return newHead;
	}
}
以上是用栈来做的,也就是DFS;再来一种用队列来做的,就是BFS。

public UndirectedGraphNode cloneGraph(UndirectedGraphNode node)
{
		if(node == null)
			return null;
		LinkedList<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
		HashMap<UndirectedGraphNode,UndirectedGraphNode> map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
		UndirectedGraphNode newHead = new UndirectedGraphNode(node.label);
		queue.add(node);
		map.put(node, newHead);
		while(!queue.isEmpty())
		{
			UndirectedGraphNode current = queue.pop();
			List<UndirectedGraphNode> currNeighbors = current.neighbors;
			for(UndirectedGraphNode aNeighbor : currNeighbors)
			{
				if(!map.containsKey(aNeighbor))
				{
					UndirectedGraphNode copy = new UndirectedGraphNode(aNeighbor.label);
					map.put(aNeighbor, copy);
					map.get(current).neighbors.add(copy);
					queue.add(aNeighbor);
				}
				else
				{
					map.get(current).neighbors.add(map.get(aNeighbor));
				}
			}
		}
		return newHead;
}
这两种方法都是异曲同工的,而且我们可以看到原理基本都是一致的,所以我们在做的时候,每次都是把节点加进来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值