Elementary Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Advertisements

Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Chapter 9: Searching, Sorting, and Algorithm Analysis
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.
Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.
Introduction to Analysis of Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
The Efficiency of Algorithms
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Algorithm Analysis (Big O)
Algorithm Cost Algorithm Complexity. Algorithm Cost.
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
For Wednesday Read Weiss chapter 3, sections 1-5. This should be largely review. If you’re struggling with the C++ aspects, you may refer to Savitch, chapter.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Chapter 19: Searching and Sorting Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
CS 221 Analysis of Algorithms Instructor: Don McLaughlin.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Analyzing algorithms & Asymptotic Notation BIO/CS 471 – Algorithms for Bioinformatics.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
©Silberschatz, Korth and Sudarshan3.1 Algorithms Analysis Algorithm efficiency can be measured in terms of:  Time  Space  Other resources such as processors,
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
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.
CSC 211 Data Structures Lecture 13
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Introduction to Analysis of Algorithms CS342 S2004.
Algorithm Analysis Part of slides are borrowed from UST.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Algorithm Analysis (Big O)
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.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Chapter 2 Algorithm Analysis
Chapter 18: Searching and Sorting Algorithms
Introduction to Search Algorithms
Algorithm design and Analysis
Searching, Sorting, and Asymptotic Complexity
Algorithms Analysis Algorithm efficiency can be measured in terms of:
Estimating Algorithm Performance
Analysis of Algorithms
Presentation transcript:

Elementary Data Structures and Algorithms Algorithm Analysis Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Algorithm Analysis There are many different algorithms to solve the same problem Ask 5 programmers to write a non-trivial program, you will get 5 different solutions Which is best? Correctness Efficiency Java Programming: Program Design Including Data Structures

Computational Resources Algorithms require resources to run Time (processor operations) Space (computer memory) Network bandwidth Programmer time Two types of costs Fixed: same every time we run the algorithm Variable: depends on the size of the input Java Programming: Program Design Including Data Structures

Measuring Resource Use How can we compare the resources used by different algorithms? Empirical Code both algorithms Run them an record the resources used You did this in the Fibonacci lab! Java Programming: Program Design Including Data Structures

Empirical Analysis Problems Depends on code quality/implementation Better/worse programmers, not the algorithm itself Depends on computer speed/architecture Depends on language/compiler efficiency Depends on the input E.g. linear search is very fast for some inputs, and very slow for others Java Programming: Program Design Including Data Structures

Analytical Approach Analyze the algorithm itself Abstract away from implementation details How many operations will be executed? How much memory is used? Consider different cases (depending on input) Best Worst Average Java Programming: Program Design Including Data Structures

Counting Operations int i = 2; int j = 2; int k = i + j; System.out.println(k); How many operations are there? Assignment: Addition: Print: Total: Java Programming: Program Design Including Data Structures

Counting Operations int i = 2; int j = 2; int k = i + j; System.out.println(i+j+k); How many operations are there? Assignment: Addition: Print: Total: Java Programming: Program Design Including Data Structures

Counting Operations int i = 0; while (i < 10) { System.out.println(i); i++; } How many operations are there? Assignment: Comparison: Increment: Print: Total: Java Programming: Program Design Including Data Structures

Counting Operations for (int i=0; i < 10; i++) { System.out.println(i); } How many operations are there? Assignment: Comparison: Increment: Print: Total: Java Programming: Program Design Including Data Structures

Counting Operations for (int i=0; i < n; i++) { System.out.println(i); } How many operations are there? Assignment: Comparison: Increment: Print: Total: Java Programming: Program Design Including Data Structures

Counting Operations for (int i=0; i < n; i++) { for (int j=0; j < n; j++) { System.out.println(i); } How many operations are there? Assignment: Comparison: Increment: Print: Total: Java Programming: Program Design Including Data Structures

Counting Operations So far, we have counted every operation This is quite tedious, especially for infrequent operations Focus on the most important operation Most frequent May need to figure out what this is Java Programming: Program Design Including Data Structures

Another Look at Search Algorithms We have discussed two ways to search a list Linear search (unordered data) Binary search (sorted data) Data is sorted by “keys” Unique for each element Well-defined order Java Programming: Program Design Including Data Structures

Linear (Sequential) Search public int seqSearch(T[] list, int length, T searchItem) { int loc; boolean found = false; for (loc = 0; loc < length; loc++) if (list[loc].equals(searchItem)) found = true; break; } if (found) return loc; else return -1; Java Programming: Program Design Including Data Structures

Sequential Search Analysis The statements in the for loop are repeated several times For each iteration of the loop, the search item is compared with an element in the list When analyzing a search algorithm, you count the number of comparisons Suppose that L is a list of length n The number of key comparisons depends on where in the list the search item is located Java Programming: Program Design Including Data Structures

Sequential Search Analysis (continued) Best case The item is the first element of the list You make only one key comparison Worst case The item is the last element of the list You make n key comparisons What is the average case Java Programming: Program Design Including Data Structures

Sequential Search Analysis (continued) To determine the average case Consider all possible cases Find the number of comparisons for each case Add them and divide by the number of cases Average case On average, a successful sequential search searches half the list Java Programming: Program Design Including Data Structures

Binary Search public int binarySearch(T[] list, int length, T searchItem) { int first = 0; int last = length - 1; int mid = -1; boolean found = false; while (first <= last && !found) mid = (first + last) / 2; Comparable<T> compElem = (Comparable<T>) list[mid]; Java Programming: Program Design Including Data Structures

Binary Search (continued) if (compElem.compareTo(searchItem) == 0) found = true; else if (compElem.compareTo(searchItem) > 0) last = mid - 1; first = mid + 1; } if (found) return mid; return -1; }//end binarySearch Java Programming: Program Design Including Data Structures

Binary Search Example Figure 18-1 Sorted list for a binary search Table 18-1 Values of first, last, and middle and the Number of Comparisons for Search Item 89 Java Programming: Program Design Including Data Structures

Performance of Binary Search Suppose that L is a sorted list of size n And n is a power of 2 (n = 2m) After each iteration of the for loop, about half the elements are left to search The maximum number of iteration of the for loop is about m + 1 Also m = log2n Each iteration makes two key comparisons Maximum number of comparisons: 2(m + 1) Java Programming: Program Design Including Data Structures

Comparison: Linear vs Binary Worst case number of comparison List Size Linear Binary 4 6 8 32 12 512 20 1048576 42 Java Programming: Program Design Including Data Structures

Asymptotic Analysis: Motivation So far, we have counted operations exactly We don’t really care about the details Computers execute billions of operations per second A few here or there is negligible Care about overall scalability As the input size grows, does computation grow quickly or slowly? Don’t lose the forest for the trees Java Programming: Program Design Including Data Structures

Asymptotic Analysis Asymptotic means the study of the function f as n becomes larger and larger without bound Consider functions g(n) = n2 and f(n) = n2 + 4n + 20 As n becomes larger and larger, the term 4n + 20 in f(n) becomes insignificant g(1000) = 1,000,000 and f(1000) = 1,004,020 You can predict the behavior of f(n) by looking at the behavior of g(n) Java Programming: Program Design Including Data Structures

Asymptotic Algorithm Analysis Identify a function that describes the growth in runtime as the input gets large An “upper bound” of sorts on the running time Typically worst-case, but occasionally average case Describe the number of operations done using a function Focus only on most important operations Ignore one time initializations, etc. Java Programming: Program Design Including Data Structures

Common Asymptotic Functions Table 18-4 Growth Rate of Various Functions Java Programming: Program Design Including Data Structures

Common Functions, visual Figure 18-9 Growth Rate of Various Functions Java Programming: Program Design Including Data Structures

Java Programming: Program Design Including Data Structures

Asymptotic Notation: Big-O Notation (continued) Table 18-7 Some Big-O Functions That Appear in Algorithm Analysis Java Programming: Program Design Including Data Structures

Big-Oh Notation (Definition) A function f(n) is O(g(n)) if there exist positive constants c and n0 such that: f(n) ≤ cg(n) for all n ≥ n0 Java Programming: Program Design Including Data Structures

Big-Oh Notation Translation: After some point, f(n) is always smaller than g(n) “Some point” refers to increasing problem size The constant c says that we don’t care about multipliers So, 2n and n have the same essential growth rate 2n is O(n) Java Programming: Program Design Including Data Structures

Asymptotic Notation: Big-O Notation (continued) Table 18-8 Number of Comparisons for a List of Length n Java Programming: Program Design Including Data Structures