Software Quality What are the important qualities of a good algorithm? · correctness · robustness · ease of implementation · readability and maintainability.

Slides:



Advertisements
Similar presentations
HST 952 Computing for Biomedical Scientists Lecture 10.
Advertisements

Reference: Tremblay and Cheston: Section 5.1 T IMING A NALYSIS.
Algorithm Analysis (Big O) CS-341 Dick Steflik. Complexity In examining algorithm efficiency we must understand the idea of complexity –Space complexity.
the fourth iteration of this loop is shown here
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Complexity Analysis (Part I)
CS107 Introduction to Computer Science
Analysis of Algorithms (Chapter 4)
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
Asymptotic Analysis Motivation Definitions Common complexity functions
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Elementary Data Structures and Algorithms
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Mathematics Review and Asymptotic Notation
Analysis of Algorithms
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Asymptotic Notation (O, Ω, )
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
3.3 Complexity of Algorithms
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis Part of slides are borrowed from UST.
Algorithm Analysis (Big O)
COSC 1P03 Data Structures and Abstraction 2.1 Analysis of Algorithms Only Adam had no mother-in-law. That's how we know he lived in paradise.
Analysis Tools. Acknowledgement Ms. Krishani Abeysekera Dr. Bun Yue Mr. Charles Moen Dr. Wei Ding Dr. Michael Goodrich.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
21-Feb-16 Analysis of Algorithms II. 2 Basics Before we attempt to analyze an algorithm, we need to define two things: How we measure the size of the.
Section 1.7 Comparing Algorithms: Big-O Analysis.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Searching algorithms can be applied to different kinds of containers. Searching algorithms can search for different things.  search for the value 4.00.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Algorithm Analysis 1.
Mathematical Foundation
Analysis of Algorithms
Introduction to Search Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Lecture 06: Linked Lists (2), Big O Notation
Big-Oh and Execution Time: A Review
GC 211:Data Structures Algorithm Analysis Tools
What is CS 253 about? Contrary to the wide spread belief that the #1 job of computers is to perform calculations (which is why the are called “computers”),
Programming and Data Structure
Programming and Data Structure
GC 211:Data Structures Algorithm Analysis Tools
Algorithms Analysis Algorithm efficiency can be measured in terms of:
At the end of this session, learner will be able to:
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Complexity Analysis (Part II)
Algorithm Efficiency and Sorting
Algorithms and data structures: basic definitions
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Software Quality What are the important qualities of a good algorithm? · correctness · robustness · ease of implementation · readability and maintainability · efficiency What does this mean?

Measuring time & space An algorithm’s run time and/or memory requirements depend upon ____. · consider an algorithm to alphabetize the La Crosse phone book. · consider a program to print new license plates for WI. · consider an algorithm to print class lists for UW-L courses. What is the relationship between time & space? · consider detasseling corn · consider the tile arranging puzzle Time and space may have different characteristics.... However, the same approach is used to study both.

Counting the Cost (execution time) private int[][] mileage = {{0, 722, 2244, 800, 896}, {722, 0, 1047, 2113, 831}, {2244, 1047, 0, 1425, 1576}, {800, 2113, 1425, 0, 2849, {896, 831, 1576, 2849, 0} }; private int[][] mileage = {{0, 722, 2244, 800, 896}, {722, 0, 1047, 2113, 831}, {2244, 1047, 0, 1425, 1576}, {800, 2113, 1425, 0, 2849, {896, 831, 1576, 2849, 0} }; Example: Averaging a distance table algorithm: Average the cells under the diagonal.

Counting the Cost (execution time) Count the number of times each instruction executes. public int averageDistance(int[][] mileage) { int fromCity, toCity; int totalDistance = 0; fromCity = 0; while (fromCity != mileage.length) { toCity = fromCity + 1; while (toCity != mileage[].length) { totalDistance = totalDistance + mileage[fromCity][toCity]; toCity++; } fromCity++; } return totalDistance / (mileage.length*(mileage.length-1)/2); } public int averageDistance(int[][] mileage) { int fromCity, toCity; int totalDistance = 0; fromCity = 0; while (fromCity != mileage.length) { toCity = fromCity + 1; while (toCity != mileage[].length) { totalDistance = totalDistance + mileage[fromCity][toCity]; toCity++; } fromCity++; } return totalDistance / (mileage.length*(mileage.length-1)/2); } Let n denote mileage.length t j denote time to execute the j th instruction ______ ______ ______ ______ _________ _________ ________ ____... ____ Count Ex. time = t 1 + t 2 + t 3 + (n+1)*t 4 + n*t 5 + (n*(n-1)/2+1)*t 6 + (n*(n-1)/2)*t 7 + (n*(n-1)/2)*t 8 + n*t 9 + t 10

execution time (sec.) Estimating time & space Consider an algorithm to determine the BCS rankings. Such an algorithm depends largely upon the number of Division 1 football teams. Our analysis of the algorithm’s execution time is based on (1) an estimate of this time. (2) an estimate based upon “larger” numbers of teams. · · · · · Number of BCS teams (n) estimate(n)

Big-O Approximation Rather than worry about exact execution time, we count... · count the number of instructions executed · count the number of variable assignments · count the number of method calls · etc. · Let count(n) = the exact count for data amount, n · We select estimate(n) = an approximation with the following property for all j (j > c 1 ) [c 2 *estimate(j) ≥ count(j)], for some constants c 1, c 2 · For any such estimate it it proper to write O(estimate(n)) = count(n) stated “the big-O for count(n) is estimate(n)” but an approximation is sufficient

Example · What is countOfConditions(n)? -- assume that n is arr.length Defn: O(est(n)) means for all j (j > c 1 ) ______________________, for constants c 1, c 2 int ndx = 0; while (ndx != arr.length-1) { if (arr[ndx] < arr[ndx+1]) { temp = arr[ndx]; arr[ndx] = arr[ndx+1]; arr[ndx+1] = temp; } ndx++; } int ndx = 0; while (ndx != arr.length-1) { if (arr[ndx] < arr[ndx+1]) { temp = arr[ndx]; arr[ndx] = arr[ndx+1]; arr[ndx+1] = temp; } ndx++; } · What is maxCountOfAssignments(n)? · This algorithm is O( n )

Simplify, Simplify, Simplify The best Big-O functions have two properties: (1) they are simplified (2) they are a good approximation of function shape Simplification axioms O( c * f(n) ) = O( f(n) ), if c is a constant O( f(n) + g(n) ) = O( f(n) ), if f(n) dominates g(n) O( f(n) * g(n) ) = O(f(n)) * O(g(n)) Common Dominating functions (for variables n and m) O(log n) dominates O(1) O( n ) dominates O(log n) O( n a ) dominates O( n b ) when a > b O( n(log n) ) dominates O( n ) O( n 2 ) dominates O( n(log n) ) O( a n ) dominates O( n b ) for any constants a, b ≥ 2 O( a n ) dominates O( b n ) when a > b O( n! ) dominates O( a n ) for any constant a O( n m ) dominates O( n! ) constantlogarithmic linear polynomial exponential factorial

Simplify the following 9 * n 3 2 * n * n 2 2 * n n 8*n 2 * (3*n 3 + 2*n 2 + 5*n) 3*m 2 + 4*n 3 25

Function Growth Does functional growth really matter? (note 3) (note 1) (note 2) nlog 2 nn2n2 n3n3 2n2n 3n3n note 1 - roughly ta day of computation for a 1 petaflop supercomputer note 2 - about 50 billion times the age of the universe note 3 - a really BIG number.

Function Pix (linear scale)

Function Pix (logarithmic scale)

Function Pix (w/o factorial)

Code Analysis Most single instructions... O(1) x = a*b / 2; System.out.println( arr[3] ); Sequence of instructions... O(S 1 ) + O(S 2 ) O(S n ) { temp = arr[j]; arr[j] = arr[k]; arr[k] = temp; } Loop... O(loopBody) * O(numberOfRepetitions) int j=0; while (j!=arr.length-1) { arr[j] = arr[j+1]; for (int k=0; k!=arr.length/2; k++) { temp = arr[k]; arr[k] = arr[arr.length-1-k]; arr[arr.length-1-k] = temp; } j++; }

More Code Analysis Selection... It’s a choice! if (condition) S 1 ; else S 2 ; in the worst case... maximum of O(S 1 ), O(S 2 ) in the best case... minimum of O(S 1 ), O(S 2 ) in the average case... expectations regarding S 1 and S 2 WARNINGS 1) Average case is generally difficult/impossible. 2) Best case is worst!

What is the Big-O? Assume that S j is O(1). for (int r=0; r!=w/2; r++) { S 1 ; for (int c=0; r!=w; r++) { S 2 ; } S 3 ; } int z = w; while (z > 0) { S 4 ; for (int r=w; r>3; r--) { S 5 ; } z = z / 2; } O(1) O(1) ____ _______ O(1) O(1) O(1) O(1) _________________ Overall: ______

/** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void selectionSort(double[] arr) { int lastSorted = -1; int minNdx; double temp; while (lastSorted != arr.length-1) { minNdx = smallestNdxAmongUnsorted(arr, lastSorted); temp = arr[minNdx]; arr[minNdx] = arr[lastSorted+1]; arr[lastSorted+1] = temp; lastSorted++; } /** post: ls+1 <= result < arr.length * and forAll j:(ls<=j<arr.length) [arr[result] <= arr[j]] */ public int smallestNdxAmongUnsorted(double[] arr, int ls) { int smallNdx = ls + 1; int j = ls + 2; while (j < arr.length) { if (arr[j] < arr[smallNdx]) smallNdx = j; j++; } return smallNdx; } /** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void selectionSort(double[] arr) { int lastSorted = -1; int minNdx; double temp; while (lastSorted != arr.length-1) { minNdx = smallestNdxAmongUnsorted(arr, lastSorted); temp = arr[minNdx]; arr[minNdx] = arr[lastSorted+1]; arr[lastSorted+1] = temp; lastSorted++; } /** post: ls+1 <= result < arr.length * and forAll j:(ls<=j<arr.length) [arr[result] <= arr[j]] */ public int smallestNdxAmongUnsorted(double[] arr, int ls) { int smallNdx = ls + 1; int j = ls + 2; while (j < arr.length) { if (arr[j] < arr[smallNdx]) smallNdx = j; j++; } return smallNdx; } Selection Sort

/** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void bubbleSort(double[] arr) { int lastSwapNdx = arr.length; int newSwapNdx; double temp; while (lastSwapNdx > 0) { newSwapNdx = 0; for (int j=0; j!=lastSwapNdx-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; newSwapNdx = j+1; } lastSwapNdx = newSwapNdx; } /** post: forAll j:(0<=j<a.length-1) [arr[j]<=arr[j+1]] */ public void bubbleSort(double[] arr) { int lastSwapNdx = arr.length; int newSwapNdx; double temp; while (lastSwapNdx > 0) { newSwapNdx = 0; for (int j=0; j!=lastSwapNdx-1; j++) { if (arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; newSwapNdx = j+1; } lastSwapNdx = newSwapNdx; } Bubble Sort What is the best case? What is the worst case?