- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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], [4], [3], [2] ],

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 −

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 for adj in graph[curr]: if pre == adj: continue if dep[adj] != −1: ans = min(ans, dep[adj]) 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], [4], [3], [2] ]))

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

1

- Related Questions & Answers
- Program to find out the critical and pseudo-critical edges in a graph in Python
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- Program to Find Out the Minimum Cost Possible from Weighted Graph in Python
- Program to find out if the graph is traversable by everybody in Python
- C++ Program to Find All Forward Edges in a Graph
- Program to find out the minimum size of the largest clique in a graph (Python)
- Program to find the diameter, cycles and edges of a Wheel Graph in C++
- C++ Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
- Program to find out the buildings that have a better view in Python
- Program to Find Out the Minimal Submatrices in Python
- Python Program to find out the size of the bus that can contain all the friends in the group
- Program to find out the sum of minimum cost within a graph among all vertices in Python
- C++ Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
- Program to find out the minimal cost so that the citizens have access to a market in Python
- Program to find out special types of subgraphs in a given graph in Python

Advertisements