Back to problems

Check if adding edge creates cycle in digraph

Algorithm · Amazon · Medium

A system stores n items identified by integer labels from 0 to n - 1. Items may have one-way links between them: a link [u, v] means that item u points to item v. The existing links are guaranteed to form a directed acyclic graph, so following links can never return to an item that was already visited. You are given n, the list of existing links edges, and a proposed new link newEdge = [u, v]. Before saving this link, determine whether adding it would keep the graph free of…

Checking your access…