Skip to content

Commit

Permalink
raise error if target graph is not connected
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Maas committed Nov 26, 2024
1 parent fab2b5e commit 10b334d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
30 changes: 29 additions & 1 deletion mt-kahypar/partition/mapping/target_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,17 @@ namespace mt_kahypar {
#ifdef KAHYPAR_ENABLE_STEINER_TREE_METRIC
void TargetGraph::precomputeDistances(const size_t max_connectivity) {
ALWAYS_ASSERT(max_connectivity >= 2);
if (!inputGraphIsConnected()) {
throw InvalidInputException("Target graph must be connected, but it is not.");
}

const size_t num_entries = std::pow(_k, max_connectivity);
if ( num_entries > MEMORY_LIMIT ) {
throw SystemException(
"Too much memory requested for precomputing steiner trees "
"of connectivity sets in the target graph.");
}
_distances.assign(num_entries, std::numeric_limits<HyperedgeWeight>::max() / 3);
_distances.assign(num_entries, kInvalidDistance);
SteinerTree::compute(_graph, max_connectivity, _distances);

_max_precomputed_connectitivty = max_connectivity;
Expand All @@ -57,6 +61,7 @@ HyperedgeWeight TargetGraph::distance(const ds::StaticBitset& connectivity_set)
const size_t idx = index(connectivity_set);
ASSERT(idx < _distances.size());
if constexpr ( TRACK_STATS ) ++_stats.precomputed;
ASSERT(_distances[idx] < kInvalidDistance);
return _distances[idx];
} else {
const uint64_t hash_key = computeHash(connectivity_set);
Expand Down Expand Up @@ -152,6 +157,29 @@ HyperedgeWeight TargetGraph::computeWeightOfMSTOnMetricCompletion(const ds::Stat
ASSERT(pq.empty());
return res;
}

bool TargetGraph::inputGraphIsConnected() const {
// stack-based DFS
std::vector<uint8_t> visited;
std::vector<HypernodeID> stack;
visited.resize(_graph.initialNumNodes(), 0);
stack.push_back(0);
visited[0] = 1;
HypernodeID num_visited = 1;
while (!stack.empty() && num_visited < _graph.initialNumNodes()) {
HypernodeID hn = stack.back();
stack.pop_back();
for (HyperedgeID edge: _graph.incidentEdges(hn)) {
HypernodeID neighbor = _graph.edgeTarget(edge);
if (visited[neighbor] == 0) {
stack.push_back(neighbor);
visited[neighbor] = 1;
num_visited++;
}
}
}
return num_visited == _graph.initialNumNodes();
}
#endif

} // namespace kahypar
3 changes: 3 additions & 0 deletions mt-kahypar/partition/mapping/target_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace mt_kahypar {
#ifdef KAHYPAR_ENABLE_STEINER_TREE_METRIC
class TargetGraph {

static constexpr HyperedgeWeight kInvalidDistance = std::numeric_limits<HyperedgeWeight>::max() / 3;
static constexpr size_t INITIAL_HASH_TABLE_CAPACITY = 100000;
static constexpr size_t MEMORY_LIMIT = 100000000;

Expand Down Expand Up @@ -256,6 +257,8 @@ class TargetGraph {
// ! connecting u and v. This gives a 2-approximation for steiner tree problem.
HyperedgeWeight computeWeightOfMSTOnMetricCompletion(const ds::StaticBitset& connectivity_set) const;

bool inputGraphIsConnected() const;

#ifdef __linux__
HashTableHandle getHandle() const {
return _cache.get_handle();
Expand Down

0 comments on commit 10b334d

Please sign in to comment.