Computer Science 3A - CSC3A10/CSC03A3: Lecture 2: Arrays, Linked Lists and Recursion

Download as pdf or txt
Download as pdf or txt
You are on page 1of 66

Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Computer Science 3A - CSC3A10/CSC03A3


Lecture 2: Arrays, Linked Lists and Recursion

Academy of Computer Science and Software Engineering


University of Johannesburg

Computer Science 3A - CSC3A10/CSC03A3 1/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

1 Arrays
Array Properties
Arrays Implemented
2 Basic ADT’s
Position ADT
List ADT
3 Singly Linked Lists
Singly Linked List Properties
Node Class
Insertion
Removal
Stack with a Singly Linked List
Queue with a Singly Linked List
Singly Linked List Implementation
Singly Linked List Performance
4 Doubly Linked Lists
Computer Science 3A - CSC3A10/CSC03A3 2/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Properties


Doubly Linked List Structure
Doubly Linked List Insertion
Doubly Linked List Removal
Doubly Linked List Performance

5 Circular Linked Lists


Circular Linked Lists

6 Recursion
Recursion Properties
Recursion Examples
Content of a Recursive Method
Visualizing recursion
Types of Recursion
Linear Recursion
Tail Recursion
Computer Science 3A - CSC3A10/CSC03A3 3/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion
Multiple Recursion
Exercises

Computer Science 3A - CSC3A10/CSC03A3 4/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Arrays

Computer Science 3A - CSC3A10/CSC03A3 5/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Arrays

Storing in an array
Object to store (such as GameEntry)
High score class
Insertion
Removal

Computer Science 3A - CSC3A10/CSC03A3 6/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Arrays II

Sorting an array
Insertion sort
Algorithm
Java.util
Equals
fill
sort
toString

Computer Science 3A - CSC3A10/CSC03A3 7/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Arrays Implemented

Pseudo-Random Number (java.util.Random and seed)


Cryptography (Caesar Cipher)
2D Arrays (Matrix and Positional games, such as Tic-Tac-Toe)

Computer Science 3A - CSC3A10/CSC03A3 8/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Basic ADT’s

Computer Science 3A - CSC3A10/CSC03A3 9/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Postion ADT

The Position ADT models the notion of place within a data structure where a
single object is stored
It gives a unified view of diverse ways of storing data, such as
a cell of an array
a node of a linked list
Just one method:
object element(): returns the element stored at the position

Computer Science 3A - CSC3A10/CSC03A3 10/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

List ADT

Accessor methods:
The List ADT models a first()
sequence of positions last()
storing arbitrary objects prev(p)
next(p)
It establishes a
Update methods:
before/after relation
replace(p, e)
between positions
insertBefore(p, e),
Generic methods: insertAfter(p, e),
size() insertFirst(e)
isEmpty() insertLast(e)
remove(p)

Computer Science 3A - CSC3A10/CSC03A3 11/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked Lists

Computer Science 3A - CSC3A10/CSC03A3 12/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked List Properties

A singly linked list is a


concrete data structure
consisting of a sequence
of nodes
Each node stores
element
link to the next node Figure: An individual node in a
Singly Linked List

Computer Science 3A - CSC3A10/CSC03A3 13/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked List II

Figure: Singly Linked List with multiple nodes

Computer Science 3A - CSC3A10/CSC03A3 14/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked List Node Class I

1 p u b l i c c l a s s Node {
2 // I n s t a n c e v a r i a b l e s :
3 private Object element ;
4 p r i v a t e Node n e x t ;
5 /∗ ∗ C r e a t e s a node w i t h n u l l r e f e r e n c e s t o i t s e l e m e n t and n e x t
node . ∗/
6 p u b l i c Node ( ) {
7 this ( null , null ) ;
8 }
9 /∗ ∗ C r e a t e s a node w i t h t h e g i v e n e l e m e n t and n e x t node . ∗/
10 p u b l i c Node ( O b j e c t e , Node n ) {
11 element = e ;
12 next = n ;
13 }
14 // A c c e s s o r methods :
15 public Object getElement () {

Computer Science 3A - CSC3A10/CSC03A3 15/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked List Node Class II

16 return element ;
17 }
18 p u b l i c Node g e t N e x t ( ) {
19 return next ;
20 }
21 // M o d i f i e r methods :
22 p u b l i c v o i d s e t E l e m e n t ( O b j e c t newElem ) {
23 e l e m e n t = newElem ;
24 }
25 p u b l i c v o i d s e t N e x t ( Node newNext ) {
26 n e x t = newNext ;
27 }
28 }

Computer Science 3A - CSC3A10/CSC03A3 16/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Inserting at the Head

1 Allocate new node


2 Insert new element
3 Have new node point to
old head
4 Update head to point to
new node

Figure: Insertion at head

Computer Science 3A - CSC3A10/CSC03A3 17/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Inserting at the Tail

1 Allocate a new node


2 Insert new element
3 Have new node point to
null
4 Have old last node point
to new node
5 Update tail to point to
new node
Figure: Insertion at tail

Computer Science 3A - CSC3A10/CSC03A3 18/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Removal at the Head

1 Update head to point to


next node in the list
2 Allow garbage collector to
reclaim the former first
node

Figure: Removal at head

Computer Science 3A - CSC3A10/CSC03A3 19/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Removal at the Tail

1 Removing at the tail of a


singly linked list is not
efficient!
2 There is no constant-time
way to update the tail to
Figure: Removal at tail
point to the previous node

Computer Science 3A - CSC3A10/CSC03A3 20/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Stack with a Singly Linked List


We can implement a stack with a singly linked list
The top element is stored at the first node of the list
The space used is O(n) and each operation of the Stack ADT takes O(1) time

Figure: A visualization of a stack implemented using a SLL

Computer Science 3A - CSC3A10/CSC03A3 21/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Queue with a Singly Linked List


We can implement a queue with a singly linked list
The front element is stored at the first node
The rear element is stored at the last node
The space used is O(n) and each operation of the Queue ADT takes O(1) time

Figure: A visualization of a queue implemented using a SLL

Computer Science 3A - CSC3A10/CSC03A3 22/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked List Implementation

Implementation (Node class - CF 3.12 and SLinkedList Class - CF 3.13)


Insertion (Head and Tail - CF 3.14, but what about the middle?
Removal (Head - CF 3.13, but what about the tail and middle?)

Computer Science 3A - CSC3A10/CSC03A3 23/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Singly Linked List Performance

In the implementation of the List ADT by means of a singly linked list


The space used by a list with n elements is O(n)
The space used by each position of the list is O(1)
Most of the operations of the List ADT run in O(1) time (except access methods)
Operation element() of the Position ADT runs in O(1) time

Computer Science 3A - CSC3A10/CSC03A3 24/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked Lists

Computer Science 3A - CSC3A10/CSC03A3 25/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Properties

Singly Linked Lists: No way of going (quickly) to predecessor node


Doubly Linked List Nodes have both Next and Previous references (CF 3.16)
They also contain Sentinel Nodes namely the header and trailer, which are empty!
(Fig 3.19)

Computer Science 3A - CSC3A10/CSC03A3 26/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Properties II

Now removing the tail?


Adding at the head (in fact, add after the header)
Insertion in the middle? (Easy: for any node v we call insertAfter(v, z) - z being
the node to insert)
Removal in the middle? (CF 3.17)

Computer Science 3A - CSC3A10/CSC03A3 27/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked Structure

A doubly linked list


provides a natural
implementation of the
List ADT
Nodes implement Position
and store:
element
link to the previous
node Figure: An individual node in a
link to the next node Doubly Linked List
Special trailer and header
nodes

Computer Science 3A - CSC3A10/CSC03A3 28/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List II

Figure: Doubly Linked List with multiple nodes

Computer Science 3A - CSC3A10/CSC03A3 29/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Insertion

Figure: Doubly Linked List before insertion

Computer Science 3A - CSC3A10/CSC03A3 30/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Insertion II

Figure: Doubly Linked List during insertion

Computer Science 3A - CSC3A10/CSC03A3 31/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Insertion III

Figure: Doubly Linked List after insertion

Computer Science 3A - CSC3A10/CSC03A3 32/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Insertion IV

1 Algorithm i n s e r t A f t e r (p , e ) :
2 C r e a t e a new node v
3 v . setElement ( e )
4 v . setPrev ( p ) { l i n k v to i t s p r e d e c e s s o r }
5 v . setNext (p . getNext () ) { l i n k v to i t s s u c c e s s o r }
6 ( p . getNext () ) . setPrev ( v ) { l i n k p ’ s old s u c c e s s o r to v}
7 p . s e t N e x t ( v ) { l i n k p t o i t s new s u c c e s s o r , v }
8 r etu rn v { the p o s i t i o n f o r the element e}

insertAfter algorithm

Computer Science 3A - CSC3A10/CSC03A3 33/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Removal

Figure: Doubly Linked List before removal

Computer Science 3A - CSC3A10/CSC03A3 34/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Removal II

Figure: Doubly Linked List during removal

Computer Science 3A - CSC3A10/CSC03A3 35/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Removal III

Figure: Doubly Linked List after removal

Computer Science 3A - CSC3A10/CSC03A3 36/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Removal IV

1 A l g o r i t h m remove ( p ) :
2 t = p . element {a temporary v a r i a b l e to hold the return v a l u e }
3 ( p . g e t P r e v ( ) ) . s e t N e x t ( p . g e t N e x t ( ) ) { l i n k i n g o u t p}
4 (p . getNext () ) . setPrev (p . getPrev () )
5 p . s e t P r e v ( n u l l ) { i n v a l i d a t i n g t h e p o s i t i o n p}
6 p . setNext ( null )
7 return t

remove algorithm

Computer Science 3A - CSC3A10/CSC03A3 37/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Doubly Linked List Performance

In the implementation of the List ADT by means of a doubly linked list


The space used by a list with n elements is O(n)
The space used by each position of the list is O(1)
All the operations of the List ADT run in O(1) time
Operation element() of the Position ADT runs in O(1) time

Computer Science 3A - CSC3A10/CSC03A3 38/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Circular Linked Lists

Computer Science 3A - CSC3A10/CSC03A3 39/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Circular Linked Lists

Always a next pointer


No null to signify end of list.
No head, no tail
Special node known as the cursor (RDBMS anyone?)

Computer Science 3A - CSC3A10/CSC03A3 40/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Circular Linked Lists II

Some methods
add
remove
advance [the cursor]
Sorting (definitely easier on a doubly linked list!)
insertion sort (CF 3.27)
Java (CF 3.28)

Computer Science 3A - CSC3A10/CSC03A3 41/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Recursion

Computer Science 3A - CSC3A10/CSC03A3 42/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Recursion Properties

When a method calls itself


Sometimes the solution has repetition

Computer Science 3A - CSC3A10/CSC03A3 43/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Recursion Examples

Classic recursive examples include:


Euclid’s algorithm
The factorial function
The Fibonacci sequence
The Ackermann function
Towers of Hanoi
Others
Every recursive function can be written in an iterative manner!

Computer Science 3A - CSC3A10/CSC03A3 44/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Recursion Examples II
Euclid’s Algorithm (Greatest Common Divisor):

(
a if b = 0,
For a, b ≥ 0, gcd(a, b) =
gcd(b, a mod b) otherwise

1 p u b l i c s t a t i c i n t gcd ( i n t a , i n t b )
2 {
3 i f ( b == 0 )
4 return a ;
5 e l s e i f ( a >= b && b > 0 )
6 r e t u r n gcd ( b , a % b ) ;
7 e l s e r e t u r n gcd ( b , a ) ;
8 }

Euclid’s Java function


Computer Science 3A - CSC3A10/CSC03A3 45/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Recursion Examples III


The factorial function:
n! = 1 · 2 · 3 · ... · (n − 1) · n

(
1 if n = 0,
f (n) =
n · f (n − 1) else

1 public static int r e c u r s i v e F a c t o r i a l ( int n) {


2 // b a s i s c a s e
3 i f ( n == 0 ) r e t u r n 1;
4 // r e c u r s i v e c a s e
5 e l s e r e t u r n n ∗ r e c u r s i v e F a c t o r i a l ( n− 1 ) ;
6 }

Factorial Java function


Computer Science 3A - CSC3A10/CSC03A3 46/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Content of a Recursive Method

Base case(s):
Values of the input variables for which we perform no recursive calls are called
base cases (there should be at least one base case).
Every possible chain of recursive calls must eventually reach a base case.
Recursive calls:
Calls to the current method.
Each recursive call should be defined so that it makes progress towards a base
case.

Computer Science 3A - CSC3A10/CSC03A3 47/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Visualizing recursion

Recursion trace
A box for each recursive
call
An arrow from each caller
to callee
An arrow from each callee
to caller showing return
value

Computer Science 3A - CSC3A10/CSC03A3 48/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Types of Recursion

Linear recursion
Tail recursion
Binary recursion
Multiple recursion

Computer Science 3A - CSC3A10/CSC03A3 49/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Linear Recursion

Test for base cases


Begin by testing for a set of base cases (there should be at least one).
Every possible chain of recursive calls must eventually reach a base case, and the
handling of each base case should not use recursion.
Recur once:
Perform a single recursive call. (This recursive step may involve a test that
decides which of several possible recursive calls to make, but it should ultimately
choose to make just one of these calls each time we perform this step.)
Define each possible recursive call so that it makes progress towards a base case.

Computer Science 3A - CSC3A10/CSC03A3 50/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Linear Recursion II

1 A l g o r i t h m Li nea rS um (A , n ) :
2 I n p u t : A i n t e g e r a r r a y A and an i n t e g e r n = 1 , s u c h t h a t A h a s a t l e a s t
n elements
3 Output : The sum o f t h e f i r s t n i n t e g e r s i n A
4 i f n = 1 then
5 return A[ 0 ]
6 else
7 r e t u r n Li nea rS um (A , n − 1 ) + A [ n − 1 ]

Linear Summation Algorithm

Computer Science 3A - CSC3A10/CSC03A3 51/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Linear Recursion III

Figure: Recursive Linear Summation Visualized

Computer Science 3A - CSC3A10/CSC03A3 52/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Linear Recursion IV

1 A l g o r i t h m R e v e r s e A r r a y (A , i , j):
2 I n p u t : An a r r a y A and n o n n e g a t i v e i n t e g e r i n d i c e s i and j
3 Output : The r e v e r s a l o f t h e e l e m e n t s i n A s t a r t i n g a t i n d e x i and
ending at j
4 i f i < j then
5 Swap A [ i ] and A [ j ]
6 R e v e r s e A r r a y (A , i + 1 , j − 1)
7 return

Reverse Algorithm

Computer Science 3A - CSC3A10/CSC03A3 53/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Linear Recursion V

Defining Arguments for Recursion


In creating recursive methods, it is important to define the methods in ways that
facilitate recursion.
This sometimes requires we define additional parameters that are passed to the
method.
For example, we defined the array reversal method as ReverseArray(A, i, j), not
ReverseArray(A).

Computer Science 3A - CSC3A10/CSC03A3 54/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Tail Recursion

Tail recursion occurs when a linearly recursive method makes its recursive call as
its last step.
The array reversal method is an example.
Such methods can be easily converted to non-recursive methods (which saves on
some resources).

Computer Science 3A - CSC3A10/CSC03A3 55/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Tail Recursion II

For example:
1 A l g o r i t h m I t e r a t i v e R e v e r s e A r r a y (A , i , j ) :
2 I n p u t : An a r r a y A and n o n n e g a t i v e i n t e g e r i n d i c e s i and j
3 Output : The r e v e r s a l o f t h e e l e m e n t s i n A s t a r t i n g a t i n d e x i and
ending at j
4
5 w h i l e i < j do
6 Swap A [ i ] and A [ j ]
7 i = i + 1
8 j = j − 1
9 return

Reverse Algorithm

Computer Science 3A - CSC3A10/CSC03A3 56/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion

Problem: add all the numbers in an integer array A:


1 A l g o r i t h m BinarySum (A , i , n ) :
2 I n p u t : An a r r a y A and i n t e g e r s i and n
3 Output : The sum o f t h e n i n t e g e r s i n A s t a r t i n g a t i n d e x i
4 i f n = 1 then
5 return A[ i ]
6 r e t u r n BinarySum (A , i , n/ 2 ) + BinarySum (A , i + n/ 2 , n/ 2 )

Binary Sum

Computer Science 3A - CSC3A10/CSC03A3 57/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion II

Figure: Example trace for binary sum

Computer Science 3A - CSC3A10/CSC03A3 58/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion III

The Fibonacci Algorithm via Binary recursion



 F0 = 1
 (or F0 = 0)
f (n) = 1 F =1

 F =F
i i−1 + Fi−2 for i > 1

Computer Science 3A - CSC3A10/CSC03A3 59/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion IV

As a recursive algorithm (first attempt):


1 Algorithm BinaryFib ( k ) :
2 Input : Nonnegative i n t e g e r k
3 Output : The k t h F i b o n a c c i number Fk
4 i f k = 0 | | k = 1 then
5 return k
6 else
7 return B i n a r y F i b ( k − 1) + B i n a r y F i b ( k − 2)

Binary Fibonacci

Computer Science 3A - CSC3A10/CSC03A3 60/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion V
Let nk denote number of recursive calls made by BinaryFib(k). Then

n0 = 1 (sometimes there is a n00 = 0)


n1 = 1
n2 = n1 + n0 = 2
n3 = n2 + n1 = 3
n4 = n3 + n2 = 2 + 3 = 5
n5 = n4 + n3 = 3 + 5 = 8
n6 = n5 + n4 = 5 + 8 = 13
n7 = n6 + n5 = 8 + 13 = 21
n8 = n7 + n6 = 13 + 21 = 34.

Note that the value at least doubles for every other value of nk . That is, nk > 2k/2 . It
is exponential!
Computer Science 3A - CSC3A10/CSC03A3 61/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Binary Recursion VI

Use linear recursion instead:


1 Algorithm LinearFibonacci (k ) :
2 Input : A nonnegative integer k
3 Output : P a i r o f F i b o n a c c i numbers ( Fk , Fk −1)
4 i f k = 1 then
5 return ( k , 0)
6 else
7 (i , j ) = L i n e a r F i b o n a c c i ( k − 1)
8 r e t u r n ( i +j , i )

Linear Fibonacci

Runs in O(k) time!

Computer Science 3A - CSC3A10/CSC03A3 62/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Multiple Recursion

Motivating example: summation puzzles

pot + pan = bib


dog + cat = pig
boy + girl = baby

Multiple recursion: makes potentially many recursive calls (not just one or two).

Computer Science 3A - CSC3A10/CSC03A3 63/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Multiple Recursion II
1 A l g o r i t h m P u z z l e S o l v e ( k , S , U) :
2 I n p u t : An i n t e g e r k , s e q u e n c e S , and s e t U ( t h e e l e m e n t s u s e d t o t e s t )
3 Output : An e n u m e r a t i o n o f a l l k−l e n g t h e x t e n s i o n s t o S u s i n g e l e m e n t s
in U
4 without r e p e t i t i o n s
5
6 f o r a l l e i n U do
7 Remove e from U { e i s now b e i n g u s e d }
8 Add e t o t h e end o f S
9 i f k = 1 then
10 Test whether S i s a c o n f i g u r a t i o n that s o l v e s the p u z z l e
11 i f S s o l v e s the puzzle then
12 return ‘ ‘ S o l u t i o n found : ‘ ‘ S
13 else
14 P u z z l e S o l v e ( k − 1 , S , U)
15 Add e back t o U { e i s now u n u s e d }
16 Remove e from t h e end o f S
Computer Science 3A - CSC3A10/CSC03A3 64/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Multiple Recursion II
Example trace for PuzzleSolve(3,S,U) where S=() and U={a,b,c}

Computer Science 3A - CSC3A10/CSC03A3 65/66 Academy of Computer Science and Software Engineering
Outline Arrays Basic ADT’s Singly Linked Lists Doubly Linked Lists Circular Linked Lists Recursion

Reinforcement exercises: (U.S. version in parenthesis)


R-3.1
R-3.4
R-3.7
Creativity exercises:
C-3.15
C-3.18
C-3.21
C-3.22
C-3.23
C-3.30
C-3.31

Computer Science 3A - CSC3A10/CSC03A3 66/66 Academy of Computer Science and Software Engineering

You might also like