8. Bridge Edge in a Graph
The problem can be found at the following link: Question Link
Problem Description
Given an undirected graph with V vertices and E edges, along with a specific edge connecting vertices c and d, the task is to determine whether this edge is a bridge. An edge (cβd) is considered a bridge if removing it increases the number of connected components in the graph, meaning that vertices c and d were part of the same connected component before, but become disconnected after its removal.
Note: Sorry for uploading late, my Final Sem exam is going on.
Examples
Example 1:
Input:
V = 5, E = 5
Edges = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {2, 4}}
c = 1, d = 2
Output:
True
Explanation:
Removing the edge between nodes 1 and 2 disconnects the graph, indicating that it is a bridge.
Example 2:
Input:
V = 5, E = 5
Edges = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {2, 4}}
c = 2, d = 4
Output:
False
Explanation:
Removing the edge between nodes 2 and 4 does not affect the connectivity of the graph, indicating that it is not a bridge.
Constraints:
$1 \leq V, E \leq 10^5$
$0 \leq c, d \leq V-1$
My Approach
To determine if the edge (c, d) is a bridge, we can use the following approach:
Graph Representation: Represent the graph using an adjacency list for efficient traversal.
Depth First Search (DFS): Perform a DFS traversal to check the connectivity between nodes c and d after temporarily removing the edge (c, d).
Connectivity Check:
Temporarily remove the edge (c, d) from the graph.
Use DFS starting from node c to see if node d is still reachable.
If node d is not reachable from node c after removing the edge, then the edge (c, d) is a bridge.
Algorithm Steps:
Build the Adjacency List: Construct the adjacency list from the given list of edges.
Remove the Edge (c, d): Temporarily remove the edge by not including it in the adjacency list.
DFS Traversal:
Initialize a visited array to keep track of visited nodes.
Start DFS traversal from node c.
Mark all reachable nodes from c as visited.
Check Reachability of d:
After the DFS traversal, check if node d has been visited.
If node d is not visited, it means removing the edge (c, d) disconnects node d from node c, indicating that the edge is a bridge.
Restore the Edge (c, d): After the check, restore the edge to its original state in the adjacency list.
Time and Auxiliary Space Complexity
Expected Time Complexity: O(V + E), as we perform a DFS traversal which visits each vertex and edge once.
Expected Auxiliary Space Complexity: O(V), due to the space required for the visited array and the recursion stack during DFS.
Code (C++)
class Solution {
public:
void dfs(vector<vector<int>> &adj, int u, vector<bool> &vis) {
vis[u] = true;
for (int v : adj[u])
if (!vis[v]) dfs(adj, v, vis);
}
bool isBridge(int V, vector<vector<int>> &edges, int c, int d) {
vector<vector<int>> adj(V);
for (auto &e : edges)
if (!(e[0] == c && e[1] == d || e[0] == d && e[1] == c))
adj[e[0]].push_back(e[1]), adj[e[1]].push_back(e[0]);
vector<bool> vis(V);
dfs(adj, c, vis);
return !vis[d];
}
};
Code (Java)
class Solution {
void dfs(List<List<Integer>> g, boolean[] vis, int u) {
vis[u] = true;
for (int v : g.get(u)) if (!vis[v]) dfs(g, vis, v);
}
public boolean isBridge(int V, int[][] edges, int c, int d) {
List<List<Integer>> g = new ArrayList<>();
for (int i = 0; i < V; i++) g.add(new ArrayList<>());
for (int[] e : edges)
if (!(e[0] == c && e[1] == d || e[0] == d && e[1] == c)) {
g.get(e[0]).add(e[1]);
g.get(e[1]).add(e[0]);
}
boolean[] vis = new boolean[V];
dfs(g, vis, c);
return !vis[d];
}
}
Code (Python)
class Solution:
def dfs(self, g, vis, u):
vis[u] = 1
for v in g[u]:
if not vis[v]: self.dfs(g, vis, v)
def isBridge(self, V, edges, c, d):
g = [[] for _ in range(V)]
for u, v in edges:
if (u, v) != (c, d) and (v, u) != (c, d):
g[u].append(v)
g[v].append(u)
vis = [0] * V
self.dfs(g, vis, c)
return not vis[d]
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Letβs make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated