Skip to content

Commit e81fc16

Browse files
authored
Merge branch 'main' into main
2 parents 8d84489 + 7d29522 commit e81fc16

File tree

4 files changed

+265
-6
lines changed

4 files changed

+265
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ In order to achieve greater coverage and encourage more people to contribute to
130130
</a>
131131
</td>
132132
<td> <!-- C++ -->
133-
<a href="./CONTRIBUTING.md">
134-
<img align="center" height="25" src="./logos/github.svg" />
133+
<a href="./src/cpp/FloydWarshall.cpp">
134+
<img align="center" height="25" src="./logos/cplusplus.svg" />
135135
</a>
136136
</td>
137137
<td> <!-- Java -->
@@ -246,8 +246,8 @@ In order to achieve greater coverage and encourage more people to contribute to
246246
</a>
247247
</td>
248248
<td> <!-- C++ -->
249-
<a href="./CONTRIBUTING.md">
250-
<img align="center" height="25" src="./logos/github.svg" />
249+
<a href="./src/cpp/GraphSearch.cpp">
250+
<img align="center" height="25" src="./logos/cplusplus.svg" />
251251
</a>
252252
</td>
253253
<td> <!-- Java -->
@@ -1174,8 +1174,8 @@ In order to achieve greater coverage and encourage more people to contribute to
11741174
</a>
11751175
</td>
11761176
<td> <!-- C++ -->
1177-
<a href="./CONTRIBUTING.md">
1178-
<img align="center" height="25" src="./logos/github.svg" />
1177+
<a href="./src/cpp/ConnectedComponents.cpp">
1178+
<img align="center" height="25" src="./logos/cplusplus.svg" />
11791179
</a>
11801180
</td>
11811181
<td> <!-- Java -->

src/cpp/ConnectedComponents.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
#define VERTICES 6
5+
#define INF -1
6+
7+
std::vector<bool> visited(VERTICES, false); // Array to track visited vertices
8+
int components = 0;
9+
10+
// Adjacency matrix representing the graph
11+
int matrix[VERTICES][VERTICES] = {{0, INF, 1, INF, INF, INF},
12+
{INF, 0, INF, 1, 1, INF},
13+
{1, INF, 0, INF, INF, INF},
14+
{INF, 1, INF, 0, 1, 1},
15+
{INF, 1, INF, 1, 0, 1},
16+
{INF, INF, INF, 1, 1, 0}};
17+
18+
// Recursive method to find connected components using adjacency matrix
19+
void findConnectedComponents(int current)
20+
{
21+
for (int i = 0; i < VERTICES; i++)
22+
{
23+
if (!visited[i] && matrix[current][i] == 1)
24+
{
25+
visited[i] = true;
26+
components++;
27+
std::cout << "(" << i << ")-";
28+
findConnectedComponents(i);
29+
}
30+
}
31+
}
32+
33+
int main()
34+
{
35+
// Initialize all vertices as unvisited
36+
for (int i = 0; i < VERTICES; i++)
37+
visited[i] = false;
38+
39+
// For each vertex, if it is unvisited, start a DFS and count components
40+
for (int i = 0; i < VERTICES; i++)
41+
{
42+
if (!visited[i])
43+
{
44+
components = 0;
45+
visited[i] = true;
46+
std::cout << "Starting at vertex (" << i << ")-";
47+
findConnectedComponents(i);
48+
std::cout << "\nNumber of connected components starting from vertex " << i << ": " << components << "\n\n";
49+
}
50+
}
51+
52+
return 0;
53+
}

src/cpp/FloydWarshall.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
// Function to display the matrix
6+
void showMatrix(const vector<vector<long>> &matrix, int numVertices)
7+
{
8+
for (int i = 0; i < numVertices; i++)
9+
{
10+
for (int j = 0; j < numVertices; j++)
11+
{
12+
if (matrix[i][j] < 10) // For better alignment
13+
cout << " ";
14+
cout << matrix[i][j] << " ";
15+
}
16+
cout << endl;
17+
}
18+
cout << endl;
19+
}
20+
21+
// Floyd-Warshall algorithm
22+
void floydWarshall(vector<vector<long>> &matrix, int n)
23+
{
24+
for (int k = 0; k < n; k++) // Intermediary vertex
25+
{
26+
for (int i = 0; i < n; i++) // Origin vertex
27+
{
28+
for (int j = 0; j < n; j++) // Destination vertex
29+
{
30+
if (matrix[i][k] != LONG_MAX && // i -> k exists
31+
matrix[k][j] != LONG_MAX && // k -> j exists
32+
matrix[i][j] > matrix[i][k] + matrix[k][j]) // i -> j is shorter via k
33+
{
34+
matrix[i][j] = matrix[i][k] + matrix[k][j]; // Update i -> j
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
int main()
42+
{
43+
int numVertices = 5;
44+
45+
// Initialize matrix with given values
46+
vector<vector<long>> matrix = {
47+
{0, 2, 10, 5, 7},
48+
{2, 0, 3, 3, 1},
49+
{10, 3, 0, 1, 2},
50+
{5, 3, 1, 0, LONG_MAX},
51+
{7, 1, 2, 2, 0}};
52+
53+
// Display the original matrix
54+
cout << "Original matrix:" << endl;
55+
showMatrix(matrix, numVertices);
56+
57+
// Apply Floyd-Warshall algorithm
58+
floydWarshall(matrix, numVertices);
59+
60+
// Display the updated matrix
61+
cout << "Updated matrix:" << endl;
62+
showMatrix(matrix, numVertices);
63+
64+
// Show all shortest paths in 3 columns: source, destination, shortest distance
65+
cout << "Source\tDestination\tShortest Distance" << endl;
66+
for (int i = 0; i < numVertices; i++)
67+
for (int j = 0; j < numVertices; j++)
68+
cout << i << "\t" << j << "\t\t" << matrix[i][j] << endl;
69+
70+
return 0;
71+
}

src/cpp/GraphSearch.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
#define MAX_VERTICES 6 // Maximum number of vertices in the graph
6+
7+
// Structure that defines each Vertex of the Graph
8+
struct Vertex
9+
{
10+
char id;
11+
std::vector<Vertex *> neighbors; // List of neighbors
12+
bool visited;
13+
14+
Vertex(char id) : id(id), visited(false) {}
15+
};
16+
17+
// Creates a vertex and returns it
18+
Vertex *createVertex(char id)
19+
{
20+
return new Vertex(id);
21+
}
22+
23+
// Links two vertices (makes them neighbors)
24+
void linkVertices(Vertex *v1, Vertex *v2)
25+
{
26+
v1->neighbors.push_back(v2); // Add v2 as a neighbor of v1
27+
v2->neighbors.push_back(v1); // Add v1 as a neighbor of v2
28+
}
29+
30+
/*
31+
* Depth First Search (DFS)
32+
* Recursively visits neighbors of the starting vertex
33+
*/
34+
int depthFirstSearch(Vertex *start, Vertex *destination, int steps)
35+
{
36+
start->visited = true; // Mark the current vertex as visited
37+
if (start == destination)
38+
return steps; // If found, return the distance
39+
40+
for (Vertex *neighbor : start->neighbors)
41+
{ // Visit all neighbors
42+
if (!neighbor->visited)
43+
{ // If neighbor hasn't been visited
44+
int result = depthFirstSearch(neighbor, destination, steps + 1);
45+
if (result != -1)
46+
return result; // If destination found, return result
47+
}
48+
}
49+
return -1; // Destination not found
50+
}
51+
52+
/*
53+
* Breadth First Search (BFS)
54+
* Uses a queue to traverse level by level
55+
*/
56+
int breadthFirstSearch(Vertex *start, Vertex *destination)
57+
{
58+
std::queue<Vertex *> q;
59+
q.push(start); // Enqueue starting vertex
60+
start->visited = true;
61+
62+
int steps = 0;
63+
64+
while (!q.empty())
65+
{
66+
int qSize = q.size(); // Current queue size (level size)
67+
68+
// Process all vertices at the current level
69+
for (int i = 0; i < qSize; i++)
70+
{
71+
Vertex *current = q.front();
72+
q.pop();
73+
74+
if (current == destination)
75+
return steps; // If destination found, return steps
76+
77+
// Enqueue all unvisited neighbors
78+
for (Vertex *neighbor : current->neighbors)
79+
{
80+
if (!neighbor->visited)
81+
{
82+
neighbor->visited = true;
83+
q.push(neighbor);
84+
}
85+
}
86+
}
87+
steps++; // Increment the level
88+
}
89+
return -1; // Destination not found
90+
}
91+
92+
int main()
93+
{
94+
// Create vertices
95+
Vertex *A = createVertex('A');
96+
Vertex *B = createVertex('B');
97+
Vertex *C = createVertex('C');
98+
Vertex *D = createVertex('D');
99+
Vertex *E = createVertex('E');
100+
Vertex *F = createVertex('F');
101+
102+
// Link vertices as per the graph structure
103+
linkVertices(A, B);
104+
linkVertices(A, C);
105+
linkVertices(B, D);
106+
linkVertices(C, D);
107+
linkVertices(B, E);
108+
linkVertices(D, E);
109+
linkVertices(E, F);
110+
linkVertices(D, F);
111+
112+
// Perform Depth First Search
113+
int result = depthFirstSearch(A, F, 0);
114+
if (result != -1)
115+
std::cout << "DFS - Found. Distance: " << result << std::endl;
116+
else
117+
std::cout << "DFS - Not Found." << std::endl;
118+
119+
// Reset visited status for all vertices
120+
A->visited = false;
121+
B->visited = false;
122+
C->visited = false;
123+
D->visited = false;
124+
E->visited = false;
125+
F->visited = false;
126+
127+
// Perform Breadth First Search
128+
result = breadthFirstSearch(A, F);
129+
if (result != -1)
130+
std::cout << "BFS - Found. Distance: " << result << std::endl;
131+
else
132+
std::cout << "BFS - Not Found." << std::endl;
133+
134+
return 0;
135+
}

0 commit comments

Comments
 (0)