Exercise 1: DFS-Based Topological Sort of X-Y, X-Z, Y-W, Z-W — Possible Solution ==================================================================== GIVEN ------------------------------ Edges: X->Y, X->Z, Y->W, Z->W STEP 1: DFS FROM X, TRACKING FINISH ORDER ------------------------------ Visit X (gray). First neighbor: Y. Visit Y (gray). Its only neighbor is W. Visit W. Visit W (gray). W has no outgoing edges, so it finishes immediately - mark W black, push W onto the finish stack. Finish stack so far: [W] Back at Y: no more neighbors to explore. Y finishes - mark Y black, push Y. Finish stack so far: [W, Y] Back at X: next neighbor is Z. Visit Z. Visit Z (gray). Its only neighbor is W, which is already black (fully finished) - skip it, no need to revisit. Z finishes - mark Z black, push Z. Finish stack so far: [W, Y, Z] Back at X: no more neighbors. X finishes - mark X black, push X. Finish stack so far: [W, Y, Z, X] STEP 2: REVERSE THE FINISH STACK ------------------------------ Finish stack: [W, Y, Z, X] Reversed (answer): [X, Z, Y, W] RESULT ------------------------------ A valid topological order is: X, Z, Y, W Checking every edge points forward in this order: X->Y (X before Y, correct), X->Z (X before Z, correct), Y->W (Y before W, correct), Z->W (Z before W, correct). All four edges point forward, confirming this is a valid topological order. WHY THIS WORKS AS AN ANSWER ------------------------------ The DFS-based method from this chapter is applied exactly as specified - each vertex is pushed onto the finish stack only once every one of its outgoing edges has been fully explored, an already-finished (black) vertex is correctly skipped rather than re-explored, and the final answer is obtained by reversing the raw finish-order stack rather than reading it directly.