Add Edges to Make Degrees of All Nodes Even - Complete Solution Guide
Add Edges to Make Degrees of All Nodes Even is LeetCode problem 2508, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
There is an undirected graph consisting of n nodes numbered from 1 to n . You are given the integer n and a 2D array edges where edges[i] = [a i , b i ] indicates that there is an edge between nodes a i and b i . The graph can be disconnected. You can add at most two additional edges (possibly none) to this graph so that there are no repeated edges and no self-loops. Return true if it is possible to make the degree of each node in the graph even, otherwise return false . The degree of a node is
Detailed Explanation
The problem asks us to determine if it's possible to make the degree (number of connected edges) of every node in an undirected graph even by adding at most two new edges. The graph can be disconnected initially. We are given the number of nodes 'n' and a list of existing edges 'edges'. We need to return 'true' if it's possible to make all node degrees even by adding 0, 1, or 2 edges, and 'false' otherwise. Constraints include the number of nodes being between 3 and 10^5, and the number of edges being between 2 and 10^5. No self-loops or repeated edges are allowed.
Solution Approach
The solution first constructs an adjacency list representation of the graph and calculates the degree of each node. Then, it identifies the nodes with odd degrees. Based on the number of odd-degree nodes (0, 2, or 4), it checks different conditions to see if adding at most two edges can make all degrees even. The approach leverages the properties of graph degrees and efficiently explores possible edge additions.
Step-by-Step Algorithm
- Step 1: Create an adjacency list (adj) and an array (degrees) to store the degree of each node.
- Step 2: Populate the adjacency list and degree array based on the given 'edges'. For each edge (u, v), add 'v' to the adjacency list of 'u' and 'u' to the adjacency list of 'v'. Increment the degrees of both 'u' and 'v'.
- Step 3: Identify all nodes with odd degrees and store them in a list called 'odd_nodes'.
- Step 4: Check the number of odd-degree nodes (k = len(odd_nodes)).
- Step 5: If k == 0, return true (all degrees are already even).
- Step 6: If k == 2, let the odd-degree nodes be 'u' and 'v'. If there isn't an edge between 'u' and 'v', add an edge between them and return true. Otherwise, check if there is a third node 'w' such that 'w' is not connected to 'u' and 'w' is not connected to 'v'. If such a 'w' exists, add edges (u, w) and (v, w) and return true. If no such w exists, then return false.
- Step 7: If k == 4, let the odd-degree nodes be a, b, c, and d. Check if any of the following combinations allow all degrees to become even: (a, b) and (c, d), (a, c) and (b, d), (a, d) and (b, c). For each combination, verify that the edges to be added don't already exist. If any of these combinations work, return true. Otherwise, return false.
- Step 8: If k > 4, return false (it's impossible to make all degrees even with at most 2 edges).
Key Insights
- Insight 1: The sum of degrees in any graph is always even. Therefore, the number of nodes with odd degrees must also be even.
- Insight 2: If there are 0 nodes with odd degrees, we're already done (return true).
- Insight 3: If there are 2 nodes with odd degrees, we can simply add an edge between them if one doesn't already exist. If it does exist, we need to find a node that isn't connected to either of these odd degree nodes and connect them via two new edges.
- Insight 4: If there are 4 nodes with odd degrees, we can try all possible pairings of these nodes into two edges to see if any combination works.
- Insight 5: If there are more than 4 nodes with odd degrees, it's impossible to make all node degrees even by adding at most 2 edges (return false).
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Hash Table, Graph.
Companies
Asked at: Hudson River Trading.