Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.13 KB | None | 0 0
  1. //using System;
  2. //using System.Collections.Generic;
  3. //using System.Linq;
  4. //using System.Text;
  5. //using System.Threading.Tasks;
  6.  
  7. //namespace lab10_LFT
  8. //{
  9. // class Program
  10. // {
  11. // public static void Main(string[] args)
  12. // {
  13. // var vertices = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  14. // var edges = new[]{Tuple.Create(1,2), Tuple.Create(1,3),
  15. // Tuple.Create(2,4), Tuple.Create(3,5), Tuple.Create(3,6),
  16. // Tuple.Create(4,7), Tuple.Create(5,7), Tuple.Create(5,8),
  17. // Tuple.Create(5,6), Tuple.Create(8,9), Tuple.Create(9,10)};
  18.  
  19. // var graph = new Graph<int>(vertices, edges);
  20. // var algorithms = new Algorithms();
  21.  
  22. // Console.WriteLine(string.Join(", ", algorithms.DFS(graph, 1)));
  23. //// 1, 3, 6, 5, 8, 9, 10, 7, 4, 2
  24. // }
  25. // }
  26. //}
  27.  
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Linq;
  31. using System.Text;
  32. using System.Data;
  33. namespace ConsoleApplication1
  34. {
  35. class Program
  36. {
  37. public class Neighbor
  38. {
  39. public Node node { get; set; }
  40. public int distance { get; set; }
  41. }
  42. public class Node
  43. {
  44.  
  45. public string name { get; set; }
  46. public Dictionary<string, List<string>> distanceDict { get; set; }
  47. public Boolean visited { get; set; }
  48. public List<Neighbor> neighbors { get; set; }
  49. }
  50. //A connected to B
  51. //B connected to A, C , D
  52. //C connected to B, D
  53. //D connected to B, C , E
  54. //E connected to D.
  55. static List<Node> graph = new List<Node>() {
  56. new Node {name = "A", distanceDict = new Dictionary<string,List<string>>(){
  57. {"B",new List<string>(){"B"}}, {"C",new List<string>(){"C"}}, {"D",new List<string>(){"D"} } },
  58. visited = false},
  59. new Node {name = "B", distanceDict = new Dictionary<string,List<string>>(){
  60. {"A",new List<string>(){"A"}}, {"C",new List<string>(){"C"}}, {"E",new List<string>(){"E"}}},
  61. visited = false},
  62. new Node {name = "C", distanceDict = new Dictionary<string,List<string>>(){
  63. {"A",new List<string>(){"A"}}, {"B",new List<string>(){"B"}}, {"D",new List<string>(){"D"}}},
  64. visited = false},
  65. new Node {name = "D", distanceDict = new Dictionary<string,List<string>>(){
  66. {"A",new List<string>(){"A"}}, {"C",new List<string>(){"C"}}, {"E",new List<string>(){"E"}}},
  67. visited = false},
  68. new Node {name = "E", distanceDict = new Dictionary<string,List<string>>(){
  69. {"B",new List<string>(){"B"}}, {"D",new List<string>(){"D"}}},
  70. visited = false}
  71. };
  72.  
  73. static List<Node> graph2 = new List<Node>() {
  74. new Node {name = "1", distanceDict = new Dictionary<string,List<string>>(){
  75. {"2",new List<string>(){"2"}} },
  76. visited = false},
  77. new Node {name = "2", distanceDict = new Dictionary<string,List<string>>(){
  78. {"4",new List<string>(){"4"}}},
  79. visited = false},
  80. new Node {name = "3", distanceDict = new Dictionary<string,List<string>>(){
  81. {"1",new List<string>(){"1"}}, {"2",new List<string>(){"2"}}, {"4",new List<string>(){"4"}}},
  82. visited = false},
  83. new Node {name = "4", distanceDict = new Dictionary<string,List<string>>(){
  84. {"3",new List<string>(){"3"}} },
  85. visited = false}
  86.  
  87. };
  88. static void Main(string[] args)
  89. {
  90. //initialize neighbors using predefined dixtionary
  91. foreach (Node node in graph)
  92. {
  93. node.neighbors = new List<Neighbor>();
  94. foreach (KeyValuePair<string, List<string>> neighbor in node.distanceDict)
  95. {
  96. Neighbor newNeightbor = new Neighbor();
  97. foreach (Node graphNode in graph)
  98. {
  99. if (graphNode.name == neighbor.Key)
  100. {
  101. newNeightbor.node = graphNode;
  102. newNeightbor.distance = neighbor.Value.Count;
  103. node.neighbors.Add(newNeightbor);
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. TransverNode(graph[0]);
  110. foreach (Node node in graph)
  111. {
  112. Console.WriteLine("Node : {0}", node.name);
  113. foreach (string key in node.distanceDict.Keys.OrderBy(x => x))
  114. {
  115. Console.WriteLine(" Path to node {0} is {1}", key, string.Join(",", node.distanceDict[key].ToArray()));
  116. }
  117. }
  118.  
  119. Console.WriteLine("\n\n\n\n");
  120.  
  121. //initialize neighbors using predefined dixtionary
  122. foreach (Node node in graph2)
  123. {
  124. node.neighbors = new List<Neighbor>();
  125. foreach (KeyValuePair<string, List<string>> neighbor in node.distanceDict)
  126. {
  127. Neighbor newNeightbor = new Neighbor();
  128. foreach (Node graphNode in graph2)
  129. {
  130. if (graphNode.name == neighbor.Key)
  131. {
  132. newNeightbor.node = graphNode;
  133. newNeightbor.distance = neighbor.Value.Count;
  134. node.neighbors.Add(newNeightbor);
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. TransverNode(graph2[0]);
  141. foreach (Node node in graph2)
  142. {
  143. Console.WriteLine("Node : {0}", node.name);
  144. foreach (string key in node.distanceDict.Keys.OrderBy(x => x))
  145. {
  146. Console.WriteLine(" Path to node {0} is {1}", key, string.Join(",", node.distanceDict[key].ToArray()));
  147. }
  148. }
  149. }
  150. static void TransverNode(Node node)
  151. {
  152. if (!node.visited)
  153. {
  154. node.visited = true;
  155. foreach (Neighbor neighbor in node.neighbors)
  156. {
  157. TransverNode(neighbor.node);
  158. string neighborName = neighbor.node.name;
  159. int neighborDistance = neighbor.distance;
  160. //compair neibors dictionary with current dictionary
  161. //update current dictionary as required
  162. foreach (string key in neighbor.node.distanceDict.Keys)
  163. {
  164. if (key != node.name)
  165. {
  166. int neighborKeyDistance = neighbor.node.distanceDict[key].Count;
  167. if (node.distanceDict.ContainsKey(key))
  168. {
  169. int currentDistance = node.distanceDict[key].Count;
  170. if (neighborKeyDistance + neighborDistance < currentDistance)
  171. {
  172. List<string> nodeList = new List<string>();
  173. nodeList.AddRange(neighbor.node.distanceDict[key].ToArray());
  174. nodeList.Insert(0, neighbor.node.name);
  175. node.distanceDict[key] = nodeList;
  176. }
  177. }
  178. else
  179. {
  180. List<string> nodeList = new List<string>();
  181. nodeList.AddRange(neighbor.node.distanceDict[key].ToArray());
  182. nodeList.Insert(0, neighbor.node.name);
  183. node.distanceDict.Add(key, nodeList);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement