Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Ex1
  5. {
  6. class Graph1
  7. {
  8. private Dictionary<string, HashSet<string>> adjacencyList;
  9.  
  10. public Graph1()
  11. {
  12. adjacencyList = new Dictionary<string, HashSet<string>>();
  13. }
  14.  
  15. static void Main(string[] args)
  16. {
  17. DoGraphThingies();
  18. }
  19.  
  20. static void DoGraphThingies()
  21. {
  22. int graphNum = 2;
  23. Graph1 g = CreateGraph(graphNum);
  24. g.PrintGraph();
  25.  
  26. bool recursive = true;
  27.  
  28. g.DFSTraversal("b", recursive);
  29. g.DFSTraversal("c", recursive);
  30.  
  31. g.DFSTraversal("a", recursive);
  32. g.DFSTraversal("a", !recursive);
  33.  
  34. g.BFSTraversal("a");
  35. }
  36.  
  37. static Graph1 CreateGraph(int graphNum)
  38. {
  39. Graph1 g = new Graph1();
  40. if (graphNum == 1)
  41. {
  42. g.AddEdge("a", "b");
  43. g.AddEdge("b", "c");
  44. g.AddEdge("b", "d");
  45. g.AddEdge("d", "c");
  46. g.AddEdge("d", "c");
  47. g.AddEdge("c", "a");
  48. }
  49. else if (graphNum == 2)
  50. {
  51. g.AddEdge("a", "b");
  52. g.AddEdge("b", "c");
  53. g.AddEdge("c", "d");
  54. g.AddEdge("d", "d1");
  55. g.AddEdge("d1", "d2");
  56. g.AddEdge("c", "c1");
  57. g.AddEdge("c1", "c2");
  58. }
  59. else if (graphNum == 3) // this one is essentially a binary tree
  60. {
  61. g.AddEdge("a", "b");
  62. g.AddEdge("a", "c");
  63. g.AddEdge("b", "d");
  64. g.AddEdge("b", "e");
  65. g.AddEdge("c", "f");
  66. g.AddEdge("c", "g");
  67. g.AddEdge("d", "h");
  68. g.AddEdge("d", "i");
  69. }
  70. else
  71. {
  72. g.AddEdge("a", "b");
  73. g.AddEdge("a", "c");
  74. }
  75. return g;
  76. }
  77.  
  78. private void DFSTraversal(string startingVertex, bool recursive)
  79. {
  80. Console.WriteLine("DFS " + (recursive ? "recursive" : "non-recursive") +
  81. " starting at vertex " + startingVertex + ":");
  82.  
  83. Dictionary<string, bool> visited = new Dictionary<string, bool>();
  84.  
  85. if (recursive)
  86. DFS(startingVertex, visited);
  87. else
  88. DFSNonRecursive(startingVertex, visited);
  89.  
  90. Console.WriteLine();
  91. }
  92.  
  93. private void VisitVertex(string v, Dictionary<string, bool> visited)
  94. {
  95. visited[v] = true;
  96. Console.Write(v + " ");
  97. }
  98.  
  99. private void DFSNonRecursive(string v, Dictionary<string, bool> visited)
  100. {
  101. Stack<string> s = new Stack<string>();
  102. s.Push(v);
  103.  
  104. while (s.Count > 0)
  105. {
  106. string vertex = s.Pop();
  107. VisitVertex(vertex, visited);
  108.  
  109. PushUnvisitedNeighbors(vertex, s, visited);
  110. }
  111. }
  112. private void PushUnvisitedNeighbors(string vertex, Stack<string> s, Dictionary<string, bool> visited)
  113. {
  114. // If vertex has no neighbors, nothing to do
  115. if (!adjacencyList.ContainsKey(vertex))
  116. return;
  117.  
  118. HashSet<string> neighbors = adjacencyList[vertex];
  119.  
  120. foreach (var neighborVertex in neighbors)
  121. {
  122. if (!visited.ContainsKey(neighborVertex) || (visited[neighborVertex] == false))
  123. {
  124. s.Push(neighborVertex);
  125. }
  126. }
  127. }
  128.  
  129. private void PushUnvisitedNeighborsMaintainDFSRecursiveOrder(string vertex, Stack<string> s, Dictionary<string, bool> visited)
  130. {
  131. // If vertex has no neighbors, nothing to do
  132. if (!adjacencyList.ContainsKey(vertex))
  133. return;
  134.  
  135. HashSet<string> neighbors = adjacencyList[vertex];
  136. string neighborsStr = string.Join(" ", adjacencyList);
  137.  
  138. Stack<string> s2 = new Stack<string>();
  139.  
  140. foreach (var neighborVertex in neighbors)
  141. {
  142. if (!visited.ContainsKey(neighborVertex) || (visited[neighborVertex] == false))
  143. {
  144. s2.Push(neighborVertex);
  145. }
  146. }
  147. foreach( var vtx in s2)
  148. {
  149. s.Push(vtx);
  150. }
  151. }
  152.  
  153. private void DFS(string v, Dictionary<string, bool> visited)
  154. {
  155. VisitVertex(v, visited);
  156.  
  157. // If v has no neighbors, nothing more to do
  158. if (!adjacencyList.ContainsKey(v))
  159. return;
  160.  
  161. HashSet<string> neighbors = adjacencyList[v];
  162. foreach( var neighborVertex in neighbors)
  163. {
  164. if (!visited.ContainsKey(neighborVertex) || (visited[neighborVertex] == false))
  165. {
  166. DFS(neighborVertex, visited);
  167. }
  168. }
  169. }
  170.  
  171. private void BFSTraversal(string startingVertex)
  172. {
  173. Console.WriteLine("BFS starting at vertex " + startingVertex + ":");
  174. Dictionary<string, bool> visited = new Dictionary<string, bool>();
  175. BFS(startingVertex, visited);
  176. Console.WriteLine();
  177. }
  178.  
  179. private void BFS(string v, Dictionary<string, bool> visited)
  180. {
  181. Queue<string> q = new Queue<string>();
  182. q.Enqueue(v);
  183.  
  184. while (q.Count > 0)
  185. {
  186. string vertex = q.Dequeue();
  187. VisitVertex(vertex, visited);
  188.  
  189. EnqueueUnvisitedNeighbors(vertex, q, visited);
  190. }
  191. }
  192.  
  193. private void EnqueueUnvisitedNeighbors(string vertex, Queue<string> q, Dictionary<string, bool> visited)
  194. {
  195. // If v has no neighbors, nothing to do
  196. if (!adjacencyList.ContainsKey(vertex))
  197. return;
  198.  
  199. HashSet<string> neighbors = adjacencyList[vertex];
  200. foreach (var neighborVertex in neighbors)
  201. {
  202. if (!visited.ContainsKey(neighborVertex) || (visited[neighborVertex] == false))
  203. {
  204. q.Enqueue(neighborVertex);
  205. }
  206. }
  207. }
  208.  
  209. public void AddEdge(string v1, string v2)
  210. {
  211. if (!adjacencyList.ContainsKey(v1))
  212. {
  213. adjacencyList[v1] = new HashSet<string>();
  214. }
  215. adjacencyList[v1].Add(v2);
  216. }
  217.  
  218. public void PrintGraph()
  219. {
  220. foreach (var entry in adjacencyList)
  221. {
  222. HashSet<string> vertexNeighbors = entry.Value;
  223. Console.Write(entry.Key + ": ");
  224. PrintVertices(vertexNeighbors);
  225. Console.WriteLine();
  226. }
  227. }
  228.  
  229. public void PrintVertices(HashSet<string> vertices)
  230. {
  231. foreach ( var entry in vertices)
  232. {
  233. Console.Write(entry + " ");
  234. }
  235. }
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement