Graph.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. /// <summary>
  6. /// the data struct of graph.
  7. /// ||数据结构-图。
  8. /// </summary>
  9. public class Graph
  10. {
  11. public bool directed;
  12. public List<GraphNode> nodes = new List<GraphNode>();
  13. public List<GraphEdge> edges = new List<GraphEdge>();
  14. public Dictionary<string, GraphNode> nodeMap = new Dictionary<string, GraphNode>();
  15. public Dictionary<string, GraphEdge> edgeMap = new Dictionary<string, GraphEdge>();
  16. public Graph(bool directed)
  17. {
  18. this.directed = directed;
  19. }
  20. public void Clear()
  21. {
  22. nodes.Clear();
  23. edges.Clear();
  24. nodeMap.Clear();
  25. edgeMap.Clear();
  26. }
  27. public void Refresh()
  28. {
  29. foreach (var node in nodes)
  30. {
  31. node.depth = GetNodeDepth(node);
  32. }
  33. }
  34. public static double GetNodesTotalValue(List<GraphNode> nodes)
  35. {
  36. double totalValue = 0;
  37. foreach (var node in nodes)
  38. {
  39. totalValue += node.totalValues;
  40. }
  41. return totalValue;
  42. }
  43. public List<List<GraphNode>> GetDepthNodes()
  44. {
  45. List<List<GraphNode>> depthNodes = new List<List<GraphNode>>();
  46. var maxDepth = GetMaxDepth();
  47. for (int i = 0; i <= maxDepth; i++)
  48. {
  49. depthNodes.Add(new List<GraphNode>());
  50. }
  51. foreach (var node in nodes)
  52. {
  53. if (node.inDegree == 0)
  54. {
  55. depthNodes[0].Add(node);
  56. }
  57. else
  58. {
  59. int deep = GetNodeDepth(node);
  60. depthNodes[maxDepth - deep].Add(node);
  61. }
  62. }
  63. return depthNodes;
  64. }
  65. public List<GraphNode> GetRootNodes()
  66. {
  67. List<GraphNode> rootNodes = new List<GraphNode>();
  68. foreach (var node in nodes)
  69. {
  70. if (node.inDegree == 0)
  71. {
  72. rootNodes.Add(node);
  73. }
  74. }
  75. return rootNodes;
  76. }
  77. public int GetMaxDepth()
  78. {
  79. int maxDepth = 0;
  80. foreach (var node in nodes)
  81. {
  82. int deep = GetNodeDepth(node);
  83. if (deep > maxDepth)
  84. {
  85. maxDepth = deep;
  86. }
  87. }
  88. return maxDepth;
  89. }
  90. // public int GetNodeDepth(GraphNode node)
  91. // {
  92. // int depth = 0;
  93. // GetNodeDepth(node, ref depth);
  94. // return depth;
  95. // }
  96. // public void GetNodeDepth(GraphNode node, ref int depth, int recursiveCount = 0)
  97. // {
  98. // if (recursiveCount > 50)
  99. // {
  100. // XLog.Error("Graph.GetNodeDeep(): recursiveCount > 50, maybe graph is ring");
  101. // return;
  102. // }
  103. // if (node.inDegree == 0)
  104. // {
  105. // return;
  106. // }
  107. // else
  108. // {
  109. // depth += 1;
  110. // foreach (var edge in node.inEdges)
  111. // {
  112. // GetNodeDepth(edge.node1, ref depth, recursiveCount + 1);
  113. // }
  114. // }
  115. // }
  116. public int GetNodeDepth(GraphNode node, int recursiveCount = 0)
  117. {
  118. if (recursiveCount > 50)
  119. {
  120. XLog.Error("Graph.GetNodeDeep(): recursiveCount > 50, maybe graph is ring");
  121. return 0;
  122. }
  123. int depth = 0;
  124. if (node.outDegree == 0)
  125. {
  126. return depth;
  127. }
  128. else
  129. {
  130. foreach (var edge in node.outEdges)
  131. {
  132. int otherDeep = GetNodeDepth(edge.node2, recursiveCount + 1);
  133. if (otherDeep > depth)
  134. {
  135. depth = otherDeep;
  136. }
  137. }
  138. return depth + 1;
  139. }
  140. }
  141. public GraphNode GetNode(string nodeId)
  142. {
  143. if (nodeMap.ContainsKey(nodeId))
  144. {
  145. return nodeMap[nodeId];
  146. }
  147. else
  148. {
  149. return null;
  150. }
  151. }
  152. public GraphEdge GetEdge(string nodeId1, string nodeId2)
  153. {
  154. if (directed)
  155. {
  156. return edgeMap[nodeId1 + "_" + nodeId2];
  157. }
  158. else
  159. {
  160. var key = nodeId1 + "_" + nodeId2;
  161. if (edgeMap.ContainsKey(key))
  162. {
  163. return edgeMap[key];
  164. }
  165. else
  166. {
  167. key = nodeId2 + "_" + nodeId1;
  168. if (edgeMap.ContainsKey(key))
  169. {
  170. return edgeMap[key];
  171. }
  172. else
  173. {
  174. return null;
  175. }
  176. }
  177. }
  178. }
  179. public GraphNode AddNode(string nodeId, string nodeName, int dataIndex)
  180. {
  181. if (nodeMap.ContainsKey(nodeId))
  182. {
  183. return nodeMap[nodeId];
  184. }
  185. else
  186. {
  187. GraphNode node = new GraphNode(nodeId, nodeName, dataIndex);
  188. node.hostGraph = this;
  189. nodeMap.Add(nodeId, node);
  190. nodes.Add(node);
  191. return node;
  192. }
  193. }
  194. public GraphEdge AddEdge(string nodeId1, string nodeId2, double value)
  195. {
  196. GraphNode node1, node2;
  197. if (!nodeMap.TryGetValue(nodeId1, out node1))
  198. {
  199. XLog.Warning("Graph.AddEdge(): " + nodeId1 + " not exist");
  200. return null;
  201. }
  202. if (!nodeMap.TryGetValue(nodeId2, out node2))
  203. {
  204. XLog.Warning("Graph.AddEdge(): " + nodeId2 + " not exist");
  205. return null;
  206. }
  207. if (node1 == null)
  208. {
  209. XLog.Warning("Graph.AddEdge(): node1 is null");
  210. return null;
  211. }
  212. if (node2 == null)
  213. {
  214. XLog.Warning("Graph.AddEdge(): node2 is null");
  215. return null;
  216. }
  217. if (node1 == node2)
  218. {
  219. XLog.Warning("Graph.AddEdge(): node1 == node2");
  220. return null;
  221. }
  222. string edgeKey = nodeId1 + "_" + nodeId2;
  223. if (edgeMap.ContainsKey(edgeKey))
  224. {
  225. return edgeMap[edgeKey];
  226. }
  227. else
  228. {
  229. GraphEdge edge = new GraphEdge(node1, node2, value);
  230. edge.key = edgeKey;
  231. edge.hostGraph = this;
  232. if (directed)
  233. {
  234. node1.outEdges.Add(edge);
  235. node2.inEdges.Add(edge);
  236. }
  237. node1.edges.Add(edge);
  238. if (node1 != node2)
  239. {
  240. node2.edges.Add(edge);
  241. }
  242. edgeMap.Add(edgeKey, edge);
  243. edges.Add(edge);
  244. return edge;
  245. }
  246. }
  247. public void EachNode(System.Action<GraphNode> onEach)
  248. {
  249. if (onEach == null) return;
  250. foreach (var node in nodes)
  251. {
  252. onEach(node);
  253. }
  254. }
  255. public void BreadthFirstTraverse(GraphNode startNode, System.Action<GraphNode> onTraverse)
  256. {
  257. if (startNode == null) return;
  258. foreach (var node in nodes)
  259. {
  260. node.visited = false;
  261. }
  262. onTraverse(startNode);
  263. startNode.visited = true;
  264. Queue<GraphNode> queue = new Queue<GraphNode>();
  265. queue.Enqueue(startNode);
  266. while (queue.Count > 0)
  267. {
  268. var currentNode = queue.Dequeue();
  269. foreach (var edge in currentNode.edges)
  270. {
  271. var otherNode = edge.node1 == currentNode ? edge.node2 : edge.node1;
  272. if (!otherNode.visited)
  273. {
  274. onTraverse(otherNode);
  275. otherNode.visited = true;
  276. queue.Enqueue(otherNode);
  277. }
  278. }
  279. }
  280. }
  281. public void DeepFirstTraverse(GraphNode startNode, System.Action<GraphNode> onTraverse)
  282. {
  283. if (startNode == null) return;
  284. foreach (var node in nodes)
  285. {
  286. node.visited = false;
  287. }
  288. Stack<GraphNode> stack = new Stack<GraphNode>();
  289. stack.Push(startNode);
  290. while (stack.Count > 0)
  291. {
  292. var currentNode = stack.Pop();
  293. if (!currentNode.visited)
  294. {
  295. onTraverse(currentNode);
  296. currentNode.visited = true;
  297. }
  298. foreach (var edge in currentNode.edges)
  299. {
  300. var otherNode = edge.node1 == currentNode ? edge.node2 : edge.node1;
  301. if (!otherNode.visited)
  302. {
  303. stack.Push(otherNode);
  304. }
  305. }
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// The node of graph.
  311. /// ||图的节点。
  312. /// </summary>
  313. public class GraphNode
  314. {
  315. public string id;
  316. public string name;
  317. public List<GraphEdge> edges = new List<GraphEdge>();
  318. public List<GraphEdge> inEdges = new List<GraphEdge>();
  319. public List<GraphEdge> outEdges = new List<GraphEdge>();
  320. public Graph hostGraph;
  321. public int dataIndex;
  322. public bool visited;
  323. public int depth = -1;
  324. public GraphNode(string id, string name, int dataIndex)
  325. {
  326. this.id = id;
  327. this.name = name;
  328. this.dataIndex = dataIndex;
  329. }
  330. public int degree { get { return edges.Count; } }
  331. public int inDegree { get { return inEdges.Count; } }
  332. public int outDegree { get { return outEdges.Count; } }
  333. public double totalValues
  334. {
  335. get
  336. {
  337. double totalValue = 0;
  338. if (inEdges.Count == 0)
  339. {
  340. foreach (var edge in outEdges)
  341. {
  342. totalValue += edge.value;
  343. }
  344. }
  345. else
  346. {
  347. foreach (var edge in inEdges)
  348. {
  349. totalValue += edge.value;
  350. }
  351. }
  352. return totalValue;
  353. }
  354. }
  355. public override string ToString()
  356. {
  357. return name;
  358. }
  359. }
  360. /// <summary>
  361. /// The edge of graph.
  362. /// ||图的边。
  363. /// </summary>
  364. public class GraphEdge
  365. {
  366. public string key;
  367. public GraphNode node1;
  368. public GraphNode node2;
  369. public double value;
  370. public Graph hostGraph;
  371. public List<Vector3> points = new List<Vector3>();
  372. public float width;
  373. public bool highlight;
  374. public GraphEdge(GraphNode node1, GraphNode node2, double value)
  375. {
  376. this.node1 = node1;
  377. this.node2 = node2;
  378. this.value = value;
  379. }
  380. }
  381. }