Data Structures and Algorithms



Similar documents
Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Big Ideas in Mathematics

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

NEW YORK STATE TEACHER CERTIFICATION EXAMINATIONS

Sequential Data Structures

Lecture 3: Finding integer solutions to systems of linear equations

Why? A central concept in Computer Science. Algorithms are ubiquitous.

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Integer Factorization using the Quadratic Sieve

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

MATHEMATICS: CONCEPTS, AND FOUNDATIONS Vol. III - Logic and Computer Science - Phokion G. Kolaitis

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Algebra 2 Chapter 1 Vocabulary. identity - A statement that equates two equivalent expressions.

Current Standard: Mathematical Concepts and Applications Shape, Space, and Measurement- Primary

1 Teaching notes on GMM 1.

Overview. Essential Questions. Precalculus, Quarter 4, Unit 4.5 Build Arithmetic and Geometric Sequences and Series

Solving Systems of Linear Equations Using Matrices

MATH. ALGEBRA I HONORS 9 th Grade ALGEBRA I HONORS

Creating, Solving, and Graphing Systems of Linear Equations and Linear Inequalities

Continued Fractions and the Euclidean Algorithm

Georgia Department of Education Kathy Cox, State Superintendent of Schools 7/19/2005 All Rights Reserved 1

Nonlinear Optimization: Algorithms 3: Interior-point methods

CHAPTER 2 Estimating Probabilities

Note on growth and growth accounting

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

The Running Time of Programs

Objectives. Materials

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Testing LTL Formula Translation into Büchi Automata

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Method To Solve Linear, Polynomial, or Absolute Value Inequalities:

General Framework for an Iterative Solution of Ax b. Jacobi s Method

ALGEBRA 2 CRA 2 REVIEW - Chapters 1-6 Answer Section

U.C. Berkeley CS276: Cryptography Handout 0.1 Luca Trevisan January, Notes on Algebra

Figure 1: Graphical example of a mergesort 1.

Lies My Calculator and Computer Told Me

Precalculus REVERSE CORRELATION. Content Expectations for. Precalculus. Michigan CONTENT EXPECTATIONS FOR PRECALCULUS CHAPTER/LESSON TITLES

1 Short Introduction to Time Series

5 Homogeneous systems

recursion, O(n), linked lists 6/14

The van Hoeij Algorithm for Factoring Polynomials

Sources: On the Web: Slides will be available on:

Quotient Rings and Field Extensions

Instruction Set Architecture (ISA)

11 Multivariate Polynomials

Examples of Functions

This unit will lay the groundwork for later units where the students will extend this knowledge to quadratic and exponential functions.

Data Structures. Algorithm Performance and Big O Analysis

Sequence of Numbers. Mun Chou, Fong QED Education Scientific Malaysia

Circuit Analysis using the Node and Mesh Methods

Data Structure [Question Bank]

Algebra Academic Content Standards Grade Eight and Grade Nine Ohio. Grade Eight. Number, Number Sense and Operations Standard

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

TU e. Advanced Algorithms: experimentation project. The problem: load balancing with bounded look-ahead. Input: integer m 2: number of machines

A simple and fast algorithm for computing exponentials of power series

AP Calculus AB Syllabus

Linear Programming for Optimization. Mark A. Schulze, Ph.D. Perceptive Scientific Instruments, Inc.

CHAPTER 1 Splines and B-splines an Introduction

Partial Fractions. (x 1)(x 2 + 1)

Interpretation of Test Scores for the ACCUPLACER Tests

Complexity Classes P and NP

Entry Level College Mathematics: Algebra or Modeling

Review of Fundamental Mathematics

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Lecture Notes on Linear Search

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Mathematics (MAT) MAT 061 Basic Euclidean Geometry 3 Hours. MAT 051 Pre-Algebra 4 Hours

8 Divisibility and prime numbers

Thnkwell s Homeschool Precalculus Course Lesson Plan: 36 weeks

WritePlacer Sample Topic. WritePlacer. Arithmetic

0.8 Rational Expressions and Equations

Regular Languages and Finite Automata

Math 115 Spring 2011 Written Homework 5 Solutions

1 Review of Newton Polynomials

MapReduce and Distributed Data Analysis. Sergei Vassilvitskii Google Research

AN INTRODUCTION TO NUMERICAL METHODS AND ANALYSIS

MATH BOOK OF PROBLEMS SERIES. New from Pearson Custom Publishing!

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

CORRELATED TO THE SOUTH CAROLINA COLLEGE AND CAREER-READY FOUNDATIONS IN ALGEBRA

Determinants can be used to solve a linear system of equations using Cramer s Rule.

Optimization Modeling for Mining Engineers

Several Views of Support Vector Machines

Access Code: RVAE4-EGKVN Financial Aid Code: 6A9DB-DEE3B-74F

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

Section IV.1: Recursive Algorithms and Recursion Trees

Chapter 5 Names, Bindings, Type Checking, and Scopes

PES Institute of Technology-BSC QUESTION BANK

How To Understand And Solve Algebraic Equations

Static Program Transformations for Efficient Software Model Checking

Fairness in Routing and Load Balancing

Math 1050 Khan Academy Extra Credit Algebra Assignment

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

FRACTIONS OPERATIONS

The PageRank Citation Ranking: Bring Order to the Web

The Methodology of Application Development for Hybrid Architectures

Transcription:

Data Structures and Algorithms Computational Complexity Escola Politècnica Superior d Alcoi Universitat Politècnica de València

Contents Introduction Resources consumptions: spatial and temporal cost Costs characterization Cost boundaries: best, worst and average cases Asymptotic notation: computational costs hierarchy Additional aspects in computational complexity Computational complexity Data Structures and Agorithms 06-07 2

Introduction Generally, a problem can be solved by using several algorithms or programs. Although, not all the solutions are equally good. How can we measure how good a program (that solves a problem) is? How elegant is the idea in which the algorithm is based on? How clear is the program code organized? How showiness are the results presented? How efficient the algorithm is in obtaining the solution? From a computational point of view, the essential factor is efficiency. Computational complexity Data Structures and Agorithms 06-07 3

Introduction Algorithm analysis is a very important activity in the development process, especially in constrained resource environments (in time or memory). Algorithm analysis is required in order to: Compare two or more different algorithms. Foresee the behavior of an algorithm in extreme conditions. Adjust the algorithm parameters to get the best results. Computational complexity Data Structures and Agorithms 06-07 4

Introduction Algorithm analysis can be carried out in two different ways: empirically (experimental, a posteriori), or theoretically (a priori) Type Advantages Disadvantages empirically Easy, real results We need same quality in the implementations, real data, machine dependency theoretically Flexible, cheap, machine independent Not exact results Computational complexity Data Structures and Agorithms 06-07 5

Resources consumption: spatial and temporal costs Efficiency: capacity of solving the proposed problem using a low consumption of computational resources. Two fundamental factors of efficiency: Spatial cost or quantity of memory required Temporal cost or time required to solve the problem In order to solve a given problem, an algorithm or program A will be better than another B if A solves the problem in less time and/or uses less quantity of memory than B. Generally there is a tradeoff between time and space, so a good algorithm is the one that solves the problem with a good commitment. We will focus our attention mainly on temporal cost. Computational complexity Data Structures and Agorithms 06-07 6

A simple example Sometimes, just time or memory are not only suitable in order to appreciate the quality of a program. Let s consider three simple programs to calculate 10 2 : Be the times required to carried out a product, sum, and successor. Computational complexity Data Structures and Agorithms 06-07 7

A simple example Let s assume that A1, A2, A3 are executed in four different computers with different characteristics (different times for successor, sum and product execution). Which program is the best, A1, A2 or A3? For each computer there s a best one. Computational complexity Data Structures and Agorithms 06-07 8

Problems, instances and size of an instance Our simple problem was too restrictive. In a normal situation we would like to calculate no only 10 2 but, in general, n 2, where n is a program data. With this new point of view, the specific problem of calculating 10 2 is considered as an instance of the general problem of calculating n 2. Given a problem, in general not all of its instances are of the same size. The size of an instance is an integer number that measures how big this instance is. In our problem of calculating n 2, a natural measurement of the size is precisely n. So, the size of the instance 10 2 is 10 and the size of the instance 2345678 2 is 2345678. Computational complexity Data Structures and Agorithms 06-07 9

Cost as a function of the size of the problem We can easily rewrite A1, A2, A3 in order to calculate n 2 instead of 10 2. Size=n; temporal costs (μs): Cost still depends on Computational complexity Data Structures and Agorithms 06-07 10

Cost as a function of the size is still not suitable Intuitively, it seems clear that A1 is the best program and A3 the worst. But, even fixing the computer characteristics, we will see that our intuition is not clearly kept. For example, if we fix Which is the better, A1, A2, A3? For each instance there s a best one. A good cost characterization should allow to establish the program quality independently of the computer and of the particular sizes of the instances to process. Computational complexity Data Structures and Agorithms 06-07 11

Asymptotic cost In general, we should appreciate a relative behavior of A1, A2, A3 as follows: Calculus of n 2 : relative costs of A1, A2, A3 Temporal cost Size A good computational characterization of a program: Cost functional dependency with the size of input for large sizes! Computational complexity Data Structures and Agorithms 06-07 12

Advantages in the asymptotic characterization of the computational cost in function of the size of input Generally, programs are useful to solve problems of large sizes of input (if they are small, we could solve them manually Considering large sizes of input, we can carry out simple approximations that simplify considerably cost analysis Programs no longer depend on specific execution time values of the different elementary instructions used (if they don t depend on the size of input), and don t depend on sizes of input of specific instances of the program to solve Computational complexity Data Structures and Agorithms 06-07 13

Simplification of cost analysis: the step concept A STEP is the execution of a code segment which processing time doesn t depend on the size of input of the considered program, or it is bounded by a constant. Computational cost of a program: number of STEPS as a function of the size of input of the program Elements to which 1 STEP can be assigned: Assignment, arithmetic or logical operations, comparison, access to a vector or matrix element, etc. Any finite sequence of the previous instructions which length doesn t depend on the size of input. Elements to which 1 STEP cannot be assigned, but a number of STEPS as a function of the size of input: Assignment of structured variables (ex: arrays) which number of elements depends on the size of input Loops (recursion) which number of iteration (calls) depends on the size of input Computational complexity Data Structures and Agorithms 06-07 14

Cost analysis (STEPS) of n 2 programs Computational complexity Data Structures and Agorithms 06-07 15

Another example on cost analysis with STEPS (printing the elements of an array in reverse order) STEP: Accesses to v; Size of input = n; T invord (n) = n+n=2n STEPS Computational complexity Data Structures and Agorithms 06-07 16

More examples on cost analysis with STEPS (searching the closest element to the average) STEP: Accesses to A; Size of input = n; T closest (n) =??? STEPS Computational complexity Data Structures and Agorithms 06-07 17

More examples on cost analysis with STEPS (statistical mode for a series of ages: data input) Computational complexity Data Structures and Agorithms 06-07 18

More examples on cost analysis with STEPS (inefficient calculus of statistical mode) STEP: if comparisons; Size of input = n; T mod (n) =??? STEPS Computational complexity Data Structures and Agorithms 06-07 19

More examples on cost analysis with STEPS (more efficient calculus of statistical mode) STEP: if comparisons; Size of input = n; T mod (n) =??? STEPS Computational complexity Data Structures and Agorithms 06-07 20

More examples on cost analysis with STEPS (efficient calculus of statistical mode) STEP: if comparisons; Size of input = n; T mod (n) =??? STEPS Computational complexity Data Structures and Agorithms 06-07 21

Costs for the different mode approaches Time (seconds) Size of input (number of ages processed) Computational complexity Data Structures and Agorithms 06-07 22

Costs for the different mode approaches (efficient aproaches) Time (seconds) Size of input (number of ages processed) Computational complexity Data Structures and Agorithms 06-07 23

More examples on cost analysis with STEPS (weird functions) STEP: +=; Size of input = n; T main (n) =??? STEPS Computational complexity Data Structures and Agorithms 06-07 24

Sometimes, cost is not (only) a function of the size of input In the examples viewed so far, all the instances of a problem had the same computational cost. But this is not always the case. In the following example, which is the cost of the function busca(n)? Computational complexity Data Structures and Agorithms 06-07 25

Sometimes, cost is not (only) a function of the size of input STEP: if comparison; size of input=n; T busca (n) =??? Dependsonthearraycontentsandtheparticular valuetolookfor! Computational complexity Data Structures and Agorithms 06-07 26

Cost extremes: best, worst and average cases Temproal cost Size of input Computational complexity Data Structures and Agorithms 06-07 27

Sometimes it is difficult to calculate the exact costs for a size of input. Example: Computational complexity Data Structures and Agorithms 06-07 28

Sometimes it is difficult to calculate the exact costs for a size of input. Example: STEP: accesses to X. The total number varies in different ways with the instancies of the size of input depending if that is multiple or not of 3 Computational complexity Data Structures and Agorithms 06-07 29

Superior and inferior cost boundaries Temproal cost Size of input Generally, it is enough to determine the asymptotic boundaries, i.e. for large size of inputs. Computational complexity Data Structures and Agorithms 06-07 30

Essential aspects in computational costs determination Cost cannot be expressed with just one value. It has to be expressed as a cost function the size of input of the problem to solve. Comparison of cost functions has to be insensitive to implementation constants, as particular execution times of individual operations (which costs is independent of the size of input) The independency from the constants is achieved by only considering the asymptotic behavior of the function cost (i.e., for large sizes of input) Frequently, for a given size of input, there are several instancies with different costs, therefore the cost cannot be properly expressed as a function of the size of input. In such cases, it is convenient to determine superior and inferior boundaries of the cost function, i.e., in the best and worst cases. There are even situations in which the boundaries for best and worst cases are complex functions. Generally, it will be enough to determine simple functions that bound superiorly and inferiorly the costs of all the instancies for large sizes of input. Computational complexity Data Structures and Agorithms 06-07 31

Asymptotic notation Abstraction of constants associated to STEP concept, notion of large sizes of input and asymptotic boundaries Be a function from natural numbers to the positive real. It is defined: From the definition of we have: Computational complexity Data Structures and Agorithms 06-07 32

Examples Computational complexity Data Structures and Agorithms 06-07 33

Examples Computational complexity Data Structures and Agorithms 06-07 34

Examples of costs in asymptotic notation Mode: (inefficient) (efficient) busca: Worst case: Best case: Average case: raro: Worst case: Best case: Average case: Computational complexity Data Structures and Agorithms 06-07 35

Asymptotic notation: some useful properties Order relation among order functions: Order of sum of functions: dominant function Computational complexity Data Structures and Agorithms 06-07 36

Asymptotic notation: some useful properties Limit Rule For two arbitrary functions and if then if then but and if then but For example, and. Which is the relative order? Computational complexity Data Structures and Agorithms 06-07 37

Asymptotic notation: some useful properties Some sums of series Computational complexity Data Structures and Agorithms 06-07 38

Asymptotic notation: some useful properties Order of polynomial functions Computational complexity Data Structures and Agorithms 06-07 39

Asymptotic notation: some useful properties Order of exponential, logarithmic etc. functions Computational complexity Data Structures and Agorithms 06-07 40

Asymptotic notation: computational costs hierarchy Some usual order relations: Computational complexity Data Structures and Agorithms 06-07 41

Asymptotic notation: computational costs hierarchy sublinear, linear and superlinear Computational complexity Data Structures and Agorithms 06-07 42

Asymptotic notation: computational costs hierarchy superlinear, polynomial and exponential Computational complexity Data Structures and Agorithms 06-07 43

Computational costs hierarchy: practical consequences Maximum size of a problem, n, that can be solved by several algorithms and computers with different processing performance: By increasing the calculus time and/or the computer performance, an algorithm... logarithmic one increases enormously the size of input of tackled problems linear ones (or nlogn) achieves linear increments (or almost) of the tackled sizes quadratic (polynomial) ones achieve proportional moderate improvements exponential ones only achieve negligible additive improvements Computational complexity Data Structures and Agorithms 06-07 44

Computational costs hierarchy: practical consequences Execution times of a machine that executes 10 9 steps by second (~ 1 GHz), as a function of the algorithm cost and the size of input n: Computational complexity Data Structures and Agorithms 06-07 45

Other concepts of interest in computational complexity Spatial Complexity Memory address concept: storage space where one or more data is located, which extension is independent of the size of input of the instancies of the considered problem Spatial cost of a program or algorithm: number of memory addresses required for its execution All the concepts studied in temporal cost analysis are directly applicable to spatial cost Computational complexity Data Structures and Agorithms 06-07 46