无向图 使用邻接表法 实现 LinkedUndigraph

实现

using System.Collections.Generic;
namespace DataStructure
{

  public class LinkedUndigraph : BaseSolution
  {
    HashSet<int> valueSet = new HashSet<int>();
    Dictionary<int, Node> valToNode = new Dictionary<int, Node>();
    List<Node> vertexes = new List<Node>();

    public void Add(int a, int b)
    {
      /*
      如果没有这个顶点,增加顶点
      增加边
       */
      Add(a);
      Add(b);
      var nodeA = Get(a);
      var nodeB = Get(b);
      if (!nodeA.neighbors.Contains(nodeB))
      {
        nodeA.neighbors.Add(nodeB);
      }
      if (!nodeB.neighbors.Contains(nodeA))
      {
        nodeB.neighbors.Add(nodeA);
      }
    }

    public void Add(int a)
    {
      if (Contains(a))
      {
        return;
      }

      valueSet.Add(a);
      var node = new Node(a, new List<Node>());
      valToNode.Add(a, node);
      vertexes.Add(node);
    }

    public bool Contains(int val)
    {
      return valueSet.Contains(val);
    }

    public void Remove(int a)
    {
      /*
      删除边,在所有的顶点中查找包含顶点a的边
      删除顶点
      删除关系数据
       */
      var node = Get(a);
      if (node == null)
      {
        return;
      }
      foreach (var aNode in vertexes)
      {
        aNode.neighbors.Remove(node);
      }
      valueSet.Remove(a);
      valToNode.Remove(a);
      vertexes.Remove(node);
    }

    public Node Get(int value)
    {
      Node node;
      if (valToNode.TryGetValue(value, out node))
      {
        return node;
      }
      return null;
    }

    public void Create(int[][] input)
    {
      /*
      找到所有节点
      建立节点关系映射
      建立图
       */
      for (var i = 0; i < input.Length; i++)
      {
        var kv = input[i];
        valueSet.Add(kv[0]);
        valueSet.Add(kv[1]);
      }

      var enumerator = valueSet.GetEnumerator();
      while (enumerator.MoveNext())
      {
        var node = new Node(enumerator.Current, new List<Node>());
        valToNode.Add(enumerator.Current, node);
        vertexes.Add(node);
      }

      for (var i = 0; i < input.Length; i++)
      {
        var kv = input[i];
        var node = valToNode[kv[0]];
        var neighbor = valToNode[kv[1]];
        //无向图 双向都加
        node.neighbors.Add(neighbor);
        neighbor.neighbors.Add(node);
      }
    }

    public void Print()
    {
      foreach (var node in vertexes)
      {
        foreach (var neighbor in node.neighbors)
        {
          println(node.val + " -> " + neighbor.val);
        }
      }
    }

  }

}

测试

using System.Collections.Generic;
namespace DataStructure
{
  // class T12{
  //   public int v;
  //   public T12(int val){
  //     v = val;
  //   }
  // }
  public class LinkedUndigraphTest : BaseSolution
  {
    public void Test1()
    {
      // var set = new HashSet<int>(new int[]{1,2,3});
      var set = new HashSet<string>(new[] { "1", "2", "3" });
      // var set = new HashSet<T12>(new []{new T12(1),new T12(2),new T12(3)});
      var enumerator = set.GetEnumerator();
      println(enumerator.Current == null);
      while (enumerator.MoveNext())
      {
        println(enumerator.Current);
      }
    }

    public void Test()
    {
      var graph = new LinkedUndigraph();
      graph.Create(new int[][]{
        new int[]{1,2},
        new int[]{1,3},
        new int[]{1,4},

        new int[]{2,3},
        new int[]{2,5},

        new int[]{3,4},

        new int[]{4,5},
        });
      
      graph.Print();

      println("-----------------------");

      graph.Add(1,5);
      graph.Add(1,6);
      graph.Add(7,8);

      graph.Print();
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值