Taking on a new challenge: solving GeeksforGeeks POTD daily and sharing my solutions! π»π₯
The goal: sharpen problem-solving skills, level up coding, and learn something new every day. Follow my journey! π
100DaysOfCode #CodingChallenge #ProblemSolving #GeeksforGeeks #DeveloperJourney
Problem:
https://www.geeksforgeeks.org/problems/max-dag-edges/1
Max DAG Edges
Difficulty: Medium Accuracy: 75.07%
Given a directed acyclic graph (DAG) with V vertices numbered from 0 to V-1 and E edges, represented as a 2D array edges[][], where each entry edges[i] = [u, v] denotes a directed edge from vertex u to vertex v, find the maximum number of additional edges that can be added to the graph without forming any cycles.
Note: The resulting graph must remain a DAG, meaning that adding any further edge would not create a cycle.
Examples:
Input: V = 3, E = 2, edges[][] = [[0, 1], [1, 2]]
Output: 1
Explanation: The given DAG allows one more edge, 0 -> 2, which keeps the structure acyclic. Adding anything else would create a cycle.
Input: V = 4, E = 4, edges[][] = [[0, 1], [0, 2], [1, 2], [2, 3]]
Output: 2
Explanation: Two additional edges (0 -> 3, 1 -> 3) can be added without forming cycles.
Constraints:
1 β€ V β€ 103
0 β€ E β€ (V*(V-1))/2
0 β€ edges[i][0], edges[i][1] < V
Solution:
class Solution:
def maxEdgesToAdd(self, V, edges):
E = len(edges)
max_total_edges = (V * (V - 1)) // 2
additional_edges = max_total_edges - E
return additional_edges
Top comments (0)