Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5.  
  6. namespace PDP_Lab8
  7. {
  8. using Graph = ConcurrentDictionary<Int32, List<Int32>>;
  9. using State = List<Int32>;
  10. using StateQueue = ConcurrentQueue<List<Int32>>;
  11.  
  12. class Task
  13. {
  14. public Task(Graph g, StateQueue q)
  15. {
  16. m_Graph = g;
  17. m_Queue = q;
  18. m_Cycles = new List<State>();
  19. }
  20.  
  21. public void Run()
  22. {
  23. Console.WriteLine("Starting thread...");
  24. try
  25. {
  26. while (!m_Done)
  27. {
  28. //Thread.Sleep(new Random().Next(50, 100));
  29. State currentState;
  30. if (m_Queue.TryDequeue(out currentState))
  31. {
  32. Int32 node = currentState[currentState.Count - 1];
  33. if (m_Graph.Count == currentState.Count && m_Graph[node].Contains(currentState[0]))
  34. {
  35. m_Cycles.Add(currentState);
  36. m_Done = true;
  37. return;
  38. }
  39. foreach (Int32 neighbour in m_Graph[node])
  40. {
  41. if (!currentState.Contains(neighbour))
  42. {
  43. State newState = new State();
  44. newState.AddRange(currentState);
  45. newState.Add(neighbour);
  46. m_Queue.Enqueue(newState);
  47. }
  48. }
  49. }
  50. }
  51. }
  52. catch { }
  53. }
  54.  
  55. public Graph m_Graph;
  56. public StateQueue m_Queue;
  57. public List<State> m_Cycles;
  58. public bool m_Done = false;
  59. }
  60.  
  61. class Program
  62. {
  63. public static Graph GenerateGraph(Int32 nrOfNodes, Int32 nrOfEdges)
  64. {
  65. Graph g = new Graph();
  66. Random r = new Random();
  67. for (Int32 i = 0; i < nrOfEdges; ++i)
  68. {
  69. Int32 x = r.Next(1, nrOfNodes + 1);
  70. Int32 y = r.Next(1, nrOfNodes + 1);
  71. while (g.ContainsKey(x) && g[x].Contains(y))
  72. {
  73. x = r.Next(1, nrOfNodes + 1);
  74. y = r.Next(1, nrOfNodes + 1);
  75. }
  76.  
  77. if (g.ContainsKey(x))
  78. g[x].Add(y);
  79. else
  80. g.TryAdd(x, new State { y });
  81. }
  82. String[] lines = new String[nrOfEdges];
  83. Int32 edge = 0;
  84. foreach (Int32 node in g.Keys)
  85. foreach (Int32 neighbour in g[node])
  86. {
  87. lines[edge] = node.ToString() + " " + neighbour.ToString();
  88. edge++;
  89. }
  90.  
  91. System.IO.File.WriteAllLines("data.txt", lines);
  92. return g;
  93. }
  94.  
  95. public static Graph ReadGraph()
  96. {
  97. Graph g = new Graph();
  98. String[] text = System.IO.File.ReadAllLines("data.txt");
  99. foreach (String line in text)
  100. {
  101. String[] tokens = line.Split(' ');
  102. Int32 n1, n2;
  103. Int32.TryParse(tokens[0], out n1);
  104. Int32.TryParse(tokens[1], out n2);
  105.  
  106. if (g.ContainsKey(n1))
  107. g[n1].Add(n2);
  108. else
  109. g.TryAdd(n1, new State{ n2 });
  110. }
  111.  
  112.  
  113. return g;
  114. }
  115.  
  116. static void Main(string[] args)
  117. {
  118. Int32 threads = 8;
  119. Graph g = ReadGraph();
  120. StateQueue sq = new StateQueue();
  121.  
  122. foreach (Int32 node in g.Keys)
  123. sq.Enqueue(new State { node });
  124.  
  125. Task task = new Task(g, sq);
  126. var tasks = new System.Threading.Tasks.Task[threads];
  127. var watch = System.Diagnostics.Stopwatch.StartNew();
  128. for (Int32 i = 0; i < threads; ++i)
  129. tasks[i] = System.Threading.Tasks.Task.Factory.StartNew(() => task.Run());
  130.  
  131. System.Threading.Tasks.Task.WaitAll(tasks);
  132. watch.Stop();
  133. if (task.m_Cycles.Count > 0)
  134. {
  135. Console.WriteLine("Found Hamiltonean Cycle:");
  136. foreach (Int32 node in task.m_Cycles[0])
  137. Console.Write(node.ToString() + " ");
  138. Console.WriteLine();
  139. }
  140. else
  141. Console.WriteLine("No Hamiltonean Cycle.");
  142. Console.WriteLine("Total runtime: " + watch.ElapsedMilliseconds.ToString() + "ms.");
  143. }
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement