Notes on Labs and Assignments. Commenting Revisited You have probably been able to “get away” with poor inline documentation is labs  Tasks are straightforward.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lecture: Algorithmic complexity
Practice Quiz Question
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Tyler Robison Summer
CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis Dan Grossman Spring 2010.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Analysis of Algorithms (Chapter 4)
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
CS503: First Lecture, Fall 2008 Michael Barnathan.
Elementary Data Structures and Algorithms
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
Analysis CS 367 – Introduction to Data Structures.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Arrays Tonga Institute of Higher Education. Introduction An array is a data structure Definitions  Cell/Element – A box in which you can enter a piece.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Major objective of this course is: Design and analysis of modern algorithms Different variants Accuracy Efficiency Comparing efficiencies Motivation thinking.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Introduction to: Programming CS105 Lecture: Yang Mu.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
CS 150: Analysis of Algorithms. Goals for this Unit Begin a focus on data structures and algorithms Understand the nature of the performance of algorithms.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Searching Topics Sequential Search Binary Search.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Search Algorithms Written by J.J. Shepherd. Sequential Search Examines each element one at a time until the item searched for is found or not found Simplest.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
CMPT 438 Algorithms.
Analysis of Algorithms
Introduction to Algorithms
COMP 53 – Week Seven Big O Sorting.
Introduction to Algorithms
Teach A level Computing: Algorithms and Data Structures
Big-Oh and Execution Time: A Review
Algorithm design and Analysis
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Objective of This Course
Big O Notation.
Analysis of Algorithms
Algorithmic complexity
Algorithms and data structures: basic definitions
Presentation transcript:

Notes on Labs and Assignments

Commenting Revisited You have probably been able to “get away” with poor inline documentation is labs  Tasks are straightforward  Often only one key method is involved On assignments this is not going to work  Bigger variety of solutions  Tougher to follow as a marker (or user)

The Header Comment It is good practice to include an initial comment in the source file for each class /*AlphaChars.java Author: Aaron Hunter Date: September 26, 2006 CMPT Lab 2 Provides methods to check if a string contains letters. */

Method Descriptions It is also good practice to include a comment preceding each method /*Checks if a single character is a letter.*/ public static boolean isAlpha( char c) { if (((c >= 'a') && (c = 'A') && (c <= 'Z'))) return true; else return false; }

Another Method /* Checks if a string contains all letters. */ public static boolean isAllAlpha(String s) { for(int i=0; i<s.length(); i++) { \\ Check each character. If not a letter, \\ return false. if(!isAlpha(s.charAt(i))) return false; } \\ If the loop completes without returning false, \\ then everything is a letter and we should \\ \\ \\ return true. return true; }

Another Method /* Checks if a string contains all letters. */ public static boolean isAllAlpha(String s) { for(int i=0; i<s.length(); i++) { if(!isAlpha(s.charAt(i))) return false; } return true; }

Abstracting The General Problem Given a method hasProperty(Type x)  checks if x has some property, returns true if it does Given a variable xArray of type Type[] : Does every element of xArray have the property? Does some element of xArray have the property? Do no elements of xArray have the property?

Solution – All public static boolean allHaveProperty(Type[] xArray) { for(int i=0; i<xArray.length; i++) { if(!hasProperty(xArray[i])) return false; } return true; }

Solution – Some public static boolean someHaveProperty(Type[] xArray) { for(int i=0; i<xArray.length; i++) { if(hasProperty(xArray[i])) return true; } return false; }

Solution – None public static boolean noneHaveProperty(Type[] xArray) { for(int i=0; i<xArray.length; i++) { if(hasProperty(xArray[i])) return false; } return true; }

Lab 2 – Basic Comments Use comments  comments before methods  comments about important bits of code e.g. Why are you casting a char to an int ? Use sub-methods  if you are copying and pasting lots of code, maybe you need another method Make the code testable

Running Time

Speed When writing programs, we often want them to be fast Several things affect this:  The algorithm that is implemented  The way the algorithm is implemented  Programming language used  Capabilities of the hardware We won’t worry about the 3 rd and 4 th points

Algorithm vs. Implementation The algorithm is the step by step procedure used to solve a problem  One problem can be solved by different algorithms e.g. linear search vs. binary search  One algorithm can be implemented in different ways in Java e.g. linear search with a for loop vs. linear search with a while loop

Algorithm vs. Implementation So… the algorithm is the procedure that is used to solve the problem … the implementation of the algorithm is the way the algorithm is described in Java  (or some other programming language) Algorithms are the kind of thing we can describe with pseudo-code – implementations are described in Java.

Implementation For a given algorithm, there will be many ways it can be implemented  e.g. loop forward or backwards, order of if conditions, how to split into methods, lazy/active boolean operators…  some of these affect the speed of the program No rules here: programming experience helps  so does knowledge of system architecture, compilers, interpreters, language features,…

Algorithm Analysis To analyze an algorithm is to determine the amount of “resources” needed for execution Typically there are two key resources:  Time  Space (memory) We will only be concerned with running time today

Algorithm Analysis Donald Knuth (1938--)  pioneer in algorithm analysis  wrote “The Art of Programming” (I-III) mails $2.56 for every error found  developed the Tex typesetting system  bible analysis through random sampling "Beware of bugs in the above code; I have only proved it correct, not tried it."

Algorithm Analysis The inherent running time of an algorithm almost always overshadows the implementation  e.g. There is nothing we can do to make Power1 run as fast as Power2 (for large values of y)  e.g. For sorted arrays, binary search is always faster (for large arrays, in the worst case)

Measuring Running Time To evaluate the efficiency of an algorithm:  We can’t just time it Different architectures, hidden processes, etc.  Need something that allows us to generalize Idea: count the number of “steps” required  For an input of size n Will be measured in terms of “Big-O” notation

Measuring Running Time We define algorithms for any input (of appropriate type) What is the size of the input?  For numerical inputs… it is just the input value n  For string inputs… normally the length What is a step?  Normally it is one command

Actual Running Time Consider the following pseudocode algorithm: Given input n int[] n_by_n = new int[n][n]; for i<n,j<n set n_by_n[i][j] = random(); print “The random array is created”;

Actual Running Time How many steps does it take for input n?  1 step: declare an n by n array  n 2 steps: put random numbers in the array  1 step: print out the final statement Total steps: n Note: the extra 2 steps don’t carry the same importance as the n 2 steps  as n gets big… the extra two steps are negligible

Actual Running Time We also think of constants as negligible  we want to say n 2 and c*n 2 have “essentially” the same running time as n increases  more accurately: the same asymptotic running time Commercial programmers would argue here… constants can matter in practice

Actual Running Time But for large n, the constants don’t matter nearly as much Plug in n=1000  n = [+500]  2n 2 = [ ]  n 3 = [ ]  2 n = (approx)[ ]

Big O Notation Running time will be measured with Big-O notation Big-O is a way to indicate how fast a function grows e.g. “Linear search has running time O(n) for an array of length n”  indicates that linear search takes about O(n) steps

Big-O Notation When we say an algorithm has running time O(n):  we are saying it runs in the same time as other functions with time O(n)  we are describing the running time ignoring constants  we are concerned with large values of n

Big-O Rules Ignore constants:  O(c * f(n)) = O(f(n)) Ignore smaller powers:  O(n 3 + n) = O(n 3 ) Logarithms cost less than a power  Think of log n as equvialent to n 0.000….001  O(n a+0.1 ) > O(n a log n) > O(n a )  e.g. O(n log n + n) = O(n log n)  e.g. O(n log n +n 2 ) = O(n 2 )

Why Big-O? Look at what happens for large inputs  small problems are easy to do quickly  big problems are more interesting  larger function makes a huge difference for big n Ignores irrelevant details  Constants and lower order terms depend on implementation  Don’t worry about that until we’ve got a good algorithm

Running Time Graphs

Determining Running Time Need to count the number of “steps” to complete  need to consider the worst case  for input of size n  a “step” must take constant (O(1)) time Often:  iterations of the inner loop * work per loop  recursive calls * work per call

Why Does log Keep Coming Up? By default, we write log n for log 2 n High school math:  log b c = e meansb e = c  so: log 2 n is the inverse of 2 n log 2 n is the power of 2 that gives result n

Why Does log Keep Coming Up? Exponential algorithm – O(2 n )  Increasing input by 1 doubles running time Logarithmic algorithm – log n  The inverse of doubling…  Doubling input size increases running time by 1  Intuition: O(log n) means that every step in the algorithm divides the problem size in half

Example 1: Search Linear search:  checks each element in array  does some other stuff in java implementation… but just a constant number of steps  O(n) - “order n” Binary search  chops array in half with each step  n n/2 n/4 n/8 …. 2 1  takes log n steps: O(log n) - “order log n”

Example 2 Power 1:  x y = x * x y-1  makes y recursive calls: O(y) Power 2:  x y = x y/2 * x y/2  Makes log y recursive calls: O(log y)  Had to be careful not to compute x y/2 twice Would have created a O(y) algorithm Instead: calculated once and stored in a variable

Computational Complexity All of this falls in the larger field of computational complexity theory Historically:  recursion theory: what can be computed? not everything it turns out  complexity theory: given a computable function, how much time and space are needed?

Computational Complexity Polynomial good, exponential bad  polynomial time = O(n k ) for any k  exponential time = basically O(2 n ) This is a big jump in time Will not be bridged by “faster computers”  polynomial algorithm on modern computer <1 second for large n  exponential algorithm can be in the centuries  1000 times faster computers… still in the centuries

Non-Deterministic Algorithms Allow the algorithm to “guess” a value and assume it is right  Det: check if a student is enrolled in CMPT 126  Non: find a student enrolled in CMPT 126 Intuitively: finding an example is harder than checking an example The big question: Can non-deterministic polynomial algorithms be captured with deterministic ones?

Non-Deterministic Algorithms Commonly called the “P=NP” problem Open for 30 years Currently there is a 1 million dollar prize for a solution (from the Clay Institute) All modern cryptography and e-commerce relies on the (unproved) assumption of a solution