Deadlock Detection
Deadlock Detection
Given a wait-for graph, detect if a deadlock exists.
Function
func DetectDeadlock(n int, edges [][2]int) bool
Behavior
- There are
nprocesses labeled0..n-1. - Each edge
(u, v)means processuis waiting forv. - Interpret edges as mandatory current waits on exclusive ownership, with no timeout, external release, or preemption. Under this model, a cycle means deadlock. Graphs for multiple interchangeable instances or optional alternatives need more state; this function only detects directed cycles.
- Return
trueiff the wait-for graph contains a cycle. - Search disconnected components; a self-loop counts as a cycle. An edge to a previously completed DFS vertex alone does not.
Notes
- Use any cycle detection algorithm (DFS with colors is sufficient).
n >= 0. Edges with either endpoint outside0..n-1are ignored; duplicate valid edges do not change the result. The empty graph returnsfalse.
Run tests to see results
No issues detected