CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.

Slides:



Advertisements
Similar presentations
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Advertisements

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
CS 3610/5610N data structures Lecture: complexity analysis Data Structures Using C++ 2E1.
Chapter 1 – Basic Concepts
the fourth iteration of this loop is shown here
Big-O and Friends. Formal definition of Big-O A function f(n) is O(g(n)) if there exist positive numbers c and N such that: f(n) = N Example: Let f(n)
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
20-Jun-15 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.
Cmpt-225 Algorithm Efficiency.
The Efficiency of Algorithms
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.
Algorithm Cost Algorithm Complexity. Algorithm Cost.
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.
CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a CSC 201 Analysis and Design of Algorithms Lecture 03: Introduction to a lgorithms.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
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.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Analysis CS 367 – Introduction to Data Structures.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
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,
CS 1704 Introduction to Data Structures and Software Engineering.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Analysis of Algorithms
SNU IDB Lab. Ch3. Asymptotic Notation © copyright 2006 SNU IDB Lab.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
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.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Complexity of Algorithms
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Fundamentals of Algorithms MCS - 2 Lecture # 8. Growth of Functions.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Introduction to Analysis of Algorithms CS342 S2004.
Lecture 2 Analysis of Algorithms How to estimate time complexity? Analysis of algorithms Techniques based on Recursions ACKNOWLEDGEMENTS: Some contents.
Chapter 7 Analysis of Algorithms © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Asymptotic Performance. Review: Asymptotic Performance Asymptotic performance: How does algorithm behave as the problem size gets very large? Running.
Big O David Kauchak cs302 Spring Administrative Assignment 1: how’d it go? Assignment 2: out soon… Lab code.
CS2852 Week 2, Class 2 Today Big-O runtime analysis Linked Lists Muddiest Point Lab Quiz Includes writing a method from ArrayList class (See next slide)
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 3358 NOTE SET 2 Data Structures and Algorithms 1.
Week 4 - Friday CS221.
Algorithm Analysis CSE 2011 Winter September 2018.
DATA STRUCTURES Introduction: Basic Concepts and Notations
Big-Oh and Execution Time: A Review
Algorithm Analysis (not included in any exams!)
Analysis of Algorithms
Asst. Dr.Surasak Mungsing
CSE 12 – Basic Data Structures
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Estimating Algorithm Performance
Presentation transcript:

CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at Permissions beyond the scope of this license may be available at LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org

Today’s Topics Algorithm analysis 1. More Big-O review: Big Omega and Big Theta 2. Finish up Linked Lists 3. Benchmarking of algorithms 2

Big-Omega Interpreting graphs

f1f1 f2f2 f(n) = O (g(n)), if there are positive constants c and n 0 such that f(n) ≤ c * g(n) for all n ≥ n 0.  Obviously f 1 = O(f 2 ) because f 1 > f 2 (after about n=10, so we set n 0 = 10)  f 2 is clearly a lower bound on f 1 and that’s what big- Ω is all about  But f 2 = Ω(f 1 ) as well!  We just have to use the “ c ” to adjust so f 1 that it moves below f 2 f(n) = Ω (g(n)), if there are positive constants c and n 0 such that f(n) ≥ c * g(n) for all n ≥ n 0.

f(n) = O (g(n)), if there are positive constants c and n 0 such that f(n) ≤ c * g(n) for all n ≥ n 0.  Obviously f 1 = O(f 2 ) because f 1 > f 2 (after about n=10, so we set n 0 = 10)  f 2 is clearly a lower bound on f 1 and that’s what big- Ω is all about  But f 2 = Ω(f 1 ) as well!  We just have to use the “ c ” to adjust so f 1 that it moves below f 2 f(n) = Ω (g(n)), if there are positive constants c and n 0 such that f(n) ≥ c * g(n) for all n ≥ n 0. f1f1 f2f2 c * f 2

f 3 = Ω(f 1 ) but f 1 ≠ Ω(f 3 )  There is no way to pick a c that would make an O(n 2 ) function (f 3 ) stay below an O(n) function (f 1 ). f1f1 f3f3

Summary Big-O  Upper bound on a function  f(n) = O(g(n)) means that we can expect f(n) will always be under the bound g(n)  But we don’t count n up to some starting point n 0  And we can “cheat” a little bit by moving g(n) up by multiplying by some constant c Big-Ω  Lower bound on a function  f(n) = Ω(g(n)) means that we can expect f(n) will always be over the bound g(n)  But we don’t count n up to some starting point n 0  And we can “cheat” a little bit by moving g(n) down by multiplying by some constant c

Big-θ  Tight bound on a function.  If f(n) = O(g(n)) and f(n) = Ω(g(n)), then f(n) = θ(g(n)).  Basically it means that f(n) and g(n) are interchangeable  Examples:  3n+20 = θ(10n+7)  5n n + 3 = θ(5n )

Big-θ and sloppy usage  Sometimes people say, “This algorithm is O(n 2 )” when it would be more precise to say that it is θ(n 2 )  They are intending to give a tight bound, but use the looser “big-O” term instead of the “big-θ” term that actually means tight bound  Not wrong, but not as precise  I don’t know why, this is just a cultural thing you will encounter among computer scientists

Count how many times each line executes, then say which O( ) statement(s) is(are) true. A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 3 ) E. Other/none/more (assume n = arr.length) int maxDifference(int[] arr){ max = 0; for (int i=0; i<arr.length; i++) { for (int j=0; j<arr.length; j++) { if (arr[i] – arr[j] > max) max = arr[i] – arr[j]; } return max; } Line #

Count how many times each line executes, then say which O( ) statement(s) is(are) true. A. f(n) = θ(2 n ) B. f(n) = θ(n 2 ) C. f(n) = θ(n) D. f(n) = θ(n 3 ) E. Other/none/more (assume n = arr.length) int maxDifference(int[] arr){ max = 0; for (int i=0; i<arr.length; i++) { for (int j=0; j<arr.length; j++) { if (arr[i] – arr[j] > max) max = arr[i] – arr[j]; } return max; } Line #

Linked Lists

Linked list of Objects public class LinkedList { private Node head; private int size; public class Node { public Object data; public Node next; public Node(Object d, Node n) { data = d; next = n; } ublic class List12 implements java.util.List YOUR HW ONE IS SLIGHTLY DIFFERENT! public class List12 implements java.util.List

Complete the code...  In the next set of slides, we will explore some common misunderstandings about linked lists by asking you to identify missing lines of code in linked list methods  This will be slightly different from your HW, to give you the idea but not do it for you  Methods we will look at are:  addFirst(Object o)  addLast(Object o)  remove(Object o)

How our class looks in memory - “head” points to first node - We keep track of size Null next pointer means “end of the list” next data next data next data next data head next data next data next data next data ls size 4 head NULL

addFirst (Object o) head NULL addLast (Object o) remove (Object o) NULL head

What line of code will correctly complete this method? public void addFirst (Object newItem) { Node newNode = new Node(newItem, head); ________________________; } A) No line is needed. The code is correct as written. B) head = head.next; C) head = newNode; D) newNode.next = head; Question to think about/discuss: what is the purpose or effect of passing head as an argument here?

What replaces the XXXXXX? public void addLast (Object newItem) { if (head == null) head = new Node(newItem, null); else { Node current = head; while (XXXXXX){ current = current.next; } current.next = new Node(newItem, null); } A) current == head B) current != null C) current.next != null D) head != null

What is worst case time complexity of addFirst and addLast? E) Other/none/more addFirstaddLast A)O(1) B)O(1)O(n) C)O(n)O(1) D)O(n)

Removal from Linked List public boolean remove (Object item){ // Removes the first instance of the object item // from the calling list, if present. // Returns true if item is present, false if not.... } NULL head

Suppose we have a reference (current) to the node containing the item to be removed. What additional information do we need to successfully remove the node? A) Nothing additional. B) A reference to the node immediately prior to the deleted node. C) A reference to the node immediately after the node to be deleted. D) Both B and C. NULL head current (we want to remove this one)

Removal from Linked List public boolean remove (Object item){ // Removes the first instance of the object item // from the calling list, if present. // Returns true if item is present, false if not. } NULL head

Interview questions you might encounter Q. How do you remove an element from a singly linked list without having a pointer to the previous element? Q. How reverse a singly linked list in place?

Other topics/issues to think about Remove all instances Copying a list What does equality mean of two lists/testing for equality Iterators

Benchmarking code An alternative/supplement to big-O style analysis

Reading Quiz! The accuracy of measuring an algorithm is affected by (best answer) A. The number of applications running B. The speed of the computer C. The number of lines of the algorithm D. The number of loops in the algorithm

The standard deviation an indicator of the following A. how confident that your algorithm is efficient B. how confident that your average reflects a true value

Reading Quiz! An algorithm's performance is typically measured by these metrics: A. robustness, correctness, energy B. time, robustness, correctness C. time, space, energy D. time, space, robustness

Big-O Analysis vs. Benchmarking  Two different approaches to evaluating algorithms  As you know, Big-O glosses over some important details  What about n < n0?  Multiplying by “c”—the difference between n and n might matter!  Benchmarking is an experimental approach, rather than an analysis approach

Basic procedure for benchmarking for problem size N = min,...max 1. initialize the data structure 2. get the current (starting) time 3. run the algorithm on problem size N 4. get the current (finish) time 5. timing = finish time – start time

Timer methods in Java  Java has two static methods in the System class: /** Returns the current time in milliseconds. */ static long System.currentTimeMillis() /** Returns the current value of the most precise available system timer, in nanoseconds */ static long System.nanoTime()  If the algorithm can take less than a millisecond to run, you should use System.nanoTime() !

Sources of inaccuracy in benchmarking  Multiple processes running on system  Open up Task Manager on your PC  Or on Linux/UNIX or Mac, in command window run “ps –eaf | more”  Even if you shut down all your major application programs, all kinds of utilities and other things still running  Hardware interrupts  Network activity  Mouse/keyboard activity  JVM garbage collection could start running unexpectedly

Reading Quiz! One way to improve the accuracy of measuring the running time of algorithm is by A. Using a faster computer B. Averaging the measurement results C. Starting up more applications to have a larger set of running applications D. Improving the time the algorithm takes to finish executing

IMPROVED procedure for benchmarking for problem size N = min,...max 1. initialize the data structure 2. for K runs: 1. get the current (starting) time 2. run the algorithm on problem size N 3. get the current (finish) time 4. timing = finish time – start time 3. Average timings for K runs for this N

Explain these results... Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds)  From these measurements, what is the likely big-O time cost of findMax() on array of int? n array of int array of Integer 800, ,000, ,000,

Explain these results... Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds)  From these measurements, what is the likely big-O time cost of findMax() on array of Integer? n array of int array of Integer 800, ,000, ,000,

Explain these results... Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds)  Discussion:  Why would array of Integer take more time? n array of int array of Integer 800, ,000, ,000,

See Kube’s slides for HW tips!  How to calculate standard deviation  How to disable garbage collection so it doesn’t happen at unexpected times