Download presentation

Presentation is loading. Please wait.

Published byChloe Dawson Modified over 3 years ago

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

2
Slide: 2 CAMP 09 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

3
Slide: 3 CAMP 09 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

4
Slide: 4 CAMP 09 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 )

5
Slide: 5 CAMP 09 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.

6
Slide: 6 CAMP 09 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.

7
Slide: 7 CAMP 09 Fundamentals of Complexity Asymptotic Notations Upper bound Lower bound Tight bound What is Complexity ? Examples of Complexity

8
Slide: 8 CAMP 09 Asymptotic (Upper) : O() for all n > n 0 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 (n 2 ) 3000*n = O (n 2 ) 15*n 2 = O (n 2 ) 0.1*nlgn = O (n 2 ) 0.00000000000001*n 3 O (n 2 ) n0n0 c g(n) f(n) n

9
Slide: 9 CAMP 09 Asymptotic (Lower): () for all n > n 0 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. 2n 3 + 4n = (n 2 ) n0n0 c g(n) f(n) n

10
Slide: 10 CAMP 09 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 n 2 – 3n = (n 2 ) n0n0 c 1 g(n) c 2 g(n) f(n) n

11
Slide: 11 CAMP 09 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

12
Slide: 12 CAMP 09 Sorting Complexities

13
Slide: 13 CAMP 09 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); }

14
Slide: 14 CAMP 09 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)*log 2 (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) *log 2 (arrSize) + c+a*(arrSize+1)) = O(n 2 ) arrSize=1

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google