Comp631 Chap6
Comp631 Chap6
Data Structures
6.1 Introduction and Overview
• Entity set
• Entities with similar attributes (e.g., all the employees
in an organization)
• fields, records and files reflects the relationship between
attributes, entities and entity sets
• a field is a single elementary unit of information
representing an attribute of an entity
• a record is the collection of field values of a given entity
• a file is the collection of records of the entities in a given
entity set.
Example 1
(a) Suppose an automobile dealership maintains an inventory file where each
record contains the following data:
Serial Number, Type, Year, Price, Accessories
The Serial Number field - primary key
(a unique serial number)
1 John Brown
2 Sandra Gold
3 Tom Jones
4 June Kelly
5 Mary Reed
6 Alan Smith
Example 3
A chain of 28 stores, each store having 4 departments, may list its weekly sales (to the nearest dollar).
Such data can be stored in the computer using a two-dimensional array in which the first subscript denotes the store and the second subscript the department.
If SALES is the name given to the array, then
SALES[1,1] = 2872, SALES[1, 2] = 805, SALES[1, 3] = 3211,... SALES[28,4] = 982
The size of this array is denoted by 284 (read 28 by 4), since it contains 28 rows (the horizontal lines of numbers) and 4 columns (the vertical lines of numbers).
Dept.
1 2 3 4
Store
1 2872 805 3211 1560
2 2196 1223 2525 1744
3 3257 1017 3686 1951
...
Salesperson Pointer
1 Jones 3,6
2 Ray 2,4,7,9
3 Smith 1, 5,8
• Another very popular way to store the type of data, each salesperson has
1 Adams 5 Jones 3 1
2 Brown 4 Ray 2 2
3 Clark 6 Smith 1 3
4 Drew 7
5 Evans 8
6 Farmer 0
7 Geller 9
8 Hill 0
9 Infeld 0
Trees
• Data frequently contain a hierarchical relationship between various elements.
• The data structure which reflects this relationship is called a rooted tree graph or, simply, a tree.
Boston
Chicago New York
Phaladelphia
Los Angeles
Miami
Airline flights
6.1.3 Data Structure Operations
(1) Traversing
Accessing each record exactly once so that certain items in the record
may be processed. (This accessing and processing is sometimes called
"visiting" the record.)
(2) Searching
Finding the location of the record with a given key value, or finding the
locations of all records which satisfy one or more conditions.
(3) Inserting
Adding a new record to the structure.
(4) Deleting
Removing a record from the structure.
• Sometimes two or more of the operations may be used in a given situation; e.g., we may
want to delete the record with a given key, which may mean we first need to search for
the location of the record.
The following two operations, which are used in special situations, will also be considered:
(1) Sorting
Arranging the records in some logical order (e.g., alphabetically according to some
NAME key, or in numerical order according to some NUMBER key, such as social
security number or account number)
(2) Merging
Combining the records in two different sorted files into a single sorted file
Other operations, e.g., copying and concatenation.
6.1.4 Algorithms: Complexity, Time-Space Tradeoff
Linear Search
• Search each record of the file, one at a time, until finding the given Name and hence
the corresponding telephone number
• time required to execute the algorithm is proportional to the number of comparisons
• assuming that each name in the file is equally likely to be picked, it is intuitively clear
that the average number of comparisons for a file with n records is equal to n/2
• complexity of the linear search algorithm is C(n) = nl2.
• above algorithm would be impossible in practice if we were searching through a list in a telephone book.
• if the names are sorted alphabetically, then we can use an efficient algorithm called binary search.
Binary Search
• Compare the given Name with the name in the middle of the list; this tells which half of the list contains Name.
• Then compare Name with the name in the middle of the correct half to determine which quarter of the list contains Name. Continue the process until finding Name in the list.
• complexity of the binary search algorithm is C(n) = log2 n
6.2 Preliminaries
6.2.1 Mathematical Notation and Functions
Floor and Ceiling Functions
• Let x be any real number. Then x lies between two integers called the floor and the ceiling of x.
x, called the floor of x, denotes the greatest integer that does not exceed x.
x , called the ceiling of x, denotes the least integer that is not less than x.
• If x is itself an integer, then x = x ;
otherwise x +1= x.
Example 7
and
n n
a
• The letter j is a dummy index or dummy variable. Other letters frequently used as dummy variables
a
are i, k, s and t.
j 1
j j
j m
Example 8
n
a b
i 1
i i a1b1 a 2 b2 a n bn
5
j 2
j 2 2 2 3 2 4 2 5 2 4 9 16 25 54
n
j 1 2 n
i 1
n(n 1)
1 2 n
2
Factorial Function
• The product of the positive integers from 1 to n, inclusive, is denoted by n! (read "n factorial").
That is,
n! = 1·2·3 ···(n - 2)(n - 1)n
It is also convenient to define 0! = 1.
Example 9
(a) 2! = 1·2 = 2; 3! = 1·2·3 = 6; 4! = 1·2·3·4 =24
(b) For n > 1, we have n! = n · (n -1)! Hence
5! = 5 · 4! = 5 · 24 = 120;
6! =6·5! = 6·120 = 720
Permutations
• A permutation of a set of n elements is an arrangement of
the elements in a given order.
• For example, the permutations of the set consisting of the
elements a, b, c are as follows:
abc, acb, bac, bca, cab, cba
• There are n! permutations of a set of n elements .
4! = 24 permutations of a set with 4 elements,
5! = 120 permutations of a set with 5 elements, and so on.
Exponents and Logarithms
• Recall the following definitions for integer exponents
(where m is a positive integer):
1
am = a · a ··· a (m times), a0 = 1, a-m =
am
Exponents are extended to include all rational numbers by
defining, for any rational number m/n,
a m/n
an m
( a)
n m
For example, 1 1
24 = 16, 2-4 = 4
, 1252/3 = 52 = 25
2 16
• exponents are extended to include all real numbers by defining, for
any real number x.
K1
LOC 1
MAX DATA[1]
K K+1
Yes
Is K>N ? Write: LOC, MAX
No
STOP
No
Is MAX<DATA[K] ?
Yes
LOC K
MAX DATA[K]
Identifying Number
Steps, Control, Exit
• The steps of the algorithm are executed one after the other, beginning
with Step 1
• Control may be transferred to Step n of the algorithm by the statement
"Go to Step n."
• If several statements appear in the same step, e.g.,
Set K := 1, LOC := 1 and MAX := DATA[l].
then they are executed from left to right.
The algorithm is completed when the statement
Exit.
Comments
• Each step may contain a comment in brackets which indicates the main
purpose of the step.
• usually appear at the beginning or the end of the step.
Variable Names
• use capital letters, as in MAX and DATA
• single-letter names of variables used as counters or subscript will be
capitalized in the algorithms (K and N, for example)
• even through lowercase may be used for these same variables (k and n) in
the accompanying mathematical description and analysis.
Assignment Statement
• use the dots-equal notation :=
For example,
MAX := DATA[1]
assigns the value in DATA[1] to MAX.
Module A
Module B
Module C
Module A
Module B
Module C
(2) Selection Logic (Conditional Flow)
3 Types of Sturctures
If condition, then:
[Module A]
[End of If structure .]
• If the condition holds, then Module A, which may consist of
one or more statements, is executed; otherwise Module A is
skipped and control transfers to the next step of the
algorithm.
No
Condition ? No
Condition ?
Yes Yes
Module A Module A Module B
If condition, then:
[Module A]
Else:
[Module B]
[End of If structure.]
If condition(1), then:
[Module A1]
Else if condition(2), then:
[Module A2]
.
.
Else if condition(M), then:
[Module AM]
Else:
[Module B]
[End of If structure.]
Example 11
The solutions of the quadratic equation
ax2 + bx + c = 0
where a 0, are given by the quadratic formula
Yes
Is K > S ? Yes
Module
No (body of loop)
Module
(body of loop)
K ← K+T
(b) Consider the integer array AUTO with lower bound LB=1932 and
upper bound UB=1984.
FORTRAN 77 INTEGER AUTO(1932:1984)
Pascal VAR AUTO: ARRAY[1932..1984] of INTEGER
6.3.2 Representation of Linear Arrays in Memory
• Let LA be a linear array in the memory of the compute.
• LOC(LA[K]) = address of the element LA[K] of the array LA
• elements of LA are stored in successive memory cells.
• computer does not need to keep track of the address of every element of LA, but needs to keep track only of the address of the first element of LA, denoted by
Base( LA)
base address of LA
• Using this address Base(LA), the computer calculates the address of any element of LA by the following formula:
LOC(LA[K]) = Base(LA) + w(K - lower bound)
where w is the number of words per memory cell for the array LA.
Example 15
Consider the array AUTO in Example 14(b), which records the number
of automobiles sold each year from 1932 through 1984. Suppose AUTO
appears in memory as pictured in the following figure.
That is, Base(AUTO) = 200, and w= 4 words per memory cell for AUTO.
Then
LOC(AUTO[1932]) = 200, LOC(AUTO[1933])=204,
LOC(AUTO[1934])=208, . . .
The address of the array element for the year K=1965 can be obtained
LOC(AUTO[1965]) = Base(AUTO) + w(1965-lower bound)
=200+4(1965-1932)=332
200
201
202 AUTO[1932]
203
204
205
206 AUTO[1933]
207
208
209
210 AUTO[1934]
211
. .
. .
. .
6.3.3 Traversing Linear Arrays
8 8 8
Complexity of the Linear Search Algorithm
• Two important cases to consider are the average case and
the worst case.
• Clearly the worst case occurs when one must search
through the entire array DATA, i.e., when ITEM does not
appear in DATA. In this case, the algorithm requires
f(n) = n + 1 comparisons. Thus, in the worst case, the
running time is proportional to n.
• The running time of the average case uses the probabilistic
notion of expectation. Suppose pk is the probability that
ITEM appears in DATA[K], and suppose q is the probability
that ITEM does not appear in DATA. (Then p1 + p2 + .... +
pn + q = 1.) Since the algorithm uses k comparisons when
ITEM appears in DATA[K], the average number of
comparisons is given by
f(n) = 1 · p1 + 2 · p2 + ... + n· pn + (n +1) · q
• In particular, suppose q is very small and ITEM appears
with equal probability in each element of DATA. Then q 0
and each pi, = 1/n. Accordingly,
f(n) = (n+1)/2
6.3.7 Binary Search
(2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
[Successful]
(b) Suppose ITEM = 85.
(1) Again initially, BEG = 1, END = 13, MID = 7 and DATA[MID] =
55.
(2) Since 85 > 55, BEG has its value changed by BEG = MID + 1 =
8. Hence
MID = INT[(8 + 13)/2] = 10 and so DATA[MID] = 77
(3) Since 85 > 77, BEG has its value changed by BEG = MID +
1 = 11. Hence
MID = INT[(11 + 13) /2] = 12 and so DATA[MID] = 88
(4) Since 85 < 88, END has its value changed by END = MID -
1 = 11. Hence
MID = INT[(11 + 11)/2] = 11 and so DATA[MID] = 80
(Observe that now BEG = END = MID = 11.)
Since 85 > 80, BEG has its value changed by BEG = MID + 1 =
12. But now BEG > END. Hence ITEM does not belong to DATA.
(1) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(2) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(3) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
(4) 11, 22, 30, 33, 40, 44, 55, 60, 66, 77, 80, 88, 99
[Unsuccessful]
Complexity of the Binary Search Algorithm
Two-Dimensional Arrays
• A two-dimensional m n array.
• A is a collection of m n data elements such that each element is
specified by a pair of integers (such as J, K), called subscripts,
with the property that
1Jm and 1Kn
The element of A with first subscript j and second subscript k
will be denoted by
AJ,K or A[J, K]
• called matrices in mathematics and
• tables in business applications
• sometimes called matrix arrays
Columns
1 2 3 4
Two-dimensional 3 4 array A.
Example 23
Suppose each student in a class of 25 students is given 4
tests. Assuming the students are numbered from 1 to 25, the
test scores can be assigned to a 25 4 matrix array SCORE.
Thus SCORE[K, L] contains the Kth student's score on the Lth
test. In particular, the second row of the array.
SCORE[2,1], SCORE[2,2], SCORE[2,3], SCORE[2, 4]
contains the four test scores of the second student.
Student Test 1 Test 2 Test 3 Test 4
1 84 73 88 81
2 95 100 88 96
3 72 66 77 72
. . . . .
. . . . .
25 78 82 70 85
Array SCORE
Representation of Two-Dimensional Arrays in Memory
• Let A be a two-dimensional m n array.
• Although A is pictured as a rectangular array of elements with
m rows and n columns, the array will be represented in memory
by a block of m n sequential memory locations.
• Specifically, the programming language will store the array A
either
(1) column by column, is what is called column-major order, or
(2) row by row, in row-major order.
• The following figure shows these two ways when A is a two-
dimensional 3 4 array.
A Subscripts A Subscripts
(1, 1) (1, 1)
(2, 1) Column 1 (1, 2) Row 1
(3, 1) (1, 3)
(1,2) (1, 4)
(2, 2) Column 2 (2,1)
(3, 2) (2, 2) Row 2
(1, 3) (2, 3)
(2, 3) Column 3 (2, 4)
(3, 3) (3, 1)
(1, 4) (3, 2) Row 3
(2, 4) Column 4 (3, 3)
(3, 4) (3, 4)
(a) Column-major order. (b) Row-major order.
• Recall that, for a linear array LA, the computer does not
keep track of the address LOC(LA[K]) of every element
LA[K] of LA, but does keep track of Base(LA), the address
of the first element,of LA. The computer uses the formula
LOC(LA[K]) = Base(LA) + w(K - 1)
• to find the address of LA[K] in time independent of K.
(Here w is the number of words per memory cell for the
array LA, and 1 is the lower bound of the index set of LA.)
• A similar situation also holds for any two-dimensional m n
array A. That is, the computer keeps track of Base(A)-the
address of the first element A[1, 1] of A- and computes the
address LOC(A[J, K]) of A[J, K] using the formula
(Column-major order)
LOC(A[J, K]) = Base(A) + w[M(K - 1) + (J - 1)]
or
(Row-major order)
LOC(A[J, K]) = Base(A) + w[N(J - 1) + (K - 1)]
Example 24
Consider the 25 x 4 matrix array SCORE in example 23.
Suppose Base(SCORE) = 200 and there are w= 4 words per
memory cell. Furthermore, suppose the programming language
stores two-dimensional arrays using row-major order.
Then the address of SCORE[12, 3], the third test of the twelfth
student, follows.
LOC(SCORE[12, 3]) = 200 +4[4(12 - 1)+ (3 - l)]
= 200 + 4[46] = 384
6.3.9 Pointers; Pointer Array
• DATA be any array.
• A variable P is called a pointer if P "points" to an element in DATA,
i.e., if P contains the address of an element in DATA.
• Analogously, an array PTR is called a pointer array if each element
of PTR is a pointer.
• Pointers and pointer arrays are used to facilitate the processing of
the information in DATA.
• Consider an organization which divides its membership list into
four groups, where each group contains an alphabetized list of
those members living in a certain area. Observe that there are 21
people and the groups contain 4, 9, 2 and 6 people, respectively.
Group 1 Group 2 Group 3 Group 4
Evans Conrad Davis Baker
Harris Felt Segal Cooper
Lewis Glass Ford
Shaw Hill Gray
King Jones
Penn Reed
Silver
Troy
Wagner
• Suppose the membership list is to be stored in memory keeping
track of the different groups.
• One way to do this is to use a two-dimensional 4 n array
where each row contains a group, or to use a two-dimensional n
× 4 array where each column contains a group.
• Although this data structure does allow us to access each
individual group, much space will wasted when the groups vary
greatly in size.
• Specifically, the data in the above figure will require at least a
36-element 4 9 or 9 4 array to store the 21 names, which is
almost twice the space that is necessary.
• The following figure shows the representation of the 4 x 9 array;
the asterisks denote data elements and the zeros denote unused
storage locations. (Arrays whose rows- or column- begin with
different numbers of data elements and end with unused storage
location are said to be jagged.)
* * * * 0 0 0 0 0
* * * * * * * * *
* * 0 0 0 0 0 0 0
* * * * * * 0 0 0
Jagged array
• Another way the membership list can be stored in memory is pictured
in figure (a).
• the list is placed in a linear array, one group after another.
• this method is space-efficient. Also, the ist can easily be processed-
one can easily print all the names on the list, for example.
• On the other hand, there is no way to access any particular group;
e.g., there is no way to find and print only the name in the third group.
• A modified version of the above method is pictured in figure (b).
• the names are listed in a linear array; group by group, except now
some sentinel or marker, such as the three dollar signs used here, will
indicate the end of a group.
MEMBER MEMBER
1 Evans 1 Evans
2 Harris Group 1 2 Harris Group 1
3 Lewis 3 Lewis
4 Shaw 4 Shaw
5 Conrad 5 $$$
. Group 2 6 Conrad Group 2
. . .
13 Wagner
14 Davis Group 3 14 Wagner
15 Segal 15 $$$
16 Baker 16 Davis Group 3
. Group 4 17 Segal
. 18 $$$
21 Reed 19 Baker
. . Group 4
. .
24 Reed
25 $$$
• This method uses only a few extra memory cells--one for
each group--but now one can access any particular group.
• For example, a programmer can now find and print those
names in the third group by locating those names which
appear after the second sentinel and before the third
sentinel.
• The main drawback of this representation is that the list
still must be traversed from the beginning in order to
recognize the third group. In other words, the different
groups are not indexed with this representation.
Pointer Arrays
• pointer array which contains the locations of the different
groups or, more specifically, the locations of the first
elements in the different groups.
• Observe that GROUP[L] and GROUP[L + 1]-1 contain,
respectively, the first and last elements in group L.
MEMBER
1 Evans
2 Harris Group 1
GROUP 3 Lewis
1 1 4 Shaw
2 5 5 Conrad
3 14 . Group 2
4 16
5 22 13 Wagner
14 Davis Group 3
15 Segal
16 Baker
. Group 4
.
21 Reed
22 $$$
Example 25
Suppose one wants to print only the names in the Lth group,
where the value of L is part of the input. Since GROUP[L] and
GROUP[L + 1] - 1 contain, respectively, the locations of the first and
last name in the Lth group, the following module accomplishes our
task:
1. Set FIRST := GROUP[L] and LAST := GROUP[L+1] - l.
2. Repeat for K = FIRST to LAST:
Write: MEMBER[K].
[End of loop.]
3. Return.
MEMBER
1 Evans
2 Harris Group 1
GROUP 3 Lewis
1 1 4 Shaw
2 7 5
3 19 6
• A slight variation of 4 23 7 Conrad
the data structure is 8 Felt
. . Group 2
pictured in the
. .
following figure, . .
where unused NUMB 15 Wagne
r
memory cells are
1 4 16
indicated by the 2 9 17
shading. 3 2 18
4 6 19 Davis Group 3
20 Segal
21
22
FREE 23 Baker
1 2 24 Cooper
2 3 . . Group 4
3 2 . .
4 4 28 Reed
29
30
• Observe that now there are some empty cells between the groups.
• Accorrdingly, a new element may be inserted in a group without
necessarily moving the elements in any other group.
• Using this data structure, one requires an array NUMB which gives
the number of elements in each group.
• Observe that GROUP[K + 1] - GROUP[K] is the total amount of space
available for Group K; hence
FREE[K] = GROUP[K + 1] - GROUP[K] - NUMB[K]
is the number of empty cells following GROUP K.
• Sometimes it is convenient to explicitly define the extra array FREE.
Example 26
Suppose, again, one wants to print only the names in the Lth group,
where L is part of the input. Observe that
GROUP[L] and GROUP[L] + NUMB[L] - 1
contain, respectively, the locations of the first and last names in the
Lth group. Thus the following module accomplishes our task:
1. Set FIRST := GROUP[L] and LAST := GROUP[L] + NUMB[L] - 1.
2. Repeat for K = FIRST to LAST:
Write: MEMBER[K].
[End of loop.]
3. Return.
6.3.10 Records; Record Structures
• Collections of data are frequently organized into a
hierarchy of field, records and files.
• Specifically, a record is a collection of related data items,
each of which is called a field or attribute, and a file is a
collection of similar records.
• Each data item may be a group item composed of
subitems, those items which are indecomposable are
called elementary items or atoms or scalars.
• The names given to the various data items are called
identifiers.
• Under the relationship of group item to subitem, the data
items in a record form a hierarchical structure which can
be described by means of "level" numbers
1 3 2 0 -4
A = and B =
2 4 3 2 6
The product matrix AB is defined and is a 2×3 matrix. The elements in the first
row of AB are obtained, respectively, by multiplying the first row of A by each
of the columns of B:
1
3 2 0 4 1 2 33 1 0 3 2 1 (4) 3 6 11 6 14
2
4 3 2 6
Similarly, the elements in the second row of AB are obtained, respectively, by multiplying the second row of A by each of the columns of B:
That is,
1
3 2 0 4 11 6 14 11 6 14
2
4 3 2 6 2 2 4 3 2 0 4 2 2 (4) 4 6 16 8 16
11 6 14
AB
16 8 16
Algorithm 9: (Matrix Multiplication) MATMUL(A, B, C, M, P, N)
Let A be an M × P matrix array, and let B be a P × N matrix array. This
algorithm stores the product of A and B in an M × N matrix array C.
1. Repeat Steps 2 to 4 for I = 1 to M:
2. Repeat Steps 3 and 4 for J = 1 to N:
3. Set C[I, J] := 0. [Initializes C[I, J]]
4. Repeat for K = 1 to P:
C[I, J] := C[I, J] + A[I, K] * B[K, J].
[End of inner loop.]
[End of Step 2 middle loop.]
[End of Step 1 outer loop.]
5. Exit.
6.4 Stacks, Queues and Recursion
6.4.1 Introduction
• A stack is a linear structure in which items may be
added or removed only at one end.
• three everyday examples of such a structure: a stack of
dishes, a stack of pennies and a stack of folded towels.
• the last item to be added to a stack is the first item to
be removed.
• also called last-in first-out (LIFO) lists.
• Other names are "piles" and "push-down lists".
• A queue is a linear list in which items may be added only at one end and items may be removed only at the other end.
• a queue of people waiting at a bus stop. Each new person who comes takes his or her place at the end of the line, and when the bus comes,
the people at the front of the line board first.
• the first person in the line is the first person to leave.
• are also called first-in first-out (FIFO) lists.
6.4.2 Stacks
Frequently, TOP and MAXSTK are global variables; hence the procedures may be called using only
PUSH(STACK, ITEM) and POP(STACK, ITEM)
Example 35.
(a) Consider the stack in the above figure. We simulate the operation PUSH(STACK, WWW):
1. Since TOP = 3, control is transferred to Step 2.
2. TOP=3+1=4.
3. STACK[TOP] = STACK[4] = WWW.
4. Return.
Note that WWW is now the top element in the stack.
44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66
• finds the final position of one of the numbers; we use the first number, 44.
• Beginning with the last number, 66, scan the list from right to left, comparing each number with 44 and
stopping at the first number less than 44. The number is 22.
• Interchange 44 and 22 to obtain the list
22, 33, 11, 55, 77, 90, 40, 60, 99, 44, 88, 66
(Observe that the numbers 88 and 66 to the right of 44 are each greater than 44.)
• Beginning with 22, next scan the list in the opposite direction, from left to right, comparing each number with 44 and stopping
at the first number greater than 44. The number is 55.
• Interchange 44 and 55 to obtain the list
22, 33, 11, 44, 77, 90, 40, 60, 99, 55, 88, 66
(Observe that the numbers 22, 33 and 11 to the left of 44 are each less than 44.)
• Beginning this time with 55, now scan the list in the original direction, from right to left, until meeting the first number less
than 44. It is 40.
• Interchange 44 and 40 to obtain the list
22, 33, 11, 40, 77, 90, 44, 60, 99, 55, 88, 66
• (Again, the numbers to the right of 44 are each greater than 44.) Beginning with 40, scan the list from left to right. The
first number greater than 44 is 77.
• Interchange 44 and 77 to obtain the list
22, 33, 11, 40, 44, 90, 77, 60, 99, 55, 88, 66
• (Again, the numbers to the left of 44 are each less than 44.) Beginning with 77, scan the list from right to left seeking a
number less than 44. We do not meet such a number before meeting 44.
• This means all numbers have been scanned and compared with 44.
• Furthermore, all numbers less than 44 now form the sublist of numbers to the left of 44, and all numbers greater than 44
now form the sublist of numbers to the right of 44, as shown below:
22, 33, 11, 40, 44, 90, 77, 60, 99, 55, 88, 66
First sublist Second sublist
• Thus 44 is correctly placed in its final position
• The above reduction step is repeated with each sublist containing 2 or
more elements.
• by using two stacks, called LOWER and UPPER, to temporarily "hold" such
sublists.
• That is, the addresses of the first and last elements of each sublist, called
its boundary values, are pushed onto the stacks LOWER and UPPER
Example 39
Consider the above list A with n = 12 elements. The algorithm begins by pushing the boundary values 1 and 12 of A
onto the stacks to yield
LOWER: 1 UPPER: 12
In order to apply the reduction step, the algorithm first removes the top values 1 and 12 from the stacks, leaving
LOWER: (empty) UPPER: (empty)
The reduction step, as executed above, finally places the first element, 44, in A[5]. Accordingly, the algorithm pushes
the boundary values 1 and 4 of the first sublist and the boundary values 6 and 12 of the second sublist onto the stacks to yield
LOWER: 1, 6 UPPER: 4, 12
In order to apply the reduction step again, the algorithm removes the top values, 6 and 12, from the
stacks, leaving
LOWER: 1 UPPER: 4
and then applies the reduction step to the corresponding sublist A[6], A[7], . . . , A[12].
The reduction step changes this list as in the following figure.. Observe that the second sublist has
only one element. Accordingly, the algorithm pushes only the boundary values 6 and 10 of the first sublist
onto the stacks to yield
LOWER: 1, 6 UPPER: 4, 10
and so on.
The algorithm ends when the stacks do not contain any sublist to be processed by the reduction step.
• Observe that the first procedure evaluates N! using an iterative loop process.
• The second procedure, on the other hand, is a recursive procedure, since it contains a call to itself.
• Suppose P is a recursive procedure.
• During the running of an algorithm or a program which contains P, we associate a level number with
each given execution of procedure P as follows.
• The original execution of procedure P is assigned level 1; and each time procedure P is executed
because of a recursive call, its level is 1 more than the level of the execution that has made the recursive
call.
• In Example 6.4.8, Step 1 belongs to level 1. Hence Step 2 belongs to level 2, Step 3 to level 3, Step 4 to
level 4 and Step 5 to level 5. On the other hand, Step 6 belongs to level 4, since it is the result of a return
from level 5.
• In other words, Step 6 and Step 4 belong to the same level of execution. Similarly, Step 7 belongs to
level 3, Step 8 to level 2, and the final step, Step 9, to the original level 1.
Fibonacci Sequence
The celebrated Fibonacci sequence (usually denoted by F0, F1, F2, . . .) is as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .
That is, F0 = 0 and F1 = 1 and each succeeding term is the sum of the two
preceding terms. For example, the next two terms of the sequence are
34 + 55 = 89 and 55 + 89 = 144
Definition (Fibonacci Sequence)
(a) If n = 0 or n = 1, then Fn = n.
(b) If n > 1, then Fn = Fn-2 + Fn-1
This is another example of a recursive definition, since the definition refers to itself
when it uses Fn-2 and Fn-1. Here (a) the base values are 0 and 1, and (b) the value of
Fn. is defined in terms of smaller values of n which are closer to the base values.
Accordingly, this function is well-defined.
Procedure 5: FIBONACCI(FIB, N)
This procedure calculates FN and returns the value in the first parameter FIB.
1. If N = 0 or N = 1, then: Set FIB:= N, and Return.
2. Call FIBONACCI(FIBA, N - 2).
3. Call FIBONACCI(FIBB, N - 1).
4. Set FIB:= FIBA + FIBB.
5. Return.
This is another example of a recursive procedure, since the procedure contains a call to itself. In fact, this
procedure contains two calls to itself. We note that one can also write an iterative procedure to calculate Fn. which
does not use recursion.
6.4.7 Towers of Hanoi
• Suppose three pegs, labeled A, B and C, are given, and suppose on peg A
there are placed a finite number n of disks with decreasing size.
• The object of the game is to move the disks from peg A to peg C using peg B
as an auxiliary.
• The rules of the game are as follows:
(a) Only one disk may be moved at a time. Specifically, only the top disk on
any peg may be moved to any other peg.
(b) At no time can a larger disk be placed on a smaller disk.
Initial setup of Towers of Hanoi with n=6
• Sometimes we will write X Y to denote the instruction "Move top disk from peg X to peg Y," where X and Y may be any of the three pegs.
• The solution to the Towers of Hanoi problem for n=3 appears in the following figure. Observe that it consists of the following seven moves:
n = 3: Move top disk from peg A to peg C.
Move top disk from peg A to peg B.
Move top disk from peg C to peg B.
Move top disk from peg A to peg C.
Move top disk, from peg B to peg A.
Move top disk from peg B to peg C.
Move top disk from peg A to peg C.
A B C A B C A B C A B C
Rather than finding a separate solution for each n , we use the technique of recursion to develop a general solution. First we observe that the solution to the Towers of Hanoi problem forn > 1 disks may be reduced to the following subproblems:
(1) Move the top n - 1 disks from peg B
(2) Move the top disk from peg A to peg C. A C.
(3) Move the top n - 1 disks from peg B to peg C.
This reduction is illustrated in the following figure for n = 6. That is, first we move the top five disks from peg A
to peg B, then we move the large disk from peg A to peg C, and then we move the top five disks from peg B to peg C.
A B C A B C
(a) Initial n=6 (b) Move top five disks from peg A to
peg B
A B C A B C
(c) Move top disk from peg A to peg C (d) Move top five disks from peg B to peg C
Let us now introduce the general notation
TOWER(N, BEG, AUX, END)
to denote a procedure which moves the top n disks from the initial peg BEG to the final peg END using the peg AUX as an auxiliary. When n = 1, we have the following obvious solution:
TOWER(1, BEG, AUX, END) consists of the single instruction BEG END
Furthermore, as discussed above, when n>1, the solution may be reduced to the solution of the following three subproblems:
(1) TOWER(N-1, BEG, END, AUX)
FRONT: = FRONT + 1
• Similarly, whenever an element is added to the queue, the value of REAR is increased by 1; this can be implemented by the assignment
REAR:= REAR + 1
• Suppose we want to insert an element ITEM into a queue at the time the queue does occupy the last part of the array, i.e., when REAR = N.
• One way to do this is to simply move the entire queue to the beginning of the array, changing FRONT and REAR accordingly, and then inserting ITEM as above.
• This procedure may be very expensive. The procedure we adopt is to assume that the array QUEUE is circular, that is, that QUEUE[1] comes after QUEUE[N] in the array. With this assumption, we insert ITEM into the queue by assigning ITEM to QUEUE[l]. Specifically, instead of increasing REAR to N+1, we reset REAR=1 and then assign
QUEUE[REAR]: = ITEM
• Similarly, if FRONT=N and an element of QUEUE is deleted, we reset FRONT=1 instead of increasing FRONT to N+1.
QUEUE
(c)
(d) D and then E inserted: FRONT: 2
B C D E
REAR: 5
1 2 3 4 5
(d)
(e) B and C deleted: FRONT: 4 D E
REAR: 5
1 2 3 4 5
(e)
(f) F is inserted: FRONT: 4
F D E
REAR: 1
1 2 3 4 5
(f)
(g) D deleted: FRONT: 5
F E
REAR: 1
1 2 3 4 5
(g)
(h) G and then H inserted: FRONT: 5 F G H E
REAR: 3
1 2 3 4 5
(h)
(i) E deleted: FRONT: 1 F G H
REAR: 3
1 2 3 4 5
(i)
(j) F deleted: FRONT: 2
G H
REAR: 3
1 2 3 4 5
(j)
(k) K inserted: FRONT: 2
G H K
REAR: 4
1 2 3 4 5
(k)
(l) G and H deleted: FRONT: 4
K
REAR: 4
1 2 3 4 5
(l)
(m) K deleted, QUEUE empty: FRONT: 0
REAR: 0
1 2 3 4 5
(m)
Procedure 7: QINSERT(QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue.
1 . [Queue already filled?]
If FRONT = 1 and REAR = N, or if FRONT = REAR+1, then:
Write: OVERFLOW, and Return.
2. [Find new value of REAR.]
If FRONT:= NULL, then: [Queue initially empty.]
Set FRONT := 1 and REAR := 1.
Else if REAR= N, then:
Set REAR := 1.
Else:
Set REAR := REAR + 1.
[End of If structure.]
3. Set QUEUE[REAR] := ITEM. [This inserts new element.]
4. Return.
Procedure 8: QDELETE(QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to the variable ITEM.
1. [Queue already empty?]
If FRONT : = NULL, then: Write: UNDERFLOW, and Return.
2. Set ITEM := QUEUE[FRONT]
3. [Find new value of FRONT.]
If FRONT= REAR, then: [ Queue has only one element to st art.]
Set FRONT := NULL. and REAR := NULL.
Else if FRONT = N, then:
Set FRONT:= 1.
Else:
Set FRONT:= FRONT+ 1.
[End of If structure.]
4. Return.
6.4.9 DEQUES
• A deque (pronounced either "deck" or "dequeue") is a linear list in which elements can be added or removed at either end but not in the middle.
• double-ended queue.
• circular array DEQUE with pointers LEFT and RIGHT, which point to the two ends of the deque.
• assume that the elements extend from the left end to the right end in the array.
• "circular" comes from the fact that we assume that DEQUE[1] comes after DEQUE[N] in the array. The following figure pictures two deques, each with 4 elements maintained in an array with N=8 memory locations. The condition LEFT = NULL will be used to indicate that a deque is empty.
• There are two variations of a deque
input-restricted deque
output-restricted deque
• which are intermediate between a deque and a queue.
• input-restricted deque is a deque which allows insertions at only one end of the list but allows deletions at both ends of the list
• an output-restricted deque is a deque which allows deletions at only one end of the list but allows insertions at both ends of the list.
QUEUE
(a)
QUEUE
LEFT: 7
YYY ZZZ WWW XXX
RIGHT: 2
1 2 3 4 5 6 7 8
(b)
6.4.10 Priority Queues
• A priority queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules:
(1) An element of higher priority is processed before any element of lower priority.
(2) Two elements with the same priority are processed according to the order in which they were added to the queue.
• A prototype of a priority queue is a timesharing system: programs of high priority are processed first, and programs with the same priority form a standard queue.
• There are various ways of maintaining a priority queue in memory.
• We discuss two of them here: one uses a one-way list, and the other uses multiple queues.
• The ease or difficulty in adding elements to or deleting them from a priority queue clearly depends on the representation that one chooses.
One-Way List Representation of a Priority Queue
• One way to maintain a priority queue in memory is by means of a one-way list, as follows:
(a) Each node in the list will contain three items of information: an information field INFO, a priority number PRN and a link number LINK.
(b) A node X precedes a node Y in the list (1) when X has higher priority than Y or (2) when both have the same priority but X was added to the list before Y. This means that the order in the one-way list corresponds to the order of the priority queue.
• Priority numbers will operate in the usual way: the lower the priority number, the higher the priority.
Example 43
• figure (a) shows a schematic diagram of a priority queue with 7 elements. The diagram does not tell us whether BBB was added to the list before or after DDD. On the other hand, the
diagram does tell us that BBB was inserted before CCC, because BBB and CCC have the same priority number and BBB appears before CCC in the list.
• figure (b) shows the way the priority queue may appear in memory using linear arrays INFO, PRN and LINK.
(a)
INFO PRN LINK
1 BBB 2 6
START 5
2 7
3 DDD 4 4
AVAIL 2
4 EEE 4 9
5 AAA 1 1
6 CCC 2 3
7 10
8 GGG 5 0
9 FFF 4 8
10 11
11 12
12 0
(b)
Algorithm 13: This algorithm deletes and processes the first element in a priority queue which appears in memory as a one-way list.
1. Set ITEM:= INFO[START]. [This saves the data in the first node.]
2. Delete first node from the list.
3. Process ITEM.
4. Exit.
Adding an element to our priority queue is much more complicated than deleting an element from the queue, because we need to find the correct place to insert the element.
Algorithm 14: This algorithm adds an ITEM, with priority number N to a priority queue which is maintained in memory as a one-way list.
(a) Traverse the one-way list until finding a node X whose priority number exceeds N. Insert ITEM in front of node X.
(b) If no such node is found, insert ITEM as the last element of the list.
The above insertion algorithm may be pictured as a weighted object "sinking" through layers of elements until it meets an element with a heavier weight.
Example 44
• Consider the priority queue in figure (a).
• Suppose an item XXX with priority number 2 is to be inserted into the queue.
• We traverse the list, comparing priority numbers.
• Observe that DDD is the first element in the list whose priority number exceeds that of XXX.
• Hence XXX is inserted in the list in front of DDD.
• Observe that XXX comes after BBB and CCC, which have the same priority as XXX.
• Suppose now that an element is to be deleted from the queue.
• It will be AAA, the first element in the list. Assuming no other insertions, the next element to be deleted will be BBB, then CCC, then XXX, and so on.
START
XXX 2