THINK FIRST·CODE LATER

← All labs

Wait-for graph: find cycles and choose victims

Problem

Resources have a single instance. Input lines (any order) until the end of input:

  • holds P R — process P holds resource R;
  • waits P R — process P waits for resource R;
  • cost P c — the cost of aborting P (default 0).
  1. Build the wait-for graph: for each waits P R (in input order) where R is held by another process Q, add the edge P → Q and print Edge P -> Q (R).
  2. Repeatedly find a cycle using DFS: start from the processes in alphabetical order, visit neighbours in alphabetical order, and report the first cycle found as Cycle: P1 -> P2 -> P3 -> P1 (starting from the node where the back edge points). Abort the process of the cycle with the smallest cost (ties: alphabetical) — print Abort P2 (cost 10) — and remove it with all its edges.
  3. When no cycle remains, print No deadlock if there was never a cycle, otherwise Deadlock resolved.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.