Advertisement
Ilya_Bykonya

Untitled

Oct 10th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3. using System.Numerics;
  4. using System.Reflection.Emit;
  5. using System.Runtime.Intrinsics;
  6.  
  7.  
  8. namespace Lab2
  9. {
  10. class Graph
  11. {
  12. private sbyte[,] IncidenceMatrix { get; set; } = new sbyte[0, 0];
  13. private string[] VertexesLabels { get; set; } = new string[0];
  14. private Int64[] EdgesWeights { get; set; } = new Int64[0];
  15. public IEnumerable<int> VertexIds => Enumerable.Range(0, VertexesLabels.Length);
  16.  
  17. public UInt32? First(UInt32 id)
  18. {
  19. for (UInt32 i = 0; i < EdgesWeights.Length; i++)
  20. if (IncidenceMatrix[id, i] == -1)
  21. for(UInt32 j = 0; j < VertexesLabels.Length; ++j)
  22. if (IncidenceMatrix[j, i] == 1)
  23. return j;
  24.  
  25. return null;
  26. }
  27. public UInt32? Next(UInt32 from, UInt32 id)
  28. {
  29. bool takeNext = false;
  30. for (UInt32 i = 0; i < EdgesWeights.Length; i++)
  31. {
  32. if (IncidenceMatrix[from, i] == -1)
  33. {
  34. for (UInt32 j = 0; j < VertexesLabels.Length; ++j)
  35. {
  36. if (IncidenceMatrix[j, i] != 1)
  37. continue;
  38.  
  39. if (takeNext)
  40. return j;
  41. else if(j == id)
  42. takeNext = true;
  43. }
  44. }
  45. }
  46.  
  47. return null;
  48. }
  49. public string GetVertex(UInt32 id)
  50. {
  51. return VertexesLabels[id];
  52. }
  53.  
  54. public void AddVertex(string label)
  55. {
  56. VertexesLabels = VertexesLabels.Append(label).ToArray();
  57. var newIncidenceMatrix = new sbyte[VertexesLabels.Length, EdgesWeights.Length];
  58. for (int i = 0; i < IncidenceMatrix.GetLength(0); i++)
  59. for (int j = 0; j < IncidenceMatrix.GetLength(1); j++)
  60. newIncidenceMatrix[i, j] = IncidenceMatrix[i, j];
  61.  
  62. IncidenceMatrix = newIncidenceMatrix;
  63. }
  64. public void RenameVertex(UInt32 id, string label)
  65. {
  66. VertexesLabels[id] = label;
  67. }
  68. public void RemoveVertex(UInt32 id)
  69. {
  70. throw new NotImplementedException();
  71. }
  72.  
  73. public void AddEdge(UInt32 from, UInt32 to, Int64 weight)
  74. {
  75. EdgesWeights = EdgesWeights.Append(weight).ToArray();
  76. var newIncidenceMatrix = new sbyte[VertexesLabels.Length, EdgesWeights.Length];
  77. for (int i = 0; i < IncidenceMatrix.GetLength(0); i++)
  78. for (int j = 0; j < IncidenceMatrix.GetLength(1); j++)
  79. newIncidenceMatrix[i, j] = IncidenceMatrix[i, j];
  80.  
  81. newIncidenceMatrix[from, EdgesWeights.Length - 1] = -1;
  82. newIncidenceMatrix[to, EdgesWeights.Length - 1] = 1;
  83. IncidenceMatrix = newIncidenceMatrix;
  84. }
  85. public void SetEdgeWeight(UInt32 from, UInt32 to, Int64 weight)
  86. {
  87. for (int i = 0; i < EdgesWeights.Length; i++)
  88. if (IncidenceMatrix[from, i] == -1 && IncidenceMatrix[to, i] == 1)
  89. EdgesWeights[i] = weight;
  90. }
  91. public void RemoveEdge(UInt32 from, UInt32 to)
  92. {
  93. throw new NotImplementedException();
  94. }
  95. };
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. static class WaysSearcher
  115. {
  116. public static List<List<UInt32>> SelectPathes(Graph graph, int length)
  117. {
  118. List<List<UInt32>> result = new();
  119. foreach(var id in graph.VertexIds)
  120. {
  121. var pathes = SelectPathesRecursive(graph, (UInt32)id, new List<UInt32> {}, length);
  122. foreach (var path in pathes)
  123. result.Add(path);
  124. }
  125. return result;
  126. }
  127. private static List<List<UInt32>> SelectPathesRecursive(Graph graph, UInt32 currentVertex, List<UInt32> visitedVertexes, int remainedLength)
  128. {
  129. var currentPathes = visitedVertexes.ToList().Append(currentVertex).ToList();
  130. if (remainedLength == 0)
  131. return new() { currentPathes };
  132.  
  133. List<List<UInt32>> result = new();
  134. for (var id = graph.First(currentVertex); id.HasValue; id = graph.Next(currentVertex, id.Value))
  135. {
  136. if (visitedVertexes.Contains(id.Value))
  137. continue;
  138.  
  139. var pathes = SelectPathesRecursive(graph, id.Value, currentPathes, remainedLength - 1);
  140. foreach (var path in pathes)
  141. result.Add(path);
  142. }
  143. return result;
  144. }
  145. }
  146.  
  147. class Program
  148. {
  149. static void Main(string[] args)
  150. {
  151. Graph graph = new Graph();
  152. graph.AddVertex("vertex_1");
  153. graph.AddVertex("vertex_2");
  154. graph.AddVertex("vertex_3");
  155. graph.AddVertex("vertex_4");
  156. graph.AddEdge(0, 1, 12);
  157. graph.AddEdge(0, 2, 16);
  158. graph.AddEdge(1, 3, 12);
  159. graph.AddEdge(3, 0, 12);
  160. foreach (var path in WaysSearcher.SelectPathes(graph, 3))
  161. {
  162. foreach(var vertex in path)
  163. Console.Write($"{vertex} => ");
  164. Console.WriteLine();
  165. }
  166. }
  167. }
  168. }
  169.  
  170.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement