TRIQS/triqs_modest 3.3.0
Brillouin zone summation
Loading...
Searching...
No Matches
graph_algo.hpp
Go to the documentation of this file.
1// Copyright (c) 2025--present, The Simons Foundation
2// This file is part of TRIQS/modest and is licensed under the terms of GPLv3 or later.
3// SPDX-License-Identifier: GPL-3.0-or-later
4// See LICENSE in the root of this distribution for details.
5
6#pragma once
7#include <algorithm>
8#include <vector>
9#include <deque>
10#include <nda/nda.hpp>
11
12namespace nda {
20 std::vector<std::vector<int>> find_blocks(nda::matrix_view<double> m, double threshold) {
21 auto n = m.extent(0);
22 std::vector<bool> visited(n, false);
23 std::vector<std::vector<int>> components; // the result
24 using std::abs;
25
26 std::vector<std::vector<int>> adjacency_list(n);
27 for (int i = 0; i < n; ++i)
28 for (int j = 0; j < n; ++j)
29 if (abs(m(i, j)) > threshold) adjacency_list[i].push_back(j);
30
31 auto bfs = [&](int node) {
32 std::deque<int> queue = {node};
33 std::vector<int> component;
34 while (!queue.empty()) {
35 int curr = queue.front();
36 queue.pop_front();
37 if (!visited[curr]) {
38 visited[curr] = true;
39 component.push_back(curr);
40 for (int neighbor : adjacency_list[curr]) {
41 if (!visited[neighbor]) queue.push_back(neighbor);
42 }
43 }
44 }
45 return component;
46 };
47
48 for (int i = 0; i < n; ++i)
49 if (!visited[i]) components.push_back(bfs(i));
50
51 // Sort each component
52 for (auto &c : components) std::ranges::sort(c);
53 return components;
54 }
55} // namespace nda
std::vector< std::vector< int > > find_blocks(nda::matrix_view< double > m, double threshold)
Find a block structure of a Matrix.