Академический Документы
Профессиональный Документы
Культура Документы
Data Structures
Chuan-Ming Liu
Computer Science & Information Engineering
National Taipei University of Technology
Taiwan
1
CSIE, NTUT, TAIWAN
Instructor
Chuan-Ming Liu ()
Office: 1530 Technology Building
Computer Science and Information Engineering
National Taipei University of Technology
TAIWAN
Phone: (02) 2771-2171 ext. 4251
Email: cmliu@csie.ntut.edu.tw
Office Hours: Mon: 11:10-12:00, 13:10-14:00 and
Thu:10:10 - 12:00, OR by appointment.
2
CSIE, NTUT, TAIWAN
Teaching Assisant
Bill In-Chi Su ()
Office: 1226 Technology Building
Office Hours: Tue:10:00~12:00 and Wed:
10:00 ~ 12:00, OR by appointment
Email: billandcs@gmail.com
Phone: 02-2771-2171 ext. 4262
3
CSIE, NTUT, TAIWAN
Text Books
Ellis Horowitz, Sartaj Sahni, and Susan
Anderson-Frees, Fundamentals of Data
Structures in C, 2nd edition, Silicon Press,
2008.
Supplementary Texts
Michael T. Goodrich and Roberto Tamassia, Data Structures and
Algorithms in JAVA, 4th edition, John Wiley & Sons, 2006. ISBN: 0471-73884-0.
Sartaj Sahni, Data Structures, Algorithms, and Applications in JAVA,
2nd edition, Silicon Press, 2005. ISBN: 0-929306-33-3.
Frank M. Carranno and Walter Savitch, Data Structures and Abstractions
with Java, Prentice Hall, 2003. ISBN: 0-13-017489-0.
4
CSIE, NTUT, TAIWAN
Course Outline
Course Work
Assignments (50%): 6-8 homework sets
Midterm (20%)
Final exam (30%)
6
CSIE, NTUT, TAIWAN
First Thing to Do
Please visit the course web site
http://www.cc.ntut.edu.tw/~cmliu/DS/NTUT_
DS_S09u-GIT/
Send an email to me using the email
address:cmliu@csie.ntut.edu.tw. I will make a
mailing list for this course. All the
announcements will be broadcast via this
mailing list.
9
CSIE, NTUT, TAIWAN
Introduction
Chuan-Ming Liu
Computer Science & Information Engineering
National Taipei University of Technology
Taiwan
10
CSIE, NTUT, TAIWAN
Outline
Data Structures and Algorithms
Pseudo-code
Recursion
11
CSIE, NTUT, TAIWAN
13
CSIE, NTUT, TAIWAN
15
CSIE, NTUT, TAIWAN
Definiteness
Output
Effectiveness
Finiteness
Computational Procedures
16
CSIE, NTUT, TAIWAN
17
CSIE, NTUT, TAIWAN
Expressing Algorithms
Ways to express an algorithm
Graphic (flow chart)
Programming languages (C/C++)
Pseudo-code representation
18
CSIE, NTUT, TAIWAN
Outline
Data Structures and Algorithms
Pseudo-code
Recursion
19
CSIE, NTUT, TAIWAN
22
Pseudo-Code Conventions
Indentation as block structure
Loop and conditional constructs similar to
those in PASCAL, such as while, for,
repeat( do while) , if-then-else
// as the comment in a line
Using = for the assignment operator
Variables local to the given procedure
23
CSIE, NTUT, TAIWAN
Pseudo-Code Conventions
Relational operators: ==, != , , .
Logical operators: &&, ||, !.
Array element accessed by A[i] and A[1..j] as
the subarray of A
24
CSIE, NTUT, TAIWAN
Outline
Data Structures and Algorithms
Pseudo-code
Recursion
25
CSIE, NTUT, TAIWAN
Recursion
Recursion is the concept of defining a method
that makes a call to itself
A method calling itself is making a recursive
call
A method M is recursive if it calls itself (direct
recursion) or another method that ultimately
leads to a call back to M (indirect recursion)
26
CSIE, NTUT, TAIWAN
Repetition
Repetition can be achieved by
Loops (iterative) : for loops and while loops
Recursion (recursive) : a function calls itself
Factorial function
General definition: n! = 1 2 3 (n-1) n
Recursive definition
1
if n = 0
f ( n) =
else
n f (n 1)
27
CSIE, NTUT, TAIWAN
28
CSIE, NTUT, TAIWAN
Recursive calls
Calls to the current method.
Each recursive call should be defined so that it makes
progress towards a base case.
29
CSIE, NTUT, TAIWAN
Visualizing Recursion
Example recursion trace:
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
return 4*6
recursiveFactorial(4)
return 3*2
recursiveFactorial(3)
return 2*1
recursiveFactorial(2)
return 1*1
recursiveFactorial(1)
return 1
recursiveFactorial(0)
30
CSIE, NTUT, TAIWAN
10
About Recursion
Advantages
Avoiding complex case analysis
Avoiding nested loops
Leading to a readable algorithm description
Efficiency
Examples
File-system directories
Syntax in modern programming languages
31
CSIE, NTUT, TAIWAN
Linear Recursion
The simplest form of recursion
A method M is defined as linear recursion if it
makes at most one recursive call
Example: Summing the Elements of an Array
Given: An integer array A of size m and an integer
n, where m n1.
Problem: the sum of the first n integers in A.
32
CSIE, NTUT, TAIWAN
33
11
Recursive Method
An important property of a recursive method
the method terminates
An algorithm using linear recursion has the
following form:
Test for base cases
Recur
34
CSIE, NTUT, TAIWAN
return 15 + A[4] = 15 + 5 = 20
LinearSum (A,5)
call
return 13 + A[3] = 13 + 2 = 15
LinearSum (A,4)
call
return 7 + A[2] = 7 + 6 = 13
LinearSum (A,3)
call
return 4 + A[1] = 4 + 3 = 7
LinearSum (A,2)
call
return A[0] = 4
LinearSum (A,1)
35
CSIE, NTUT, TAIWAN
Example
Reversing an Array by Recursion
Given: An array A of size n
Problem: Reverse the elements of A (the first
element becomes the last one, )
Solutions
Nested loop ?
Recursion
36
CSIE, NTUT, TAIWAN
12
Reversing an Array
Algorithm ReverseArray(A, i, j):
Input: An array A and nonnegative integer indices i
and j
Output: The reversal of the elements in A starting at
index i and ending at j
if i < j then
Swap A[i] and A[ j]
ReverseArray(A, i + 1, j - 1)
return
37
CSIE, NTUT, TAIWAN
Facilitating 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).
38
CSIE, NTUT, TAIWAN
p( x, n) =
else
x p( x, n 1)
Following the definition leads to an O(n) time
recursive algorithm (for we make n recursive
calls).
We can do better than this, however.
39
CSIE, NTUT, TAIWAN
13
Recursive Squaring
We can derive a more efficient linearly
recursive algorithm by using repeated squaring:
1
if n = 0
p( x, n) = x p( x, (n 1) / 2) 2 if n > 0 is odd
p( x, n / 2) 2
if n > 0 is even
For example,
24
25
26
27
=
=
=
=
40
14
Tail Recursion
Tail recursion occurs when a linearly
recursive method makes its recursive call as
its last step.
Such methods can be easily converted to
non-recursive methods (which saves on
some resources).
The array reversal method is an example.
43
CSIE, NTUT, TAIWAN
return
44
CSIE, NTUT, TAIWAN
Binary Recursion
Binary recursion occurs whenever there are
two recursive calls for each non-base case.
Example: BinaySum
45
CSIE, NTUT, TAIWAN
15
Example Summing n
Elements in an Array
Recursion Trace
0, 8
0, 4
4, 4
0, 2
0, 1
2, 2
1, 1
2, 1
4, 2
3, 1
4, 1
6, 2
5, 1
6, 1
7, 1
47
CSIE, NTUT, TAIWAN
Fibonacci Numbers
Fibonacci numbers are defined recursively:
F0 = 0
F1 = 1
Fi = Fi-1 + Fi-2 for i > 1.
Example: 0, 1, 1, 2, 3, 5, 8,
48
CSIE, NTUT, TAIWAN
16
Computing Lab
Fibonacci Numbers Applied
Binary
Recursion
Algorithm BinaryFib(k):
Input: Nonnegative integer k
Output: The kth Fibonacci number Fk
if k 1 then
return k
else
return BinaryFib(k-1) + BinaryFib(k-2)
49
CSIE, NTUT, TAIWAN
17
Multiple Recursion
Motivating example: summation puzzles
pot + pan = bib
dog + cat = pig
boy + girl = baby
52
CSIE, NTUT, TAIWAN
Visualizing PuzzleSolve()
Initial call
PuzzleSolve (3,() ,{a,b,c})
PuzzleSolve (2,a,{b,c})
abc
bac
acb
bca
cba
54
CSIE, NTUT, TAIWAN
18