24#ifndef OR_TOOLS_GRAPH_CLIQUES_H_
25#define OR_TOOLS_GRAPH_CLIQUES_H_
36#include "absl/strings/str_cat.h"
50void FindCliques(std::function<
bool(
int,
int)> graph,
int node_count,
51 std::function<
bool(
const std::vector<int>&)> callback);
60 std::function<
bool(
const std::vector<int>&)> callback);
149template <
typename NodeIndex>
172 : is_arc_(
std::move(is_arc)),
173 clique_callback_(
std::move(clique_callback)),
174 num_nodes_(num_nodes) {}
227 State(
const State& other)
228 : pivot(other.pivot),
229 num_remaining_candidates(other.num_remaining_candidates),
230 candidates(other.candidates),
231 first_candidate_index(other.first_candidate_index),
232 candidate_for_recursion(other.candidate_for_recursion) {}
234 State& operator=(
const State& other) {
236 num_remaining_candidates = other.num_remaining_candidates;
237 candidates = other.candidates;
238 first_candidate_index = other.first_candidate_index;
239 candidate_for_recursion = other.candidate_for_recursion;
246 inline void MoveFirstCandidateToNotSet() {
247 ++first_candidate_index;
248 --num_remaining_candidates;
252 std::string DebugString() {
254 absl::StrAppend(&buffer,
"pivot = ", pivot,
255 "\nnum_remaining_candidates = ", num_remaining_candidates,
257 for (CandidateIndex
i(0);
i < candidates.size(); ++
i) {
258 if (i > 0) buffer +=
", ";
259 absl::StrAppend(&buffer, candidates[i]);
262 &buffer,
"]\nfirst_candidate_index = ", first_candidate_index.value(),
263 "\ncandidate_for_recursion = ", candidate_for_recursion.value());
272 int num_remaining_candidates;
281 util_intops::StrongVector<CandidateIndex, NodeIndex> candidates;
285 CandidateIndex first_candidate_index;
289 CandidateIndex candidate_for_recursion;
302 static const double kPushStateDeterministicTimeSecondsPerCandidate;
318 void InitializeState(State* state);
322 return node1 == node2 || is_arc_(node1, node2);
328 CandidateIndex SelectCandidateIndexForRecursion(State* state);
331 std::string CliqueDebugString(
const std::vector<NodeIndex>& clique);
349 std::vector<State> states_;
352 std::vector<NodeIndex> current_clique_;
356 int64_t num_remaining_iterations_;
360 TimeLimit* time_limit_;
378 void SetWeight(
int i,
double weight) { weights_[i] = weight; }
404 std::vector<std::vector<int>>
Run();
409 return clique_index_and_weight_;
414 bool HasEdge(
int i,
int j)
const {
return graph_[i][j]; }
418 int64_t work_limit_ = std::numeric_limits<int64_t>::max();
419 double weight_threshold_ = 0.0;
421 std::vector<double> weights_;
422 std::vector<Bitset64<int>> graph_;
425 std::vector<int> queue_;
430 std::vector<int> clique_;
435 std::vector<double> clique_weight_;
438 std::vector<Bitset64<int>> left_to_process_;
439 std::vector<Bitset64<int>> x_;
441 std::vector<std::pair<int, double>> clique_index_and_weight_;
444template <
typename NodeIndex>
445void BronKerboschAlgorithm<NodeIndex>::InitializeState(State* state) {
446 DCHECK(state !=
nullptr);
447 const int num_candidates = state->candidates.size();
448 int num_disconnected_candidates = num_candidates;
450 CandidateIndex pivot_index(-1);
451 for (CandidateIndex pivot_candidate_index(0);
452 pivot_candidate_index < num_candidates &&
453 num_disconnected_candidates > 0;
454 ++pivot_candidate_index) {
455 const NodeIndex pivot_candidate = state->candidates[pivot_candidate_index];
457 for (CandidateIndex i(state->first_candidate_index); i < num_candidates;
459 if (!IsArc(pivot_candidate, state->candidates[i])) {
463 if (count < num_disconnected_candidates) {
464 pivot_index = pivot_candidate_index;
465 state->pivot = pivot_candidate;
466 num_disconnected_candidates = count;
469 state->num_remaining_candidates = num_disconnected_candidates;
470 if (pivot_index >= state->first_candidate_index) {
471 std::swap(state->candidates[pivot_index],
472 state->candidates[state->first_candidate_index]);
473 ++state->num_remaining_candidates;
477template <
typename NodeIndex>
479BronKerboschAlgorithm<NodeIndex>::SelectCandidateIndexForRecursion(
481 DCHECK(state !=
nullptr);
482 CandidateIndex disconnected_node_index =
483 std::max(state->first_candidate_index, state->candidate_for_recursion);
484 while (disconnected_node_index < state->candidates.size() &&
485 state->candidates[disconnected_node_index] != state->pivot &&
486 IsArc(state->pivot, state->candidates[disconnected_node_index])) {
487 ++disconnected_node_index;
489 state->candidate_for_recursion = disconnected_node_index;
490 return disconnected_node_index;
493template <
typename NodeIndex>
494void BronKerboschAlgorithm<NodeIndex>::Initialize() {
495 DCHECK(states_.empty());
496 states_.reserve(num_nodes_);
497 states_.emplace_back();
499 State*
const root_state = &states_.back();
500 root_state->first_candidate_index = 0;
501 root_state->candidate_for_recursion = 0;
502 root_state->candidates.resize(num_nodes_, 0);
503 std::iota(root_state->candidates.begin(), root_state->candidates.end(), 0);
504 root_state->num_remaining_candidates = num_nodes_;
505 InitializeState(root_state);
507 DVLOG(2) <<
"Initialized";
510template <
typename NodeIndex>
511void BronKerboschAlgorithm<NodeIndex>::PopState() {
512 DCHECK(!states_.empty());
514 if (!states_.empty()) {
515 State*
const state = &states_.back();
516 current_clique_.pop_back();
517 state->MoveFirstCandidateToNotSet();
521template <
typename NodeIndex>
522std::string BronKerboschAlgorithm<NodeIndex>::CliqueDebugString(
523 const std::vector<NodeIndex>& clique) {
524 std::string message =
"Clique: [ ";
526 absl::StrAppend(&message, node,
" ");
532template <
typename NodeIndex>
533void BronKerboschAlgorithm<NodeIndex>::PushState(
NodeIndex selected) {
534 DCHECK(!states_.empty());
535 DCHECK(time_limit_ !=
nullptr);
536 DVLOG(2) <<
"PushState: New depth = " << states_.size() + 1
537 <<
", selected node = " << selected;
538 util_intops::StrongVector<CandidateIndex, NodeIndex> new_candidates;
540 State*
const previous_state = &states_.back();
541 const double deterministic_time =
543 previous_state->candidates.size();
544 time_limit_->AdvanceDeterministicTime(deterministic_time,
"PushState");
551 new_candidates.
reserve(previous_state->candidates.size());
552 for (CandidateIndex
i(0);
i < previous_state->first_candidate_index; ++
i) {
553 const NodeIndex candidate = previous_state->candidates[
i];
554 if (IsArc(selected, candidate)) {
558 const CandidateIndex new_first_candidate_index(new_candidates.size());
559 for (CandidateIndex i = previous_state->first_candidate_index + 1;
560 i < previous_state->candidates.size(); ++i) {
561 const NodeIndex candidate = previous_state->candidates[
i];
562 if (IsArc(selected, candidate)) {
567 current_clique_.push_back(selected);
568 if (new_candidates.empty()) {
571 DVLOG(2) << CliqueDebugString(current_clique_);
572 const CliqueResponse response = clique_callback_(current_clique_);
577 num_remaining_iterations_ = 1;
579 current_clique_.pop_back();
580 previous_state->MoveFirstCandidateToNotSet();
587 states_.emplace_back();
588 State*
const new_state = &states_.back();
589 new_state->candidates.swap(new_candidates);
590 new_state->first_candidate_index = new_first_candidate_index;
592 InitializeState(new_state);
595template <
typename NodeIndex>
600 if (states_.empty()) {
603 for (num_remaining_iterations_ = max_num_iterations;
604 !states_.empty() && num_remaining_iterations_ > 0 &&
606 --num_remaining_iterations_) {
607 State*
const state = &states_.back();
608 DVLOG(2) <<
"Loop: " << states_.size() <<
" states, "
609 << state->num_remaining_candidates <<
" candidate to explore\n"
610 << state->DebugString();
611 if (state->num_remaining_candidates == 0) {
616 const CandidateIndex selected_index =
617 SelectCandidateIndexForRecursion(state);
618 DVLOG(2) <<
"selected_index = " << selected_index;
619 const NodeIndex selected = state->candidates[selected_index];
620 DVLOG(2) <<
"Selected candidate = " << selected;
622 NodeIndex& f = state->candidates[state->first_candidate_index];
623 NodeIndex& s = state->candidates[selected_index];
628 time_limit_ =
nullptr;
633template <
typename NodeIndex>
635 int64_t max_num_iterations) {
640template <
typename NodeIndex>
645template <
typename NodeIndex>
std::function< bool(NodeIndex, NodeIndex)> IsArcCallback
BronKerboschAlgorithmStatus RunWithTimeLimit(TimeLimit *time_limit)
BronKerboschAlgorithmStatus Run()
std::function< CliqueResponse(const std::vector< NodeIndex > &)> CliqueCallback
BronKerboschAlgorithm(IsArcCallback is_arc, NodeIndex num_nodes, CliqueCallback clique_callback)
BronKerboschAlgorithmStatus RunIterations(int64_t max_num_iterations)
BronKerboschAlgorithmStatus RunWithTimeLimit(int64_t max_num_iterations, TimeLimit *time_limit)
void Initialize(int num_nodes)
void SetWeight(int i, double weight)
void SetMinimumWeight(double min_weight)
Set the minimum weight of the maximal cliques we are looking for.
std::vector< std::pair< int, double > > & GetMutableIndexAndWeight()
void AddEdge(int a, int b)
Add an edge in the graph.
bool HasEdge(int i, int j) const
void TakeTransitiveClosureOfImplicationGraph()
std::vector< std::vector< int > > Run()
void SetWorkLimit(int64_t limit)
We count the number of basic operations, and stop when we reach this limit.
void push_back(const value_type &val)
void reserve(size_type n)
#define DEFINE_INT_TYPE(int_type_name, value_type)
In SWIG mode, we don't want anything besides these top-level includes.
void FindCliques(std::function< bool(int, int)> graph, int node_count, std::function< bool(const std::vector< int > &)> callback)
BronKerboschAlgorithmStatus
@ COMPLETED
The algorithm has enumerated all maximal cliques.
const double BronKerboschAlgorithm< NodeIndex >::kPushStateDeterministicTimeSecondsPerCandidate
@ CONTINUE
The algorithm will continue searching for other maximal cliques.
void CoverArcsByCliques(std::function< bool(int, int)> graph, int node_count, std::function< bool(const std::vector< int > &)> callback)