0

This question is very similar to Leetcode's Critical Connections in a Network. Given an undirected graph, we want to find all bridges. An edge in an undirected connected graph is a bridge iff removing it disconnects the graph.

Variant

Instead of finding all bridges, I want to maximise the number of edges to remove so that the graph remains connected.

Example 1

Input: n = 5, edges = [[1, 2], [1, 3], [3, 4], [1, 4], [4, 5]]

Output: 1

Firstly, I can remove [3,4], [1,3], or [1,4]. Next, after removing either of the 3 edges, the remaining edges are all bridges. Hence, the maximum number of edges to remove so that the graph remains connected is 1.

Example 2

Input: n = 6, edges = [[1, 2], [1, 3], [2, 3], [2, 4], [2, 5], [4, 6], [5, 6]]

Output: 2

hongsy
  • 1,498
  • 1
  • 27
  • 39
  • Does this answer your question? [Finding the minimum spanning tree of a graph using Kruskal's Algorithm](https://stackoverflow.com/questions/55210491/finding-the-minimum-spanning-tree-of-a-graph-using-kruskals-algorithm) – Prune Nov 07 '19 at 19:51

1 Answers1

3

Well this is easy, if we have E edges and N nodes in a connected graph we can remove E-N+1 edges so that graph remains connected.

How to do this?:

Just do DFS/BFS to find any spanning tree of the graph, since spanning tree is connected we can just remove all other edges.

Photon
  • 2,717
  • 1
  • 18
  • 22
  • This is the correct answer. Note that if you just want the output to be the *number* of edges to remove, then you don't need to construct the spanning tree; `E - N + 1` is the answer, always. – kaya3 Nov 07 '19 at 17:06