You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

93 lines
3.0 KiB

  1. /*
  2. This file is part of BioD.
  3. Copyright (C) 2012 Artem Tarasov <lomereiter@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the "Software"),
  6. to deal in the Software without restriction, including without limitation
  7. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. and/or sell copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. DEALINGS IN THE SOFTWARE.
  19. */
  20. module bio.bam.utils.graph;
  21. import std.exception;
  22. import std.array;
  23. import std.algorithm;
  24. import std.range;
  25. /// This class is used only in utils/samheadermerger.d for merging
  26. /// reference sequence dictionaries, thus not much functionality
  27. /// is presented here.
  28. class DirectedGraph {
  29. void addEdge(string a, string b) {
  30. auto indA = addNode(a);
  31. auto indB = addNode(b);
  32. _edges[indA] ~= indB;
  33. }
  34. /// Returns: unique integer identifier of node
  35. size_t addNode(string a) {
  36. if (a !in _indices) {
  37. _nodes ~= a;
  38. _edges.length = _nodes.length;
  39. return _indices[a] = _nodes.length - 1;
  40. } else {
  41. return _indices[a];
  42. }
  43. }
  44. string[] topologicalSort() {
  45. auto predecessor_count = new size_t[_nodes.length];
  46. foreach (node_neighbours; _edges) {
  47. foreach (neighbour; node_neighbours)
  48. predecessor_count[neighbour] += 1;
  49. }
  50. size_t[] queue;
  51. queue.reserve(_nodes.length);
  52. for (size_t i = 0; i < _nodes.length; i++)
  53. if (predecessor_count[i] == 0)
  54. queue ~= i;
  55. string[] result;
  56. result.reserve(_nodes.length);
  57. while (!queue.empty) {
  58. auto front = queue[0];
  59. result ~= _nodes[front];
  60. queue = queue[1 .. $];
  61. foreach (successor; _edges[front]) {
  62. predecessor_count[successor] -= 1;
  63. if (predecessor_count[successor] == 0) {
  64. queue ~= successor;
  65. }
  66. }
  67. }
  68. if (result.length < _nodes.length) {
  69. throw new Exception("graph contains cycles");
  70. }
  71. return result;
  72. }
  73. private:
  74. string[] _nodes;
  75. size_t[string] _indices;
  76. size_t[][] _edges;
  77. }