Algorithms and Data Structures - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Algorithms and Data Structures

Description:

abstraction of a number of practical data types such as ... Fiesta. 1998. VW. Polo. 2000. Jeep. Grand. 1997. Ford. Fiesta. 1998. Sequence Abstract Data Type ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 32
Provided by: zumao
Category:

less

Transcript and Presenter's Notes

Title: Algorithms and Data Structures


1
Algorithms and Data Structures
  • Abstraction and Abstract Data Types
  • 2
  • Dr. Zumao Weng

2
Abstraction and Abstract Data Types ADT 2
3
Sequences
  • A sequence is an arbitrary number of components
    (of the same type) in some significant order.
  • abstraction of a number of practical data types
    such as strings, stacks, queues, files.
  • 42 2 395 278 integer sequence
  • 1.56 3.45 9.123 6.6 3.2 float sequence
  • M r A . N . O t h e r char sequence
  • which may be sorted but are not necessarily so
  • order refers to the position of an object and not
    its value.
  • OO languages object type permits mixed type
    sequences using Object type.

4
Sequences
  • May create a sequence of object types

class Car String make String
model int registration_year
....collection of operations...
Ford Fiesta 1998
VW Polo 2000
Jeep Grand 1997
Ford Fiesta 1998
......
5
Sequence Abstract Data Type
  • Defined as a Java class with public operations
    that manipulate values of the type.
  • Instances of the class can be assigned to
    variables.
  • Sequence my_sequence new Sequence()
  • Implementation data structure of the ADT is
    private to the ADT class.

class Sequence public Object op1 ()
public Object op2 ()
6
Sequence Model
  • A sequence is a collection of elements in some
    order
  • order refers to the position of elements.
  • Every sequence element except the first has a
    predecessor and every element except the last has
    a successor.
  • A sequence grows as elements are added and
    contracts as elements are removed.
  • add elements incrementally.
  • there is no defined memory allocated for all
    sequence elements when sequence created.

7
Sequence Models
8
Sequence Operations
  • Define a minimal collection of operations
    suitable for manipulating sequences.
  • use a model that is simple
  • easy to implement and test
  • provide building blocks for other operations
  • Operations are
  • creating sequence values, called construction
  • extract elements from sequence values, called
    de-construction or selection
  • comparing sequence values, called relations.
  • Construction, selection and relations common to
    all ADTs

9
Sequence Operations
  • Construction
  • An empty sequence by sequence constructor
  • Sequence Sequence()
  • A sequence element added at front
  • Sequence make(Object elem)
  • De-construction/Selection
  • Extracting the first element of a sequence
  • Object head ()
  • Sequence with the first element removed
  • Sequence tail()
  • Relations
  • Is the sequence empty
  • boolean is_empty()

10
Sequence Operations
Sequence Constructor
Make
11
Sequence Operations
Sequence Constructor
Make
12
Sequence Operations
Head Element
13
Operational Model Summary
  • Starting point is the creation of an empty
    sequence using sequence instance constructor
  • Sequence elements are added to the front of an
    existing sequence, using make operation.
  • Element at front of the sequence may be extracted
    using the head operation.
  • Sequence without the first element may be
    extracted using the tail operation.
  • Whether the sequence contains elements can be
    queried using the is_empty operation.

14
A Sequence Type
  • Is a class instance with the operators as public
    methods.
  • the operators are the only means of manipulating
    sequence values.
  • The implementation details of the type are not
    disclosed to the user.
  • so the implementation can be changed without
    affecting software that uses the sequence.
  • the software components that use the sequence
    type are thus loosely coupled from the sequence
    class.

15
A Java Sequence Interface
  • An interface defines a collection of operations
    that define some protocol.
  • methods do not have body definitions
  • interface name can be used as a type

interface Sequence // sequence constructor
provided by implementation Sequence
make(Object elem) Object head()
Sequence tail() boolean is_empty()
16
Interface Implementation
class my_sequence implements Sequence
..... public Sequence make(Object elem)
...the implementation... .....
  • the interface protocol can be implemented by a
    class.
  • the operations of the interface must be given
    realisations ie. program code.
  • the signature of the method must be exactly the
    same as that of the interface definition.
  • where signature is result type, argument types
    and order.
  • the class can define additional methods.

17
An Interface as a Type
  • An interface name can be used as a type for
    variables and method results.
  • The sequence interface declares make and tail as
    returning values of type Sequence.
  • An interface is a collection definitions, how
    can it provide operations?
  • An interface is implemented by a class.
  • Using the interface as a type enables the
    implementation class to be hidden and further
    reduces coupling.
  • an implementation can replace the implementation
    class easily.
  • An instance declared of an interface type can
    only use defined interface methods.

18
Interface Specification
interface Sequence Sequence make(Object
elem) Object head() Sequence tail()
boolean is_empty()
  • How does a programmer implementing an interface
    know what is expected?
  • Create a specification of the operations.
  • specification defines the effect of the methods
    but not how the methods achieve the effect.
  • a single specification serves many
    implementations of the interface.
  • the specification serves as documentation for the
    interface protocol

19
Algebraic Specification
  • Specifies behaviour by case analysis of input
    arguments.
  • particular suitable for ADTs which have
    constructors, de-constructors and relations.
  • each case specifies an input and defines an
    effect.
  • e.g. a method public int add1(int a)
  • add1(1) ? 2
  • add1(2) ? 3 etc
  • I need money
  • if bank open then cash a cheque
  • if bank closed then use autoteller
  • if ATM refuses money then borrow money from Fred
  • If Fred away then .....
  • With ADTs there is not that many permutations!

20
Sequence Specification
interface Sequence Sequence make(Object
elem) Object head() Sequence tail()
boolean is_empty()
  • consider a sequence S T h e S e q u e n c e
  • S.tail() ? h e S e q u e n c e
  • S.head() ? T
  • S.is_empty() ? false
  • S.make(X) ? X T h e S e q u e n c e
  • impossible to specify for every possible value
  • the value of elements is not important.

21
Sequence Specification
interface Sequence Sequence make(Object
elem) Object head() Sequence tail()
boolean is_empty()
  • interested in the relationship between
    constructors and de-constructors.
  • S h e S e q u e n c e e T
  • (S.make(e)).tail() ? S ? h e S e q u e n c e
  • (S.make(e)).head() ? e ? T
  • Value of S and the value T are not actually
    important
  • the relationship between make, head and tail

22
Complete Specification
  • Construction
  • new ... () provided by implementation
  • make()
  • De-construction/Selection
  • Operation Value Result
  • S.head() S L.make(e) ? e
  • S.head() S new Sequence() ? Error
  • S.tail() S L.make(e) ? L
  • S.tail() S new Sequence() ? Error
  • Relations
  • S.is_empty() S new Sequence() ? true
  • S.is_empty() S L.make(e) ? false

23
Head Specification
  • De-construction/Selection
  • Operation Value Result
  • S.head() S L.make(e) ? e
  • S.head() S new Sequence() ? Error
  • consider a sequence S T h e S e q u e n c e
  • S.head() ? T
  • S is L.make(e) S.head() is e
  • L is h e S e q u e n c e
  • e is T
  • consider a sequence S 87 45 45 333
  • L is 45 45 333
  • e is 87
  • S.head() is 87 which is e

24
Tail Specification
  • De-construction/Selection
  • Operation Value Result
  • S.tail() S L.make(e) ? L
  • S.tail() S new Sequence() ? Error
  • consider a sequence S T h e S e q u e n c
    e
  • S.tail() ? h e S e q u e n c e
  • S L.make(e)
  • L is he S e q u e n c e
  • e is T
  • consider a sequence S 87 45 45 333
  • L is 45 45 333
  • e is 87
  • S L.make(e) then S.tail() is L

25
is_empty Specification
  • Relations
  • S.is_empty() S new Sequence() ? true
  • S.is_empty() S L.make(e) ? false
  • consider a sequence S T h e S e q u e n c
    e
  • S.is_empty() ? false
  • S L.make(e)
  • L is he S e q u e n c e
  • e is T
  • consider a sequence S 87 45 45 333
  • L is 45 45 333
  • e is 87
  • S L.make(e) then S.is_empty() is false
  • The only true case is when it is a newly
    constructed sequence

26
Why no definition of constructor and make?
  • These definitions are not necessary because a
    definition would necessarily define an
    implementation.
  • More importantly, as de-constructors/relations
    are the only operations on values of the type you
    do not need to say what they do.

27
Complete Specification
  • Construction
  • new ... () provided by implementation
  • make()
  • De-construction/Selection
  • Operation Value Result
  • S.head() S L.make(e) ? e
  • S.head() S new Sequence() ? Error
  • S.tail() S L.make(e) ? L
  • S.tail() S new Sequence() ? Error
  • Relations
  • S.is_empty() S new Sequence() ? true
  • S.is_empty() S L.make(e) ? false

28
Sequence Specification
interface Sequence Sequence make(Object
elem) Object head() throws
SequenceEmptyException Sequence tail()
throws SequenceEmptyException boolean
is_empty()
public class SequenceEmptyException extends
Throwable public SequenceEmptyException ( )
public SequenceEmptyException (String report )
super(report)
  • Introduce exceptions to deal with errors that
    might occur in operations applied to empty
    sequences.

29
Sequence Definition Summary
  • The sequence interface operations are the only
    operations that can manipulate sequence values.
  • values are created using the class constructor
    and make
  • sequences de-constructed using head/tail
  • additional operations can be constructed using
    the operations.
  • The sequence interface does not need to know the
    type of sequence elements.
  • interface is generic (Object) to the element
    type.
  • An implementation is required for sequence.
  • Specification defined behaviour by defining
    effect.
  • all data is passed as arguments, no side
    effects.

30
Java example append1 ()
  • Using the interface definition above, develop an
    algebraic specification for an append1 operation
    which creates a new sequence by appending a new
    element as the last element in the sequence.
    Provide a Java method for the operation.
  • append1
  • append1(S, t) S is a sequence, t an object
  • S new Sequence()
  • -gt S.make(t)
  • S L.make(e) L is a sequence value
  • -gt (append1(L,t)).make(e)

31
Java example append1 ()
  • Sequence append1(Sequence seq, Object elem)
  • if (seq.is_empty())
  • return seq.make(elem)
  • else
  • return (append1(seq.tail,
    elem)).make(seq.head())
Write a Comment
User Comments (0)
About PowerShow.com