美文网首页
克隆无向图

克隆无向图

作者: devmisaky | 来源:发表于2019-07-10 18:54 被阅读0次
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Collections;

class Program

{

    public class UndirectedGraphNode

    {

    public int label;

    public IList<UndirectedGraphNode> neighbors;

    public UndirectedGraphNode(int x) { label = x; neighbors = new List<UndirectedGraphNode>(); }

    };

    static void Main(string[] args)

    {

        //克隆一张无向图,图中的每个节点包含一个 label (标签)和一个 neighbors (邻接点)列表 。

        //OJ的无向图序列化:

        //节点被唯一标记。

        //我们用 # 作为每个节点的分隔符,用 , 作为节点标签和邻接点的分隔符。

        //例如,序列化无向图 {

        //            0,1,2#1,2#2,2}。

        //该图总共有三个节点, 被两个分隔符  # 分为三部分。

        //第一个节点的标签为 0,存在从节点 0 到节点 1 和节点 2 的两条边。

        //第二个节点的标签为 1,存在从节点 1 到节点 2 的一条边。

        //第三个节点的标签为 2,存在从节点 2 到节点 2(本身) 的一条边,从而形成自环。

        //我们将图形可视化如下:

        //      1

        //      / \

        //    /  \

        //    0-- - 2

        //        / \

        //        \_ /

        string[] strs = Console.ReadLine().Split('#');

        for(int i = 0; i < strs.Length; i++)

        {

            string[] tmpStrs = strs[i].Split(',');

            UndirectedGraphNode node = new UndirectedGraphNode(int.Parse(tmpStrs[0]));

            List<UndirectedGraphNode> neighbors = new List<UndirectedGraphNode>();

            for (int j=1;j < tmpStrs.Length; j++)

            {

                UndirectedGraphNode tmpNode = new UndirectedGraphNode(int.Parse(tmpStrs[j]));

                neighbors.Add(tmpNode);

            }

            node.neighbors = neighbors;

            CloneGraph(node);

        }



    }

    private static UndirectedGraphNode CloneGraph(UndirectedGraphNode node)

    {

        return Clone(node);

    }

    private static Hashtable map = new Hashtable();

    private static UndirectedGraphNode Clone(UndirectedGraphNode node)

    {

        if (node == null) return null;

        if (map.ContainsKey(node.label))

        {

            return map[node.label] as UndirectedGraphNode;

        }

        UndirectedGraphNode clone = new UndirectedGraphNode(node.label);

        map.Add(clone.label, clone);

        for (int i = 0; i < node.neighbors.Count; i++)

        {

            clone.neighbors.Add(node.neighbors[i]);

        }

        //打印

        Console.Write(clone.label);

        for(int i = 0; i < clone.neighbors.Count; i++)

        {

            Console.Write(" " + clone.neighbors[i].label);

        }

        Console.WriteLine();

        return clone;

    }

}

//在忙,日后回来补全

相关文章

  • 克隆无向图

  • Leetcode 133. Clone Graph

    题意:克隆一个无向图。 思路:因为是克隆所以需要一个map来存储克隆的映射关系,我们可以从给定的第一个节点向它的n...

  • LeetCode-133-克隆图

    克隆图 题目描述:给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。图中的每个节点都包含它的值 ...

  • 力扣(LeetCode)-133 克隆图

    本地考察的是图搜索 题目描述 克隆一张无向图,图中的每个节点包含一个 label (标签)和一个 neighbor...

  • 图的定义及抽象表示

    一、无向图 1.1 无向图的定义 边没有方向的图称为无向图。 API定义: 1.2 无向图的抽象表示 1.2.1 ...

  • 任务调度-DAG和Oozie基础

    本文主要内容 有向无环图 拓扑排序 Oozie 有向无环图 什么是有向无环图 有向无环图(Directed Acy...

  • 71_图的定义与操作

    关键词:图的定义、无向边与无向图、无向边与无向图、顶点邻接(Adjacent)的定义、度(Degree)的定义、 ...

  • 邻接矩阵深广遍历

    /无向图 //正确,无向图可以改成有向图\ //如果用模板,要记得 //template //...

  • Leetcode133克隆图

    题目 给定无向连通图中一个节点的引用,返回该图的深拷贝(克隆)。图中的每个节点都包含它的值 val(Int) 和其...

  • 【算法题】20.克隆图

    题目 给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。 图中的每个节点都包含它的值 val(i...

网友评论

      本文标题:克隆无向图

      本文链接:https://www.haomeiwen.com/subject/zadkkctx.html