Algorithm.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace DrawGraph
  7. {
  8. public class Algorithm
  9. {
  10. private static int MinDistance(int[] path, bool[] includedToPath, int n)
  11. {
  12. // Initialize min value
  13. int min = int.MaxValue, min_index = -1;
  14. for (int v = 0; v < n; v++)
  15. if (includedToPath[v] == false && path[v] <= min)
  16. {
  17. min = path[v];
  18. min_index = v;
  19. }
  20. return min_index;
  21. }
  22. public static int DijkstraCount(Graph graph, int startIndex, int finishIndex)
  23. {
  24. var adj = Matrix.AdjacencyCreate(graph);
  25. return DijkstraCount(adj, startIndex, finishIndex);
  26. }
  27. public static int DijkstraCount(int[,] adjacencyMatrix, int startIndex, int finishIndex)
  28. {
  29. int n = adjacencyMatrix.GetLength(0);
  30. int[] path = new int[n];
  31. bool[] includedToShortestPath = new bool[n];
  32. for (int i = 0; i < n; i++)
  33. {
  34. path[i] = int.MaxValue;
  35. includedToShortestPath[i] = false;
  36. }
  37. path[startIndex] = 0;
  38. for (int i = 0; i < n - 1; i++)
  39. {
  40. int min = MinDistance(path, includedToShortestPath, n);
  41. includedToShortestPath[min] = true;
  42. for (int j = 0; j < n; j++)
  43. {
  44. if (!includedToShortestPath[j] && adjacencyMatrix[min, j] != 0 &&
  45. path[min] + adjacencyMatrix[min, j] < path[j])
  46. {
  47. path[j] = path[min] + adjacencyMatrix[min, j];
  48. }
  49. }
  50. }
  51. return path[finishIndex];
  52. }
  53. public static Node[] DepthSearch(Graph graph, int startIndex)
  54. {
  55. bool[] visitedBool = new bool[graph.Nodes.Length];
  56. Node[] visited = new Node[graph.Nodes.Length];
  57. visitedBool[startIndex] = true;
  58. visited[startIndex] = new Node(graph.Nodes[startIndex].X, graph.Nodes[startIndex].Y);
  59. var matrix = Matrix.AdjacencyCreate(graph.Nodes, graph.Edges);
  60. for (int i = 0; i < graph.Nodes.Length; i++)
  61. {
  62. if (matrix[startIndex, i] != 0 && !visitedBool[i])
  63. {
  64. DepthSeach(graph.Nodes, graph.Edges, i);
  65. }
  66. }
  67. return visited;
  68. }
  69. public static Node[] DepthSeach(Node[] nodes, Edge[] edges, int startIndex)
  70. {
  71. bool[] visitedBool = new bool[nodes.Length];
  72. Node[] visited = new Node[nodes.Length];
  73. visitedBool[startIndex] = true;
  74. visited[startIndex] = new Node(nodes[startIndex].X, nodes[startIndex].Y);
  75. var matrix = Matrix.AdjacencyCreate(nodes, edges);
  76. for(int i = 0; i < nodes.Length; i++)
  77. {
  78. if(matrix[startIndex, i]!=0 && !visitedBool[i])
  79. {
  80. DepthSeach(nodes, edges, i);
  81. }
  82. }
  83. return visited;
  84. }
  85. // TODO
  86. public static Node[] BreadthFirstSearch(Node[] nodes, Edge[] edges, int startIndex)
  87. {
  88. var visited = new HashSet<Node>();
  89. var visitedBool = new bool[nodes.Length];
  90. visited.Add(nodes[startIndex]);
  91. var queue = new Queue<Node>();
  92. queue.Enqueue(nodes[startIndex]);
  93. while (queue.Count > 0)
  94. {
  95. var current = queue.Dequeue();
  96. bool HasAdj = false;
  97. for(int i = 0; i < edges.Length; i++)
  98. {
  99. if (Node.Compare(current, edges[i].SourceNode) || Node.Compare(current, edges[i].TargetNode))
  100. HasAdj = true;
  101. }
  102. if (HasAdj)
  103. {
  104. return nodes;
  105. }
  106. return nodes;
  107. }
  108. return nodes;
  109. }
  110. }
  111. }