Exercise · Volume 1 · 2.2.4-3
Exercise 2.2.4-3
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Kind | K3, find a value: compute a number, a set or a closed form from the written solution |
| Section | Section 2.2.4 of Volume 1 |
The task
Find what operation (3) does when and point to nodes of the same circular list.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
Number the nodes of the list in the order of the links, and let the two pointers be at positions and .
Hint 2
Treat the case separately.
Solution
Show the solution
Number the nodes around the circle, with and . Let and .
Operation (3) swaps the two links, then sets and .
If the swap exchanges a link with itself, so nothing changes in the list, and changes nothing. The only effect is .
If , the swap sends to and to (indices modulo ). Following the new links from we get and then back to . This is a circle of the nodes met on the way from to , not counting the first and counting the last. Following the links from we get and then again. It is a second circle with the remaining nodes. So the circle is cut into two circles by cutting it at two places and rejoining the ends crosswise.
The last two assignments make , which is on the first of these circles. So now points at a circular list of the nodes that one meets when one follows the links from to . The second circle, , can no longer be reached from the pointers, and unless some other variable points into it, its nodes are lost.
Check
With , , the new circles are , which holds , and , with nodes. A python run over all and all confirmed that the circle of has nodes and the other one has the rest.
Remarks
So operation (3) is a join for two different lists and a split for one list. For the split the two nodes at the cuts decide the sizes of the parts. Before using it, one has to know that the lists are disjoint.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.