Advertisement
Ilya_Bykonya

Untitled

Oct 9th, 2022
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System;
  3.  
  4. namespace Lab2
  5. {
  6. class Graph
  7. {
  8. public record struct Vertex(UInt32 id, string label);
  9. public record struct Edge(UInt32 from, UInt32 to, Int64 Weight);
  10.  
  11. private UInt32 CurrentVertexMaxId { get; set; } = 0;
  12. private List<Vertex> Vertices { get; set; } = new();
  13. private List<Edge> Edges { get; set; } = new();
  14.  
  15.  
  16.  
  17. public IEnumerable<UInt32> GetVertexesIds() {
  18. return Vertices.Select(vertex => vertex.id);
  19. }
  20. public Vertex GetVertex(UInt32 id) {
  21. foreach (var vertex in Vertices)
  22. if(vertex.id == id)
  23. return vertex;
  24.  
  25. throw new ArgumentOutOfRangeException("Vertex index out of range");
  26. }
  27. public UInt32? First(UInt32 id) {
  28. foreach(var edge in Edges)
  29. if(edge.from == id)
  30. return edge.to;
  31.  
  32. return null;
  33. }
  34. public UInt32? Next(UInt32 id, UInt32 subvertexId) {
  35. bool takeNext = false;
  36. foreach (var edge in Edges)
  37. {
  38. if (edge.from != id)
  39. continue;
  40.  
  41. if(edge.to == subvertexId)
  42. takeNext = true;
  43. else if(takeNext)
  44. return edge.to;
  45. }
  46.  
  47. return null;
  48. }
  49.  
  50. public UInt32 AddVertex(string label) {
  51. var Id = ++CurrentVertexMaxId;
  52. Vertices.Add(new Vertex(Id, label));
  53. return Id;
  54. }
  55. public void EditVertex(UInt32 id, string label) {
  56. for (int index = 0; index < Vertices.Count; ++index)
  57. if (Vertices[index].id == id)
  58. Vertices[index] = new Vertex(id, label);//immutable dto
  59. }
  60. public void RemoveVertex(UInt32 id) {
  61. Vertices = Vertices.Where(vertex => vertex.id != id).ToList();
  62. Edges = Edges.Where(edge => edge.from != id && edge.to != id).ToList();
  63. }
  64.  
  65. public void AddEdge(UInt32 from, UInt32 to, Int64 weight) {
  66. if (!Edges.Any(edge => edge.from == from && edge.to == to))
  67. Edges.Add(new Edge(from, to, weight));
  68. }
  69. public void EditEdge(UInt32 from, UInt32 to, Int64 weight) {
  70. for(int index = 0; index < Edges.Count; ++index)
  71. if (Edges[index].from == from && Edges[index].to == to)
  72. Edges[index] = new Edge(from, to, weight);
  73. }
  74. public void RemoveEdge(UInt32 from, UInt32 to) {
  75. Edges = Edges.Where(edge => edge.from == from && edge.to == to).ToList();
  76. }
  77. }
  78.  
  79. static class WaysSearcher
  80. {
  81. public static List<List<UInt32>> SelectPathes(Graph graph, int length)
  82. {
  83. List<List<UInt32>> result = new();
  84. foreach(var id in graph.GetVertexesIds())
  85. {
  86. var pathes = SelectPathesRecursive(graph, id, new List<UInt32> {}, length);
  87. foreach (var path in pathes)
  88. result.Add(path);
  89. }
  90. return result;
  91. }
  92. private static List<List<UInt32>> SelectPathesRecursive(Graph graph, UInt32 currentVertex, List<UInt32> visitedVertexes, int remainedLength)
  93. {
  94. var currentPathes = visitedVertexes.ToList().Append(currentVertex).ToList();
  95. if (remainedLength == 0)
  96. return new() { currentPathes };
  97.  
  98. List<List<UInt32>> result = new();
  99. for (var id = graph.First(currentVertex); id.HasValue; id = graph.Next(currentVertex, id.Value))
  100. {
  101. if (visitedVertexes.Contains(id.Value))
  102. continue;
  103.  
  104. var pathes = SelectPathesRecursive(graph, id.Value, currentPathes, remainedLength - 1);
  105. foreach (var path in pathes)
  106. result.Add(path);
  107. }
  108. return result;
  109. }
  110. }
  111.  
  112. class Program
  113. {
  114. static void Main(string[] args)
  115. {
  116. Graph graph = new Graph();
  117. graph.AddVertex("vertex_1");
  118. graph.AddVertex("vertex_2");
  119. graph.AddVertex("vertex_3");
  120. graph.AddVertex("vertex_4");
  121. graph.AddEdge(1, 2, 12);
  122. graph.AddEdge(1, 3, 16);
  123. graph.AddEdge(2, 4, 12);
  124. graph.AddEdge(4, 1, 12);
  125. foreach (var path in WaysSearcher.SelectPathes(graph, 3))
  126. {
  127. foreach(var vertex in path)
  128. Console.Write($"{vertex} => ");
  129. Console.WriteLine();
  130. }
  131. }
  132. }
  133. }
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement