-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathCC.cpp.in
More file actions
67 lines (62 loc) · 1.87 KB
/
Copy pathCC.cpp.in
File metadata and controls
67 lines (62 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright (C) 2024-2026 landerrosette <57791410+landerrosette@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/******************************************************************************
* % ./CC tinyG.txt
* 3 components
* 6 5 4 3 2 1 0
* 8 7
* 12 11 10 9
*
* % ./KosarajuSCC tinyDG.txt
* 5 components
* 1
* 5 4 3 2 0
* 12 11 10 9
* 8 6
* 7
******************************************************************************/
#include "algs4/@CC@.hpp"
#include <fstream>
#include <iostream>
#include <vector>
#include "algs4/Bag.hpp"
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "Not enough arguments" << std::endl;
return 1;
}
std::ifstream in(argv[1]);
if (!in.is_open()) {
std::cerr << "Cannot open file: " << argv[1] << std::endl;
return 1;
}
#ifndef DIRECTED
algs4::Graph G(in);
#else
algs4::Digraph G(in);
#endif
algs4::@CC@ cc(G);
int M = cc.count();
std::cout << M << " components" << std::endl;
std::vector<algs4::Bag<int>> components(M);
for (int v = 0; v < G.V(); ++v) components[cc.id(v)].add(v);
for (int i = 0; i < M; ++i) {
for (int v : components[i]) std::cout << v << " ";
std::cout << std::endl;
}
return 0;
}