Minimum Operations to Connect a Graph

You are given a graph with 'N' vertices labeled from 1 to 'N' and 'M' edges. In one operation, you can shift an edge from being between two directly connected vertices to between a pair of disconnected vertices, making them directly connected. The task is to determine the minimum number of such operations required to make the entire graph connected. If the graph cannot be connected, return -1.

Example:

Input:
N = 4, M = 3
Edges = [(1,2), (2,3), (1,3)]
Output:
1
Explanation:

You can shift the edge from (1,3) to (1,4) to connect the graph. There are multiple ways to make the graph connected. In this scenario, only one operation is needed.

Input:

The first line contains a single integer 'T', the number of test cases.
The first line for each test case contains two integers 'N' and 'M', representing the number of vertices and edges.
Each of the next 'M' lines contains two integers denoting the vertices connected by an edge.

Output:

For each test case, print a single integer indicating the minimum number of operations required to connect the graph. If the graph cannot be connected, print -1.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10000
  • 1 <= M <= 10000
  • 1 <= U[i], V[i] <= N

Where 'T' is the number of test cases, 'N' is the number of vertices, 'M' is the number of edges, and 'U[i]' and 'V[i]' are vertices of the i-th edge.

Time Limit: 1 sec.

Note:

You do not need to handle any input or output processes, just implement the given function.

AnswerBot
4mo

Determine the minimum number of operations to connect a graph by shifting edges between disconnected vertices.

  • Use a disjoint set union (DSU) data structure to keep track of connected components.

  • Count ...read more

Help your peers!
Select
Add answer anonymously...

Uber Software Developer Intern interview questions & answers

A Software Developer Intern was asked 9mo agoQ. Changes on graph structure
A Software Developer Intern was asked Q. Find Indices for Local Minima and Maxima Given an integer array arr of size N, y...read more
A Software Developer Intern was asked Q. Return Subsets Sum to K Problem Statement Given an integer array 'ARR' of size '...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Find Indices for Local Minima and Maxima Given an integer array arr of size N, y...read more
A Software Developer Intern was asked Q2. Return Subsets Sum to K Problem Statement Given an integer array 'ARR' of size '...read more
A Software Developer Intern was asked Q3. Game of Stones Problem Statement Two players, 'Ale' and 'Bob', are playing a gam...read more
Uber Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits