###########################
Cpsc331 Lecture 1.
Material relevant to the very
start of Chapter 3
##########################
What is a Data Structure?
First of all, what we are going
to be talking about is a data-items storage structure, but
it is convenient to abbreviate the name to just data structure.
Unfortunately, this shorter term data
structure can now be confused with the idea of the structure of data, for the term is sometime used with that meaning
in mind.
Data Structure Definition:
A data-items storage structure,
or data
structure,
is an organized arrangement of storage
receptacles (in a computer memory, or on disk, or on plain paper, etc) for
storing data items.
A data structure is thus a container
for data, and can be empty, full, or partly full.
When we say a data structure is organized (that is, has multiple
organs), we mean that it may be made up of substructures, each of which is also
a data structure.
Many different kinds of arrangement of the storage
receptacles are possible, so that there are many kinds of data structure.
***An array in main memory is a well-known data
structure example, and can be empty or
full.
***An array of arrays is another data structure,
this one containing sub data structures
(the inner arrays).
***A computer file on disk is another data structure,
containing records as sub data
structures.
###############################
Data Structure Analogies
A. We can have a personal
items structure (like a lady's purse) as an arrangement of storage
receptacles for personal items. Many arrangements (i.e. purse designs) are
possible. Thus a lady's purse holds personal items, just as a data structure holds data items.
A. We can have a food
items structure (or supermarket structure), as an organized arrangement
of receptacles for storing food items, organized into shelving units, display
cases, and refrigerated units. Many arrangements are possible (supermarket
structure design).
A
(supermarket) food items structure is thus a container for food items, and can
be empty, full, or partly full. It is an organized structure because, for
example, a shelving unit will be a food items substructure within the overall
food items structure, and will itself have substructures, such as individual
shelves.
#############################
Data structures versus Data
items
Always be careful distinguish
between a data structure and the data items it contains.
For example, we could copy the
contents of an array into a stack, and also into a queue. Three different data
structures could now be holding an identical set of data items.
Conversely, over the execution time period of
a program, an array could hold three different sets of data. One data
structure, three sets of data.
Analogously, a lady would never confuse
the personal items in her purse with the purse itself.
################################
Efficient Data Structures
A good data structure is an efficient
data structure.
An efficient data structure
1. Allows (close to) the fastest possible access
to data items, for the purposes intended
2. Consumes (close to) the least possible storage space
Analogously, supermarkets are
very good and very visible examples of efficient structures for food items. You
have fast access, and they make optimum use of the floor space.
######################################
Efficient programs and algorithms
Most importantly:
An efficient data structure will
permit efficient programs and algorithms.
An efficient program or algorithm
is one where:
(a) the task is carried out in
the least number of possible steps, and therefore in the least time
(a) the task is carried out with
the least possible use of resources, particularly storage space
resources
SO:
EFFICIENT DATA STRUCTURES
ARE A REQUIREMENT FOR
EFFICIENT PROGRAMS OR ALGORITHMS
###########################################
Algorithm:
Definition: A stepwise procedure
for performing a task in a finite amount of time, that is, a useful sequence of
processing steps.
For any given task there will be
many possible algorithms, some more efficient, i.e. requiring fewer steps and
less space for data structures, than others. For example, there are many different
algorithms for sorting an array of data items.
[Primarily, though, we are
interested in the number of steps in an algorithm, since this will be directly
proportional to the Running Time on a computer.]
Analogously, there are many
different ways (algorithms)
for cutting the grass on a lawn
with a lawn mower, e,g. turn-in-spiral method, up-turnright-down method, up-back-turnright
method, etc., some more efficient (less distance traveled) than others.
##########################################
TRANSIENT VERSUS
PERMANENT DATA STRUCTURES
A data structure can be
(a) transient
i.e. it is created when a program starts
and is destroyed when the
program ends.
Most data structures in main
memory are
transient, for example, an array
of data.
(b) Permanent
i.e. it already
exists when a program starts
and is preserved when the
program ends.
Most data structures on disk are
permanent, for example, a file of data, or
a cross-linked data file
collection (a database).
In this course we are mostly concerned with the
transient data structures common in main memory.
[ Other courses deal with files,
and with advanced data structures like databases.]
##################################
DATA
[STRUCTURE] TYPES
A data structure type, called a data type, is an abstraction of,
that is, what is common to all members of, a set of data structures of the same
type. Each specific data structure is then an instance of the general data
structure type.
In
a program, such as a C, Pascal, or Java
program, a data structure type is used as a template for the manufacture of
specific data structures of that type, (by allocating the right number and size
of memory receptacles).
Java Data Structure instances
public class Sailor {
String name;
String position;
double
salary;
int seasSailed;
} // data structure type specified
class executionClass
{
public static void main (String[] args ) {
Sailor s1, s2;
// two record data structure
// instances
created.
s1.salary = 3567.72;
. . .
s2.seasSailed = 7;
…
}}
##################################
ABSTRACT DATA [STRUCTURE] TYPE (ADT)
An abstract data structure type, abbreviated to abstract data type, or
to the acronym ADT, is an abstraction of, that is, what is common to members
of, a set of data structures of the same type, plus a set of procedures (or methods) that
can be used to manipulate the contents of any instance of a data structure of
that type. Each specific ADT instance will have the use of
the associated set of procedures, and is an instance of the abstract data type.
In
a Java program, the implementation of an ADT is called a class, and is used as a
template for the manufacture of specific ADT instances or class instances, (by
allocating the right number and size of memory receptables).
An ADT instance is still primarily
a container
for data items, and if access to the data items is via the associated
set of procedures only, the data items are said to be encapsulated.
Java ADT (or class)
instances
public class Sailor {
String name;
String position;
double salary;
int seasSailed;
Sailor (. . .) { . . . . .}; //constructor
public void upSalaryBy
(double raise) { . . . . .};
public int getSeasSailed() {. . . . .};
public void
promoteTo (String newposition)
{ . . .};
etc
}
//ADT template or class specified above
//ADT or class instances s1, s2 created
and used below
public class executionClass
{
public static void main (String[] args ) {
Sailor
s1, s2; // two ADT
// instances
created.
s1.salary = 3567.72;
. . .
s1.upSalaryBy (105.10) ; //new salary
3672.82
. . .
s2.seasSailed = 7;
…
System.out.println
(s2.getSeasSailed);
}}
Java ADT or class
instances with encapsulation of the data items
public class Sailor {
private String name;
private String position;
private double salary;
private int oceansSailed;
Sailor (. .
.) { . . . . .}; //constructor
public void upSalaryBy
(double raise) { . . . . .};
public int getSeasSailed() {. . . . .};
public
void promoteTo (String
newposition) { . . .};
etc
}
/* ADT template or class specified
above
ADT or class instances s1, s2
created and
attempted used below */
public class executionClass
{
public static void main (String[] args ) {
Sailor
s1, s2; // two ADT
// instances
created.
s1.salary = 3567.72; //illegal--encapsulated
. . .
s1.upSalaryBy (105.10) ;
. . .
s2.seasSailed = 7;
//illegal--encapsulated
…
System.out.println (s2.getSeasSailed); }}
Material Relevant to around page
103 in Chapter 3 follows.
Estimating steps in a program
Example
public class ArrayMaxProgram
{
public static void main (String[] args ) {
int[] num =
{ 10, 15, 3, 5, 56, 107, 22, 16, 85};
int returnedMax;
long
startTime, endTime;
startTime = System.currentTimeMillies();
//returns
about one trillion
returnedMax = arrayMax (num
n, n);
endTime = System.currentTimeMillies();
System.out.println ("elapsed time : "
+
(endTime - startTime)
+ " milliseconds" );
System.outprintln ("Max value: " + returnedMax);
}
/* *
* * *
* * * */
// returns maximum value in array of n elements
static int arrayMax ( int[] A,
int n)
{
max =
A[0]; // 1
time, 2 steps
for ( int i = 1; i < n; i++
) /* 1
time; 2 steps
n times; n steps
( n-1)
times 2(n-1) steps (NOTE: respectively.) */
{
if ( max < A[i]
) / / (n -1) times 2(n -1) steps
max
= A[i];
}; /* worst case:( n-1) times 2(n-1)
steps
best case:
0 times */ 0 steps
return max; } // 1 time 1 step ???
//worst case TOTAL 7n -1
steps
S = f(n) = 7n -1 steps
}
[More About this in Lecture 2]
###############################