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();
}
}
}
using System.Collections.Generic;namespace DataStructure{ public class LinkedUndigraph : BaseSolution { HashSet<int> valueSet = new HashSet<int>(); Dictionary<int, Node&g...