You are on page 1of 87

Motivation

Abstract Data Types

Abstract Data Types


Algorithms and Complexity Theory

Matei Popovici1

1 POLITEHNICA University of Bucharest

Computer Science and Engineering Department, Bucharest, Romania

9 noiembrie 2012

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

Implement a List of integers datatype (in Java):


insert (at the beginning of the list)
extract (returns the first element of the list)
remove (remove the first element of the list).

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

Implement a List of integers datatype (in Java):


insert (at the beginning of the list)
extract (returns the first element of the list)
remove (remove the first element of the list).

Code !

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:

The PointerVector implementation:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:


relies on a primitive Array datatype

The PointerVector implementation:


relies on Java references

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:


relies on a primitive Array datatype
full encapsulation (operations modify the object)

The PointerVector implementation:


relies on Java references
partial encapsulation (two kinds of objects)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:


relies on a primitive Array datatype
full encapsulation (operations modify the object)
insert may be (1) or O(n)
The PointerVector implementation:
relies on Java references
partial encapsulation (two kinds of objects)
insertion is always be (1)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:


relies on a primitive Array datatype
full encapsulation (operations modify the object)
insert may be (1) or O(n)
The PointerVector implementation:
relies on Java references
partial encapsulation (two kinds of objects)
insertion is always be (1)
Open problems: Algorithms with complex data structures

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:


relies on a primitive Array datatype
full encapsulation (operations modify the object)
insert may be (1) or O(n)
The PointerVector implementation:
relies on Java references
partial encapsulation (two kinds of objects)
insertion is always be (1)
Open problems: Algorithms with complex data structures
how to we reason about complexity ?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The ListVector implementation:


relies on a primitive Array datatype
full encapsulation (operations modify the object)
insert may be (1) or O(n)
The PointerVector implementation:
relies on Java references
partial encapsulation (two kinds of objects)
insertion is always be (1)
Open problems: Algorithms with complex data structures
how to we reason about complexity ?
how do we know algorithms are correct ?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

Consider another implementation of the List of integers


datatype (this time, not in Java)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

Consider another implementation of the List of integers


datatype (this time, not in Java)

Code !

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built
specifies operations defined for our type

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built
specifies operations defined for our type
does not specify what a list of integers is

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built
specifies operations defined for our type
does not specify what a list of integers is

Ignoring implementation

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built
specifies operations defined for our type
does not specify what a list of integers is

Ignoring implementation we do this all the time !

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built
specifies operations defined for our type
does not specify what a list of integers is

Ignoring implementation we do this all the time !

how is a double represented (in Java) ?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Motivation

The previous program:


specifies how values of type List of integers can be built
specifies operations defined for our type
does not specify what a list of integers is

Ignoring implementation we do this all the time !

how is a double represented (in Java) ?


do we care about the representation when we add doubles
?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Abstract Data Types

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List):

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List
Cons : Integer List List

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List
Cons : Integer List List
Operations:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List
Cons : Integer List List
Operations:
extract : List \ {Void} Integer

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List
Cons : Integer List List
Operations:
extract : List \ {Void} Integer
extract (Cons(e,l)) = e

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List
Cons : Integer List List
Operations:
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Abstract Data Types

Definition (Abstract Data Type [1])


An Abstract Data Type (ADT or TDA) is a specification of the
possible values of a data type as well as a specification of the
possible operations that can be performed on those values, in
terms of the operations inputs, outputs, effects.

Example (the ADT List): All possible values:


Void : List
Cons : Integer List List
Operations:
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Matei Popovici Abstract Data Types
Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:
Base constructors

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:
Base constructors: Those constructors which can jointly
generate all values of the type

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:
Base constructors: Those constructors which can jointly
generate all values of the type
(Simple) constructors

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:
Base constructors: Those constructors which can jointly
generate all values of the type
(Simple) constructors All the rest.

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:
Base constructors: Those constructors which can jointly
generate all values of the type
(Simple) constructors All the rest.
Axioms

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
Cons : Integer List List
extract : List \ {Void} Integer
extract (Cons(e,l)) = e
remove : List \ {Void} List
extract (Cons(e,l)) = l
Taxonomy:
Base constructors: Those constructors which can jointly
generate all values of the type
(Simple) constructors All the rest.
Axioms

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:
Nullary constructors

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:
Nullary constructors

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:
Nullary constructors
External constructors

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:
Nullary constructors
External constructors: constructors that use values
which do not belong to the datatype at hand

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:
Nullary constructors
External constructors: constructors that use values
which do not belong to the datatype at hand
Internal constructors

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

void : List
cons : Integer List List
extract : List \ {Void} Integer
extract (cons(e,l)) = e
remove : List \ {Void} List
extract (cons(e,l)) = l
Taxonomy:
Nullary constructors
External constructors: constructors that use values
which do not belong to the datatype at hand
Internal constructors: constructors that use only values
of the datatype at hand

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Let T denote an ADT, and a base constructor of T . Then:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Let T denote an ADT, and a base constructor of T . Then:


0 = { | : T }

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Let T denote an ADT, and a base constructor of T . Then:


0 = { | : T }
e = { | : A1 . . . An T such that i Ai 6= T }

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Let T denote an ADT, and a base constructor of T . Then:


0 = { | : T }
e = { | : A1 . . . An T such that i Ai 6= T }
i = { | : A1 . . . Am T such that j Xj = T }

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Taxonomy

Taxonomy is less important, but useful in certain discussions.

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

The Ring ADT

What is a Ring ?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

The Ring ADT

What is a Ring ?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

The Ring ADT

What is a Ring ?
Applications: circular buffers / (FIFO with fixed size)
(overwrite data when consumer cannot keep up)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

The Ring ADT

What is a Ring ?
Applications: circular buffers / (FIFO with fixed size)
(overwrite data when consumer cannot keep up)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Where are rings useful

Advantages of circular buffers:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Where are rings useful

Advantages of circular buffers:


no memory handling necessary

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Where are rings useful

Advantages of circular buffers:


no memory handling necessary
useful in drivers: 8 bit registers can be used as memory
addresses (natural overflow)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Where are rings useful

Advantages of circular buffers:


no memory handling necessary
useful in drivers: 8 bit registers can be used as memory
addresses (natural overflow)
can be easily parallelized (if reads/writes are atomic)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Where are rings useful

Advantages of circular buffers:


no memory handling necessary
useful in drivers: 8 bit registers can be used as memory
addresses (natural overflow)
can be easily parallelized (if reads/writes are atomic)
suitable implementation for Queue (FIFO) of fixed size

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

The Ring ADT

Implementation of the Ring ADT


(a rather simpler version, with only one pointer, and unlimited
size)
- blackboard -

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Full specification for List

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

void : List
cons : Integer List List
extract : List \ {void} Integer
remove : List \ {void} List
reverse : List List
rev : List List List
concat : List List List
L : List List Bool

(ext) extract(cons(e, l)) = e


(rem) remove(cons(e, l)) = l

(rev1) rev (l, void) = l


(rev2) rev (l1 , cons(e, l2 )) = rev (cons(e, l1 ), l2 )
(rev) reverse(l) = rev (void, l)

(con1) concat(void, l) = l
(con2) concat(cons(e, l1 ), l2 ) = cons(e, concat(l1 , l2 ))

(equivL1) void L void = true


(equivL2) void L cons(e, l) = cons(e, l) L void = false
(equivL3) cons(e1 , l1 ) L cons(e2 , l2 ) = e1 Integer e2 l1 L l2

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Full specification for Ring

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

empty : Ring
ins : Integer Ring Ring
del : Ring \ {empty } Ring
top : Ring \ {empty } Integer
move : Ring \ {empty } Ring
isempty : Ring Bool

(del) del(ins(e, X )) = X
(top) top(ins(e, X )) = e

(mov1) move(ins(e, empty )) = ins(e, empty )


(mov2) move(ins(e, ins(f , l))) = ins(f , move(ins(e, l)))

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Full specification for IRing

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

the same operators as for Ring


normalize : IRing IRing
: IRing IRing IRing

(norm1) normalize(< L, cons(e, R) >) =< L, cons(e : R) >


(norm2) normalize(< L, void >) =< void, reverse(L) >

(equivR) < L1 , R1 >R < L2 , R2 >= concat(R1 , reverse(L1 )) L


concat(R2 , reverse(L2 ))

(emptyImpl) empty =impl < void, void >


(insImpl) ins(e, < L, R >) =impl < L, cons(e, R) >
(delImpl) del(< L, R >) =impl normalize(< L, remove(R) >)
(topImpl) top(< L, R >) =impl extract(R)
(moveImpl)
move(< L, R >) =impl normalize(< cons(extract(R), L), remove(R) >)
(isemptyImpl) isEmpty (< L, R >) = null(R)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Correctness

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Correctness

Correctness: Verifying whether:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Correctness

Correctness: Verifying whether: (i) certain useful properties of


ADTs hold

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Correctness

Correctness: Verifying whether: (i) certain useful properties of


ADTs hold (ii) checking that a data type implementation obeys
the ADT specification

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Methods for studying correctness

mathematical induction:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Methods for studying correctness


P(k )
P(0) k N.
P(k + 1)
mathematical induction:
n N.P(n)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Methods for studying correctness


P(k )
P(0) k N.
P(k + 1)
mathematical induction:
n N.P(n)
structural induction:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Methods for studying correctness


P(k )
P(0) k N.
P(k + 1)
mathematical induction:
n N.P(n)
structural induction:
an extension of mathematical induction

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Methods for studying correctness


P(k )
P(0) k N.
P(k + 1)
mathematical induction:
n N.P(n)
structural induction:
an extension of mathematical induction
suitable for proving properties of recursively-defined
ADTs

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Methods for studying correctness


P(k )
P(0) k N.
P(k + 1)
mathematical induction:
n N.P(n)
structural induction:
an extension of mathematical induction
suitable for proving properties of recursively-defined
ADTs
the induction step (k k + 1) is generalised to a
c
construction step (d d 0 , where d, d 0 are values of a ADT)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

Assume we have cells with a virus in a container

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

Assume we have cells with a virus in a container


New cells can be formed by:

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

Assume we have cells with a virus in a container


New cells can be formed by:
merging

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

Assume we have cells with a virus in a container


New cells can be formed by:
merging
division

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

Assume we have cells with a virus in a container


New cells can be formed by:
merging
division
Are all cells in the container with a virus ?

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

cell value of an ADT

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

cell value of an ADT


with virus property P of the values of the ADT

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

cell value of an ADT


with virus property P of the values of the ADT
division and merging base constructors of the ADT

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Analogy

cell value of an ADT


with virus property P of the values of the ADT
division and merging base constructors of the ADT
The structural induction scheme (where t1 , . . . tk T ):

P() 0 P(t1 ) . . . P(tk )


i
P((e)) e e dom() P((. . . , t1 , . . . tk , . . .))
t T , P(t)

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Examples

- Blackboard -

Matei Popovici Abstract Data Types


Motivation
Abstract Data Types

Bibliography I

David J. Eck.
Data structures and algorithms.
Lecture notes, 2004.
Militon Frentiu.
Correctness: A very important quality factor in
programming.
STUDIA UNIV. BABES BOLYAI, INFORMATICA, 2005.

Matei Popovici Abstract Data Types