02. Max DAG Edges
β GFG solution to the Max DAG Edges problem: find maximum number of edges that can be added to a Directed Acyclic Graph (DAG) without forming cycles. π
The problem can be found at the following link: π Question Link
π§© Problem Description
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
Example 1
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.Example 2
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 \le V \le 10^3$
$0 \le E \le \frac{V \times (V-1)}{2}$
$0 \le \text{edges}[i][0], \text{edges}[i][1] < V$
β
My Approach
The solution uses a mathematical formula based on graph theory properties to determine the maximum possible edges in a DAG:
Complete Graph Formula
Understanding Maximum Edges:
In a complete directed graph with
Vvertices, the maximum number of edges possible is when every vertex connects to every other vertex.For a directed graph, this is
V Γ (V - 1) / 2edges (choosing any 2 vertices from V vertices).
Current Edge Count:
The graph already has
Eedges (given asedges.size()).
Available Slots:
Maximum additional edges = Total possible edges - Current edges
Formula:
V Γ (V - 1) / 2 - E
Why This Works:
A DAG can have at most as many edges as a complete directed graph without creating cycles.
The directed acyclic nature allows for a topological ordering where all edges point "forward."
Subtracting existing edges from the maximum capacity gives us the remaining capacity.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(1), as we perform only a constant number of arithmetic operations (multiplication, division, and subtraction) regardless of the number of vertices or edges.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for storing the result, without any extra data structures.
π§βπ» Code (C++)
class Solution {
public:
int maxEdgesToAdd(int V, vector<vector<int>>& edges) {
return V * (V - 1) / 2 - edges.size();
}
};β Code (Java)
class Solution {
public int maxEdgesToAdd(int V, int[][] edges) {
return V * (V - 1) / 2 - edges.length;
}
}π Code (Python)
class Solution:
def maxEdgesToAdd(self, V, edges):
return V * (V - 1) // 2 - len(edges)π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated