# Program to Find Out the Edges that Disconnect the Graph in Python

PythonServer Side ProgrammingProgramming

Suppose we have been provided with an undirected graph that has been represented as an adjacency list, where graph[i] represents node i's neighbor nodes. We have to find the number of edges that satisfies the following condition.

If the edge is removed, the graph becomes disconnected.

So, if the input is like graph = [
[0, 2],
[0, 4],
[1, 2, 3],
[0, 3, 4],
,
,

],

then the output will be 1.

To solve this, we will follow these steps −

• Define a function dfs(). This will take curr, pre, d

• ans := infinity

• dep[curr] := d

• for each adj in graph[curr], do

• if pre is same as adj, then

• continue the next iteration without performing the other steps

• if dep[adj] is not same as −1, then

• ans := minimum of ans, dep[adj]

• otherwise,

• ans := minimum of ans, dfs(adj, curr, d + 1)

• if d > 0 and d <= ans, then

• re := re + 1

• return ans

• Now, from the main function call the function dfs().

• dep := a list of the size of the graph initialized with −1.

• re := 0

• dfs(0, −1, 0)

• return re

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, graph):
dep = [−1] * len(graph)
INF = int(1e9)
self.re = 0
def dfs(curr, pre, d):
ans = INF
dep[curr] = d
continue
else:
ans = min(ans, dfs(adj, curr, d + 1))
if d > 0 and d <= ans:
self.re += 1
return ans
dfs(0, −1, 0)
return self.re
ob = Solution()
print(ob.solve(graph = [
[0, 2],
[0, 4],
[1, 2, 3],
[0, 3, 4],
,
,

]))

## Input

graph = [
[0, 2],
[0, 4],
[1, 2, 3],
[0, 3, 4],
,
,

]

## Output

1