Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.16 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using GraphDataStructure;
  4.  
  5. namespace Tests
  6. {
  7. public static class Helper
  8. {
  9. public static string ToJoinedString(this char[] array)
  10. {
  11. return string.Join(" ", array);
  12. }
  13. }
  14.  
  15. [TestClass]
  16. public class Tests
  17. {
  18. #region graphs
  19. public static char[][] graph1 = new char[][] {
  20. new char [] {'A', 'B'},
  21. new char [] {'A', 'D'},
  22. new char [] {'A', 'C'},
  23. new char [] {'C', 'D'},
  24. new char [] {'C', 'E'}
  25. };
  26.  
  27. public static char[][] graph2 = new char[][]
  28. {
  29. new char [] {'A', 'G'},
  30. new char [] {'A', 'C'},
  31. new char [] {'A', 'B'},
  32. new char [] {'A', 'D'},
  33. new char [] {'C', 'G'},
  34. new char [] {'C', 'B'},
  35. new char [] {'D', 'B'},
  36. new char [] {'D', 'E'},
  37. new char [] {'B', 'F'},
  38. };
  39. public static char[][] graph3 = new char[][]
  40. {
  41. new char [] {'A', 'G'}
  42. };
  43. #endregion
  44.  
  45. #region graph1
  46. [TestMethod]
  47. public void TestGraph1Initialisation()
  48. {
  49. var graph = new Graph();
  50. graph.initGraph(graph1);
  51.  
  52. var expectedAdjecencyMatrix = new char[][]
  53. {
  54. new char[] { 'B', 'C', 'D' },
  55. new char[] { 'A' },
  56. new char[] { 'A', 'D', 'E' },
  57. new char[] { 'A', 'C' },
  58. new char[] { 'C' },
  59. };
  60.  
  61. //Testing vertices
  62. var expectedVertices = new char[] { 'A', 'B', 'C', 'D', 'E' };
  63. Assert.AreEqual(expectedVertices.ToJoinedString(), graph.Vertices.ToJoinedString());
  64.  
  65. //Testing adjecency matrix
  66. int i = 0;
  67. foreach (var row in expectedAdjecencyMatrix)
  68. Assert.AreEqual(row.ToJoinedString(), graph.AdjecencyMatrix[i++].ToJoinedString());
  69. }
  70.  
  71. [TestMethod]
  72. public void TestGraph1GetAdjecents()
  73. {
  74. var graph = new Graph();
  75. graph.initGraph(graph1);
  76.  
  77. var expectedAdjecents = new char[] { 'B', 'C', 'D' };
  78. Assert.AreEqual(expectedAdjecents.ToJoinedString(), graph.getAdjecents('A').ToJoinedString());
  79.  
  80. expectedAdjecents = new char[] { 'C' };
  81. Assert.AreEqual(expectedAdjecents.ToJoinedString(), graph.getAdjecents('E').ToJoinedString());
  82. }
  83.  
  84. [TestMethod]
  85. [ExpectedException(typeof(InvalidOperationException))]
  86. public void TestGraph1GetAdjecentsOfNonExistentVertex()
  87. {
  88. var graph = new Graph();
  89. graph.initGraph(graph1);
  90.  
  91. graph.getAdjecents('Y');
  92. }
  93.  
  94. [TestMethod]
  95. public void TestGraph1Traversal()
  96. {
  97. var graph = new Graph();
  98. graph.initGraph(graph1);
  99.  
  100. var expectedResult = new char[] { 'A', 'B', 'C', 'D', 'E' } ;
  101. char[] result = graph.Traverse();
  102.  
  103. Assert.AreEqual(expectedResult.ToJoinedString(), result.ToJoinedString());
  104. }
  105. #endregion
  106.  
  107. #region graph2
  108. [TestMethod]
  109. public void TestGraph2Initialisation()
  110. {
  111. var graph = new Graph();
  112. graph.initGraph(graph2);
  113.  
  114. var expectedAdjecencyMatrix = new char[][]
  115. {
  116. new char[] { 'B', 'C', 'D', 'G' }, //A
  117. new char[] { 'A', 'C', 'D', 'F' }, //B
  118. new char[] { 'A', 'B', 'G' }, //C
  119. new char[] { 'A', 'B', 'E' }, //D
  120. new char[] { 'D' }, //E
  121. new char[] { 'B' }, //F
  122. new char[] { 'A', 'C' }, //G
  123. };
  124.  
  125. //Testing vertices
  126. var expectedVertices = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
  127. Assert.AreEqual(expectedVertices.ToJoinedString(), graph.Vertices.ToJoinedString());
  128.  
  129. //Testing adjecency matrix
  130. int i = 0;
  131. foreach (var row in expectedAdjecencyMatrix)
  132. Assert.AreEqual(row.ToJoinedString(), graph.AdjecencyMatrix[i++].ToJoinedString());
  133. }
  134.  
  135. [TestMethod]
  136. public void TestGraph2GetAdjecents()
  137. {
  138. var graph = new Graph();
  139. graph.initGraph(graph2);
  140.  
  141. var expectedAdjecents = new char[] { 'B', 'C', 'D', 'G' };
  142. Assert.AreEqual(expectedAdjecents.ToJoinedString(), graph.getAdjecents('A').ToJoinedString());
  143.  
  144. expectedAdjecents = new char[] { 'A', 'B', 'E' };
  145. Assert.AreEqual(expectedAdjecents.ToJoinedString(), graph.getAdjecents('D').ToJoinedString());
  146. }
  147.  
  148. [TestMethod]
  149. [ExpectedException(typeof(InvalidOperationException))]
  150. public void TestGraph2GetAdjecentsOfNonExistentVertex()
  151. {
  152. var graph = new Graph();
  153. graph.initGraph(graph2);
  154.  
  155. graph.getAdjecents('Y');
  156. }
  157.  
  158. [TestMethod]
  159. public void TestGraph2Traversal()
  160. {
  161. var graph = new Graph();
  162. graph.initGraph(graph2);
  163.  
  164. var expectedResult = new char[] { 'A', 'B', 'C', 'G', 'D', 'E', 'F' };
  165. char[] result = graph.Traverse();
  166.  
  167. Assert.AreEqual(expectedResult.ToJoinedString(), result.ToJoinedString());
  168. }
  169. #endregion
  170.  
  171. #region graph3
  172. [TestMethod]
  173. public void TestGraph3Initialisation()
  174. {
  175. var graph = new Graph();
  176. graph.initGraph(graph3);
  177.  
  178. var expectedAdjecencyMatrix = new char[][]
  179. {
  180. new char[] { 'G' }, //A
  181. new char[] { 'A' }, //G
  182. };
  183.  
  184. //Testing vertices
  185. var expectedVertices = new char[] { 'A', 'G' };
  186. Assert.AreEqual(expectedVertices.ToJoinedString(), graph.Vertices.ToJoinedString());
  187.  
  188. //Testing adjecency matrix
  189. int i = 0;
  190. foreach (var row in expectedAdjecencyMatrix)
  191. Assert.AreEqual(row.ToJoinedString(), graph.AdjecencyMatrix[i++].ToJoinedString());
  192. }
  193.  
  194. [TestMethod]
  195. public void TestGraph3GetAdjecents()
  196. {
  197. var graph = new Graph();
  198. graph.initGraph(graph3);
  199.  
  200. var expectedAdjecents = new char[] { 'G' };
  201. Assert.AreEqual(expectedAdjecents.ToJoinedString(), graph.getAdjecents('A').ToJoinedString());
  202.  
  203. expectedAdjecents = new char[] { 'A'};
  204. Assert.AreEqual(expectedAdjecents.ToJoinedString(), graph.getAdjecents('G').ToJoinedString());
  205. }
  206.  
  207. [TestMethod]
  208. [ExpectedException(typeof(InvalidOperationException))]
  209. public void TestGraph3GetAdjecentsOfNonExistentVertex()
  210. {
  211. var graph = new Graph();
  212. graph.initGraph(graph3);
  213.  
  214. graph.getAdjecents('Y');
  215. }
  216.  
  217. [TestMethod]
  218. public void TestGraph3Traversal()
  219. {
  220. var graph = new Graph();
  221. graph.initGraph(graph3);
  222.  
  223. var expectedResult = new char[] { 'A', 'G'};
  224. char[] result = graph.Traverse();
  225.  
  226. Assert.AreEqual(expectedResult.ToJoinedString(), result.ToJoinedString());
  227. }
  228. #endregion
  229. }
  230. }
  231.  
  232.  
  233. using System;
  234. using System.Collections.Generic;
  235. using System.Linq;
  236.  
  237. namespace GraphDataStructure
  238. {
  239. public class Graph
  240. {
  241. private char[][] adjecencyMatrix;
  242. private char[] vertices;
  243.  
  244. public Graph()
  245. {
  246. }
  247.  
  248. public char[] Vertices { get { return vertices; } }
  249.  
  250. public char[][] AdjecencyMatrix { get { return adjecencyMatrix; } }
  251.  
  252. public void initGraph(char[][] edges)
  253. {
  254. vertices = edges.SelectMany(x => x).Distinct().OrderBy(x => x).ToArray();
  255.  
  256. adjecencyMatrix = new char[vertices.Length][];
  257. for (int j = 0; j < vertices.Length; j++)
  258. {
  259. var adjecents = new List<char>();
  260. foreach(var row in edges)
  261. {
  262. if (row.Contains(vertices[j]))
  263. adjecents.Add(row.First(x => x != vertices[j]));
  264. }
  265. adjecencyMatrix[j] = adjecents.OrderBy(x => x).ToArray();
  266. }
  267. }
  268.  
  269. public char[] getAdjecents(char c)
  270. {
  271. if (Array.IndexOf(vertices, c) < 0)
  272. throw new InvalidOperationException("Invalid vertex");
  273.  
  274. return adjecencyMatrix[Array.IndexOf(vertices, c)];
  275. }
  276.  
  277. //DFS algorithm
  278. public char[] Traverse()
  279. {
  280. var stack = new Stack<char>();
  281. var result = new List<char>();
  282. var c = Vertices[0];
  283. result.Add(c);
  284. stack.Push(c);
  285. while(stack.Count > 0)
  286. {
  287. var adjecents = getAdjecents(stack.Peek());
  288. if (adjecents.Any(x => !result.Contains(x)))
  289. {
  290. c = adjecents.Where(x => !result.Contains(x)).ElementAt(0);
  291. result.Add(c);
  292. stack.Push(c);
  293. }
  294. else
  295. stack.Pop();
  296. }
  297. return result.ToArray();
  298. }
  299. }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement