Você está na página 1de 3

Abstract Data Types (ADT)

MIS 301: ADT, Algorithm, Time Complexity & Big-O Notation


Niranjan KESANI Nov 5th, 2003

Definition: A set of data values and associated operations that are precisely specified independent of any particular implementation. Since the data values and operations are defined with mathematical precision , rather than as an implementation in a computer language, we may reason about effects of the operations, relations to other abstract data types, whether a program implements the data type, etc.

ADT Example
n

ADT Example (cont.)


n

One of the simplest abstract data types is the stack. The operations new(), push(v, S), top(S), and pop(S) may be defined as following:
q q q

new() returns a stack pop(push(v, S)) = S top(push(v, S)) = v

From these axioms, one may define equality between stacks, define a pop function which returns the top value in a non-empty stack, etc. For instance, the predicate isEmpty(S) may be added and defined with the following additional axioms:
q q

where S is a stack and v is a value.

isEmpty(new()) = true isEmpty(push(v, S)) = false

ADT (Cont.)
n

ADT (Cont.)
n

An implementation of an ADT consists of storage structures commonly called data structures to store the data items and algorithms for the basic operations and relations. The idea of data abstraction, in which the definition of the data type is separated from its implementation, is an important concept in software design. Data abstraction improves program modularity and information hiding.

Four levels of the refinement process of data specification


q q q q

on the abstract level on the data structures level on the implementation level on the application level

Algorithm
n

Algorithm (Cont.)
n

Definition: An algorithm is a finite set of instructions which accomplish a particular task and satisfy the following criteria:
q q q q q

input: zero or more output: at least one finiteness: must terminate in finite time correctness preciseness: clear and unambiguous

Comparison: q storage requirement q execution time requirement (trade-off between time and efficiency) q time to develop the algorithm q time to maintain the algorithm Heuristics vs. Algorithms q Heuristics do not guarantee to terminate in finite time, nor do they guarantee correctness. q Heuristics are useful to solve problems that have no known or efficient algorithmic solutions. Sometimes they are based on rul eof-thumb or argument derived from experience. There DO exist unsolvable problems (Gdel Theorem)!!

Time Complexity
n

Time Complexity (Cont.)


n

Running times for different algorithms fall into different complexity classes. Each complexity class is characterized by a different family of curves. All of the curves in a given complexity class share the same basic shape. This shared shape is characterized by an equation that gives running times as a function of problem size.
q q q

constant linear a quadratic complexity class has the function


n

f(n) = an2 + bn + c

The execution time of an algorithm is influenced by several factors. One is the size of the input. The execution time T of an algorithm must be expressed as a function T (n) of the size n of the input. We use T (n) as the approximate count of the instructions executed, NOT the real time units.

exponential

Time Complexity (Cont.)


n

Big-O Notation: Order-of-Magnitude


n

Consider the following algorithm for finding the mean of a set of n numbers stored in an array: sum = 0; i = 0; while ( i < n) { sum += x[i]; i++; } mean = sum/n; The computing time for this algorithm T (n) is 3n + 4.

In general, the computing time T(n) of an algorithm is said to have order of magnitude f(n), denoted T(n) is O(f(n)) if there is some constant C such that T(n) = Cf(n) for all sufficiently large values of n. key insights: focusing on dominant terms; ignoring the constant of proportionality. The complexity of the preceding algorithm is O(n).

Big-O Notation (Cont.)


n

Big-O Notation (Cont.)


n

The best-case performance of an algorithm is usually not very informative, the average performance is often difficult to determine. T(n) is frequently taken as a measure of the algorithms performance in the worse case. Common orders: O(1) mean computing time that is bounded by a constant; O(n) means that the time is directly proportional to n, and is called linear time; We call O(n 2) quadratic time, O(n3) cubic, O(log n) logarithmic time, O(2 n) exponential.

It is important to know that O-notation should not be used to draw general conclusions for problems of small size. NP complete, NP hard problems: very hard problems, presumably exponential.

Typical Complexity Classes


n n n n

Practical Hints
n

n n n n

Algorithms which have the same O( ) complexity belong to the same complexity class. Common complexity classes: O(1) constant time: independent of input length O(log N) logarithmic: usually results from splitting the task into smaller tasks, where the size of the task is reduced by a constant fraction O(N) linear: usually results when a given constant amount of processing is carried out on each element in the input. O(N log N) : splitting into subtasks and combining the results later O(N2): quadratic. Usually arises when all pairs of input elements need to be processed O(2N ): exponential. Usually emerges from a brute-force solution to a problem.

n n

Find the actual function which shows how the time/memory usage grows depending on the input N. Omit all constant factors. If the function contains different powers of N, (e.g. N 4 + N 3 + N 2), leave only the highest power (N 4). Similarly, an exponential (2N) eventually outgrows any polynomial in N.

Warning about O-Notation


n

O-notation only gives sensible comparisons of algorithms when N is large. Consider two algorithms for same task:
q q

Linear: g(N) = 1000 N is in O(N) Quadratic: g'(N) = N2/1000 is in O(N2 )

n n

The quadratic one is faster for N < 1 000 000. Some constant factors are machine dependent, but others are a property of the algorithm itself.