Skip to content

Commit 0109a17

Browse files
authored
Create Bruteforce Approach.cpp
1 parent 6cf9b51 commit 0109a17

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public:
3+
// Helper function to perform DFS
4+
void dfs(int node, vector<int>& visited, vector<vector<int>>& adjacencyList) {
5+
visited[node] = true;
6+
for (auto neighbor : adjacencyList[node]) {
7+
if (!visited[neighbor]) {
8+
dfs(neighbor, visited, adjacencyList);
9+
}
10+
}
11+
}
12+
13+
// Function to check if (c, d) is a bridge
14+
int isBridge(int V, vector<int> adj[], int c, int d) {
15+
vector<vector<int>> adjacencyList(V);
16+
17+
// Build adjacency list from adj[]
18+
for (int i = 0; i < V; i++) {
19+
for (auto neighbor : adj[i]) {
20+
adjacencyList[i].push_back(neighbor);
21+
}
22+
}
23+
24+
// Temporarily remove edge (c, d)
25+
adjacencyList[c].erase(remove(adjacencyList[c].begin(), adjacencyList[c].end(), d), adjacencyList[c].end());
26+
adjacencyList[d].erase(remove(adjacencyList[d].begin(), adjacencyList[d].end(), c), adjacencyList[d].end());
27+
28+
// Perform DFS from node `c`
29+
vector<int> visited(V, false);
30+
dfs(c, visited, adjacencyList);
31+
32+
// Check if node `d` is reachable from `c`
33+
int isBridge = (visited[d] == false) ? 1 : 0;
34+
35+
// Restore edge (c, d)
36+
adjacencyList[c].push_back(d);
37+
adjacencyList[d].push_back(c);
38+
39+
return isBridge;
40+
}
41+
};

0 commit comments

Comments
 (0)