PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace/Collections/TopologicalSort.cs

https://bitbucket.org/VahidN/interlace
C# | 101 lines | 59 code | 16 blank | 26 comment | 8 complexity | ca00f1a79770c8952dd079d93815453d MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. #endregion
  31. namespace Interlace.Collections
  32. {
  33. public delegate IEnumerable<T> EdgeGetter<T>(T forObject);
  34. public class TopologicalSort
  35. {
  36. enum VertexState
  37. {
  38. Unexplored = 0,
  39. Discovered,
  40. Finished
  41. }
  42. public static ICollection<T> Sort<T>(ICollection<T> objects, EdgeGetter<T> edgeGetter)
  43. {
  44. List<T> finishedObjects = new List<T>();
  45. Dictionary<T, VertexState> states = new Dictionary<T, VertexState>(objects.Count);
  46. foreach (T o in objects)
  47. {
  48. if (states.ContainsKey(o))
  49. {
  50. throw new TopologicalSortException("An object in a list to be topologically " +
  51. "sorted is equal to another object in the list, which is not valid.");
  52. }
  53. states[o] = VertexState.Unexplored;
  54. }
  55. foreach (T visiting in objects)
  56. {
  57. if (states[visiting] == VertexState.Unexplored)
  58. {
  59. DepthFirstSearchVisit(objects, edgeGetter, states, visiting, finishedObjects);
  60. }
  61. }
  62. finishedObjects.Reverse();
  63. return finishedObjects;
  64. }
  65. static void DepthFirstSearchVisit<T>(ICollection<T> objects, EdgeGetter<T> edgeGetter, Dictionary<T, VertexState> states, T visiting, List<T> finishedObjects)
  66. {
  67. states[visiting] = VertexState.Discovered;
  68. foreach (T edge in edgeGetter(visiting))
  69. {
  70. if (states[edge] == VertexState.Unexplored)
  71. {
  72. DepthFirstSearchVisit(objects, edgeGetter, states, edge, finishedObjects);
  73. }
  74. else if (states[edge] == VertexState.Discovered)
  75. {
  76. // The edge is a back-edge; the graph is cyclic:
  77. throw new TopologicalSortException("The supplied graph is cyclic, and therefore has " +
  78. "no topological sort.");
  79. }
  80. }
  81. states[visiting] = VertexState.Finished;
  82. finishedObjects.Add(visiting);
  83. }
  84. }
  85. }