1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmComputeComponentGraph.h"
18 //----------------------------------------------------------------------------
19 cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input):
22 // Identify components.
25 // Compute the component graph.
26 this->ComponentGraph.resize(0);
27 this->ComponentGraph.resize(this->Components.size());
28 this->TransferEdges();
31 //----------------------------------------------------------------------------
32 cmComputeComponentGraph::~cmComputeComponentGraph()
36 //----------------------------------------------------------------------------
37 void cmComputeComponentGraph::Tarjan()
39 int n = static_cast<int>(this->InputGraph.size());
40 TarjanEntry entry = {0,0};
41 this->TarjanEntries.resize(0);
42 this->TarjanEntries.resize(n, entry);
43 this->TarjanComponents.resize(0);
44 this->TarjanComponents.resize(n, -1);
45 this->TarjanWalkId = 0;
46 this->TarjanVisited.resize(0);
47 this->TarjanVisited.resize(n, 0);
48 for(int i = 0; i < n; ++i)
50 // Start a new DFS from this node if it has never been visited.
51 if(!this->TarjanVisited[i])
53 assert(this->TarjanStack.empty());
55 this->TarjanIndex = 0;
61 //----------------------------------------------------------------------------
62 void cmComputeComponentGraph::TarjanVisit(int i)
64 // We are now visiting this node.
65 this->TarjanVisited[i] = this->TarjanWalkId;
67 // Initialize the entry.
68 this->TarjanEntries[i].Root = i;
69 this->TarjanComponents[i] = -1;
70 this->TarjanEntries[i].VisitIndex = ++this->TarjanIndex;
71 this->TarjanStack.push(i);
73 // Follow outgoing edges.
74 EdgeList const& nl = this->InputGraph[i];
75 for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
79 // Ignore edges to nodes that have been reached by a previous DFS
80 // walk. Since we did not reach the current node from that walk
81 // it must not belong to the same component and it has already
82 // been assigned to a component.
83 if(this->TarjanVisited[j] > 0 &&
84 this->TarjanVisited[j] < this->TarjanWalkId)
89 // Visit the destination if it has not yet been visited.
90 if(!this->TarjanVisited[j])
95 // If the destination has not yet been assigned to a component,
96 // check if it has a better root for the current object.
97 if(this->TarjanComponents[j] < 0)
99 if(this->TarjanEntries[this->TarjanEntries[j].Root].VisitIndex <
100 this->TarjanEntries[this->TarjanEntries[i].Root].VisitIndex)
102 this->TarjanEntries[i].Root = this->TarjanEntries[j].Root;
107 // Check if we have found a component.
108 if(this->TarjanEntries[i].Root == i)
111 int c = static_cast<int>(this->Components.size());
112 this->Components.push_back(NodeList());
113 NodeList& component = this->Components[c];
115 // Populate the component list.
119 // Get the next member of the component.
120 j = this->TarjanStack.top();
121 this->TarjanStack.pop();
123 // Assign the member to the component.
124 this->TarjanComponents[j] = c;
125 this->TarjanEntries[j].Root = i;
127 // Store the node in its component.
128 component.push_back(j);
131 // Sort the component members for clarity.
132 std::sort(component.begin(), component.end());
136 //----------------------------------------------------------------------------
137 void cmComputeComponentGraph::TransferEdges()
139 // Map inter-component edges in the original graph to edges in the
141 int n = static_cast<int>(this->InputGraph.size());
142 for(int i=0; i < n; ++i)
144 int i_component = this->TarjanComponents[i];
145 EdgeList const& nl = this->InputGraph[i];
146 for(EdgeList::const_iterator ni = nl.begin(); ni != nl.end(); ++ni)
149 int j_component = this->TarjanComponents[j];
150 if(i_component != j_component)
152 // We do not attempt to combine duplicate edges, but instead
153 // store the inter-component edges with suitable multiplicity.
154 this->ComponentGraph[i_component].push_back(
155 cmGraphEdge(j_component, ni->IsStrong()));