View
236
Download
3
Embed Size (px)
Citation preview
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 11
Chapter 3Chapter 3Arrays, Linked Lists, and Arrays, Linked Lists, and
RecursionRecursion
ObjectivesObjectives– Using ArraysUsing Arrays– Singly Linked ListsSingly Linked Lists– Doubly Linked ListsDoubly Linked Lists– Circularly Linked ListsCircularly Linked Lists– Linked-List SortingLinked-List Sorting– RecursionRecursion
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 22
ArraysArraysDeclarationDeclarationPropertiesProperties– Index: 0 through length-1Index: 0 through length-1– Length: the number of elementsLength: the number of elements– Fixed lengthFixed length– Elements could be objectsElements could be objects
OperationsOperations– Insertion: Insertion: add(Object e)add(Object e)– Deletion: Deletion: remove(Object e)remove(Object e)
remove(int i)remove(int i)– Search: Search: find(Object e)find(Object e)
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 33
Sorting an ArraySorting an Array
Insertion-sort algorithmInsertion-sort algorithm
Selection-sort algorithmSelection-sort algorithm
Merge-sort algorithmMerge-sort algorithm
Bubble-sort algorithmBubble-sort algorithm
Quick-sort algorithmQuick-sort algorithm
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 44
java.util Methods for Arraysjava.util Methods for Arrays
Simple methodsSimple methods– equals(A, B)equals(A, B)– fill(A, x)fill(A, x)– sort(A)sort(A)– toString(A)toString(A)
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 55
Pseudo-Random NumbersPseudo-Random NumbersRandom rand = new Random();Random rand = new Random();
Rand.setSeed(System.currentTimeMills());Rand.setSeed(System.currentTimeMills());…………
rand.nextInt(100); // rand.nextInt(100); // between 0 and 99between 0 and 99
Pseudo-random number generatorPseudo-random number generator
seedseed
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 66
Two-Dmensional ArraysTwo-Dmensional ArraysArray of arraysArray of arrays– IndexIndex– LengthLength– ReferenceReference– Assignment of arraysAssignment of arrays
MatrixMatrix
High-dimension arraysHigh-dimension arrays
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 77
Singly Linked ListsSingly Linked ListsLinked list: a collection of nodes in a linear orderLinked list: a collection of nodes in a linear orderNodesNodes– Link – pointing to the next nodeLink – pointing to the next node– Data Data
Head – refer to the first nodeHead – refer to the first nodeOperations:Operations:– InsertionInsertion
addFirst(Object)addFirst(Object) -- O(1)-- O(1)addLast(Object)addLast(Object) -- O(1)-- O(1)
– RemovalRemovalremoveFirst()removeFirst() -- O(1)-- O(1)removeLast()removeLast() -- O(1)-- O(1)
– SearchSearchfind(Object)find(Object) -- O(n)-- O(n)
ImplementationImplementation– Boundary scenarios:Boundary scenarios:
insert into and remove from an empty listinsert into and remove from an empty list
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 88
Doubly Linked ListsDoubly Linked ListsDoubly linked list – a linked list with each node Doubly linked list – a linked list with each node having two links pointing to its previous and next having two links pointing to its previous and next nodes respectivelynodes respectively
Node: DNodeNode: DNode– Fields: Fields:
Data – elementData – element
Link to previous node – prevLink to previous node – prev
Link to next node – nextLink to next node – next
– Methods:Methods:getElement()getElement()
getNext()getNext()
getPrev()getPrev()
setElement(Object)setElement(Object)
setNext(DNode)setNext(DNode)
setPrev(DNode)setPrev(DNode)
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 99
Doubly Linked ListsDoubly Linked ListsHeader and Trailer SentinelsHeader and Trailer Sentinels– Separate header and trailerSeparate header and trailer
One header, one trailerOne header, one trailer
– Integrated header and trailerIntegrated header and trailerOne node with One node with prevprev pointing to the last node and pointing to the last node and nextnext pointing to the first node pointing to the first node
Operations – at endsOperations – at ends– Insertion Insertion – Removal Removal
Operations – in the middleOperations – in the middle– InsertionInsertion– removalremoval
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1010
Circularly Linked ListsCircularly Linked ListsA linked list without head or tailA linked list without head or tail
Traversal means circle through all nodesTraversal means circle through all nodes
Cursor:Cursor: current node current node
Operations:Operations:– add(Object) – immediately after the cursoradd(Object) – immediately after the cursor– remove() – immediately after the cursorremove() – immediately after the cursor– advance() – go to the next nodeadvance() – go to the next node
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1111
Sorting a Linked ListSorting a Linked ListInsertion-sortInsertion-sort– Using single linked listUsing single linked list
Start from the first to the current Start from the first to the current Find the appropriate position and insertFind the appropriate position and insert
– Using two linked listUsing two linked listRemove the first from the source listRemove the first from the source listInsert into the target listInsert into the target list
Selection-sortSelection-sort– Using single linked listUsing single linked list
Select the maximum from the original first Select the maximum from the original first Insert at the firstInsert at the first
– Using two linked listUsing two linked listSelect the minimum from the source listSelect the minimum from the source listInsert at the last to the target listInsert at the last to the target list
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1212
Recursion PatternRecursion Pattern
RecursionRecursion: when a method calls itself: when a method calls itselfClassic example--the factorial function:Classic example--the factorial function:– n! = 1n! = 1· · 22· · 33· ··· · · ··· · (n-1)(n-1)· · nn
Recursive definition:Recursive definition:
As a Java method:As a Java method:// recursive factorial function// recursive factorial function public static int public static int recursiveFactorial(recursiveFactorial(int int n) n) { { if if ((n n == == 00) ) return return 11;; // basis case// basis case else return else return n n * * recursiveFactorialrecursiveFactorial((nn- - 11);); // recursive // recursive
casecase}}
elsenfn
nnf
)1(
0 if1)(
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1313
Linear RecursionLinear RecursionTest for base cases.Test for base cases. – Begin by testing for a set of base cases (there Begin by testing for a set of base cases (there
should be at least one). should be at least one). – Every possible chain of recursive calls Every possible chain of recursive calls mustmust
eventually reach a base case, and the eventually reach a base case, and the handling of each base case should not use handling of each base case should not use recursion.recursion.
Recur once. Recur once. – Perform a single recursive call. (This recursive Perform a single recursive call. (This recursive
step may involve a test that decides which of step may involve a test that decides which of several possible recursive calls to make, but it several possible recursive calls to make, but it should ultimately choose to make just one of should ultimately choose to make just one of these calls each time we perform this step.)these calls each time we perform this step.)
– Define each possible recursive call so that it Define each possible recursive call so that it makes progress towards a base case.makes progress towards a base case.
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1414
A Simple Example of Linear A Simple Example of Linear RecursionRecursion
Algorithm Algorithm LinearSum(LinearSum(A, nA, n):):Input: Input: An integer array An integer array A A and an and an
integer integer n n 1, such that 1, such that A A has at least has at least n n elementselements
Output: Output: The sum of the first The sum of the first n n
integers in integers in AAif if n n = 1 = 1 thenthen return return AA[0][0]elseelse return return LinearSum(LinearSum(A, n - A, n - 1) 1)
+ + AA[[n - n - 1]1]
Example recursion trace:
LinearSum (A,5)
LinearSum (A,1)
LinearSum (A,2)
LinearSum (A,3)
LinearSum (A,4)
call
call
call
call return A[0] = 4
return 4 + A[1] = 4 + 3 = 7
return 7 + A[2] = 7 + 6 = 13
return 13 + A[3] = 13 + 2 = 15
call return 15 + A[4] = 15 + 5 = 20
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1515
Reversing an ArrayReversing an ArrayAlgorithm Algorithm ReverseArray(ReverseArray(A, i, jA, i, j):): Input: Input: An array An array A A and nonnegative and nonnegative
integer indices integer indices i i and and jj Output: Output: The reversal of the elements in The reversal of the elements in
A A starting at index starting at index i i and and ending at ending at jj
if if i < j i < j thenthen Swap Swap AA[[ii] and ] and AA[ [ jj]] ReverseArray(ReverseArray(A, i A, i + 1+ 1, j - , j - 1)1)
returnreturn
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1616
Defining Arguments for Defining Arguments for RecursionRecursion
In creating recursive methods, it is In creating recursive methods, it is important to define the methods in ways important to define the methods in ways that facilitate recursion.that facilitate recursion.
This sometimes requires we define This sometimes requires we define additional parameters that are passed to additional parameters that are passed to the method.the method.
For example, we defined the array reversal For example, we defined the array reversal method as ReverseArray(method as ReverseArray(A, i, jA, i, j), not ), not ReverseArray(ReverseArray(AA).).
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1717
Computing PowersComputing PowersThe power function, p(x,n)=xThe power function, p(x,n)=xnn, can be , can be defined recursively:defined recursively:
This leads to an power function that runs This leads to an power function that runs in O(n) time (for we make n recursive in O(n) time (for we make n recursive calls).calls).
We can do better than this, however.We can do better than this, however.
else)1,(
0 if1),(
nxpx
nnxp
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1818
Recursive SquaringRecursive SquaringWe can derive a more efficient linearly We can derive a more efficient linearly recursive algorithm by using repeated recursive algorithm by using repeated squaring:squaring:
For example,For example,2244 = = 22((44//22))2 2 = (= (2244//22))2 2 = (= (2222))2 2 = = 442 2 = = 16162255 = = 2211+(+(44//22))2 2 = = 22((2244//22))2 2 = = 22((2222))2 2 = = 22((4422) = ) = 32322266 = = 22((66/ / 2)2 2)2 = (= (2266//22))2 2 = (= (2233))2 2 = = 882 2 = = 64642277 = = 2211+(+(66//22))2 2 = = 22((2266//22))2 2 = = 22((2233))2 2 = = 22((8822) = ) = 128128..
even is 0 if
odd is 0 if
0 if
)2/,(
)2/)1(,(
1
),(2
2
x
x
x
nxp
nxpxnxp
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 1919
A Recursive Squaring MethodA Recursive Squaring Method
Algorithm Algorithm Power(Power(x, nx, n):): Input: Input: A number A number x x and integer and integer n = n = 00 Output: Output: The value The value xxnn
if if n n = 0= 0 thenthenreturn return 11
if if n n is odd is odd thentheny = y = Power(Power(x, x, ((n - n - 1)1)/ / 2)2)return return x · y ·yx · y ·y
elseelsey = y = Power(Power(x, n/ x, n/ 2)2)return return y · yy · y
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2020
Analyzing the Recursive Analyzing the Recursive Squaring MethodSquaring Method
Algorithm Algorithm Power(Power(x, nx, n):): Input: Input: A number A number x x and and
integer integer n = n = 00 Output: Output: The value The value xxnn
if if n n = 0= 0 thenthen return return 11
if if n n is odd is odd thenthen y = y = Power(Power(x, x, ((n - n - 1)1)/ / 2)2) return return x · y · yx · y · y
elseelse y = y = Power(Power(x, n/ x, n/ 2)2) return return y · yy · y
It is important that we used a variable twice here rather than calling the method twice.
Each time we make a recursive call we halve the value of n; hence, we make log n recursive calls. That is, this method runs in O(log n) time.
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2121
Tail RecursionTail RecursionTail recursion occurs when a linearly recursive Tail recursion occurs when a linearly recursive method makes its recursive call as its last step.method makes its recursive call as its last step.The array reversal method is an example.The array reversal method is an example.Such methods can be easily converted to non-Such methods can be easily converted to non-recursive methods (which saves on some resources).recursive methods (which saves on some resources).Example:Example:Algorithm Algorithm IterativeReverseArray(IterativeReverseArray(A, i, j A, i, j ):): Input: Input: An array An array A A and nonnegative integer indices and nonnegative integer indices i i and and jj Output: Output: The reversal of the elements in The reversal of the elements in A A starting at starting at
index index i i and ending at and ending at jj while while i < j i < j dodo
Swap Swap AA[[i i ] and ] and AA[ [ j j ]]i = i i = i + 1+ 1j = j - j = j - 11
returnreturn
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2222
Binary Recursive MethodBinary Recursive MethodBinary recursion occurs whenever there are Binary recursion occurs whenever there are twotwo recursive recursive calls for each non-base case.calls for each non-base case.Example Problem: add all the numbers in an integer array A:Example Problem: add all the numbers in an integer array A:Algorithm Algorithm BinarySum(BinarySum(A, i, nA, i, n):): Input: Input: An array An array A A and integers and integers i i and and nn Output: Output: The sum of the The sum of the n n integers in integers in A A starting at index starting at index ii if if n n = 1 = 1 thenthen
return return AA[[i i ]] return return BinarySum(BinarySum(A, i, n/ A, i, n/ 2) + BinarySum(2) + BinarySum(A, i A, i + + n/ n/ 22, n/ , n/ 2)2)
Example trace:Example trace:
3, 1
2, 2
0, 4
2, 11, 10, 1
0, 8
0, 2
7, 1
6, 2
4, 4
6, 15, 1
4, 2
4, 1
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2323
Computing Fibanacci NumbersComputing Fibanacci NumbersFibonacci numbers are defined recursively:Fibonacci numbers are defined recursively:
FF00 = = 00FF11 = = 11FFii = = FFii--11 + + FFii--22 for for i i > > 1.1.
As a recursive algorithm (first attempt):As a recursive algorithm (first attempt):Algorithm Algorithm BinaryFibBinaryFib((kk)):: Input: Input: Nonnegative integer Nonnegative integer kk Output: Output: The The kkth Fibonacci number th Fibonacci number FFkk
if if k <k <= = 1 1 thenthen return return kk
elseelse return return BinaryFibBinaryFib((k k - - 11) + ) +
BinaryFibBinaryFib((k k - - 22))
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2424
Analyzing the Binary Recursion Analyzing the Binary Recursion Fibonacci AlgorithmFibonacci Algorithm
Let nLet nkk denote number of recursive calls made by denote number of recursive calls made by BinaryFib(k). ThenBinaryFib(k). Then– nn00 = 1 = 1– nn11 = 1 = 1– nn22 = = nn11 + + nn00 + 1 = 1 + 1 + 1 = 3 + 1 = 1 + 1 + 1 = 3– nn33 = = nn22 + + nn11 + 1 = 3 + 1 + 1 = 5 + 1 = 3 + 1 + 1 = 5– nn44 = = nn33 + + nn22 + 1 = 5 + 3 + 1 = 9 + 1 = 5 + 3 + 1 = 9– nn55 = = nn44 + + nn33 + 1 = 9 + 5 + 1 = 15 + 1 = 9 + 5 + 1 = 15– nn66 = = nn55 + + nn44 + 1 = 15 + 9 + 1 = 25 + 1 = 15 + 9 + 1 = 25– nn77 = = nn66 + + nn55 + 1 = 25 + 15 + 1 = 41 + 1 = 25 + 15 + 1 = 41– nn88 = = nn77 + + nn66 + 1 = 41 + 25 + 1 = 67 + 1 = 41 + 25 + 1 = 67..
Note that the value at least doubles for every Note that the value at least doubles for every other value of nother value of nkk. That is, n. That is, nkk > 2 > 2k/2k/2. It is . It is exponential!exponential!
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2525
A Better Fibonacci Algorithm A Better Fibonacci Algorithm
Use linear recursion instead:Use linear recursion instead:Algorithm Algorithm LinearFibonacci(LinearFibonacci(kk):): Input: Input: A nonnegative integer A nonnegative integer kk
Output: Output: Pair of Fibonacci numbers (Pair of Fibonacci numbers (FFkk, F, Fk-k-11)) if if k <= k <= 1 1 thenthen
return return ((k, k, 0)0) elseelse
((i, ji, j) ) == LinearFibonacci(LinearFibonacci(k - k - 1)1)return return ((i i ++j, ij, i))
Runs in Runs in OO((kk) time.) time.
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2626
Multiple RecursionMultiple Recursion
Motivating example: summation Motivating example: summation puzzlespuzzles
pot pot + + pan pan = = bibbib
dog dog + + cat cat = = pigpig
boy boy + + girl girl = = babybaby
Multiple recursion: makes potentially Multiple recursion: makes potentially many recursive calls (not just one or many recursive calls (not just one or two).two).
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2727
Algorithm for Multiple RecursionAlgorithm for Multiple RecursionAlgorithmAlgorithm PuzzleSolve(k,S,U):PuzzleSolve(k,S,U): Input:Input: An integer k, sequence S, and set U (the universe of An integer k, sequence S, and set U (the universe of
elements to test)elements to test) Output:Output: An enumeration of all k-length extensions to S using An enumeration of all k-length extensions to S using
elements in U without repetitionselements in U without repetitions forfor all all e in U e in U dodo
Remove e from U Remove e from U {e is now being used}{e is now being used}Add e to the end of SAdd e to the end of Sif if k = 1 k = 1 thenthen
Test whether S is a configuration that solves the puzzleTest whether S is a configuration that solves the puzzleif if S solves the puzzle S solves the puzzle thenthen
return return “Solution found: ” S“Solution found: ” Selseelse
PuzzleSolve(k - 1, S,U)PuzzleSolve(k - 1, S,U)Add e back to U Add e back to U {e is now unused}{e is now unused}Remove e from the end of SRemove e from the end of S
Fall 2006Fall 2006 CSC311: Data StructuresCSC311: Data Structures 2828
Visualizing PuzzleSolveVisualizing PuzzleSolve
PuzzleSolve(3,(),{a,b,c})
Initial call
PuzzleSolve(2,c,{a,b})PuzzleSolve(2,b,{a,c})PuzzleSolve(2,a,{b,c})
PuzzleSolve(1,ab,{c})
PuzzleSolve(1,ac,{b}) PuzzleSolve(1,cb,{a})
PuzzleSolve(1,ca,{b})
PuzzleSolve(1,bc,{a})
PuzzleSolve(1,ba,{c})
abc
acb
bac
bca
cab
cba