Bridges in a graph

Given an undirected graph having A nodes.
A matrix B of size M x 2 is given which represents the edges such that there is a edge between
B[i][0] and B[i][1].

You have to find all the bridges in the graph.

An edge in an undirected connected graph is a bridge if removing it disconnects the graph. For a disconnected undirected graph, the definition is similar,
a bridge is an edge removing which increases the number of disconnected components.

Return an array of array integers C which contains all the edges that are bridges.
You can return the bridges in any order but for nodes in a bridge order them in ascending order.

For example:

if there are 3 bridges in the graph having 7 nodes, (2, 1), (5, 3), (1, 7)
Then you can return any one of the following array of array integers:

[ [1, 2], [3, 5], [1, 7] ]

[ [1, 2], [1, 7], [3, 5] ]

[ [3, 5], [1, 2], [1, 7] ]

[ [3, 5], [1, 7], [1, 2] ]

[ [1, 7], [1, 2], [3, 5] ]

[ [1, 7], [3, 5], [1, 1] ]

Note:

  1. There are no self-loops in the graph.

  2. There are no multiple edges between two nodes.

  3. The graph may or may not be connected.

  4. Nodes are numbered from 1 to A.

  5. Your solution will run on multiple test cases. If you are using global variables make sure to clear them.



Input Format

The first argument given is an integer A.
The second argument given is the matrix B.

Output Format

Return the array of array integer C.

Constraints

1 <= A <= 100000
1<= B[i][0], B[i][1] <= A

For Example

Input 1:
    A = 5
    B = [   [1, 2] 
            [4, 1] 
            [2, 4] 
            [3, 4] 
            [5, 2] 
            [3, 1] ] 
Output 1:
    D = [   [2,5] ]

Input 2:
    A = 5
    B = [   [1, 2]
            [2, 3] 
            [3, 4] 
            [4, 5] ] 
Output 2:
    D =[ [1, 2]
         [2, 3]
         [3, 4]
         [4, 5] ]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Bridges in a graph on Interview Code Editor
Sign Up
to access hints and editorial solutions for Bridges in a graph

Discussion


Loading...
Click here to start solving coding interview questions