Data structure and algorithm in Python

Similar documents
LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Asymptotic Analysis of Algorithms

Analysis of Algorithms

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

Outline and Reading. Analysis of Algorithms 1

Choice of C++ as Language

Elementary maths for GMT. Algorithm analysis Part I

Programming II (CS300)

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Algorithmic Complexity

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

Data Structures and Algorithms

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

Data Structures Lecture 8

What is an algorithm?

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

(Refer Slide Time: 1:27)

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

Analysis of Algorithm. Chapter 2

ANALYSIS OF ALGORITHMS

UNIT 1 ANALYSIS OF ALGORITHMS

Extended Introduction to Computer Science CS1001.py

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Assignment 1 (concept): Solutions

COMP Analysis of Algorithms & Data Structures

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Lecture Notes on Sorting

Algorithmic Analysis. Go go Big O(h)!

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Comparison of x with an entry in the array

Algorithm Analysis. October 12, CMPE 250 Algorithm Analysis October 12, / 66

Introduction to Data Structure

CSc 225 Algorithms and Data Structures I Case Studies

Extended Introduction to Computer Science CS1001.py Lecture 7: Basic Algorithms: Sorting, Merge; Time Complexity and the O( ) notation

Data Structures and Algorithms CSE 465

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

Measuring the Efficiency of Algorithms

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

ASYMPTOTIC COMPLEXITY

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Scientific Computing. Algorithm Analysis

Data Structures and Algorithms

Data structure and algorithm in Python

ASYMPTOTIC COMPLEXITY

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

CS 261 Data Structures. Big-Oh Analysis: A Review

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Lecture 5: Running Time Evaluation

What is an Algorithm?

Chapter 2: Complexity Analysis

Algorithms and Data Structures

CS:3330 (22c:31) Algorithms

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Algorithm Analysis. This is based on Chapter 4 of the text.

ECE 2400 Computer Systems Programming Fall 2018 Topic 8: Complexity Analysis

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Introduction to Computer Science

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

Round 1: Basics of algorithm analysis and data structures

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

Introduction to Analysis of Algorithms

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Module 07: Efficiency

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Data structure and algorithm in Python

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

L6,7: Time & Space Complexity

Algorithm Efficiency & Sorting. Algorithm efficiency Big-O notation Searching algorithms Sorting algorithms

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

Topic Number 2 Efficiency Complexity Algorithm Analysis

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

CS 137 Part 7. Big-Oh Notation, Linear Searching and Basic Sorting Algorithms. November 10th, 2017

Algorithm Analysis. CENG 707 Data Structures and Algorithms

IE 495 Lecture 3. Septermber 5, 2000

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

CSE 373 Spring Midterm. Friday April 21st

26 How Many Times Must a White Dove Sail...

csci 210: Data Structures Program Analysis

Analysis of Algorithms. CSE Data Structures April 10, 2002

Algorithm Efficiency, Big O Notation, and Javadoc

Lecture 5 Sorting Arrays

Algorithm Analysis. Big Oh

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Algorithmics. Some information. Programming details: Ruby desuka?

O(1) How long does a function take to run? CS61A Lecture 6

Quiz 1 Practice Problems

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Measuring algorithm efficiency

Algorithm Analysis and Design

EE 368. Week 6 (Notes)

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Algorithm Analysis. Review of Terminology Some Consequences of Growth Rates. The book s big example: Detecting anagrams. There be math here!

Transcription:

Data structure and algorithm in Python Algorithm Analysis Xiaoping Zhang School of Mathematics and Statistics, Wuhan University

Table of contents 1. Experimental studies 2. The Seven Functions used in the analysis of algorithms 3. Asymptotic Analysis 1

data structure is a systematic way of organizing and accessing dat; algorithm is a step-by-step procedure for performing some task in a finite amout of time. The primary analysis tool involves characterizing the running time of algorithms and data structure algorithms, with space usage also being of interest. 2

Running time is a natural measure of goodness, since time is a precious time - computer solutions should run as fast as possible. increases with the input size be affected by the hardware environment (e.g., the processor, clock rate, memory, disk) and software environment (e.g., the operating system, programming language). 3

Experimental studies

Experimental studies Elapsed time from time import time start_ time = time () run algorithm end_ time = time () elapsed = end_ time - start_ time 4

Experimental studies The time function in time module The clock function in time module The module timeit 5

Challenges of Experimental Analysis Experiments should be performed in the same hardware and software environments. Experiments can be done only on a limited set of test inputs; hence, they leave out the running times of inputs not included in the experiment (and these inputs may be important). An algorithm must be fully implemented in order to execute it to study its running time experimentally. 6

Experimental studies Moving Beyond Experimental Analysis

Moving Beyond Experimental Analysis Our goal is to develop an approach to analyzing the efficiency of algorithms that: Allows us to evaluate the relative efficiency of any two algorithms in a way that is independent of the hardware and software environment. Is performed by studying a high-level description of the algorithm without need for implementation. Takes into account all possible inputs. 7

Counting Primitive Operations Define a set of primitive operations: Assigning an identifier to an object Determining the object associated with an identifier Performing an arithmetic operation (for example, adding two numbers) Comparing two numbers Accessing a single element of a Python list by index Calling a function (excluding operations executed within the function) Returning from a function. Formally, a primitive operation corresponds to a low-level instruction with an execution time that is constant. 8

Counting Primitive Operations Instead of trying to determine the specific execution time of each primitive operation, we will simply count how many primitive operations are executed, and use this number t as a measure of the running time of the algorithm. The implicit assumption of operation count is the running times of different primitive operations will be fairly similar. Thus, the number, t, of primitive operations an algorithm performs will be proportional to the actual running time of that algorithm. 9

Measuring Operations as a Function of Input Size To capture the order of growth of an algorithm s running time, we will associate, with each algorithm, a function f (n) that characterizes the number of primitive operations that are performed as a function of the input size n. 10

Focusing on the Worst-Case Input Average-case analysis quite challenging requires to define a probability distribution on the set of inputs Worst-case analysis much easier requires only the ability to identify the worst-case input, which is often simple typically leads to better algorithms 11

The Seven Functions used in the analysis of algorithms

The Seven Functions used in the analysis of algorithms constant logarithm linear n-log-n quadratic cubic exponetial 1 logn n nlogn n 2 n 3 a n 12

The Seven Functions used in the analysis of algorithms Ideally, we would like data structure operations to run in times proportional to the constant or logarithm function, and we would like our algorithms to run in linear or n-log-n time; Algorithms with quadratic or cubic running times are less practical; Algorithms with exponential running times are infeasible for all but the smallest sized inputs. 13

The Seven Functions used in the analysis of algorithms 14

Asymptotic Analysis

Asymptotic Analysis In algorithm analysis, we focus on the growth rate of the running time as a function of the input size n, taking a big-picture approach. def find_max ( data ): """ Return the maximum element from a nonempty Python list. """ biggest = data [0] for val in data : if val > biggest biggest = val return biggest 15

Asymptotic Analysis The Big-Oh Notation

The Big-Oh Notation Definition Let f (n) and g(n) be functions mapping positive integers to positive real numbers. We say that f (n) is O(g(n)) if there is a real constant c > 0 and an integer constant n 0 1 such that f (n) cg(n), for n n 0. This definition is often referred to as the big-oh notation, for it is sometimes pronounced as f (n) is big-oh of g(n)". 16

The Big-Oh Notation 17

Some Properties of the Big-Oh Notation The big-oh notation allows us to ignore constant factors and lower-order terms and focus on the main components of a function that affect its growth. Example 5n 4 +3n 3 +2n 2 +4n +1 is O(n 4 ). Proposition If f (n) is a polynomial of degree d, that is, f (n) = a 0 +a 1 n + +a d n d, and a d > 0, then f (n) is O(n d ). 18

Some Properties of the Big-Oh Notation Example 5n 2 +3nlogn +2n +5 is O(n 2 ). Example 20n 3 +10nlogn +5 is O(n 3 ). Example 3logn +2 is O(logn). Example 2 n+2 is O(2 n ). Example 2n +100logn is O(n). 19

Asymptotic Analysis Comparative Analysis

Comparative Analysis 20

Comparative Analysis 21

Asymptotic Analysis Examples of Algorithm Analysis

Constant-Time Operations Example Given an instance, named data, of the Python list class, a call to the function, len(data), is evaluated in constant time. This is a very simple algorithm because the list class maintains, for each list, an instance variable that records the current length of the list. 22

Constant-Time Operations Example The expression data[j] is evaluated in O(1) time for a Python list. Because Python s lists are implemented as array-based sequences, references to a list s elements are stored in a consecutive block of memory. The j th element of the list can be found, not by iterating through the list one element at a time, but by validating the index, and using it as an offset into the underlying array. In turn, computer hardware supports constant-time access to an element based on its memory address. 23

Linear-Time Operations the Problem of Finding the Maximum of a Sequence def find_max ( data ): """ Return the maximum element from a nonempty Python list. """ biggest = data [0] for val in data : if val > biggest biggest = val return biggest Initialization uses O(1) time The loop executes n times, and within each iteration, it performs one comparison and possibly one assignment statement (as well as maintenance of the loop variable). Enacting a return statement in Python uses O(1) time. 24

Prefix Average Example : Prefix Average Given a sequence S consisting of n numbers, we want to compute a sequence A such that A[j] is the average of elements S[0],...,S[j], for j = 0,...,n1, that is, A[j] = j i=0 S[i] j +1 25

Prefix Average: Quadratic-Time Algorithm def prefix_average1 (S): """ Return list such that, for all j, A[j] equals average of S[0],..., S[j]. """ n = len (S) A = [0] * n for j in range (n): total = 0 for i in range ( j + 1): total += S[i] A[j] = total / (j +1) return A The running time of prefix_average1 is O(n 2 ) 26

Prefix Average: Another Quadratic-Time Algorithm def prefix_average2 (S): """ Return list such that, for all j, A[j] equals average of S[0],..., S[j]. """ n = len (S) A = [0] * n for j in range (n): A[j] = sum (S [0: j +1]) / (j +1) return A The running time of prefix_average2 is O(n 2 ) 27

Prefix Average: A Linear-Time Algorithm def prefix_average3 (S): """ Return list such that, for all j, A[j] equals average of S[0],..., S[j]. """ n = len (S) A = [0] * n total = 0 for j in range (n): total += S[j] A[j] = total / (j +1) return A The running time of prefix_average3 is O(n) 28

Three-Way Set Disjointness Definition : Three-Way Set Disjointness Given three sequences of numbers: A, B, and C. Assume that no individual sequence contains duplicate values, but that there may be some numbers that are in two or three of the sequences. The threeway set disjointness problem is to determine if the intersection of the three sequences is empty, namely, that there is no element x such that x A,x B, and x C. 29

Three-Way Set Disjointness def disjoint1 (A, B, C): """ Return True if there is no element common to all three lists. """ for a in A: for b in B: for c in C: if a == b == c: return False return True If each of the original sets has size n, then the worst-case running time of this function is O(n 3 ). 30

Three-Way Set Disjointness def disjoint2 (A, B, C): """ Return True if there is no element common to all three lists. """ for a in A: for b in B: if a == b: for c in C: if a == c return False return True The running time of disjoint2 is O(n 2 ) 31

Element Uniqueness Definition : Element Uniqueness Given a single sequence S with n elements, whether all elements of that collection are distinct from each other? 32

Element Uniqueness def unique1 (S): """ Return True if there are no duplicate elements in sequence S. """ for j in range ( len (S)): for k in range (j+1, len (S)): if S[j] == S[k]: return False return True The running time of unique1 is O(n 2 ) 33

Element Uniqueness def unique2 (S): """ Return True if there are no duplicate elements in sequence S. """ temp = sorted (S) for j in range (1, len ( temp )): if S[j -1] == S[j]: return False return True The running time of unique2 is O(nlogn) 34