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.

144 lines
4.1 KiB

  1. /**
  2. * @file minimun_spanning_tree.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 2011 Jean-Pierre Charras
  8. * Copyright (C) 2004-2011 KiCad Developers, see change_log.txt for contributors.
  9. *
  10. * derived from this article:
  11. * http://compprog.wordpress.com/2007/11/09/minimal-spanning-trees-prims-algorithm
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version 2
  16. * of the License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, you may find one here:
  25. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  26. * or you may search the http://www.gnu.org website for the version 2 license,
  27. * or you may write to the Free Software Foundation, Inc.,
  28. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  29. */
  30. #include <limits.h>
  31. #include <minimun_spanning_tree.h>
  32. #include <class_pad.h>
  33. /*
  34. * The class MIN_SPAN_TREE calculates the rectilinear minimum spanning tree
  35. * of a set of points (pads usually having the same net)
  36. * using the Prim's algorithm.
  37. */
  38. /*
  39. * Prim's Algorithm
  40. * Step 0
  41. * Pick any vertex as a starting vertex. (Call it S).
  42. * Mark it with any given flag, say 1.
  43. *
  44. * Step 1
  45. * Find the nearest neighbour of S (call it P1).
  46. * Mark both P1 and the edge SP1.
  47. * cheapest unmarked edge in the graph that doesn't close a marked circuit.
  48. * Mark this edge.
  49. *
  50. * Step 2
  51. * Find the nearest unmarked neighbour to the marked subgraph
  52. * (i.e., the closest vertex to any marked vertex).
  53. * Mark it and the edge connecting the vertex.
  54. *
  55. * Step 3
  56. * Repeat Step 2 until all vertices are marked.
  57. * The marked subgraph is a minimum spanning tree.
  58. */
  59. MIN_SPAN_TREE::MIN_SPAN_TREE()
  60. {
  61. MSP_Init( 0 );
  62. }
  63. void MIN_SPAN_TREE::MSP_Init( int aNodesCount )
  64. {
  65. m_Size = aNodesCount;
  66. inTree.clear();
  67. linkedTo.clear();
  68. distTo.clear();
  69. if( m_Size == 0 )
  70. return;
  71. // Reserve space in memory
  72. inTree.reserve( m_Size );
  73. linkedTo.reserve( m_Size );
  74. distTo.reserve( m_Size );
  75. // Initialize values:
  76. for( int ii = 0; ii < m_Size; ii++ )
  77. {
  78. // Initialise dist with infinity:
  79. distTo.push_back( INT_MAX );
  80. // Mark all nodes as NOT beeing in the minimum spanning tree:
  81. inTree.push_back( 0 );
  82. linkedTo.push_back( 0 );
  83. }
  84. }
  85. /* updateDistances(int target)
  86. * should be called immediately after target is added to the tree;
  87. * updates dist so that the values are correct (goes through target's
  88. * neighbours making sure that the distances between them and the tree
  89. * are indeed minimum)
  90. */
  91. void MIN_SPAN_TREE::updateDistances( int target )
  92. {
  93. for( int ii = 0; ii < m_Size; ++ii )
  94. {
  95. if( !inTree[ii] ) // no need to evaluate weight for already in tree items
  96. {
  97. int weight = GetWeight( target, ii );
  98. if( (weight > 0) && (distTo[ii] > weight ) )
  99. {
  100. distTo[ii] = weight;
  101. linkedTo[ii] = target;
  102. }
  103. }
  104. }
  105. }
  106. void MIN_SPAN_TREE::BuildTree()
  107. {
  108. /* Add the first node to the tree */
  109. inTree[0] = 1;
  110. updateDistances( 0 );
  111. for( int treeSize = 1; treeSize < m_Size; ++treeSize )
  112. {
  113. // Find the node with the smallest distance to the tree
  114. int min = -1;
  115. for( int ii = 0; ii < m_Size; ++ii )
  116. {
  117. if( !inTree[ii] )
  118. {
  119. if( (min == -1) || (distTo[min] > distTo[ii]) )
  120. min = ii;
  121. }
  122. }
  123. inTree[min] = 1;
  124. updateDistances( min );
  125. }
  126. }