Beruflich Dokumente
Kultur Dokumente
Owen Astrachan
Computer Science Dept.
Duke University
Durham, NC 27706
ola@cs.duke.edu
else Swap(A[p],A[q])
and an invariant of Figure 6: invariant for reversing a list
temp := undoneList↑.next;
3 Reversing the Elements of undoneList↑.next := head;
a Singly-linked list head := undoneList;
undoneList := temp;
Reversing the nodes of a singly-linked lists is a
problem typical of the kind given in a CS2 or By enclosing these statements in a loop with
data-structures course. This problem appeared appropriate initialization and termination state-
on the 1989 Advanced Placement Exam in Com- ments we will have finished this problem. The
puter Science. Although this problem has a appropriate initialization involves assigning val-
straightforward O(n) solution for a list of n el- ues to undoneList and head so that the invari-
ements, many students attempted complicated ant is true before the loop iterates the first time.
O(n2 ) solutions, used auxiliary stacks, and in The termination statements may be necessary to
general (and not surprisingly) made mistakes ma- achieve the state specified by the diagram that
nipulating pointers. describes the problem.
Initially, the entire list remains to be processed The simple observation that during the con-
(and, symmetrically, none of the list has been struction of the compacted array part of the ar-
processed). The statements: ray will be compacted and part of the array will
remain unprocessed leads to the picture shown in
undoneList := head; Figure 7 as an invariant.
head := nil;
achieve this state. This results in the following fi- already processed ? ? ?
Figure 8: set of points and their convex hull convex hull so far
The first step of the Graham scan algorithm Figure 10: invariant for Graham scan
is to sort the points whose convex hull is being
sought. The points are sorted with respect to As in the other examples we have presented,
their polar coordinates relative to a point known the essential part of the Graham scan algorithm
to be on the convex hull (or, alternatively, rela- is developing the step that re-establishes the in-
tive to a point known to be in the interior of the variant while ensuring termination. The three
hull). We choose to sort relative to the point with points in the window (considered in order from
smallest y-coordinate (and greatest x-coordinate the anchor node’s point) have two possible con-
if there are several points with the same small- figurations:
est y-coordinate) first by angle θ and then, for
points with equal values of θ, by distance. In • they form a convex angle
our presentation we will assume that the sorted
points are stored in a doubly-linked circular list • they form a reflex (non-convex) angle
whose first element is the point with smallest y-
coordinate with respect to which the other points In the first case (illustrated by nodes 1, 2, and
are sorted. Such a list is shown in Figure 9 3 in Figure 9), the anchor node is advanced by
This list is then processed using a three node one node. Since the points in the window form
“window” to determine which points are in the a convex angle, the invariant is maintained. In
convex hull. As an aid to reconstructing the algo- the second case (illustrated by nodes 2, 3, and
rithm, the invariant shown in Figure 10 includes 4 in Figure 9), the second node of the window
both the constructed hull and the window. The cannot be part of the convex hull and is deleted.
nodes that are labeled as “part of the convex hull The anchor node is also moved back one node so
so far” may or may not be part of the final convex that the invariant is re-established. If the anchor
hull as will be seen. Note that the first two nodes node is not moved back, the invariant will not
in the window are also part of the convex hull ac- necessarily remain true. The first two nodes of
the window cannot be guaranteed to be part of anchor := first;
the convex hull of the points considered. while anchor↑.next <> first do begin
To see that the anchor node must be moved if IsReflex(anchor, anchor↑.next,
back, consider the points shown in Figure 11. anchor↑.next↑.next)
then
anchor := anchor↑.next;
6
3 else begin
4 RemoveNode(anchor↑.next);
7 anchor := anchor↑.previous;
5 2 end;
end;
Since each iteration of the loop advances the win-
dow or removes a point from the list, the loop
above runs in O(n) time for a list of n points.
This makes the sorting of the points the bottle-
1
neck for this problem and the complexity for the
entire algorithm is then O(n log n).
Figure 11: moving the anchor node back