Similar Problems

Similar Problems not available

Add Edges To Make Degrees Of All Nodes Even - Leetcode Solution

Companies:

LeetCode:  Add Edges To Make Degrees Of All Nodes Even Leetcode Solution

Difficulty: Hard

Topics: hash-table graph  

Problem Statement:

You are given a graph with n nodes and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph.

Return the minimum number of edges you need to add to the graph to ensure that the degree of all nodes is even.

Degree of a node is the number of edges connected to it.

Approach:

  • Build a graph using the given edges array.
  • Traverse the graph and calculate the degree of each node.
  • Iterate through all the nodes and check if its degree is even. If yes, then move to the next node. Else, add an edge from the current node to a node with an odd degree.
  • Repeat the previous step until all the nodes have an even degree.
  • Return the count of edges added.

Implementation:

Step 1: Building a Graph

Firstly, we will build a graph using the given edges in the array.

We can use adjacency list to represent the graph.

For example:

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

We can represent this graph using the following adjacency list:

graph = { 0: {1, 2}, 1: {0}, 2: {0, 4, 5}, 3: {4}, 4: {3, 2}, 5: {2} }

Step 2: Calculating the Degree of each Node

Next, we will calculate the degree of each node.

We can iterate through the graph and count the number of edges connected to each node.

For example:

degree = {0: 2, 1: 1, 2: 3, 3: 1, 4: 2, 5: 1}

Step 3: Adding Edges to Make All Nodes Even

We will repeat the following steps until all nodes have an even degree:

  • Iterate through all the nodes.
  • If its degree is even, then move to the next node.
  • If its degree is odd, then add an edge from the current node to a node with an odd degree.
  • Update the degree of both nodes.
  • Increment the count of added edges.

For example:

degree = {0: 2, 1: 1, 2: 3, 3: 1, 4: 2, 5: 1}

count = 0 for node in degree: if degree[node] % 2 == 0: continue for neighbor in graph: if neighbor == node or degree[neighbor] % 2 == 0: continue graph[node].add(neighbor) graph[neighbor].add(node) degree[node] += 1 degree[neighbor] += 1 count += 1

print(count)

Output: 1

We added an edge between node 2 and node 3 to make the degree of all nodes even.

Final Code:

Here is the final code for this problem:

def addEdgesToMakeDegreesOfAllNodesEven(n, edges): graph = {i: set() for i in range(n)} for a, b in edges: graph[a].add(b) graph[b].add(a)

degree = {node: len(graph[node]) for node in graph}

count = 0
for node in degree:
    if degree[node] % 2 == 0:
        continue
    for neighbor in graph:
        if neighbor == node or degree[neighbor] % 2 == 0:
            continue
        graph[node].add(neighbor)
        graph[neighbor].add(node)
        degree[node] += 1
        degree[neighbor] += 1
        count += 1

return count

Add Edges To Make Degrees Of All Nodes Even Solution Code

1