资讯专栏INFORMATION COLUMN

Clone Graph

xioqua / 3206人阅读

题目:
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 node 1 to 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:

Solution

**S1. BFS get all nodes. (version with no level order)
S2. Use a Hashmap and store the mapping relationship btw old---new nodes.(create new node based on old one)
S3. get the neighbor info and connect the edge based on hashmap.**

 public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null) {
            return node;
        }

        // use bfs algorithm to traverse the graph and get all nodes.
        ArrayList nodes = getNodes(node);
        
        // copy nodes, store the old->new mapping information in a hash map
        HashMap mapping = new HashMap<>();
        for (UndirectedGraphNode n : nodes) {
            mapping.put(n, new UndirectedGraphNode(n.label));
        }
        
        // copy neighbors(edges)
        for (UndirectedGraphNode n : nodes) {
            UndirectedGraphNode newNode = mapping.get(n);
            for (UndirectedGraphNode neighbor : n.neighbors) {
                UndirectedGraphNode newNeighbor = mapping.get(neighbor);
                newNode.neighbors.add(newNeighbor);
            }
        }
        
        return mapping.get(node);
    }
    
    private ArrayList getNodes(UndirectedGraphNode node) {
        Queue queue = new LinkedList();
        HashSet set = new HashSet<>();
        
        queue.offer(node);
        set.add(node);
        while (!queue.isEmpty()) {
            UndirectedGraphNode head = queue.poll();
            for (UndirectedGraphNode neighbor : head.neighbors) {
                if(!set.contains(neighbor)){
                    set.add(neighbor);
                    queue.offer(neighbor);
                }
            }
        }
        
        return new ArrayList(set);
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66433.html

相关文章

  • [Leetcode] Clone Graph 复制图

    摘要:我们只要保证,对于第一次遇到的图节点,我们都会建立一个克隆节点,并在哈希表映射起来就行了。所以只要哈希表中有这个图节点,就说明我们之前已经将该图节点放入队列了,就不需要再处理了。 Clone Graph Clone an undirected graph. Each node in the graph contains a label and a list of its neighb...

    xietao3 评论0 收藏0
  • [LintCode/LeetCode] Clone Graph [BFS/DFS]

    摘要:开始看这道题目的时候,没有看懂和的作用。然后对这个放入的结点开始操作遍历的所有,当前遍历到的的叫做。当完成,则中没有新的结点了,退出循环。返回在中更新过的,结束。 Problem Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. We use #...

    fredshare 评论0 收藏0
  • LeetCode 133:克隆图 Clone Graph

    摘要:解题思路涉及到图的遍历无非就是深度优先搜索广度优先搜索,可以先看前几日的这篇文章就需要借助队列实现,可以借助栈也可以直接用递归实现。 题目: 给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆)。图中的每个节点都包含它的值 val(Int) 和其邻居的列表(list[Node])。 Given a reference of a node in a connected undirec...

    Simon 评论0 收藏0
  • 【算法】剑指 Offer II 110. 所有路径|797. 所有可能的路径(多语言实现)

    摘要:遍历路径,找到所有可以到达终点节点的路径就是结果。提示中说保证输入为有向无环图,所以我们可以认为节点间一定有着某种排列的顺序,从头到尾怎样可以有最多的路径呢,那就是在保证没有环路的情况下,所有节点都尽可能多的连接着其他节点。 ...

    wangdai 评论0 收藏0
  • jointJS系列之一:jointJS的的初步使用

    摘要:由于是基于的,因此对有一定的了解会对的理解和使用有较大帮助。由于是基于的,因此有视图和模型的概念。挂载的元素关联声明的元素的概念,就是图形显示的主体,可以有各种不同的形状,预设有常用的矩形椭圆平行四边形等。 一、jointJS简介 jointJS是一个基于svg的图形化工具库,在画布上画出支持拖动的svg图形,而且可以导出JSON,也能通过JSON配置导入直接生成图形。 可以基于joi...

    amuqiao 评论0 收藏0

发表评论

0条评论

xioqua

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<