Problem · Graph
Validate a Directed Edge Addition
Learn this problemProblem statement
You are given a directed graph as a list of source-destination pairs and one proposed directed edge.
Return false if the exact proposed edge already exists or if adding it would create a directed cycle. Otherwise, return true.
Function
isValidEdgeAddition(edges: int[][], newEdge: int[]) → booleanExamples
Example 1
edges = [[1,2],[2,3],[3,4]]newEdge = [4,1]return = falseAdding 4 -> 1 creates the cycle 1 -> 2 -> 3 -> 4 -> 1.
Example 2
edges = [[1,2],[2,3],[3,4]]newEdge = [1,2]return = falseThe exact edge 1 -> 2 already exists.
Example 3
edges = [[1,2],[2,3]]newEdge = [1,3]return = trueThe edge is new and does not create a directed cycle.