|
| 1 | +/* |
| 2 | +Implementation of Bridge edge in Graph problem |
| 3 | +
|
| 4 | +Problem statement: |
| 5 | +Given an undirected graph of V vertices and E edges and another edge (c-d), the task is to |
| 6 | +find if the given edge is a bridge in graph, i.e., removing the edge disconnects the graph. |
| 7 | +
|
| 8 | +Link to the problem: https://practice.geeksforgeeks.org/problems/bridge-edge-in-graph/1 |
| 9 | +*/ |
| 10 | + |
| 11 | +#include<bits/stdc++.h> |
| 12 | +using namespace std; |
| 13 | + |
| 14 | + |
| 15 | +class Solution |
| 16 | +{ |
| 17 | + public: |
| 18 | + |
| 19 | + void dfs( vector<int> adj[],vector<int>&v,int i) |
| 20 | + { |
| 21 | + //marking visited |
| 22 | + v[i]=1; |
| 23 | + |
| 24 | + //traversing adjacent node |
| 25 | + for(auto x:adj[i]) |
| 26 | + { |
| 27 | + if(!v[x]) |
| 28 | + dfs(adj,v,x); |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + bool connected(vector<int>adj[],int n,int c,int d) |
| 35 | + { |
| 36 | + vector<int>v(n,0); |
| 37 | + |
| 38 | + // dfs from edge c |
| 39 | + dfs(adj,v,c); |
| 40 | + |
| 41 | + //if edge d is not visited means not connected |
| 42 | + if(v[d]==0) |
| 43 | + return false; |
| 44 | + |
| 45 | + return true; |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + //Function to find if the given edge is a bridge in graph. |
| 50 | + int isBridge(int n, vector<int>adj[], int c, int d) |
| 51 | + { |
| 52 | + //if graph is not connected |
| 53 | + if(!connected(adj,n,c,d)) |
| 54 | + return 0; |
| 55 | + |
| 56 | + else |
| 57 | + { |
| 58 | + //removing edge c and d |
| 59 | + adj[c].erase(remove(adj[c].begin(), adj[c].end(), d), adj[c].end()); |
| 60 | + adj[d].erase(remove(adj[d].begin(), adj[d].end(), c), adj[d].end()); |
| 61 | + |
| 62 | + //if connected means no bridge |
| 63 | + if(connected(adj,n,c,d)) |
| 64 | + return 0; |
| 65 | + else |
| 66 | + return 1; |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +// Driver Code Starts |
| 73 | +int main() |
| 74 | +{ |
| 75 | + int t; |
| 76 | + cin >> t; |
| 77 | + while (t--) { |
| 78 | + int V, E; |
| 79 | + cin >> V >> E; |
| 80 | + vector<int> adj[V]; |
| 81 | + int i=0; |
| 82 | + while (i++<E) { |
| 83 | + int u, v; |
| 84 | + cin >> u >> v; |
| 85 | + adj[u].push_back (v); |
| 86 | + adj[v].push_back (u); |
| 87 | + } |
| 88 | + |
| 89 | + int c,d; |
| 90 | + cin>>c>>d; |
| 91 | + |
| 92 | + Solution obj; |
| 93 | + cout << obj.isBridge(V, adj, c, d) << "\n"; |
| 94 | + } |
| 95 | + |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | +/* |
| 101 | +Time Complexity : O(V+E) |
| 102 | +Space Complexity : O(V) |
| 103 | +
|
| 104 | +Input: |
| 105 | +t=1 |
| 106 | +V=4 E=3 |
| 107 | +0 1 |
| 108 | +1 2 |
| 109 | +2 3 |
| 110 | +c=1 d=2 |
| 111 | +
|
| 112 | +Output: 1 |
| 113 | +
|
| 114 | +Explanation: |
| 115 | +From the graph, we can clearly see that |
| 116 | +removing the edge 1-2 will result in |
| 117 | +disconnection of the graph. So, it is |
| 118 | +a bridge Edge and thus the Output 1. |
| 119 | +
|
| 120 | +*/ |
0 commit comments