You are on page 1of 288

Source: https://en.wikipedia.org/wiki/Insertion_sort#/media/File:Insertion_sort.

gif
Source: http://math.hws.edu/eck/cs124/javanotes3/c8/fig4.gif
Insertion Sort
for (int i = 1; i < n; i++)
{// insert a[i] into a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
Complexity
Space/Memory
Time
Count a particular operation
Count number of steps
Asymptotic complexity
Comparison Count
for (int i = 1; i < n; i++)
{// insert a[i] into a[0:i-1]
int t = a[i];
int j;
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];
a[j + 1] = t;
}
Comparison Count
Pick an instance characteristic … n
For insertion sort, pick n = number of
elements to be sorted.
Determine count as a function of this
instance characteristic.
Comparison Count
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];

How many comparisons are made?


Comparison Count
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];

number of compares depends on


a[]s and t as well as on i
Comparison Count
Worst-case count = maximum count
Best-case count = minimum count
Average count
Worst-Case Comparison Count
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];

a = [1, 2, 3, 4] and t = 0 => 4 compares


a = [1,2,3,…,i] and t = 0 => i compares
Worst-Case Comparison Count
for (int i = 1; i < n; i++)
for (j = i - 1; j >= 0 && t < a[j]; j--)
a[j + 1] = a[j];

total compares = 1 + 2 + 3 + … + (n-1)


= (n-1)n/2
Step Count
A step is an amount of computing that
does not depend on the instance
characteristic n

10 adds, 100 subtracts, 1000 multiplies


can all be counted as a single step
10n O(n), 100n O(n), etc.
n adds cannot be counted as 1 step
Step Count
Steps per execution
for (int i = 1; i < n; i++) 1
{// insert a[i] into a[0:i-1] 0
int t = a[i]; 1
int j; 0
for (j = i - 1; j >= 0 && t < a[j]; j--) 1
a[j + 1] = a[j]; 1
a[j + 1] = t; 1
} 0
Step Count
s/e isn’t always 0 or 1

x = sum(a[], n);

where n is the instance characteristic


has a s/e count of n (sum() is Program
1.30).
Step Count
s/e steps
for (int i = 1; i < n; i++) 1
{// insert a[i] into a[0:i-1] 0
int t = a[i]; 1
int j; 0
for (j = i - 1; j >= 0 && t < a[j]; j--) 1 i+ 1
a[j + 1] = a[j]; 1 i
a[j + 1] = t; 1
} 0
Step Count
step count for
for (int i = 1; i < n; i++)
is n

i=1
i=2

1+2+3+…+n = n(n-1)/2
Space Complexity
O(n) total
O(1) additional space

Source: https://en.wikipedia.org/wiki/Insertion_sort#/media/File:Insertion-sort-example-300px.gif
Asymptotic Complexity of
Insertion Sort
O(n2)
What does this mean?
Complexity of Insertion Sort
Time or number of operations does not
exceed c.n2 on any input of size n (n
suitably large)(c constant).
Actually, the worst-case time is Θ(n2)
and the best-case is Θ(n)
So, the worst-case time is expected to
quadruple each time n is doubled
Complexity of Insertion Sort
Is O(n2) too much time?
Is the algorithm practical?
Practical Complexities
109 instructions/second
2 3
n n nlogn n n
1000 1mic 10mic 1milli 1sec

10000 10mic 130mic 100milli 17min

106 1milli 20milli 17min 32years


Impractical Complexities
109 instructions/second
4 10 n
n n n 2
1000 17min 3.2 x 1013 3.2 x 10283
years years

10000 116 ??? ???


days

106 3 x 107 ?????? ??????


years
Faster Computer Vs Better
Algorithm

Algorithmic improvement more useful


than hardware improvement.

E.g. 2n to n3
To-do
Read:
– https://en.wikipedia.org/wiki/Big_O_notation
Do:
– Create GitHub (or similar) account
– Insert 0 into 1 2 3 4 5 – Count Steps
– Insert 10 into 1 2 3 4 5 – Count Steps
Implement (ungraded):
– Insertion Sort (C++/Java)
– Sort a sorted sequence/reversely sorted sequence
(n=1000,1000000), what’s the difference in
runtime?
Performance Measurement
Performance Analysis
Paper and pencil.

Don’t need a working computer


program or even a computer.
Some Uses Of Performance Analysis

•determine practicality of algorithm


•predict run time on large instance
•compare 2 algorithms that have different
asymptotic complexity
e.g., O(n) and O(n2)
Reminder
2 3
n n nlogn n n
1000 1mic 10mic 1milli 1sec

10000 10mic 130mic 100milli 17min

106 1milli 20milli 17min 32years


Limitations of Analysis
Doesn’t account for constant
factors.

but constant factor may dominate


1000n vs n2
and we are interested only in
n < 1000
Limitations of Analysis
Modern computers have a
hierarchical memory organization
with different access time for
memory at different levels of the
hierarchy.
Memory Hierarchy

MAIN
L2
L1
ALU R
8-32 32KB ~512KB ~512MB
1C 2C 10C 100C
Limitations of Analysis

Our analysis doesn’t account for


this difference in memory access
times.

Programs that do more work may


take less time than those that do
less work.
Cache-aware and Cache-
oblivious algorithms*
 Cache-aware algorithms try to minimize
“cache misses”
– Example: Matrix multiplication
 Cache-oblivious algorithms try to take
advantage of cache without knowledge
of hardware details
Performance Measurement/Benchmarking

Measure actual time on an actual


computer.

What do we need?
Performance Measurement Needs
 programming language
e.g. C++/Java
 working program
Insertion Sort
 computer
 compiler and options to use
gcc –O2
Performance Measurement Needs
 data to use for measurement
worst-case data
best-case data
average-case data

 timing mechanism --- clock


Timing In C++
double clocksPerMillis =
double(CLOCKS_PER_SEC) / 1000;
// clock ticks per millisecond
clock_t startTime = clock();

// code to be timed comes here

double elapsedMillis = (clock() – startTime) /


clocksPerMillis;
// elapsed time in milliseconds
Shortcoming
Clock accuracy
assume 100 ticks

Repeat work many times to bring total


time to be >= 1000 ticks
More Accurate Timing
clock_t startTime = clock();
long numberOfRepetitions;
do {
numberOfRepetitions++;
doSomething();
} while (clock() - startTime < 1000)
double elapsedMillis = (clock()- startTime) /
clocksPerMillis;
double timeForCode =
elapsedMillis/numberOfRepetitions;
Bad Way To Time
do {
counter++;
startTime = clock();
doSomething();
elapsedTime += clock() - startTime;
} while (elapsedTime < 1000)
Accuracy
Now accuracy is 10%.

first reading may be just about to change to


startTime + 100

second reading (final value of clock())may have


just changed to finishTime

so finishTime - startTime is off by 100 ticks


Accuracy

first reading may have just changed to


startTime

second reading may be about to change to


finishTime + 100

so finishTime - startTime is off by 100 ticks


Accuracy
Examining remaining cases, we get

trueElapsedTime =
finishTime - startTime +- 100 ticks

To ensure 10% accuracy, require


elapsedTime = finishTime – startTime
>= 1000 ticks
Timing in Java
 Same semantics, slightly different syntax
long startTime = System.nanoTime();
//Code to be measured
System.out.println(
"Elapsed time: " + (System.nanoTime() - startTime));
or Use micro-benmarking frameworks
such as JMH
What Went Wrong?
clock_t startTime = clock();
long numberOfRepetitions;
do {
numberOfRepetitions++;
insertionSort(a, n);
} while (clock() - startTime < 1000)
double elapsedMillis = (clock()- startTime) /
clocksPerMillis;
double timeForCode =
elapsedMillis/numberOfRepetitions;
The Fix
clock_t startTime = clock();
long numberOfRepetitions;
do {
numberOfRepetitions++;
// put code to initialize a[] here
insertionSort(a, n)
} while (clock() - startTime < 1000)
double elapsedMillis = (clock()- startTime) /
clocksPerMillis;
Time Shared System
UNIX
time MyProgram
To-do
 1. C++:
– http://www.cplusplus.com/reference/ctime/clock/
 1’.Java (JMH):
– http://www.oracle.com/technetwork/articles/java/
architect-benchmarking-2266277.html
– http://nitschinger.at/Using-JMH-for-Java-
Microbenchmarking/
 2. Linux:
– http://stackoverflow.com/a/556411
Updates

-TAs and office hours announced


-DRC students: can take quizzes
after class (quizzes will remain
open for 1 hour after class)
-Waiting for VM
Updates

-When using git for homework and


projects, make sure it’s PRIVATE
Data Structures

data object

set or collection of instances

integer = {0, +1, -1, +2, -2, +3, -3, …}

daysOfWeek = {S,M,T,W,Th,F,Sa}
Data Object

instances may or may not be related

myDataObject = {apple, chair, 2, 5.2, red, green, Jack}


Data Structure
Data object +
relationships that exist among instances
and elements that comprise an instance

Among instances of integer


369 < 370
280 + 4 = 284
Data Structure
Among elements that comprise an instance

369
3 is more significant than 6
3 is immediately to the left of 6
9 is immediately to the right of 6
Data Structure

The relationships are usually specified by


specifying operations on one or more
instances.

add, subtract, predecessor, multiply


Linear (or Ordered) Lists

instances are of the form


(e0, e1, e2, …, en-1)

where ei denotes a list element


n >= 0 is finite
list size is n
Linear Lists
L = (e0, e1, e2, e3, …, en-1)

relationships
e0 is the zero’th (or front) element
en-1 is the last element
ei immediately precedes ei+1
Linear List Examples/Instances
Students in COP3530 =
(Jack, Jill, Abe, Henry, Mary, …, Judy)

Exams in COP3530 =
(exam1, exam2, exam3)

Days of Week = (S, M, T, W, Th, F, Sa)

Months = (Jan, Feb, Mar, Apr, …, Nov, Dec)


Linear List Operations—size()

determine list size

L = (a,b,c,d,e)

size = 5
Linear List Operations—get(theIndex)
get element with given index

L = (a,b,c,d,e)
get(0) = a
get(2) = c
get(4) = e
get(-1) = error
get(9) = error
Linear List Operations—
indexOf(theElement)

determine the index of an element

L = (a,b,d,b,a)
indexOf(d) = 2
indexOf(a) = 0
indexOf(z) = -1
Linear List Operations—
erase(theIndex)

Remove/delete element with given index

L = (a,b,c,d,e,f,g)
erase(2) removes c
and L becomes (a,b,d,e,f,g)

index of d,e,f, and g decrease by 1


Linear List Operations—
erase(theIndex)

Remove/delete element with given index

L = (a,b,c,d,e,f,g)

remove(-1) => error


remove(20) => error
Linear List Operations—
insert(theIndex, theElement)
add an element so that the new element has
a specified index

L = (a,b,c,d,e,f,g)

insert(0,h) => L = (h,a,b,c,d,e,f,g)


index of a,b,c,d,e,f, and g increase by 1
Linear List Operations—
insert(theIndex, theElement)

L = (a,b,c,d,e,f,g)

insert(2,h) => L = (a,b,h,c,d,e,f,g)


index of c,d,e,f, and g increase by 1
add(10,h) => error
add(-6,h) => error
Data Structure Specification
Language independent
Abstract Data Type
C++
Abstract Class
Linear List Abstract Data Type
AbstractDataType LinearList
{
instances
ordered finite collections of zero or more elements
operations
empty(): return true iff the list is empty, false otherwise
size(): return the list size (i.e., number of elements in the list)
get(index): return the indexth element of the list
indexO f(x): return the index of the first occurrence of x in
the list, return -1 if x is not in the list
erase(index): remove the indexth element,
elements with higher index have their index reduced by 1
insert(theIndex, x): insert x as the indexth element, elements
with theIndex >= index have their index increased by 1
output(): output the list elements from left to right
}
Linear List As An Abstract Class

An abstract class may include


constants, variables, abstract
methods, and nonabstract methods.
Linear List As C++ Abstract Class
template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& get(int theIndex) const = 0;
virtual int indexOf(const T& theElement)const = 0;
virtual void erase(int theIndex) = 0;
virtual void insert(int theIndex,
const T& theElement) = 0;
virtual void output(ostream& out) const = 0;
};
Extending A C++ Class
template<class T>
class arrayList : public linearList<T>
{
// code for all abstract methods of linearList must come here
}
To-do
Git:
– http://www.sbf5.com/~cduan/t
echnical/git/
– http://git-scm.com/book/en/v2
C++:
– http://www.cplusplus.com/doc
/tutorial/templates/
Data Representation Methods

array --- Chapter 5


linked --- Chapter 6
Linear List Array Representation
use a one-dimensional array element[]

a b c d e

0 1 2 3 4 5 6

L = (a, b, c, d, e)
Store element i of list in element[i].
Right To Left Mapping

e d c b a
Mapping That Skips Every Other
Position

a b c d e
Wrap Around Mapping

d e a b c
Representation Used In Text

a b c d e

0 1 2 3 4 5 6 listSize = 5

put element i of list in element[i]

use a variable listSize to record current


number of elements
Insert/Erase An Element
listSize = 5
a b c d e

insert(1,g)

listSize = 6
a g b c d e
Data Type Of Array element[]
Data type of list elements is unknown.

Define element[] to be of template type T.


Use template type T
Length of Array element[]
Don’t know how many elements will be in list.

Must pick an initial length and dynamically increase


as needed.

Use variable arrayLength to store current length of


array element[].
Increasing Array Length

Length of array element[] is 6.


a b c d e f

First create a new and larger array

T* newArray = new T[15];


Increasing Array Length
Now copy elements from old array to new one.

a b c d e f

a b c d e f

#include <algorithm>

copy(element, element + 6, newArray);


Increasing Array Length

Finally, delete old array and rename new array.


delete [] element;
element = newArray;
arrayLength = 15;
element[0]

a b c d e f
template<class T>
void changeLength1D(T*& a, int oldLength,
int newLength)
{
1 if (newLength < 0)
throw illegalParameterValue();

2 T* temp = new T[newLength];


// new array
3 int number = min(oldLength, newLength);
// number to copy
4 copy(a, a + number, temp);
5 delete [] a;
// deallocate old memory
6 a = temp;
}
How Big Should The New Array Be?

At least 1 more than current array length.

Cost of increasing array length when array is


full is Q(old length).

Cost of n insert operations done on an


initially empty linear list increases by Q(n2).
Space Complexity

element[6]
a b c d e f

newArray = new char[7];

space needed = 2 * newLength – 1


= 2 * maxListSize – 1
Array Doubling

Double the array length.


a b c d e f

newArray = new char[12];


a b c d e f

Time for n inserts goes up by Q(n).


Space needed = 1.5*newLength. (e.g. 1.5 * 12)

Space needed <= 3*maxListSize – 3


How Big Should The New Array Be?

Resizing by any constant factor


new length = c * old length
increases the cost of n inserts by Q(n).

Resizing by an additive constant increases


the cost of n add operations by Q(n2).
How Big Should The New Array Be?

Resizing by any constant factor


new length = c * old length
requires at most (1+c) * (maxListSize -1) space.

Resizing by an additive constant c requires


at most (maxListSize – 1) + (maxListSize – 1 + c)
= 2 * (maxListSize – 1) + c space.
What Does C++ Do?

STL class vector … c = 1.5

arrayList of text … c = 2
To-do
Practice:
– https://www.hackerrank.com/
challenges/cpp-input-and-
output
More on insertion sort:
– https://en.wikipedia.org/wiki/I
nsertion_sort
The Class arrayList

• General purpose implementation of linear lists.

• Unknown number of lists.


Create An Empty List
arrayLinear<int> a(100), b;
arrayList<double> c(10), d;

linearList<int> *d = new arrayList<int>(1000);


linearList<char> *f = new arrayList<char>(20);
Using A Linear List
cout << a.size() << endl;
a.insert(0, 2);
d->insert(0, 4);
a.output();
cout << a << endl;
a.erase(0);
if (a.empty()) a.insert(0, 5);
Array Of Linear Lists
linearList<int> * x[4];
x[0] = new arrayList<int>(20);
x[1] = new chain<int>();
x[2] = new chain<int>();
x[3] = new arrayList<int>(15);
for (int i = 0; i < 4; i++)
x[i].insert(0, i);
The Class arrayList
// include statements come here
using namespace std;
template<class T>
class arrayList : public linearList<T>
{
public:
// constructor, copy constructor and destructor
arrayList(int initialCapacity = 10);
arrayList(const arrayList<T>&);
~arrayList() {delete [] element;}
The Class arrayList
// ADT methods
bool empty() const {return listSize == 0;}
int size() const {return listSize;}
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;

// additional method
int capacity() const {return arrayLength;}
The Class arrayList
protected:
void checkIndex(int theIndex) const;
// throw illegalIndex if theIndex invalid
T* element; // 1D array to hold list elements
int arrayLength; // capacity of the 1D array
int listSize; // number of elements in list
};
A Constructor

template<class T>
arrayList<T>::arrayList(int initialCapacity)
{// Constructor.
if (initialCapacity < 1)
{ostringstream s;
s << "Initial capacity = "
<< initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
arrayLength = initialCapacity;
element = new T[arrayLength];
listSize = 0;
}
Copy Constructor

template<class T>
arrayList<T>::arrayList(const arrayList<T>&
theList)
{// Copy constructor.
arrayLength = theList.arrayLength;
listSize = theList.listSize;
element = new T[arrayLength];
copy(theList.element, theList.element +
listSize, element);
}
The Method checkIndex

template<class T>
void arrayList<T>::checkIndex(int theIndex) const
{// Verify that theIndex is between 0 and
// listSize - 1.
if (theIndex < 0 || theIndex >= listSize)
{ostringstream s;
s << "index = " << theIndex << " size = "
<< listSize;
throw illegalIndex(s.str());
}
}
The Method get

template<class T>
T& arrayList<T>::get(int theIndex) const
{// Return element whose index is theIndex.
// Throw illegalIndex exception if no such
// element.
checkIndex(theIndex);
return element[theIndex];
}
The Method indexOf
template<class T>
int arrayList<T>::indexOf(const T& theElement)
const
{// Return index of first occurrence of
theElement.
// search for theElement
int theIndex = (int) (find(element, element
+ listSize, theElement) - element);
// check if theElement was found
if (theIndex == listSize)
return -1; // not found
else return theIndex;
}
The Method erase

template<class T>
void arrayList<T>::erase(int theIndex)
{// Delete the element whose index is theIndex.
checkIndex(theIndex);

// valid index, shift elements with higher


// index
copy(element + theIndex + 1, element +
listSize,element + theIndex);

element[--listSize].~T(); // invoke destructor


}
The Method insert
template<class T>
void arrayList<T>::insert(int theIndex,
const T& theElement)
{// Insert theElement.
if (theIndex < 0 || theIndex > listSize)
{// invalid index
// code to throw an exception comes here
}

// valid index, make sure we have space


if (listSize == arrayLength)
{// no space, double capacity
changeLength1D(element, arrayLength,
2 * arrayLength);
arrayLength *= 2;
}
The Method insert

// shift elements right one position


copy_backward(element + theIndex,
element + listSize,
element + listSize + 1);

element[theIndex] = theElement;

listSize++;
}
The Method output

template<class T>
void arrayList<T>::output(ostream& out) const
{// Put the list into the stream out.
copy(element, element + listSize,
ostream_iterator<T>(cout, " "));
}
Overloading <<

// overload <<
template <class T>
ostream& operator<<(ostream& out,
const arrayList<T>& x)
{x.output(out); return out;}
Iterators

An iterator permits you to examine the elements


of a data structure one at a time.
C++ iterators
• Input iterator
• Output iterator
• Forward iterator
• Bidirectional iterator
• Reverse iterator
Bidirectional Iterator

Allows both forward and backward


movement through the elements of
a data structure.
Bidirectional Iterator Methods
iterator(T* thePosition)
Constructs an iterator positioned at
specified element
dereferencing operators * and ->
Post and pre increment and
decrement operators ++ and –
Equality testing operators == and
!=
Iterator Class
Assume that a bidirectional iterator
class iterator is defined within the
class arrayList.
Assume that methods begin() and
end() are defined for arrayList.
• begin() returns an iterator positioned
at element 0 of list.
• end() returns an iterator positioned
one past last element of list.
Using An Iterator
arrayList<int>::iterator xHere = x.begin();
arrayList<int>::iterator xEnd = x.end();
for (; xHere != xEnd; xHere++)
examine( *xHere);

vs
for (int i = 0; i < x.size(); i++)
examine(x.get(i));
Merits Of An Iterator
• it is often possible to implement the
++ and -- operators so that their
complexity is less than that of get
• many data structures do not have a
get by index method
• iterators provide a uniform way to
sequence through the elements of a
data structure
Linked Representation

• list elements are stored, in memory,


in an arbitrary order

• explicit information (called a link)


is used to go from one element to
the next
Memory Layout
Layout of L = (a,b,c,d,e) using an array representation.

a b c d e

A linked representation uses an arbitrary layout.


c a e d b
Linked Representation

c a e d b

firstNode
pointer (or link) in e is NULL

use a variable firstNode to get to the


first element a
Normal Way To Draw A Linked List

firstNode

NULL

a b c d e

link or pointer field of node

data field of node


Chain
firstNode

NULL

a b c d e

•A chain is a linked list in which each node


represents one element.
• There is a link or pointer from one element to
the next.
• The last node has a NULL pointer.
Node Representation
template <class T>
struct chainNode
{
// data members
T element;
chainNode<T> *next; next
element
// constructors come here
};
Constructors Of chainNode
?
chainNode() {} ?

?
chainNode(const T& element)
element
{this->element = element;}

chainNode(const T& element, chainNode<T>* next) next


{this->element = element; element
this->next = next;}
get(0)
firstNode

NULL

a b c d e

checkIndex(0);
desiredNode = firstNode; // gets you to first node
return desiredNode−>element;
get(1)
firstNode

NULL

a b c d e

checkIndex(1);
desiredNode = firstNode−>next; // gets you to second node
return desiredNode−>element;
get(2)
firstNode

NULL

a b c d e

checkIndex(2);
desiredNode = firstNode−>next−>next; // gets you to third node
return desiredNode−>element;
get(5)
firstNode

NULL

a b c d e

checkIndex(5); // throws exception


desiredNode = firstNode−>next−>next−>next−>next−>next;
// desiredNode = NULL
return desiredNode−>element; // NULL.element
Erase An Element
firstNode

NULL

a b c d e

erase(0)

deleteNode = firstNode;
firstNode = firstNode−>next;
delete deleteNode;
erase(2)
firstNode

NULL

b cc
a d e

beforeNode
first get to node just before node to be removed

beforeNode = firstNode−>next;
erase(2)
firstNode

null
a b c d e

beforeNode

save pointer to node that will be deleted


deleteNode = beforeNode−>next;
erase(2)
firstNode

null
a b c d e

beforeNode

now change pointer in beforeNode


beforeNode−>next = beforeNode−>next−>next;
delete deleteNode;
insert(0,’f’)
firstNode

NULL

f a b c d e

newNode

Step 1: get a node, set its data and link fields

newNode = new chainNode<char>(theElement,


firstNode);
insert(0,’f’)
firstNode

NULL

f a b c d e

newNode

Step 2: update firstNode

firstNode = newNode;
One-Step insert(0,’f’)
firstNode

NULL

f a b c d e

newNode

firstNode = new chainNode<char>(‘f’, firstNode);


insert(3,’f’)
firstNode newNode
f
NULL

a b c d e

beforeNode
• first find node whose index is 2
• next create a node and set its data and link fields
chainNode<char>* newNode = new chainNode<char>( ‘f’,
beforeNode−>next);
• finally link beforeNode to newNode
beforeNode−>next = newNode;
Two-Step insert(3,’f’)
firstNode newNode
f
NULL

a b c d e

beforeNode

beforeNode = firstNode−>next−>next;
beforeNode−>next = new chainNode<char>
(‘f’, beforeNode−>next);
To-do
Practice:
– https://www.hackerrank.com/domai
ns/cpp/introduction
Read more on iterators:
– http://www.cplusplus.com/referenc
e/iterator/
– http://www.cplusplus.com/referenc
e/iterator/iterator/
Read more on linked lists:
– https://en.wikipedia.org/wiki/Linked
_list
Reminder: Course Policy

• Please be very careful not to cause distractions


in the classroom and be respectful of others
around you, especially considering the very large
size of the class.
• Phone calls, messaging, discussion on a game
you played recently, etc. Before OR After class
The Class chain
Reminder: Chain
firstNode

NULL

a b c d e

•A chain is a linked list in which each node


represents one element.
• There is a link or pointer from one element to
the next.
• The last node has a NULL pointer.
The Class chain
firstNode

NULL

a b c d e

listSize = number of elements

Use chainNode
next (datatype chainNode<T>*)

element (datatype T)
The Class chain
template<class T>
class chain : public linearList<T>
{
public:
// constructors and destructor defined here

// ADT methods
bool empty() const {return listSize == 0;}
int size() const {return listSize;}
// other ADT methods defined here
protected:
void checkIndex(int theIndex) const;
chainNode<T>* firstNode;
int listSize;
};
Constructor

template<class T>
chain<T>::chain(int initialCapacity = 10)
{// Constructor.
if (initialCapacity < 1)
{ostringstream s;
s << "Initial capacity = "
<< initialCapacity << " Must be > 0";
throw illegalParameterValue(s.str());
}
firstNode = NULL;
listSize = 0;
}
The Destructor

template<class T>
chain<T>::~chain()
{// Chain destructor. Delete all nodes
// in chain.
while (firstNode != NULL)
{// delete firstNode
chainNode<T>* nextNode = firstNode->next;
delete firstNode;
firstNode = nextNode;
}
}
firstNode The Method get
NULL

a b c d e

template<class T>
T& chain<T>::get(int theIndex) const
{// Return element whose index is theIndex.
checkIndex(theIndex);
// move to desired node
chainNode<T>* currentNode = firstNode;
for (int i = 0; i < theIndex; i++)
currentNode = currentNode->next;
return currentNode->element;
}
The Method indexOf
template<class T>
int chain<T>::indexOf(const T& theElement) const
{
// search the chain for theElement
chainNode<T>* currentNode = firstNode;
int index = 0; // index of currentNode
while (currentNode != NULL &&
currentNode->element != theElement)
{
// move to next node
currentNode = currentNode->next;
index++;
}
The Method indexOf
// make sure we found matching element
if (currentNode == NULL)
return -1;
else
return index;
}
Erase An Element
firstNode

NULL

a b c d e

erase(0)

deleteNode = firstNode;
firstNode = firstNode->next;
delete deleteNode;
Remove An Element
template<class T>
void chain<T>::erase(int theIndex)
{
checkIndex(theIndex);

chainNode<T>* deleteNode;
if (theIndex == 0)
{// remove first node from chain
deleteNode = firstNode;
firstNode = firstNode->next;
}
erase(2)
firstNode

null
a b c d e

beforeNode

Find & change pointer in beforeNode


beforeNode->next = beforeNode->next->next;
delete deleteNode;
Remove An Element
else
{ // use p to get to beforeNode
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;

deleteNode = p->next;
p->next = p->next->next;
}
listSize--;
delete deleteNode;
}
One-Step insert(0,’f’)
firstNode

NULL

f a b c d e

newNode

firstNode = new chainNode<char>(‘f’, firstNode);


Insert An Element
template<class T>
void chain<T>::insert(int theIndex,
const T& theElement)
{
if (theIndex < 0 || theIndex > listSize)
{// Throw illegalIndex exception
}

if (theIndex == 0)
// insert at front
firstNode = new chainNode<T>
(theElement, firstNode);
Two-Step insert(3,’f’)
firstNode newNode
f
NULL

a b c d e

beforeNode

beforeNode = firstNode->next->next;
beforeNode->next = new chainNode<char>
(‘f’, beforeNode->next);
Inserting An Element
else
{ // find predecessor of new element
chainNode<T>* p = firstNode;
for (int i = 0; i < theIndex - 1; i++)
p = p->next;

// insert after p
p->next = new chainNode<T>
(theElement, p->next);
}
listSize++;
}
Performance
50,000 operations of each type
Performance
50,000 operations of each type

Operation FastArrayLinearList Chain


get 1.0ms 13.2sec
best-case inserts 2.1ms 45.1ms
average inserts 1.5sec 49.3sec
worst-case inserts 2.5sec 12.9sec
best-case removes 2.0ms 2.1ms
average removes 1.5sec 68.8sec
worst-case removes 2.5sec 12.9sec
Performance – Updated Estimates
50,000 operations of each type

Operation ArrayList LinkedList


get 0.7ms 1.5sec
best-case inserts 2.3ms 1.3ms
worst-case inserts 170.3ms 1.56sec
Performance
Indexed AVL Tree (IAVL)
Chain With Header Node

headerNode

NULL

a b c d e
Empty Chain With Header Node

headerNode

NULL
Circular List

firstNode

a b c d e
Doubly Linked List
firstNode lastNode

NULL
NULL

a b c d e
Doubly Linked Circular List
firstNode

a b c d e
Doubly Linked Circular List With Header Node
headerNode

a b c d e
Empty Doubly Linked Circular List With Header Node

headerNode
The STL Class list

 Linked implementation of a linear list.


 Doubly linked circular list with header node.
 Has many more methods than chain.
 Similar names and signatures.
To-do

Practice (Easy Challenges):


– https://www.hackerrank.com/domai
ns/data-structures/linked-lists
– Read more on list:
– http://www.cplusplus.com/referenc
e/list/list/
Simulated Pointers
Limitations Of C++ Pointers

• May be used for internal data


structures only.
• Data structure backup requires
serialization and deserialization.
• No arithmetic.
Simulated-Pointer Memory Layout

c a e d b

Data structure of memory is an array,


and each array position has an
element field (type T) and a next
field (type int).
Node Representation
template <class T>
class simulatedNode
{
public:
// constructors defined here
protected:
T element;
int next;
}
How It All Looks

c a e d b

next 14
11 14 -1 8 0
element c a e d b
0 1 2 3 4 5 8 11 14

firstNode = 4
Still Drawn The Same

firstNode

-1
a b c d e
Memory Management
Linked system (C++ or simulated pointer)
requires:
• a way to keep track of the memory that is not
in use (i.e., a storage pool)
• way to allocate a node
C++ has the method new
• way to free a node that is no longer in use
C++ has the method delete
Garbage Collection
The system determines which nodes/memory are
not in use and returns these nodes (this memory)
to the pool of free storage.

This is done in two or three steps:


Mark nodes that are in use.
Compact free space (optional).
Move free nodes to storage pool.
Marking

c a e d b

firstNode

Unmark all nodes (set all mark bits to false).


Start at each program variable that contains a
reference, follow all pointers, mark nodes
that are reached.
Marking

c a e dd be

firstNode

Start at firstNode and mark all nodes


reachable from firstNode.

Repeat for all pointer variables.


Compaction

c a e d b Free
e Memory
d b

firstNode

Move all marked nodes (i.e., nodes in


use) to one end of memory,
updating all pointers as necessary.
Put Free Memory In Storage Pool

Scan memory for unmarked nodes (if


no compaction done), otherwise put
single free block (unless no free
memory) into pool.
Advantages Of Garbage Collection

• Programmer doesn’t have to worry


about freeing nodes as they become
free.
• However, for garbage collection to
be effective, we must set reference
variables to null when the object
being referenced is no longer
needed.
Advantages Of Garbage Collection

• Applications may run faster when


run on computers that have more
memory.
Disadvantage Of Garbage Collection

• Garbage collection time is linear in


memory size (not in amount of free
memory).
• Many different algorithms exist,
tradeoffs
Alternative To Garbage Collection

Provide a method to free/deallocate a


node.
e.g., delete method of C++

Now free nodes are always in storage


pool.
Advantage Of Alternative

• Time to free nodes is proportional


to number of nodes being freed and
not to total memory size.
Disadvantages Of Alternative

• User must write methods to free


data structure nodes.
• Time is spent freeing nodes that
may not be reused.
• Application run time does not
improve with increase in memory
size.
Storage Pool Organization When All
Nodes Have Same Size

firstNode

-1
a b c d e

• Maintain a chain of free nodes


• Allocate from front of chain
• Add node that is freed to chain front
Simulated-Pointer Memory Management
template <class T>
class simulatedSpace
{
public:
// constructor and other methods
// defined here
protected:
int firstNode;
simulatedNode<T> *node;
}
Constructor
template <class T>
simulatedSpace (int numberOfNodes)
{
node = new simulatedNode<T> [numberOfNodes];
// create nodes and link into a chain
for (int i = 0; i < numberOfNodes - 1; i++)
node[i].next = i + 1;
// last node of array and chain
node[numberOfNodes - 1].next = -1;
// first node of chain of free nodes
firstNode = 0;
}
Allocate A Node
template <class T>
int allocateNode(T& element, int next)
{// Allocate a free node and set its fields.
if (firstNode == -1)
{// double number of nodes, code omitted
}
int i = firstNode; // allocate first node
firstNode = node[i].next;
node[i].element = element;
node[i].next = next;
return i;
}
Free A Node

template <class T>


void deallocateNode(int i)
{// Free node i.
// make i first node on free space list
node[i].next = firstNode;
firstNode = i;
}
Simulated Pointers
• Can allocate a chain of nodes without having to
relink.
• Can free a chain of nodes in O(1) time when
first and last nodes of chain are known.
Simulated Pointers

• Don’t use unless you see a clear advantage to


using simulated pointers over C++ pointers.
• Use C++11 smart pointers
• Reduce bugs, retain efficiency
Arrays
1D Array Representation In C++
Memory

a b c d

start

• 1-dimensional array x = [a, b, c, d]


• map into contiguous memory locations

• location(x[i]) = start + i
Space Overhead
Memory

a b c d

start

space overhead = 4 bytes (32 bits) for start

(excludes space needed for the elements of x)


2D Arrays
The elements of a 2-dimensional array a
declared as:
int a[3][4]; (We refer to such an array as an M-by-N array)
may be shown as a table
a[0][0] a[0][1] a[0][2] a[0][3]
a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]
Rows Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3] row 0


a[1][0] a[1][1] a[1][2] a[1][3] row 1
a[2][0] a[2][1] a[2][2] a[2][3] row 2
Columns Of A 2D Array

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

column 0 column 1 column 2 column 3


The mathematical abstraction corresponding to such
tables is a Matrix.
2D Array Representation In C++
2-dimensional array x
a, b, c, d
e, f, g, h
i, j, k, l
view 2D array as a 1D array of rows
x = [row0, row1, row 2]
row 0 = [a,b, c, d]
row 1 = [e, f, g, h]
row 2 = [i, j, k, l]
and store as 4 1D arrays
2D Array Representation In C++
x[]

a b c d

e f g h

i j k l

4 separate 1-dimensional arrays


Space Overhead
x[]

a b c d

e f g h

i j k l

space overhead = overhead for 4 1D arrays


= 4 * 4 bytes
= 16 bytes
= (number of rows + 1) x 4 bytes
Array Representation In C++
x[]

a b c d

e f g h

i j k l

• This representation is called the array-of-arrays


representation.
• Requires contiguous memory of size 3, 4, 4, and 4 for the
4 1D arrays.
• 1 memory block of size number of rows and number of
rows blocks of size number of columns
Array of arrays

• The same notion extends to multi-dimensional


arrays (e.g. 3D Array)

• Enables jagged arrays:


• int jagged_row0[] = {0,1};
• int jagged_row1[] = {1,2,3};
• int* jagged[] =
{ jagged_row0,jagged_row1 };
Row-Major Mapping
• Example 3 x 4 array:
abcd
efgh
i jkl
• Convert into 1D array y by collecting elements by rows.
• Within a row elements are collected from left to right.
• Rows are collected from top to bottom.
• We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l}
row 0 row 1 row 2 … row i
Locating Element x[i][j]
0 c 2c 3c ic

row 0 row 1 row 2 … row i

• assume x has r rows and c columns


• each row has c elements
• i rows to the left of row i
• so ic elements to the left of x[i][0]
• so x[i][j] is mapped to position
ic + j of the 1D array
Space Overhead

row 0 row 1 row 2 … row i

4 bytes for start of 1D array +


4 bytes for c (number of columns)
= 8 bytes
Disadvantage

Need contiguous memory of size rc.


Column-Major Mapping
abcd
efgh
i jkl
• Convert into 1D array y by collecting elements
by columns.
• Within a column elements are collected from
top to bottom.
• Columns are collected from left to right.
• We get y = {a, e, i, b, f, j, c, g, k, d, h, l}
Matrix
Table of values. Has rows and columns, but
numbering begins at 1 rather than 0.
a b c d row 1
e f g h row 2
i jkl row 3
• Use notation x(i,j) rather than x[i][j].
• May use a 2D array to represent a matrix.
Shortcomings Of Using A 2D
Array For A Matrix

• Indexes are off by 1.


• C++ arrays do not support matrix operations
such as add, transpose, multiply, and so on.
– Suppose that x and y are 2D arrays. Can’t do x + y, x
–y, x * y, etc. in C++.
• Develop a class matrix for object-oriented
support of all matrix operations. See text.
Diagonal Matrix

An n x n matrix in which all nonzero


terms are on the diagonal.
Diagonal Matrix
1000
0200
0030
0004

• x(i,j) is on diagonal iff i = j


• number of diagonal elements in an
n x n matrix is n
• non diagonal elements are zero
• store diagonal only vs n2 whole
Lower Triangular Matrix
An n x n matrix in which all nonzero terms are either
on or below the diagonal.
100 0
230 0
456 0
7 8 9 10
• x(i,j) is part of lower triangle iff i >= j.
• number of elements in lower triangle is 1 + 2 +
… + n = n(n+1)/2.
• store only the lower triangle
Array Of Arrays Representation
x[]

2 3

4 5 6

7 8 9 l0

Use an irregular 2-D array … length of rows is not


required to be the same.
Creating And Using An Irregular Array
// declare a two-dimensional array variable
// and allocate the desired number of rows
int ** irregularArray = new int*[numberOfRows];
// now allocate space for elements in each row
for (int i = 0; i < numberOfRows; i++)
irregularArray[i] = new int [length[i]];
// use the array like any regular array
irregularArray[2][3] = 5;
irregularArray[4][6] = irregularArray[2][3]+2;
irregularArray[1][1] += 3;
Map Lower Triangular Array Into A 1D Array

Use row-major order, but omit terms that are


not part of the lower triangle.

For the matrix


100 0
230 0
456 0
7 8 9 10
we get
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Index Of Element [i][j]

0 1 3 6
r 1 r2 r3 … row i

• Order is: row 1, row 2, row 3, …


• Row i is preceded by rows 1, 2, …, i-1
• Size of row i is i.
• Number of elements that precede row i is
1 + 2 + 3 + … + i-1 = i(i-1)/2
• So element (i,j) is at position i(i-1)/2 + j -1 of
the 1D array.
To-do

http://www.cplusplus.com/doc/tutorial
/arrays/
https://en.wikipedia.org/wiki/Matrix_(
mathematics)
Don’t forget assignment 2
Reminder: Array Representation In C++
x[]

a b c d

e f g h

i j k l

• This representation is called the array-of-arrays


representation.
• Requires contiguous memory of size 3, 4, 4, and 4 for the
4 1D arrays.
• 1 memory block of size number of rows and number of
rows blocks of size number of columns
Reminder: Row-Major Mapping
• Example 3 x 4 array:
abcd
efgh
i jkl
• Convert into 1D array y by collecting elements by rows.
• Within a row elements are collected from left to right.
• Rows are collected from top to bottom.
• We get y[] = {a, b, c, d, e, f, g, h, i, j, k, l}
row 0 row 1 row 2 … row i
Sparse Matrices

sparse … many elements are zero


dense … few elements are zero
Example Of Sparse Matrices
diagonal
tridiagonal
lower triangular (?)

These are structured sparse matrices.


May be mapped into a 1D array so that a
mapping function can be used to locate an
element.
Unstructured Sparse Matrices
Airline flight matrix.
 airports are numbered 1 through n
 flight(i,j) = list of nonstop flights from airport i
to airport j
 n = 1000 (say)
 n x n array of list references => 4 million bytes
 total number of flights = 20,000 (say)
 need at most 20,000 list references => at most
80,000 bytes
Unstructured Sparse Matrices
Web page matrix.
web pages are numbered 1 through n
web(i,j) = number of links from page i to page j

Web analysis.
authority page … page that has many links to it
hub page … links to many authority pages
Web Page Matrix
 n = 2 billion (and growing by 1 million a day)
 n x n array of ints => 4* 1018 integers =
16* 1018 bytes (16 * 109 GB)
 each page links to 10 (say) other pages on
average
 on average there are 10 nonzero entries per row
 space needed for nonzero elements is
approximately 20 billion x 4 bytes = 80 billion
bytes (80 GB)
Representation Of Unstructured
Sparse Matrices
Single linear list in row-major order.
scan the nonzero elements of the sparse matrix in row-
major order
each nonzero element is represented by a triple
(row, column, value)
the list of triples may be an array list or a linked list
(chain)
Single Linear List Example

00304 list =
00570 row 1 1 2 2 4 4
00000 column 3 5 3 4 2 3
02600 value 3 4 5 7 2 6
Array Linear List Representation

row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6

element 0 1 2 3 4 5
row 1 1 2 2 4 4
column 3 5 3 4 2 3
value 3 4 5 7 2 6
Chain Representation

Node structure.

row col
value next
Single Chain

row 1 1 2 2 4 4
list = column 3 5 3 4 2 3
value 3 4 5 7 2 6

1 3 1 5 2 3 2 4 4 2 4 3
3 4 5 7 2 6 NULL

firstNode
One Linear List Per Row

00304 row1 = [(3, 3), (5,4)]


00570 row2 = [(3,5), (4,7)]
00000 row3 = []
02600 row4 = [(2,2), (3,6)]
Array Of Row Chains

Node structure.

next
col value
Array Of Row Chains

NULL

3 3 5 4

00304 NULL

00570 3 5 4 7

00000 NULL

02600
NULL

2 2 3 6

row[]
Orthogonal List Representation

Both row and column lists.

Node structure.
row col value
down next
Row Lists

1 3 3 1 5 4
N
00304
00570 2 3 5 2 4 7
N
00000
02600 NULL

4 2 2 4 3 6
N
Column Lists

1 3 3 1 5 4
N
00304
00570 2 3 5 2 4 7

00000
02600

4 2 2 4 3 6
N N
Orthogonal Lists

1 3 3 1 5 4
N N
00304
00570 2 3 5 2 4 7
N
00000
02600 NULL

4 2 2 4 3 6
N N N

row[]
Variations

May use circular lists instead of chains.


Approximate Memory Requirements

500 x 500 matrix with 1994 nonzero elements

2D array 500 x 500 x 4 = 1million bytes


Single Array List 3 x 1994 x 4 = 23,928 bytes
One Chain Per Row 23928 + 500 x 4 = 25,928
Runtime Performance
Matrix Transpose
500 x 500 matrix with 1994 nonzero elements

2D array 1.97 ms
Single Array List 0.09 ms
One Chain Per Row 1.57 ms
Performance
Matrix Addition.
500 x 500 matrices with 1994 and 999 nonzero
elements

2D array 2.69 ms
Single Array List 0.13 ms
One Chain Per Row Not Measured
Stacks

• Linear list.
• One end is called top.
• Other end is called bottom.
• Additions to and removals from the top end
only.
Stack Of Cups

top F

top E E

D D

C C

B B

bottom A bottom A

• Add a cup to the stack.


• Remove a cup from new stack.
• A stack is a LIFO list.
The Abstract Class stack

template<class T>
class stack
{
public:
virtual ~stack() {}
virtual bool empty() const = 0;
virtual int size() const = 0;
virtual T& top() = 0;
virtual void pop() = 0;
virtual void push(const T& theElement) = 0;
};
Parentheses Matching
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)
– Output pairs (u,v) such that the left parenthesis at
position u is matched with the right parenthesis at v.
• (2,6) (1,13) (15,19) (21,25) (27,31) (0,32) (34,38)
• (a+b))*((c+d)
– (0,4)
– right parenthesis at 5 has no matching left parenthesis
– (8,12)
– left parenthesis at 7 has no matching right parenthesis
Parentheses Matching
• scan expression from left to right
• when a left parenthesis is encountered, add its
position to the stack
• when a right parenthesis is encountered, remove
matching position from stack
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

2
1
0
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

15
1
0 (2,6) (1,13)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

21
1
0 (2,6) (1,13) (15,19)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

27
1
0 (2,6) (1,13) (15,19) (21,25)
Example
• (((a+b)*c+d-e)/(f+g)-(h+j)*(k-l))/(m-n)

1
0 (2,6) (1,13) (15,19) (21,25) (27,31) (0,32)

• and so on
Sample application:
Evaluate an expression

• E.g. (((2+3)+4)*2)
• If parentheses are unbalanced  Invalid
• Need two stacks
– Operator (e.g. +, /, etc.) stack
– Operand (e.g. 2, a, etc.) stack
Towers Of Hanoi/Brahma

4
3
2
1

A B C
• 64 gold disks to be moved from tower A to tower C
• each tower operates as a stack
• cannot place big disk on top of a smaller one
Towers Of Hanoi/Brahma

3
2
1

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

2
1 3

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

1 2 3

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

3
1 2

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

3
2 1

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

3 2 1

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

2
3 1

A B C
• 3-disk Towers Of Hanoi/Brahma
Towers Of Hanoi/Brahma

3
2
1

A B C
• 3-disk Towers Of Hanoi/Brahma
• 7 disk moves
Recursive Solution

A B C
• n > 0 gold disks to be moved from A to C using B
• move top n-1 disks from A to B using C
Recursive Solution

A B C
• move top disk from A to C
Recursive Solution

A B C
• move top n-1 disks from B to C using A
Recursive Solution

A B C
• moves(n) = 0 when n = 0
• moves(n) = 2*moves(n-1) + 1 = 2n-1 when n > 0
Towers Of Hanoi/Brahma

• moves(64) = 1.8 * 1019 (approximately)


• Performing 109 moves/second, a computer would take
about 570 years to complete.
• At 1 disk move/min, the monks will take about 3.4 * 1013
years.

• 2n is BIG
Chess Story

• 1 grain of rice on the first square, 2 for next, 4 for


next, 8 for next, and so on.
• Surface area needed exceeds surface area of earth.
Chess Story

• 1 penny for the first square, 2 for next, 4 for next,


8 for next, and so on.
• $3.6 * 1017 (federal budget ~ $2 * 1012) .
Switch Box Routing
1 2 3 4 5 6 7 8 9 10
40 11
39 12
38 13
37 14
36 15
Routing region
35 16
34 17
33 18
32 19
31 20
30 29 28 27 26 25 24 23 22 21
Routing A 2-pin Net
1 2 3 4 5 6 7 8 9 10
40 11
Routing Routing
39 12 for pins
for pins
1-3 and 38 13 5
18-40 is 37 14 through
confined 16 is
36 15
to lower confined
35 16 to upper
left
region. 34 17 right
33 18 region.
32 19
31 20
30 29 28 27 26 25 24 23 22 21
Routing A 2-pin Net
1 2 3 4 5 6 7 8 9 10
40 11
(u,v), Examine
39 12 pins in
u<v is a
2-pin 38 13 clock-
net. 37 14 wise
order
u is start 36 15
beginn-
pin. 35 16 ing with
v is end 34 17 pin 1.
pin. 33 18
32 19
31 20
30 29 28 27 26 25 24 23 22 21
Routing A 2-pin Net
1 2 3 4 5 6 7 8 9 10
40 11
Start pin
39 12
=> push
onto 38 13
stack. 37 14

End pin 36 15
=> start 35 16
pin must 34 17
be at top
33 18
of stack.
32 19
31 20
30 29 28 27 26 25 24 23 22 21
Method Invocation And Return
public void a()
{ …; b(); …}
public void b()
{ …; c(); …} return address in d()
public void c() return address in c()
{ …; d(); …} return address in e()
public void d() return address in d()
{ …; e(); …}
return address in c()
return address in b()
public void e()
return address in a()
{ …; c(); …}
Try-Throw-Catch
• When you enter a try block, push the address of
this block on a stack.
• When an exception is thrown, pop the try block
that is at the top of the stack (if the stack is empty,
terminate).
• If the popped try block has no matching catch
block, go back to the preceding step.
• If the popped try block has a matching catch
block, execute the matching catch block.
Rat In A Maze
Rat In A Maze

• Move order is: right, down, left, up


• Block positions to avoid revisit.
Rat In A Maze

• Move order is: right, down, left, up


• Block positions to avoid revisit.
Rat In A Maze

• Move backward until we reach a square from which


a forward move is possible.
Rat In A Maze

• Move down.
Rat In A Maze

• Move left.
Rat In A Maze

• Move down.
Rat In A Maze

• Move backward until we reach a square from which


a forward move is possible.
Rat In A Maze

• Move backward until we reach a square from which


a forward move is possible.
• Move downward.
Rat In A Maze

• Move right.
• Backtrack.
Rat In A Maze

• Move downward.
Rat In A Maze

• Move right.
Rat In A Maze

• Move one down and then right.


Rat In A Maze

• Move one up and then right.


Rat In A Maze

• Move down to exit and eat cheese.


• Path from maze entry to current position operates as
a stack.

You might also like