20-CS-122-001 |
Computer Science II |
Spring 2012 |
---|---|---|

Is The Algorithm Correct? |

Prev
Next

**Instructions:** Left click on the `Our Solution` button to
see a hypothesized solution returned by our algorithm, in green. We
suppose that our solution is not optimal. So, left click on the
`Optimal Solution` button to see an optimal solution in red.
Since our solution differs from the optimal there are some lines that
are colored red but not green (in optimal solution but not in solution
returned by our algorithm) and some colored green but not red (in
solution returned by our algorithm but not in optimal solution).
Locate the triangle containing the red line with the smallest number
that is not also a green line. In this example, the number of the
line is 89. That triangle must contain a green line that is not red
or else we do not even have a spanning network with the correct number
of lines. Since our algorithm always picks the lowest cost line
first, that green line must have lower cost than 89 (or else it would
have put 89 into the returned solution). Swap the red line with the
green line. The result of doing this is shown by left clicking the
`Contradiction` button. This provides a solution that has
lower cost than the optimal! But this is impossible! Hence we must
have supposed wrong and our algorithm finds the optimal solution.