Website Summary Files Commits Branches @commit @diff @stats
path: root / Seminar12 / src / connected-components / count-cc.cpp
diff options
context:
space:
mode:
Commit00cd4d0b3541c1026a4c0f8d66d953690299178a (patch)
Author
 Stoycho Kyosev <skyosev@192.168.100.17>
2025-01-10 07:51:36 -0500
Committer
 Stoycho Kyosev <skyosev@192.168.100.17>
2025-01-10 07:51:36 -0500
Filesf5ff87b8fec65e5fe38bad2b2186d41c3a4cd0e8 /Seminar12/src/connected-components/count-cc.cpp
Data-structures-and-algorithms-00cd4d0b3541c1026a4c0f8d66d953690299178a.tar
Data-structures-and-algorithms-00cd4d0b3541c1026a4c0f8d66d953690299178a.tar.gz
Data-structures-and-algorithms-00cd4d0b3541c1026a4c0f8d66d953690299178a.zip
Parent90b32d10c043f97b364537758e4edf610acd6bd1 (diff)
Add code for sem12
1 files changed, 51 insertions, 0 deletions
-rw-r--r--Seminar12/src/connected-components/count-cc.cpp51
diff --git a/Seminar12/src/connected-components/count-cc.cpp b/Seminar12/src/connected-components/count-cc.cpp
new file mode 100644
index 0000000..4e2e84c
--- /dev/null
+++ b/Seminar12/src/connected-components/count-cc.cpp
@@ -0,0 +1,51 @@
+#include "../graph_impl/graph/graph.h"
+#include <iostream>
+
+void dfs_visit(const graph& g, graph::vertex_t from, std::vector<bool>& visited)
+{
+ visited[from] = true;
+
+ const auto& adjacent = g.adjacent(from);
+
+ for(const auto& vertex : adjacent)
+ {
+ if(!visited[vertex])
+ {
+ dfs_visit(g, vertex, visited);
+ }
+ }
+}
+
+size_t count_cc(const graph& g)
+{
+ std::vector<bool> visited(g.vertex_count(), false);
+ size_t cnt = 0;
+
+ for (int i = 0; i < g.vertex_count(); i++)
+ {
+ if(!visited[i])
+ {
+ ++cnt;
+ dfs_visit(g, i, visited);
+ }
+ }
+
+ return cnt;
+}
+
+int main()
+{
+ graph g(6, false);
+
+ g.add_edge(0, 1);
+ g.add_edge(0, 2);
+ g.add_edge(2, 3);
+ g.add_edge(1, 4);
+ g.add_edge(3, 5);
+ g.add_edge(4, 5);
+ g.add_edge(2, 5);
+
+ g.add_vertex();
+
+ std::cout << count_cc(g);
+} \ No newline at end of file