-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCheckIfBridge.java
81 lines (70 loc) · 2.06 KB
/
CheckIfBridge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*https://practice.geeksforgeeks.org/problems/bridge-edge-in-graph/1#*/
class Solution
{
static boolean[] visited;
static int isBridge(int v, ArrayList<ArrayList<Integer>> adj,int c,int d)
{
visited = new boolean[v];
//run DFS and count the number of components
int components = 0;
for (int i = 0; i < v; ++i)
{
if (!visited[i])
{
++components;
visited[i] = true;
runDFS(adj,i);
}
}
//remove the given edge
adj.get(c).remove(new Integer(d));
adj.get(d).remove(new Integer(c));
//run DFS and count the number of components again
int newComponents = 0;
visited = new boolean[v];
for (int i = 0; i < v; ++i)
{
if (!visited[i])
{
++newComponents;
visited[i] = true;
runDFS(adj,i);
}
}
//return 0 if the components are equal otherwise 1
return newComponents-components == 0 ? 0 : 1;
}
static void runDFS(ArrayList<ArrayList<Integer>> adj, int node)
{
for (Integer adjacentNode : adj.get(node))
{
if (!visited[adjacentNode])
{
visited[adjacentNode] = true;
runDFS(adj,adjacentNode);
}
}
}
}
// User function Template for Java
class Solution
{
static boolean[] visited;
//Function to find if the given edge is a bridge in graph.
static int isBridge(int V, ArrayList<ArrayList<Integer>> adj,int c,int d)
{
// code here
visited = new boolean[V];
adj.get(c).remove(new Integer(d));
adj.get(d).remove(new Integer(c));
dfs(adj,c);
return visited[d] ? 0 : 1;
}
static void dfs(ArrayList<ArrayList<Integer>> graph, int src)
{
visited[src] = true;
for (int adjNode : graph.get(src))
if (!visited[adjNode])
dfs(graph,adjNode);
}
}