Cs50 Tideman Solution [ VALIDATED · FULL REVIEW ]

He drew on the whiteboard:

Maya ran check50 . Green smiles across the board. She leaned back.

"You’re not just looking for a loop," Kai said. "You’re looking for a chain . Before you lock a new edge from winner to loser , ask yourself: is there any path from the loser back to the winner using the edges already locked? If yes, this new edge would complete the cycle. Skip it." Cs50 Tideman Solution

Maya pointed. "I wrote a recursive function creates_cycle(winner, loser) . It checks if the loser has any locked edges pointing to another candidate. Then it checks if that candidate points back to the original winner. If yes, it’s a cycle."

In a directed graph, adding an edge from A → B creates a cycle if and only if B can already reach A. He drew on the whiteboard: Maya ran check50

She stared at her lock_pairs function. It was midnight. Her screen showed the dreaded red “:(” from check50 .

Maya was the new programmer tasked with tabulating the votes. She had the first part down: counting each ballot to build a 2D array of preferences . It told her that Alice beat Bob (5 votes to 2), Bob beat Charlie (4 to 3), and Charlie beat Alice (3 to 2). A perfect, frustrating cycle. "You’re not just looking for a loop," Kai said

Kai nodded slowly. "You are looking for a direct path back to the winner. But what if the path is three steps? Four? Your recursion only goes two levels deep."

Discover more from Home

Subscribe now to keep reading and get access to the full archive.

Continue reading