Algorithm Efficiency, Big O Notation, and Javadoc

Similar documents
Algorithmic Complexity

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

What is an algorithm?

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

Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

ECE 122. Engineering Problem Solving Using Java

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

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

Measuring algorithm efficiency

Review: Array Initializer Lists

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

Algorithmic Analysis. Go go Big O(h)!

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Outline and Reading. Analysis of Algorithms 1

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

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

Choice of C++ as Language

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

Analysis of Algorithm. Chapter 2

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

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

Data structure and algorithm in Python

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

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Data Structures Lecture 8

Algorithm Analysis. Big Oh

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

Intro to Algorithms. Professor Kevin Gold

Elementary maths for GMT. Algorithm analysis Part I

Algorithm Analysis. CENG 707 Data Structures and Algorithms

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

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

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

Algorithms A Look At Efficiency

Analysis of Algorithms

Introduction to the Analysis of Algorithms. Algorithm

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

Recitation 9. Prelim Review

We will use the following code as an example throughout the next two topics:

ASYMPTOTIC COMPLEXITY

Algorithms and Data Structures

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

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

Searching, Sorting. Arizona State University 1

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Algorithm Performance. (the Big-O)

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

CS:3330 (22c:31) Algorithms

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

CSE 100 Advanced Data Structures

Comparison of x with an entry in the array

Data Structures and Algorithms

Chapter 2: Complexity Analysis

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Instructions PLEASE READ (notice bold and underlined phrases)

CS 3410 Ch 5 (DS*), 23 (IJP^)

(Refer Slide Time: 1:27)

ECE6095: CAD Algorithms. Optimization Techniques

UNIT 1 ANALYSIS OF ALGORITHMS

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

Algorithms and Data Structures

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Programming II (CS300)

The Importance of Runtime Cost! Measuring Runtime Cost! How to Measure Runtime Cost?!

To explain growth rates and why constants and nondominating terms can be ignored in the estimation

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

An algorithm is a sequence of instructions that one must perform in order to solve a wellformulated

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

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

Searching and Sorting

EE 368. Week 6 (Notes)

CSI33 Data Structures

CSCI 261 Computer Science II

INF2220: algorithms and data structures Series 1

Introduction to Computer Science

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Arrays.

Analysis of Algorithms 1 / 18

What is an Algorithm?

Beginning Programming (Two Semesters) Semester One. Module One: Intro to Beginning Programming. Module Two: Computer Careers

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

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

Department of Computer Science and Engineering. CSE 2011: Fundamentals of Data Structures Winter 2009, Section Z

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

Data Structures Question Bank Multiple Choice

Copyright 2012 by Pearson Education, Inc. All Rights Reserved.

ASYMPTOTIC COMPLEXITY

ANALYSIS OF ALGORITHMS

Pseudo code of algorithms are to be read by.

CS 310: Order Notation (aka Big-O and friends)

CS126 Final Exam Review

Is This Algorithm Fast? Algorithm Analysis. Attendance Question 1. Grading Algorithms

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

Lecture Notes on Sorting

Objectives. Chapter 23 Sorting. Why study sorting? What data to sort? Insertion Sort. CS1: Java Programming Colorado State University

O(n): printing a list of n items to the screen, looking at each item once.

Building Java Programs Chapter 13

Transcription:

Algorithm Efficiency, Big O Notation, and Javadoc Algorithm Efficiency Big O Notation Role of Data Structures Javadoc Reading: o L&C 2.1-2.4 o http://algs4.cs.princeton.edu/14analysis o HTML Tutorial 1

Algorithm Efficiency Let s look at the following algorithm for initializing the values in an array: final int N = 500; int [] counts = new int[n]; for (int i=0; i<counts.length; i++) counts[i] = 0; The length of time the algorithm takes to execute depends on the value of N 2

Algorithm Efficiency In that algorithm, we have one loop that processes all of the elements in the array Intuitively: o If N was half of its value, we would expect the algorithm to take half the time o If N was twice its value, we would expect the algorithm to take twice the time That is true, and we say that the algorithm efficiency relative to N is linear 3

Algorithm Efficiency Let s look at another algorithm for initializing the values in a different array: final int N = 500; int [] [] counts = new int[n][n]; for (int i=0; i<counts.length; i++) for (int j=0; j<counts[i].length; j++) counts[i][j] = 0; The length of time the algorithm takes to execute still depends on the value of N 4

Algorithm Efficiency However, in the second algorithm, we have two nested loops to process the elements in the two dimensional array Intuitively: o If N is half its value, we would expect the algorithm to take one quarter the time o If N is twice its value, we would expect the algorithm to take quadruple the time That is true and we say that the algorithm efficiency relative to N is quadratic 5

Big-O Notation We use a shorthand mathematical notation to describe the efficiency of an algorithm relative to any parameter n as its Order or Big-O o We can say that the first algorithm is O(n) o We can say that the second algorithm is O(n 2 ) For any algorithm that has a function g(n) of the parameter n that describes its length of time to execute, we can say the algorithm is O(g(n)) We only include the fastest growing term and ignore any multiplying by or adding of constants 6

Eight Growth Functions Eight functions O(n) that occur frequently in the analysis of algorithms (in order of increasing rate of growth relative to n): o Constant 1 o Logarithmic log n o Linear n o Log Linear n log n o Quadratic n 2 o Cubic n 3 o Exponential 2 n o Exhaustive Search n! 7

Growth Rates Compared n=1 n=2 n=4 n=8 n=16 n=32 1 1 1 1 1 1 1 logn 0 1 2 3 4 5 n 1 2 4 8 16 32 nlogn 0 2 8 24 64 160 n 2 1 4 16 64 256 1024 n 3 1 8 64 512 4096 32768 2 n 2 4 16 256 65536 4294967296 n! 1 2 24 40320 20.9T Don t ask! 8

Travelling Salesman Problem Joke 9

Big-O for a Problem O(g(n)) for a problem means there is some O(g(n)) algorithm that solves the problem Don t assume that the specific algorithm that you are currently using is the best solution for the problem There may be other correct algorithms that grow at a smaller rate with increasing n Many times, the goal is to find an algorithm with the smallest possible growth rate 10

Role of Data Structures That brings up the topic of the structure of the data on which the algorithm operates If we are using an algorithm manually on some amount of data, we intuitively try to organize the data in a way that minimizes the number of steps that we need to take Publishers offer dictionaries with the words listed in alphabetical order to minimize the length of time it takes us to look up a word 11

Role of Data Structures We can do the same thing for algorithms in our computer programs Example: Finding a numeric value in a list If we assume that the list is unordered, we must search from the beginning to the end o On average, we will search half the list o Worst case, we will search the entire list o Algorithm is O(n), where n is size of array 12

Role of Data Structures Find a match with value in an unordered list int [] list = {7, 2, 9, 5, 6, 4}; for (int i=0; i<list.length, i++) if (value == list[i]) statement; // found it // didn t find it 13

Role of Data Structures If we assume that the list is ordered, we can still search the entire list from the beginning to the end to determine if we have a match But, we do not need to search that way Because the values are in numerical order, we can use a binary search algorithm Like the old parlor game Twenty Questions Algorithm is O(log2n), where n is size of array 14

Role of Data Structures Find a match with value in an ordered list int [] list = {2, 4, 5, 6, 7, 9}; int min = 0, max = list.length-1; while (min <= max) { if (value == list[(min+max)/2]) statement; // found it else if (value < list[(min+max)/2]) max = (min+max)/2-1; else min = (min+max)/2 + 1; } statement; // didn t find it 15

Role of Data Structures The difference in the structure of the data between an unordered list and an ordered list can be used to reduce algorithm Big-O This is the role of data structures and why we study them We need to be as clever in organizing our data efficiently as we are in figuring out an algorithm for processing it efficiently 16

Role of Data Structures The only data structure implemented in the Java language itself is the array using [ ] All other data structures are implemented in classes either our own or library classes To properly use a class as a data structure, we must know the Application Programmer s Interface (API) The API for a class is documented using Javadoc comments in the source code that can be used to auto-create a web page 17

Javadoc Javadoc is a JDK tool that creates HTML user documentation for your classes and their methods In this case, user means a programmer who will be writing Java code using your classes You can access Javadoc via the JDK CLI: > javadoc MyClass.java You can access Javadoc via Dr Java menu: Tools > Javadoc All Documents Tools > Preview Javadoc for Current Document 18

Javadoc The Javadoc tool scans your source file for specialized multi-line style comments: /** * <p>html formatted text here</p> */ Your Javadoc text is written in HTML so that it can appear within a standardized web page format 19

Block Tags for Classes At the class level, you must include these block tags with data (each on a separate line): /** * @author Your Name * @version Version Number or Date */ You should include HTML text describing the use of this class and perhaps give examples 20

Block Tags for Methods At the method level, you must include these block tags with data (each on a separate line): /** * @param HTML text for 1st parameter * @param HTML text for 2nd parameter *... * @return HTML text for return value */ If there are no parameters or return type, you can omit these Javadoc block tags 21

In Line Tags At any point in your Javadoc HTML text, you may use In- Line Tags such as @link: /** * <p>see website {@link name url} * for more details.</p> */ In-Line tags are always included inside { } These { } are inside the /** and */ so the compiler does not see them 22