CSCI 2720 Lists II

41
CSCI 2720 Lists II Eileen Kraemer University of Georgia September 13, 2005

description

CSCI 2720 Lists II. Eileen Kraemer University of Georgia September 13, 2005. Stack : Linked Memory. X2. Push X0 Push X1 Push X2. X1. X0. Stack: Linked Memory. function MakeEmptyStack():ptr return  function IsEmptyStack(ptr L):bool return L =  Function Top(ptr L):info - PowerPoint PPT Presentation

Transcript of CSCI 2720 Lists II

Page 1: CSCI 2720 Lists II

CSCI 2720Lists II

CSCI 2720Lists II

Eileen KraemerUniversity of GeorgiaSeptember 13, 2005

Eileen KraemerUniversity of GeorgiaSeptember 13, 2005

Page 2: CSCI 2720 Lists II

Stack : Linked MemoryStack : Linked Memory

• Push X0• Push X1• Push X2

• Push X0• Push X1• Push X2

X0

X1

X2

Page 3: CSCI 2720 Lists II

Stack: Linked MemoryStack: Linked Memory• function MakeEmptyStack():ptr

return

• function IsEmptyStack(ptr L):boolreturn L =

• Function Top(ptr L):infoif IsEmptyStack(L) then errorelse return Info(L)

• function MakeEmptyStack():ptrreturn

• function IsEmptyStack(ptr L):boolreturn L =

• Function Top(ptr L):infoif IsEmptyStack(L) then errorelse return Info(L)

Page 4: CSCI 2720 Lists II

Stack: Linked MemoryStack: Linked Memory

• Function Pop(locative L):infoif IsEmptyStack(L) then errorelse

X <- Top(L)L <= Next(L)return x

• Function Pop(locative L):infoif IsEmptyStack(L) then errorelse

X <- Top(L)L <= Next(L)return x

Page 5: CSCI 2720 Lists II

Stack:Linked MemoryStack:Linked Memory

• Procedure Push(info x, locative L):P <- NewCell(Node)Info(P) <- xNext(P) <- LL <= P

• Procedure Push(info x, locative L):P <- NewCell(Node)Info(P) <- xNext(P) <- LL <= P

Page 6: CSCI 2720 Lists II

What is a locative?What is a locative?

• A “new data type that makes the coding of algorithms smoother”• Elegant to write• Difficult to implement

• A “new data type that makes the coding of algorithms smoother”• Elegant to write• Difficult to implement

Page 7: CSCI 2720 Lists II

What is a locative??What is a locative??• Like a pointer variable (most of the time)

• Key(P), Next(P) ..

• In assignments, keeps track of value assigned, and the place in memory that it came from …

• P <= Q … updates value of P, and what used to point to P now points to Q …• In practice, we do this by using trailing

pointers

• Like a pointer variable (most of the time)• Key(P), Next(P) ..

• In assignments, keeps track of value assigned, and the place in memory that it came from …

• P <= Q … updates value of P, and what used to point to P now points to Q …• In practice, we do this by using trailing

pointers

Page 8: CSCI 2720 Lists II

Queue: Linked MemoryQueue: Linked Memory

• Function MakeEmptyQueue():ptrL <- NewCell(Queue)Front(L) <- Back(L) <- return L

• Function IsEmptyQueue(ptr L):booleanreturn Front(L) =

• Function MakeEmptyQueue():ptrL <- NewCell(Queue)Front(L) <- Back(L) <- return L

• Function IsEmptyQueue(ptr L):booleanreturn Front(L) =

Page 9: CSCI 2720 Lists II

Queue: Linked MemoryQueue: Linked Memory

• Function Enqueue(info x, ptr L):P <- NewCell(Node)Info(P) <- xNext(P) <- If IsEmptyQueue(L) then Front(L) <- Pelse Next(Back(L)) <- PBack(L) <- P

• Function Enqueue(info x, ptr L):P <- NewCell(Node)Info(P) <- xNext(P) <- If IsEmptyQueue(L) then Front(L) <- Pelse Next(Back(L)) <- PBack(L) <- P

Page 10: CSCI 2720 Lists II

Queue: Linked MemoryQueue: Linked Memory• Function Dequeue(ptr L):info

If IsEmptyQueue(L) then errorelse

X <- Info(Front(L))Front(L) <- Next(Front(L))If Front(L) = then

Back (L) <= return x

• Function Dequeue(ptr L):infoIf IsEmptyQueue(L) then errorelse

X <- Info(Front(L))Front(L) <- Next(Front(L))If Front(L) = then

Back (L) <= return x

Page 11: CSCI 2720 Lists II

Queue: Linked MemoryQueue: Linked Memory

• Function Front(ptr L): infoif IsEmptyQueue(L) then errorelse return Info(Front(L))

• Function Front(ptr L): infoif IsEmptyQueue(L) then errorelse return Info(Front(L))

Page 12: CSCI 2720 Lists II

Stacks and RecursionStacks and Recursion

• Recursion • Pro: expressive power• Cost: overhead = time + memory• Stacks used in implementing

recursion• Works because subprogram invocations

end in the opposite order from their beginning (LIFO property)

• Recursion • Pro: expressive power• Cost: overhead = time + memory• Stacks used in implementing

recursion• Works because subprogram invocations

end in the opposite order from their beginning (LIFO property)

Page 13: CSCI 2720 Lists II

Classical Example of Recursive Algorithm:

The Towers of Hanoi

Classical Example of Recursive Algorithm:

The Towers of Hanoi• The Legend. In an ancient city in India, so

the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk. And, there is only one other location in the temple (besides the original and destination locations) sacred enough that a pile of disks can be placed there.

• The Legend. In an ancient city in India, so the legend goes, monks in a temple have to move a pile of 64 sacred disks from one location to another. The disks are fragile; only one can be carried at a time. A disk may not be placed on top of a smaller, less valuable disk. And, there is only one other location in the temple (besides the original and destination locations) sacred enough that a pile of disks can be placed there.

Source: http://www.math.toronto.edu/mathnet/games/towers.html

.jedi .jedi .jedi .jedi

Page 14: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi

Source: http://www.mathematik.uni-muenchen.de/~hinz/tower.jpg

Page 15: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi

• How should the monks proceed?

• Will they make it?

• How should the monks proceed?

• Will they make it?

Page 16: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi• Recursive solution!1. For N = 0 do nothing2. Move the top N-1 disks from Src to

Aux (using Dst as an intermediary peg)

3. Move the bottom disks from Src to Dst

4. Move N-1 disks from Aux to Dst (using Src as an intermediary peg)

• The first call:Solve(3, 1, 2, 3)

• Recursive solution!1. For N = 0 do nothing2. Move the top N-1 disks from Src to

Aux (using Dst as an intermediary peg)

3. Move the bottom disks from Src to Dst

4. Move N-1 disks from Aux to Dst (using Src as an intermediary peg)

• The first call:Solve(3, 1, 2, 3)

Page 17: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi1. Move from Src to Dst 2. Move from Src to Aux 3. Move from Dst to Aux 4. Move from Src to Dst 5. Move from Aux to Src 6. Move from Aux to Dst 7. Move from Src to Dst

1. Move from Src to Dst 2. Move from Src to Aux 3. Move from Dst to Aux 4. Move from Src to Dst 5. Move from Aux to Src 6. Move from Aux to Dst 7. Move from Src to Dst

Page 19: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi• How much time will monks spend? • For one disk, only one move is

necessary• For two disks, we need three moves• For n disks???

• How much time will monks spend? • For one disk, only one move is

necessary• For two disks, we need three moves• For n disks???

Page 20: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi• Let Tn denote the number of moves

needed to move n disks.• T1 = 1• T2 = 3• Tn = 2*Tn-1 + 1

• Let Tn denote the number of moves needed to move n disks.

• T1 = 1• T2 = 3• Tn = 2*Tn-1 + 1

Page 21: CSCI 2720 Lists II

The Towers of HanoiThe Towers of Hanoi• Let Tn denote the number of moves

needed to move n disks.• T1 = 1• T2 = 3• Tn = 2*Tn-1 + 1

• Tn = 2n - 1• How to prove it?

• Let Tn denote the number of moves needed to move n disks.

• T1 = 1• T2 = 3• Tn = 2*Tn-1 + 1

• Tn = 2n - 1• How to prove it?

Page 22: CSCI 2720 Lists II

Induction!Induction!• Base case:

T1 = 1 = 21 - 1. OK!

• Inductive hypothesis:Tn = 2n -1

• Inductive step: we show that:Tn+1=2n+1-1.

Tn+1=2*Tn+1=2*(2n-1)+1= =2n+1-2 + 1= 2n+1-1. QED!

• Base case: T1 = 1 = 21 - 1. OK!

• Inductive hypothesis:Tn = 2n -1

• Inductive step: we show that:Tn+1=2n+1-1.

Tn+1=2*Tn+1=2*(2n-1)+1= =2n+1-2 + 1= 2n+1-1. QED!

Page 23: CSCI 2720 Lists II

Towers of HanoiTowers of Hanoi• Suppose it takes one minute for a monk to

move a disk. The whole task hence would take 264-1 minutes

= (210)6*24-1 minutes≈ (103)6*15 minutes≈ 25*1016 hours≈ 1016 days = 1000000000000000 days ≈ the age of universe

• Suppose it takes one minute for a monk to move a disk. The whole task hence would take 264-1 minutes

= (210)6*24-1 minutes≈ (103)6*15 minutes≈ 25*1016 hours≈ 1016 days = 1000000000000000 days ≈ the age of universe

Page 24: CSCI 2720 Lists II

RecursionRecursion• Sierpiński triangle• Wacław Sierpiński – Polish

mathematician 1882-1969

• Sierpiński triangle• Wacław Sierpiński – Polish

mathematician 1882-1969

Page 25: CSCI 2720 Lists II

Sierpiński TriangleSierpiński Triangle

• Draw a black triangle• Draw a white triangle in the

middle of the triangle.• Call the procedure for three left

black triangles

• Draw a black triangle• Draw a white triangle in the

middle of the triangle.• Call the procedure for three left

black triangles

Page 26: CSCI 2720 Lists II

Tail recursionTail recursion

• A special form of recursion in which the last operation of a function is a recursive call.

• The recursion may be optimized away by executing the call in the current stack frame and returning its result rather than creating a new stack frame.

• A special form of recursion in which the last operation of a function is a recursive call.

• The recursion may be optimized away by executing the call in the current stack frame and returning its result rather than creating a new stack frame.

Page 27: CSCI 2720 Lists II

Recursive formRecursive formint max_list(list l, int max_so_far) {

if (null == l) return max_so_far;

if (max_so_far < head(l)) return max_list(tail(l), head(l));

else return max_list(tail(l), max_so_far);

}

int max_list(list l, int max_so_far) {if (null == l)

return max_so_far; if (max_so_far < head(l))

return max_list(tail(l), head(l)); else

return max_list(tail(l), max_so_far); }

Page 28: CSCI 2720 Lists II

Note ….Note ….

• The return value of the current invocation is just the return value of the recursive call.

• A compiler could optimize it so it doesn't allocate new space for l and max_so_far on each invocation or tear down the stack on the returns.

• The return value of the current invocation is just the return value of the recursive call.

• A compiler could optimize it so it doesn't allocate new space for l and max_so_far on each invocation or tear down the stack on the returns.

Page 29: CSCI 2720 Lists II

Iterative formIterative formint max_list(list l, int max_so_far) { for (;;) { if (null == l) return max_so_far; if (max_so_far < head(l)) {

max_so_far = head(l);l = tail(l); }

else { max_so_far = max_so_far; l = tail(l);

} } }

int max_list(list l, int max_so_far) { for (;;) { if (null == l) return max_so_far; if (max_so_far < head(l)) {

max_so_far = head(l);l = tail(l); }

else { max_so_far = max_so_far; l = tail(l);

} } }

Page 30: CSCI 2720 Lists II

Tail recursionTail recursion

• Now no need to allocate new memory for the parameters or get rid of it during the returns, so this will run faster.

• This example simple enough to do by hand ---much harder for complex recursive data structures, such as trees.

• If compiler is good enough to find and rewrite tail recursion, it will also • collapse the loop test• eliminate the assignment of max_so_far to itself, • hoist the assignment of l after the test

• Now no need to allocate new memory for the parameters or get rid of it during the returns, so this will run faster.

• This example simple enough to do by hand ---much harder for complex recursive data structures, such as trees.

• If compiler is good enough to find and rewrite tail recursion, it will also • collapse the loop test• eliminate the assignment of max_so_far to itself, • hoist the assignment of l after the test

Page 31: CSCI 2720 Lists II

Final form …Final form …int max_list(list l, int max_so_far) { while (null != l){

if (max_so_far < head(l)) { max_so_far = head(l);

} l = tail(l);

} return max_so_far; }

int max_list(list l, int max_so_far) { while (null != l){

if (max_so_far < head(l)) { max_so_far = head(l);

} l = tail(l);

} return max_so_far; }

Page 32: CSCI 2720 Lists II

SLL -- Singly Linked ListsSLL -- Singly Linked Lists• Pro:

• Insertion: (1) • Access(L,I) -- can’t do it in O(1) …

• What is the running time of access??? (more later)

• … but given an item in the list, can get to the next item in O(1) … which can give us a traversal (visit each item once) in O(n)

• Pro:• Insertion: (1) • Access(L,I) -- can’t do it in O(1) …

• What is the running time of access??? (more later)

• … but given an item in the list, can get to the next item in O(1) … which can give us a traversal (visit each item once) in O(n)

Page 33: CSCI 2720 Lists II

Simple List traversalSimple List traversalProcedure Traverse(ptr P):// visit nodes of SLL, starting at PWhile P != {

Visit(Key(P))P <- Next(P)}

// if list is already in lexicographic order, // will give in-order traversal

Procedure Traverse(ptr P):// visit nodes of SLL, starting at PWhile P != {

Visit(Key(P))P <- Next(P)}

// if list is already in lexicographic order, // will give in-order traversal

Page 34: CSCI 2720 Lists II

“Trickier Traversals”“Trickier Traversals”Example (zig-zag scan)<canary, cat, chickadee, coelacanth, collie, corn, cup>

K = crabapple

• Want to find, given word w, the last word in L that alphabetically precedes w and ends with same letter as w

Example (zig-zag scan)<canary, cat, chickadee, coelacanth, collie, corn, cup>

K = crabapple

• Want to find, given word w, the last word in L that alphabetically precedes w and ends with same letter as w

Page 35: CSCI 2720 Lists II

One approach …One approach …• Keep both forward and back pointers:

canary <=>cat <=>chickadee <=> coelacanth <=>collie <=>corn <=>cup

• Pro: (1) from any element to predecessor• forward to cup, backward to collie• Con: 2x the pointer memory, all the time

• Keep both forward and back pointers:

canary <=>cat <=>chickadee <=> coelacanth <=>collie <=>corn <=>cup

• Pro: (1) from any element to predecessor• forward to cup, backward to collie• Con: 2x the pointer memory, all the time

Page 36: CSCI 2720 Lists II

Another approachAnother approach• Use trailing pointers Function FindLast(ptr L, key w): key// find last word in list L ending w/same letter as w// return f there is no such wordP <- LQ <- While P != and Key(P) < w do

if Key(P) ends with same letter as w then Q <- PP <- Next(P)

If Q = then return else return Key(Q)

• Use trailing pointers Function FindLast(ptr L, key w): key// find last word in list L ending w/same letter as w// return f there is no such wordP <- LQ <- While P != and Key(P) < w do

if Key(P) ends with same letter as w then Q <- PP <- Next(P)

If Q = then return else return Key(Q)

Page 37: CSCI 2720 Lists II

Back ptrs v. Trailing ptrs Back ptrs v. Trailing ptrs

• For our example, trailing ptrs saves memory • One extra pointer, only while

searching

• With other search conditions, can be too complex, code too specialized …

• For our example, trailing ptrs saves memory • One extra pointer, only while

searching

• With other search conditions, can be too complex, code too specialized …

Page 38: CSCI 2720 Lists II

Stack based …Stack based …

• Start at beginning of list• Stack pointers to all cells during

forward traversal• Pop pointers from stack to do

backward traversal

• Start at beginning of list• Stack pointers to all cells during

forward traversal• Pop pointers from stack to do

backward traversal

Page 39: CSCI 2720 Lists II

Link inversion traversalLink inversion traversal

• Idea: actually place stack into the list itself • “turn around” the next pointers• Temporarily destroys linked list

• Idea: actually place stack into the list itself • “turn around” the next pointers• Temporarily destroys linked list

Page 40: CSCI 2720 Lists II

Link Inversion TraversalLink Inversion Traversal

StartTraversal(L):

Forward(P,Q):

StartTraversal(L):

Forward(P,Q):

Back(P,Q):Back(P,Q):

P

Q

⎝ ⎜

⎠ ⎟←

Λ

L

⎝ ⎜

⎠ ⎟

P

Q

Next(Q)

⎜ ⎜ ⎜

⎟ ⎟ ⎟←

Q

Next(Q)

P

⎜ ⎜ ⎜

⎟ ⎟ ⎟€

P

Next(P)

Q

⎜ ⎜ ⎜

⎟ ⎟ ⎟←

Next(P)

Q

P

⎜ ⎜ ⎜

⎟ ⎟ ⎟

Page 41: CSCI 2720 Lists II

Doubly Linked ListsDoubly Linked Lists

• Coming soon ….• Coming soon ….