Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

14
Slide: 1 Interra Induction Training Data Structure ADT & Complexity

Transcript of Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

Page 1: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

Slide: 1

Interra Induction Training

Data Structure

ADT & Complexity

Page 2: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 2

Module Outline

• Abstract Data Types

• Fundamentals of Complexity Lower & Upper Bounds What is Complexity ? Examples of Complexity

• Simple Data Structures Array List/Iterators Stack Queue

• Trees Terminology Representation Binary Search Tree

• Standard

• Optimized – AVL, 2-3-4, Red-Black, B

• Randomized – BST, Skip List

• Amortized - Splay

Traversals• In, Pre, Post order

• Hash Tables Clustering Collision Resolving Chaining Open Addressing Comparison between two

• Graphs Terminology Representation Traversals Spanning Trees

• Data Structures Summary

• STL Introduction Containers and Iterators slist list Vector pair map hash_map

Page 3: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 3

Abstract Data Type (ADT)

An ADT comprise three components Domain, D : Set of the data items that the ADT refers

to.

Operations, F : Set of operations performed on D.

• Operations can be BASIC or DERIVED

Axioms, A : Set of properties that are satisfied by F on D d = (D,F,A)

Specification of ADT does not bother about space and time efficiency, those are implementation issues.

Data type : a collection of values and and a set of operations on those values

Page 4: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 4

Data Structure

Defines how the collection of data is to be stored.

Data structures may be static or dynamic.

More often, a Data Structure provides the CONCRETE view of an ADT and therefore talks of the implementation issues as well.

Ex: real numbers:

• ( integer , integer )

• ( sign, exponent , mantissa )

Page 5: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 5

Operator Types

• Accessors : do not change anything.

• Manipulators : make changes to the data.

• Iterators : iterate over all elements in the collection.

• Often there are trade-offs. An ADT may be implemented in several ways.

• Example: A Stack ADT may be implemented using arrays or lists.

Page 6: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 6

Implementation of Data Structure

An implementation of a data structure d is a mapping from d to a set of other data structures.

The mapping defines how every object of d is represented by the objects of e.

it requires that every function of d must be implemeted using the functions defined in e.

Ex: real numbers

in C float / double.

Page 7: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 7

Fundamentals of Complexity

• Asymptotic Notations

Upper bound

Lower bound

Tight bound

• What is Complexity ?

• Examples of Complexity

Page 8: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 8

Asymptotic (Upper) : O()

for all n > n0 there exists a function g(n) f(n) constant * g(n)Then upper bound of f(n) is g(n)O-is an Upper Bound for f(n) within a constant factor

E.g. 20 = O(n2)3000*n = O(n2)15*n2 = O(n2)0.1*nlgn = O(n2)0.00000000000001*n3 O(n2)

n0

c g(n)f(n)

n

Page 9: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 9

Asymptotic (Lower): ()

for all n > n0 there exists a function g(n) constant * g(n) f(n) Then lower bound of f(n) is g(n)- is a Lower Bound for f(n) within a constant factor

E.g. 2n3 + 4n = (n2)

n0

c g(n)

f(n)

n

Page 10: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 10

Asymptotic (Strict): ()

for all n > n0 there exists a function g(n)

c1 * g(n) f(n) c2 * g(n)

Then tight bound of f(n) is g(n)

Although (g(n)) is a set, we write f(n) = (g(n)) to indicate that f(n) is a member of (g(n))

E.g. 1/2 n2 – 3n = (n2) n0

c1g(n)

c2g(n)f(n)

n

Page 11: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 11

What is Complexity?

• Complexity is an estimate of COST / EFFORT.

• COST often is

Time – usually measured in terms of number of (certain) operations

Memory – peak / final

• Complexity can be

Worst Case

Average Case

Best Case

Probabilistic

Page 12: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 12

Sorting Complexities

Sorting Type Average Complexity Worst-case Complexity

Bubble O(n2) O(n2)

Quick O(n logn) O(n2)

Heap O(n logn) O(n logn)

Merge O(n logn) O(n logn)

Insertion O(n2) O(n2)

Bucket O(n) O(n) assumes uniform distribution over [0,1]

Radix O(d(n+k)) = O(n) O(n) k is radix, d is the number of digits

Page 13: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 13

Complexity example

int *sort(int *inpArr, int n){ int *sortedArr = new int[arrSize]; sortedArr[0] = inpArr[0]; // insert first element for (int i=1; i< n; i++) // rest inserted in sorted order addElem(sortedArr, i, inpArr[i]); return sortedArr;}

void addElem(int *sortedArr, int arrSize, int val){ int low=0, high=arrSize-1, mid=high/2; while (high > low) {

if (val > sortedArr[mid]) low = mid+1;else if (val < sortedArr[mid] high = mid-1;else { high = low = mid; break; }mid = (high-low)/2 + low;

} if (val > sortedArr[mid]) addAtLoc(sortedArr, arrSize, val, mid+1); else addAtLoc(sortedArr, arrSize, val, mid);}

Page 14: Slide: 1 Interra Induction Training Data Structure ADT & Complexity.

CAMP 09Slide: 14

Complexity example

void addAtLoc(int *sortedArr, int arrSize, int val, int loc){ for (int i=arrSize; i>=loc; i--) {

sortedArr[i+1] = sortedArr[i]; } sortedArr[loc] = val;}

Assuming time is taken by each assignment is a, comparision is c

Worstcase time Complexity of addAtLoc() = arrSize*a+a = a*(arrSize+1) = O(arrSize)

Worstcase time Complexity of addElem() = worst complexity of binary search + complexity of addAtLoc() = (2c+2a)*log2(arrSize) + c+a*(arrSize+1) = O(arrSize)

Worst case Complexity of sort() = n * complexity of addElem() = 3*a + n*c

n+ n*a + ∑ ((2c+2a) *log2(arrSize) + c+a*(arrSize+1)) = O(n2) arrSize=1