Data Structures and Algorithm Design

Fachhochschule Braunschweig / Wolfenbüttel - University of Applied Sciences - Data Structures and Algorithm Design - CSCI 340 Friedhelm Seutter Fach...
Author: Eva Bachmeier
2 downloads 1 Views 275KB Size
Fachhochschule Braunschweig / Wolfenbüttel - University of Applied Sciences -

Data Structures and Algorithm Design - CSCI 340 Friedhelm Seutter

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

© Friedhelm Seutter, 2008

Contents 1. 2. 3. 4. 6. 7. 8. 10. 13.

Analyzing Algorithms and Problems Data Abstraction Recursion and Induction Sorting Dynamic Sets and Searching Graphs and Graph Traversals Optimization and Greedy Algorithms Dynamic Programming NP-Complete Problems

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

2

© Friedhelm Seutter, 2008

1

2. Data Abstraction and

Basic Data Structures • • • •

Abstract data types Lists and trees Stacks and queues Dynamic Sets

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

3

© Friedhelm Seutter, 2008

Abstract data types An abstract data type (ADT) consists of a • data structure declaration, • a set of operations involving the data structure. A client (user) of an ADT calls these operations to create, destroy, manipulate, and interrogate objects (instances) of the ADT. A client is just some procedure defined outside the ADT. Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

4

© Friedhelm Seutter, 2008

2

Key idea behind ADTs ADT modules maintain private data that is accessible outside the module only through welldefined operations. • Information hiding • Data encapsulation Reasoning about the correctness is done only on the basis of the ADT modules. But the performance analysis depends upon the implementation. Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

5

© Friedhelm Seutter, 2008

ADT specification The specification describes the logical relationships among the public parts of the ADT. Each operation is specified by a • Precondition, • Postcondition. The clients responsibility is to meet the precondition before calling an operation. The postcondition is guaranteed to be true on returning. Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

6

© Friedhelm Seutter, 2008

3

Types of ADT operations • Constructors: Creates a new object and returns a reference to it. • Access functions: Returns information about an object. • Manipulation procedures: Modifies an object.

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

7

© Friedhelm Seutter, 2008

Example ADT person /* Data string name string firstname date dateofbirth /* Operations person construct (string aName, string aFirstname, date aDate) date getdateofbirth (person aPerson) void changename (person aPerson, string newName) ... Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

8

© Friedhelm Seutter, 2008

4

Recursive ADTs An ADT is recursive if any of its access functions returns the same class as the ADT. Examples are • Lists, • Trees.

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

9

© Friedhelm Seutter, 2008

List A list is an ordered or unordered sequence of elements. A list may be empty or satisfy the following conditions: • There is a distinguished element, the first element. • The remaining elements constitute a list again, called the rest of the list.

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

10

© Friedhelm Seutter, 2008

5

List ADT list /* Data integer ...

key

/* Operations list create (integer newElement, list oldList) integer first (list aList) list rest (list aList) ...

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

11

© Friedhelm Seutter, 2008

Implementation as linked lists Singly linked

Doubly linked

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

12

© Friedhelm Seutter, 2008

6

List operations • Construction (unordered) list create (integer newElement, list oldList) – Precondition: none – Postcondition: If x = create (newElement, oldList), then 1. x refers to a newly created object 2. x is not empty 3. first(x) refers to newElement 4. rest(x) refers to oldList Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

13

© Friedhelm Seutter, 2008

List operations • Access functions integer first (list aList) – Precondition: aList is not empty – Postcondition:none list rest (list aList) – Precondition: aList is not empty – Postcondition:none

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

14

© Friedhelm Seutter, 2008

7

List operations • Manipulation functions (destructive, ascending order) list insert (list aList, integer newElement) – Precondition: aList is in ascending order – Postcondition:aList is in ascending order, newElement is in aList list delete (list aList, integer anElement) – Precondition: aList is in ascending order – Postcondition: aList is in ascending order, anElement is not in aList Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

15

© Friedhelm Seutter, 2008

List operations • Constant list nil – Constant denoting the empty list

• Library

(operations defined by ADT operations)

list search (list aList, integer anElement) – Precondition: none – Postcondition: none length, copy, equals, reverse, sum, etc. Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

16

© Friedhelm Seutter, 2008

8

Search operation

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

17

© Friedhelm Seutter, 2008

Complexity of list search • B(n) =

1

=

Θ(1)

• W(n) =

n

=

Θ(n)

• A(n) =

(n+1)/2 =

Θ(n)

(with n number of list elements)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

18

© Friedhelm Seutter, 2008

9

Binary tree A binary tree T is a set of elements, called nodes or vertices, that is empty or satisfies the following conditions: • There is a distinguished node r called root. • The remaining nodes are divided into two disjoint subsets, L and R, each of which is a binary tree. L is called the left subtree of T and R is called the right subtree of T. Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

19

© Friedhelm Seutter, 2008

Binary search tree A binary search tree T is a binary tree. Let x be a node. • If y is some node of the left subtree of x, then key[y] ≤ key[x] holds. • If y is some node of the right subtree of x, then key[y] ≥ key[x] holds.

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

20

© Friedhelm Seutter, 2008

10

Binary search tree (example of height 2) depth root

0

internal node 1

2 leaf Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

21

© Friedhelm Seutter, 2008

Binary tree ADT tree /* Data integer ...

key

/* Operations tree create (integer newElement, tree oldLT , tree oldRT) integer root (tree aTree) tree leftTree (tree aTree) tree rightTree (tree aTree) ... Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

22

© Friedhelm Seutter, 2008

11

Implementation of a node

parent (optional) key right

left

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

23

© Friedhelm Seutter, 2008

Tree operations • Construction tree create (integer newElem, tree oldLT, tree oldRT) – Precondition: none – Postcondition: If x = create (newElem, oldLT, oldRT), then 1. x refers to a newly created object 2. x is not empty 3. root(x) refers to newElem 4. leftTree(x) refers to oldLT 5. rightTree(x) refers to oldRT Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

24

© Friedhelm Seutter, 2008

12

Tree operations • Access functions integer root (tree aTree) – Precondition: aTree is not empty tree leftTree (tree aTree) – Precondition: aTree is not empty tree rightTree (tree aTree) – Precondition: aTree is not empty Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

25

© Friedhelm Seutter, 2008

Tree operations • Manipulation functions

(destructive, search tree)

tree insert (tree aTree, integer newElem) – Precondition: aTree is a search tree – Postcondition: aTree is a search tree with newElem inserted tree delete (tree aTree, integer anElem) – Precondition: aTree is a search tree – Postcondition: aTree is a search tree with anElem deleted Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

26

© Friedhelm Seutter, 2008

13

Tree operations • Constant tree nil – Constant denoting the empty tree

• Library

(operations defined by ADT operations)

tree search (tree aTree, integer anElem) – Precondition: aTree is a search tree – Postcondition: none height, copy, equals, sum, min, max, etc. Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

27

© Friedhelm Seutter, 2008

Search operation

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

28

© Friedhelm Seutter, 2008

14

Complexity of tree search • B(n) =

Θ(1)

• W(n) =

Θ( lg n)

• A(n) =

Θ( lg n)

(with n number of tree nodes and tree is search tree and complete)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

29

© Friedhelm Seutter, 2008

Properties of binary trees

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

30

© Friedhelm Seutter, 2008

15

Binary tree traversal All nodes of the tree have to be visited starting at the root. • Preoder tree walk • Inorder tree walk • Postorder tree walk

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

31

© Friedhelm Seutter, 2008

Binary tree traversal

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

32

© Friedhelm Seutter, 2008

16

Binary tree traversal

´ Preoder : Inorder: Postorder:

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

4, 3, 2, 3´, 5, 7 2, 3, 3´, 4, 5, 7 2, 3´, 3, 7, 5, 4

33

© Friedhelm Seutter, 2008

Stack A stack is a linear structure in which insertions and deletions are always made at one end, called the top. Update strategy: last in, first out (LIFO)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

34

© Friedhelm Seutter, 2008

17

Stack ADT stack /* Data object

element

/* Operations stack create ( ) boolean isEmpty (stack aStack) object top (stack aStack) void push (stack aStack, object anElem) void pop (stack aStack)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

35

© Friedhelm Seutter, 2008

Queue A queue is a linear structure in which insertions are done at one end, called the rear or end, and all deletions are done at the other end, called the front or head. Update strategy: first in, first out (FIFO)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

36

© Friedhelm Seutter, 2008

18

Queue ADT queue /* Data object

element

/* Operations queue create ( ) boolean isEmpty (queue aQueue) object front (queue aQueue) void enqueue (queue aQueue, object anElem) void dequeue (queue aQueue)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

37

© Friedhelm Seutter, 2008

Priority Queue A priority queue is a structure in which insertions are done in an order (e. g. ascending) of an element’s priority, and all deletions are done at the front.

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

38

© Friedhelm Seutter, 2008

19

Priority Queue ADT prioqueue /* Data object

element

/* Operations prioqueue create ( ) boolean isEmpty (prioqueue aPQ) object getMin (prioqueue aPQ) void insert (prioqueue aPQ, object anElem) void delete (prioqueue aPQ)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

39

© Friedhelm Seutter, 2008

Disjoint Set A disjoint set is a structure for a set of sets, S = {S1, S2, . . . , Sn}. The elements Si and Sj are pairwise disjoint, 1 ≤ i, j ≤ n .

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

40

© Friedhelm Seutter, 2008

20

Disjoint Set ADT disjointset /* Data object

element

/* Operations disjointset create ( ) void makeSet (disjointset aSet, object anElem) set findSet (disjointset aSet, object anElem) object find (disjointset aSet, object anElem) void union (disjointset aSet, object elemA, object elemB)

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

41

© Friedhelm Seutter, 2008

Dictionary A dictionary is a data structure which consists of a key and a certain information that needs to be stored and retrieved. The access to the information is attained by the key.

Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

42

© Friedhelm Seutter, 2008

21

Dictionary ADT dictionary /* Data integer object

key information

/* Operations dictionary create ( ) boolean member (dictionary aDict, integer key) object retrieve (dictionary aDict, integer key) void store (dictionary aDict, integer key, object info) void delete (dictionary aDict, integer key) Fachhochschule Braunschweig / Wolfenbüttel Institut für Angewandte Informatik

43

© Friedhelm Seutter, 2008

22