Number of Operations to Make Network Connected

Given n vertices numbered 0 through n-1 and undirected edge occurrences, find the minimum operations to connect the network. One operation removes an existing…

Given n vertices numbered 0 through n-1 and undirected edge occurrences, find the minimum operations to connect the network. One operation removes an existing edge occurrence and adds it between any two vertices. Return -1 if impossible. Duplicate edges and self-loops are permitted; each occurrence is a separate available edge.

- 1 <= n <= 10000 - 1 <= edges.length <= 10000 - Each edge contains two integer endpoints in 0..n-1 - Duplicate occurrences and self-loops are permitted - Preset visualization uses at most 24 vertices and 48 edges; larger valid networks are execution-only

Example 1: input n = 4, edges = [[0,1],[0,2],[1,2]]; output 1. Move one triangle edge to connect isolated vertex 3.

Example 2: input n = 4, edges = [[0,1],[2,3]]; output -1. Two edges cannot connect four vertices; relocation cannot increase the edge budget.

Category: Graphs. Topic: graph. Difficulty: Hard. Patterns: graph-traversal.