20-CS-122-001 Computer Science II Spring 2012
Test For Cycles

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev     Next

Instructions: Left click on the Partial Solution button to show a possible partial solution. Lines are shown red with their numbers. There are two connected components in this example but how does the computer distinguish between them? In other words, if you give the computer a pointer to a line that is not in the partial solution, how can the computer quickly determine whether or not the endpoints of the line are in different connected components? Left click on the Connected Components button for an answer: associate a number with every object such that all objects have the same number if and only if they are in the same connected component. In this example, the objects of one component have the number 3 and the objects of the other component have the number 5. To see how this helps, left click the Add Line, Update button once. A possible new line is shown in green connecting an object of one connected component to an object of the other connected component. The computer finds the 3 and 5 associated with the endpoints of the new line and determines that 3 and 5 are different so it is safe to add the new line to the partial solution. Left click on the Add Line, Update button again to see the line added to the partial solution (it becomes red). Left click the button a third time to see what happens if a new line connecting two already connected objects is checked.

Demo of the algorithm with this test is here.