diff --git a/README.md b/README.md
index bcde5a3d..1989c20b 100644
--- a/README.md
+++ b/README.md
@@ -652,8 +652,8 @@ In order to achieve greater coverage and encourage more people to contribute to
-
-
+
+
|
diff --git a/src/cpp/ConnectedComponents.cpp b/src/cpp/ConnectedComponents.cpp
new file mode 100644
index 00000000..ac565f58
--- /dev/null
+++ b/src/cpp/ConnectedComponents.cpp
@@ -0,0 +1,53 @@
+#include
+#include
+
+#define VERTICES 6
+#define INF -1
+
+std::vector visited(VERTICES, false); // Array to track visited vertices
+int components = 0;
+
+// Adjacency matrix representing the graph
+int matrix[VERTICES][VERTICES] = {{0, INF, 1, INF, INF, INF},
+ {INF, 0, INF, 1, 1, INF},
+ {1, INF, 0, INF, INF, INF},
+ {INF, 1, INF, 0, 1, 1},
+ {INF, 1, INF, 1, 0, 1},
+ {INF, INF, INF, 1, 1, 0}};
+
+// Recursive method to find connected components using adjacency matrix
+void findConnectedComponents(int current)
+{
+ for (int i = 0; i < VERTICES; i++)
+ {
+ if (!visited[i] && matrix[current][i] == 1)
+ {
+ visited[i] = true;
+ components++;
+ std::cout << "(" << i << ")-";
+ findConnectedComponents(i);
+ }
+ }
+}
+
+int main()
+{
+ // Initialize all vertices as unvisited
+ for (int i = 0; i < VERTICES; i++)
+ visited[i] = false;
+
+ // For each vertex, if it is unvisited, start a DFS and count components
+ for (int i = 0; i < VERTICES; i++)
+ {
+ if (!visited[i])
+ {
+ components = 0;
+ visited[i] = true;
+ std::cout << "Starting at vertex (" << i << ")-";
+ findConnectedComponents(i);
+ std::cout << "\nNumber of connected components starting from vertex " << i << ": " << components << "\n\n";
+ }
+ }
+
+ return 0;
+}
|