Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 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. public class Algorithms
  10. {
  11. public HashSet<T> DFS<T>(Graph<T> graph, T start)
  12. {
  13. var visited = new HashSet<T>();
  14.  
  15. if (!graph.AdjacencyList.ContainsKey(start))
  16. return visited;
  17.  
  18. var stack = new Stack<T>();
  19. stack.Push(start);
  20.  
  21. while (stack.Count > 0)
  22. {
  23. var vertex = stack.Pop();
  24.  
  25. if (visited.Contains(vertex))
  26. continue;
  27.  
  28. visited.Add(vertex);
  29.  
  30. foreach (var neighbor in graph.AdjacencyList[vertex])
  31. if (!visited.Contains(neighbor))
  32. stack.Push(neighbor);
  33. }
  34.  
  35. return visited;
  36. }
  37. }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement