Objectives Objectives: Collections Collections
Objectives Objectives: Collections Collections
Define the concepts and terminology related to
collections
Explore the basic structure of the Java Collections
API (Application Program Interface)
Discuss the abstract design of collections
Define a bag collection
Review arrays using Java.
Use a bag collection to solve a problem
Examine an array implementation of a bag ADT
Measuring an Algorithm's Efficiency
2
ADTs and Data Structures Collections
An Abstract Data Type describes a collection A collection is a group of items that we want to
of data items and the associated operations treat as a conceptual unit.
that can be applied to that collection of items. Collections are represented by ADT’s and
implemented with Data Structures
A data structure is the implementation of an Common types of collections that we will learn
ADT. about:
An ADT defines a concept of what a particular Arrays, Lists, Stacks, Queues, Heaps, Trees, graphs,
collection of elements is maps, sets, bags…
Organizing data into collections plays an
a Data Structure tells us how we are going to important role in almost all non‐trivial programs.
represent that concept in our program. A container is a class that implements a collection
A collection hides the details of the
There is no 1 best implementation implementation
3 4
Collection Categories Collection Categories
Linear Graph
arrays undirected graph E1 E2
list E1 E2 E3 directed graph E3
stack
queue E4 E5
Hierarchical E2 Unordered E1 E2
binary tree set
E1 E3 E3
general tree bag
heap map (table) E4 E5
5 6
1
Common Operations Operations (cont.)
Search and retrieval Traversal
Search a collection for a given item or for an item Visit each item in a collection ( in some specific
at a given position. order).
Removal Test for Equality
Delete a given item or an item at a given position.
Delete a given item or an item at a given position Test a collection of items for equality.
Test a collection of items for equality
Insertion Size of a collection
Add an item to a collection, usually at some Determine the number of items in a collection.
particular position Cloning
Replacement Make a copy of an entire collection.
Combination of removal and insertion
7 8
A Bag Collection ADT of the Bag Collection
A bag is an Operation Description
unordered add Add and element
addAll Add all the elements to another bag
collection that
removeRandom Remove a random element
allows duplicates. remove Remove a specified element
nonlinear union Combine two Bags into one
No relationship to contains Does the element exist
one another equals Determine if two bags are identical
To find a particular isEmpty Is the Bag empty
element, you will size The number of elements
have to search toString String representation of the Bag
9 10
Arrays
An array is an ordered list of values
The entire array Each value has a numeric index
has a single name
0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 95 87 81 74 91
An array of size N is indexed from zero to N‐1
This array holds 10 values that are indexed
from 0 to 9
11 12
2
Arrays Arrays
A particular value in an array is referenced using the An array stores multiple values of the same
array name followed by the index in brackets
type
For example, the expression That type can be primitive types or objects
scores[2] ‐ refers to the value 94 (which is the 3rd value in
the array) Therefore, we can create an array of integers, or an
array of characters, or an array of String objects,
array of characters or an array of String objects
That expression represents a place to store a single etc.
integer, and can be used wherever an integer variable In Java, the array itself is an object.
can.
Therefore, the name of the array is a object
For example, it can be assigned a value, printed, or reference variable, and the array itself is
used in a calculation. instantiated separately
13 14
Declaring Arrays Declaring Arrays
The scores array could be declared as follows: Some examples of array declarations:
int[] scores = new int[10];
Note that the type of the array does not float[] prices = new float[500];
specify its size, but each object of that type
has a specific size boolean[]
[] flags;
g
flags = new boolean[20];
The type of the variable scores is int[] (an
array of integers) int size = 50;
It is set to a new array object that can hold 10 char[] codes = new char[size];
integers
15 16
Example Bounds Checking
// declare an integer array of size 10
Once an array is created, it has a fixed size
int[] list = new int[10];
An index used in an array reference must
// Initialize the array values to the index * 10 specify a valid element
for (int index = 0; index < 10; index++) {
list[index] = index * 10;
That is, the index value must be in bounds (0
} to N )
to N‐1)
list[5] = 999; // change one array value The Java interpreter will throw an exception if
// print out the array index: value
an array index is out of bounds
for (int index = 0; index < 10; index++) { This is called automatic bounds checking
System.out.println(index + ": " + list[index]);
}
17 18
3
Bounds Checking Bounds Checking
If the array can hold 100 values, it can only be Each array object has a public constant called
indexed using the numbers 0 to 99 length that stores the size of the array
If count has the value 100, then the reference It is referenced using the array name (just like
will cause an ArrayOutOfBoundsException: any other object):
System.out.println (codes[count]);
scores.length
It’s common to introduce off‐by‐one errors
when using arrays Note that length holds the number of
for (int index=0; index <= 100; index++) elements, not the largest index
codes[index] = index*50;
20
Array Declarations Revisited Initializer Lists
The brackets of the array type can be An initializer list can be used to instantiate and
associated with the element type or with the initialize an array in one step
name of the array The values are delimited by braces and
Therefore, the following declarations are
separated by commas
equivalent: Examples:
p
Initializer Lists Example
Note that when an initializer list is used: int[] primes = {2, 3, 5, 7, 11, 13, 17, 19};
the new operator is not used
System.out.println ("Array length: "
no size value is specified
+ primes.length);
The size of the array is determined by the
number of items in the initializer list System.out.println ("The first few prime “
An initializer list can only be used in the + “numbers are:");
declaration of an array
for (int i = 0; i < primes.length; i++)
System.out.print (primes[i] + " ");
23 24
4
Arrays as Parameters Arrays of Objects
An entire array can be passed to a method as a The elements of an array can be object
parameter references
Like any other object, the reference to the The following declaration reserves space to
array is passed, making the formal and actual store 25 references to String objects
parameters aliases of each other String[]
i [] words
d = new String[25];
i [25]
Changing an array element in the method It does NOT create the String objects
changes the original themselves
An array element can be passed to a method Each object stored in an array must be
as well, and will follow the parameter passing instantiated separately
rules of that element's type
25 26
Example Arrays of Objects
String[] grades = {"A+", "A", "A-", "B+",
"B", "B-", "C+", "C", "D", "F"};
Objects can have arrays as instance variables
Therefore, fairly complex structures can be
int[] cutoff = {95, 90, 85, 80, 75, 70, 65, created simply with arrays and objects
60, 50, 0};
The software designer must carefully
for (int level = 0; determine an organi ation of data and objects
determine an organization of data and objects
level < cutoff.length; that makes sense for the situation
level++) {
System.out.print(grades[level]);
System.out.println("\t" + cutoff[level]);
}
27 28
Two‐‐Dimensional Arrays
Two Example
A one‐dimensional array stores a simple list of char[][] table = new char[5][5];
values
A two‐dimensional array can be thought of as for (int row=0;
a table of values, with rows and columns row < table.length;
row++) {
A two‐dimensional array element is
y
for (int col=0;
referenced using two index values
col < table[row].length;
To be precise, a two‐dimensional array in Java
col++) {
is an array of arrays
table[row][col] = '*';
Example: }
int[][] table = new int[5][10]; }
29 30
5
Multidimensional Arrays
An array can have as many dimensions as
needed, creating a multidimensional array
Each dimension subdivides the previous one
into the specified number of elements
Each array dimension has its own length
Each arra dimension has its o n length
constant
Because each dimension is an array of array
references, the arrays within one dimension
could be of different lengths
31 32
What is an algorithm? Measuring Efficiency
Formal definition: The goal of efficiency is to measure and
A well‐ordered collection of unambiguous and compare algorithms and data structures
effectively computable operations that, when We cannot use just execution speed because
executed, produces a result and halts in a finite
Different machines have different running times
amount of time.
Some running times are so long that it is
Or, more informally: impractical to check them
A step‐by‐step set of instructions for
Algorithms usually take more time/space as
accomplishing some task.
the size of the problem grows
33 34
Analysis of Algorithms Algorithm Analysis
Analysis is based on Provide a formula that predicts the running
Time: the amount of time to execute the time and memory usage of an algorithm
algorithm. There is no real bounds on time. There O(n2), O(log n), etc.
is no deadline (except in real‐time programming)
Use formal, mathematical techniques to
Space: we are limited to the amount of memory
p y provide platform independent assessments
provide platform‐independent assessments
that is available.
Algorithm complexity is a big problem in
Time‐Space Tradeoff
software engineering
seldom we are fortunate to get an algorithm that is
both, we usually have to choose one or the other
when we program.
35 36
6
Algorithm Analysis A Choice of Algorithms
We can control complexity by: It is possible to come up with several different
breaking software into smaller, simpler pieces algorithms to solve the same problem.
making sure that each piece knows only what But which one is the "best"?
other pieces do, not how they do it Most efficient: Time vs. Space?
abstraction, information hiding, encapsulation, Easiest to maintain?
coupling How do we measure time efficiency?
Benefits of this approach: Running time? Machine dependent!
ease of use Number of steps?
ease of modification We cannot compute actual time for an
algorithm so we measure worst‐case time
37 38
Measuring Algorithm Efficiency Measuring Algorithm Efficiency
Three algorithms for computing 1 + 2 + … n The number of operations required by the
for an integer n > 0 algorithms
Algorithm A
g for (int i = 1; i <= n; i++) Alg. A Alg. B Alg. C
sum = sum + i;
i
thru the loop n + 1 1 + n (n + 1) / 2 1
Algorithm B for (int i = 1; i <= n; i++)
for (int j = 1; j < = i; j++) Additions n 1+n (n + 1) / 2 1
sum = sum + i;
Multiplications 1
Algorithm C sum = n * (n+1) / 2; Divisions 1
Total operations 2n + 1 n2 + n + 1 4
39 40
Big Oh Notation Picturing Efficiency
To say "Algorithm A has a worst‐case time The time to process one million items by
requirement proportional to n" algorithms of various orders at the rate of
We say A is O(n) one million operations per second.
Read "Big Oh of n"
For the other two algorithms G
Growth Rate
th R t f(106)/10
f( )/ 6
Algorithm B is O(n2) log n 0.0000199 seconds
Algorithm C is O(1) n 1 second
n log n 19.9 seconds
n2 11.6 days
n3 31 709.8 years
2n 10301016 years
41 42
7
Comments on Efficiency Comments on Efficiency
We are not interested in knowing the exact A programmer can use O(n2), O(n3) or O(2n) as
number of steps the algorithm performs. long as the problem size is small
Interested in knowing how the number of At one million operations per second it would
steps grows with increased input size! take 1 second …
Why? For a problem size of 1000 with O(n2)
Given large enough input, the algorithm with For a problem size of 1000 with O(n3)
faster growth will execute more steps. For a problem size of 20 with O(2n)
O(...), measures how the number of steps
grows with input size n.
43 44
Rules For Finding
Rules For Finding Magnitude
Magnitude Rules For Finding Magnitude
Rules For Finding Magnitude
Rule 1: loops Rule 3: consecutive statements
The running time of loops is at most the running Add each statement
time of the statements inside the loop, including Rule 4: if‐then‐else
the test * iterations
The running time is never more than the running
Rule 2: Nested Loops time of the test plus the larger of the running times
The total running time of a statement inside a of either portion of the condition. (test + if , where
group of nested loops is the running time of the if > else OR test + else)
statement multiplied by the product of the size of
the loops (iterations * iterations)
45 46
i3
Simple Example =
Simple Example = Big‐‐O Notation
Big
Line 1 int sum ( int n) { We simplify the f(n ) O(f(n ))
Line 2 int partial_sum = 0;
function by: 0.3n2 + 20n + 512 O(n2)
Line 3 for ( int i = 1; i <=n; i++)
ignoring all constant 0.0001n4 + 10000n2 O(n4)
Line 4 partial_sum += i * i * i;
coefficients
Line 55 return partial_sum;
p ;
Line 6 } ignoring all but the
i i ll b h 3n + n
2 O(3n)
O(
dominant term 42log2n O(log2n)
Line 2: one unit [1]
the dominant
Line 3: initialization = 1; testing = n+1, and incrementing = n [2n+2] term is the one 7n log10n + 2n – 12 O(n)
Line 4: three units per time executed (2 multiplications, 1 addition) that grows fastest 42 O(1)
and executed n times [3n units] when n grows
Line 5: one unit [1]
Total: 1 + 2n+2 + 3n + 1 = 5n+4 O (n)
47 48
8
Complexity Classes Analysis of Array Bag ADT
When determining the Complexity O-notation addAll(BagADT bag) add(Object element):
From least to
big‐Oh time of a Class
problem, we try to: O(n) If the array is not full:
Constant O(1)
make the bound as contains(Object O(1)
log log n O(log log n )
tight as possible element) If the array is full: O(n)
Logarithmic O(log n )
make the function as
o most complex
simple as possible Linear O(n ) O(n) remove(Object
(Obj
n log n O(n log n )
union(BagADT bag) element)
In practice, this leads to Quadratic O(n2) O(n)
O(n+m)
only a handful of big‐ Cubic O(n3)
removeRandom()
Oh expressions, so we Exponential O(2n), O(3n) equals(BagADT bag)
give them names: O(1)
O(n)
49
But can you see WHY?
WHY? 50