Advertisement
Ilya_Bykonya

Untitled

Oct 9th, 2022
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 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. using static Lab2.Graph;
  7.  
  8. namespace Lab2
  9. {
  10. class Graph
  11. {
  12. public class Edge
  13. {
  14. public Int64 Weight { get; set; }
  15. public UInt32 From { get; }
  16. public UInt32 To { get; }
  17. public Edge(UInt32 from, UInt32 to, Int64 weight)
  18. {
  19. Weight = weight;
  20. From = from;
  21. To = to;
  22. }
  23. }
  24. public class Vertex
  25. {
  26. public UInt32 Id { get; }
  27. public string Label { get; set; }
  28. public SortedDictionary<UInt32, Edge> Edges { get; } = new();
  29.  
  30. public Vertex(UInt32 id, string label)
  31. {
  32. Id = id;
  33. Label = label;
  34. }
  35.  
  36. void addEdge(UInt32 to, Int64 weight)
  37. {
  38. Edges.TryAdd(to, new Edge(Id, to, weight));
  39. }
  40. void removeEdge(UInt32 to)
  41. {
  42. Edges.Remove(to);
  43. }
  44. }
  45.  
  46. private SortedDictionary<UInt32, Vertex> Vertexes { get; } = new();
  47. public List<UInt32> VertexIds => Vertexes.Keys.ToList();
  48. private UInt32 GlobalVertexId { get; set; } = 0;
  49.  
  50. public UInt32? First(UInt32 id)
  51. {
  52. if (Vertexes.ContainsKey(id))
  53. if (Vertexes[id].Edges.Count > 0)
  54. return Vertexes[id].Edges.Keys.First();
  55.  
  56. return null;
  57. }
  58. public UInt32? Next(UInt32 from, UInt32 id)
  59. {
  60. if ((Vertexes.ContainsKey(id) && Vertexes.ContainsKey(from)) == false)
  61. return null;
  62.  
  63. var nextVertexesIds = Vertexes[from].Edges.Keys.ToList();
  64. for(int index = 0; index < nextVertexesIds.Count; ++index)
  65. if (nextVertexesIds[index] == id)
  66. if(index != nextVertexesIds.Count - 1)
  67. return nextVertexesIds[index + 1];
  68.  
  69. return null;
  70. }
  71. public Vertex? GetVertex(UInt32 id)
  72. {
  73. return Vertexes.GetValueOrDefault(id);
  74. }
  75.  
  76. public void AddVertex(string label)
  77. {
  78. var id = ++GlobalVertexId;
  79. Vertexes.Add(id, new Vertex(id, label));
  80. }
  81. public void RenameVertex(UInt32 id, string label)
  82. {
  83. var vertex = Vertexes.GetValueOrDefault(id);
  84. if (vertex is not null)
  85. vertex.Label = label;
  86. }
  87. public void RemoveVertex(UInt32 id)
  88. {
  89. Vertexes.Remove(id);
  90. foreach(var vertex in Vertexes)
  91. vertex.Value.Edges.Remove(id);
  92. }
  93.  
  94. public void AddEdge(UInt32 from, UInt32 to, Int64 weight)
  95. {
  96. if(Vertexes.ContainsKey(from) && Vertexes.ContainsKey(to))
  97. Vertexes[from].Edges.TryAdd(to, new Edge(from, to, weight));
  98. }
  99. public void SetEdgeWeight(UInt32 from, UInt32 to, Int64 weight)
  100. {
  101. if (Vertexes.ContainsKey(from) && Vertexes.ContainsKey(to))
  102. if(Vertexes[from].Edges.ContainsKey(to))
  103. Vertexes[from].Edges[to] = new Edge(from, to, weight);
  104. }
  105. public void RemoveEdge(UInt32 from, UInt32 to)
  106. {
  107. if (Vertexes.ContainsKey(from) && Vertexes.ContainsKey(to))
  108. Vertexes[from].Edges.Remove(to);
  109. }
  110. };
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. static class WaysSearcher
  130. {
  131. public static List<List<UInt32>> SelectPathes(Graph graph, int length)
  132. {
  133. List<List<UInt32>> result = new();
  134. foreach(var id in graph.VertexIds)
  135. {
  136. var pathes = SelectPathesRecursive(graph, id, new List<UInt32> {}, length);
  137. foreach (var path in pathes)
  138. result.Add(path);
  139. }
  140. return result;
  141. }
  142. private static List<List<UInt32>> SelectPathesRecursive(Graph graph, UInt32 currentVertex, List<UInt32> visitedVertexes, int remainedLength)
  143. {
  144. var currentPathes = visitedVertexes.ToList().Append(currentVertex).ToList();
  145. if (remainedLength == 0)
  146. return new() { currentPathes };
  147.  
  148. List<List<UInt32>> result = new();
  149. for (var id = graph.First(currentVertex); id.HasValue; id = graph.Next(currentVertex, id.Value))
  150. {
  151. if (visitedVertexes.Contains(id.Value))
  152. continue;
  153.  
  154. var pathes = SelectPathesRecursive(graph, id.Value, currentPathes, remainedLength - 1);
  155. foreach (var path in pathes)
  156. result.Add(path);
  157. }
  158. return result;
  159. }
  160. }
  161.  
  162. class Program
  163. {
  164. static void Main(string[] args)
  165. {
  166. Graph graph = new Graph();
  167. graph.AddVertex("vertex_1");
  168. graph.AddVertex("vertex_2");
  169. graph.AddVertex("vertex_3");
  170. graph.AddVertex("vertex_4");
  171. graph.AddEdge(1, 2, 12);
  172. graph.AddEdge(1, 3, 16);
  173. graph.AddEdge(2, 4, 12);
  174. graph.AddEdge(4, 1, 12);
  175. foreach (var path in WaysSearcher.SelectPathes(graph, 3))
  176. {
  177. foreach(var vertex in path)
  178. Console.Write($"{vertex} => ");
  179. Console.WriteLine();
  180. }
  181. }
  182. }
  183. }
  184.  
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement