Deadlock detection resource graph
Your task
Detect deadlock from a resource-allocation graph with single-instance resources. There are p processes (0..p-1) and r resources (0..r-1). held is a list of [resource, process] meaning that resource is currently allocated to that process. wanted is a list of [process, resource] meaning that process is blocked waiting to acquire that resource. With single-instance resources, deadlock exists iff the resource-allocation graph has a cycle. Build the wait-for graph (process A waits-for process B if A wants a resource that B holds) and return True if a cycle (deadlock) exists, else False. Assume each resource is held by at most one process.
Examples
rag_deadlock(p: int, r: int, held: list[list[int]], wanted: list[list[int]]) -> boolExample 1
Input
rag_deadlock(2, 2, [[0, 0], [1, 1]], [[0, 1], [1, 0]])Output
True