90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures.

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Efficiency of Algorithms
Algorithm Analysis.
Lecture: Algorithmic complexity
HST 952 Computing for Biomedical Scientists Lecture 10.
Chapter 1. The Phases of Software Development. Data Structure 2 Chapter outline  Objectives  Use Javadoc to write a method’s complete specification.
Computer Science 101 Efficiency and Complexity Analysis.
Fundamentals of Python: From First Programs Through Data Structures
11.2 Complexity Analysis. Complexity Analysis As true computer scientists, we need a way to compare the efficiency of algorithms. Should we just use a.
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
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Analysis of Algorithms intro.  What is “goodness”?  How to measure efficiency? ◦ Profiling, Big-Oh  Big-Oh: ◦ Motivation ◦ Informal examples ◦ Informal.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Introduction to Analysis of Algorithms
Efficiency of Algorithms
Complexity Analysis (Part I)
CS107 Introduction to Computer Science
© 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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm.
Elementary Data Structures and Algorithms
Bigointro1 Algorithm Analysis & Program Testing An introduction.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Algorithm Analysis (Big O)
Analysis of Performance
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
Analysis of Algorithms
CSC 205 Java Programming II Algorithm Efficiency.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Catie Baker Spring 2015.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Lauren Milne Summer 2015.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
CS 2601 Runtime Analysis Big O, Θ, some simple sums. (see section 1.2 for motivation) Notes, examples and code adapted from Data Structures and Other Objects.
M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 1.
Chapter 7 Analysis of Algorithms © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Algorithm Analysis (Big O)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
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.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Complexity Analysis (Part I)
Complexity In examining algorithm efficiency we must understand the idea of complexity Space complexity Time Complexity.
Introduction to Data Structures
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Searching, Sorting, and Asymptotic Complexity
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 2: Basics Data Structures and Algorithms for Information Processing Lecture 2: Basics

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 2 Lecture 2: Basics Today ’ s Topics Intro to Running-Time Analysis Summary of Object-Oriented Programming concepts (see slides on schedule).

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 3 Lecture 2: Basics Running Time Analysis Reasoning about an algorithm ’ s speed “ Does it work fast enough for my needs? ” “ How much longer when the input gets larger? ” “ Which algorithm is fastest? ”

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 4 Lecture 2: Basics Elapsed Time vs. No. of Operations Q: Why not just use a stopwatch? A: Elapsed time depends on independent factors Number of operations carried out is the same for two runs of the same code with the same arguments -- no matter what the environment might be

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 5 Lecture 2: Basics Stair-Counting Problem Two people at the top of the Eiffel Tower Three methods to count the steps –X walks down, keeping a tally –X walks down, but Y keeps the tally –Z provides the answer immediately (2689!)

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 6 Lecture 2: Basics Stair-Counting Problem Choosing the operations to count –Actual time? Varies due to several factors not related to the efficiency of the algorithm –Each time X walk up or down one step = 1 operation –Each time X or Y marks a symbol on the paper = 1 operation

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 7 Lecture 2: Basics Stair-Counting Problem How many operations for each of the 3 methods? Method 1: –2689 steps down –2689 steps up –2689 marks on the paper –8067 total operations

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 8 Lecture 2: Basics Stair-Counting Problem Method 2: –3,616,705 steps down (1+2+…+2689) –3,616,705 steps up –2689 marks on the paper –7,236,099 total operations

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 9 Lecture 2: Basics Stair-Counting Problem Method 3: –0 steps down –0 steps up –4 marks on the paper (one for each digit) –4 total operations

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 10 Lecture 2: Basics Analyzing Programs Count operations, not time –operations is “ small step ” –e.g., a single program statement; an arithmetic operation; assignment to a variable; etc. No. of operations depends on the input –“ the taller the tower, the larger the number of operations ”

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 11 Lecture 2: Basics Analyzing Programs When time analysis depends on the input, time (in operations) can be expressed by a formula: –Method 1: –Method 2: –Method 3: no. of digits in number n

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 12 Lecture 2: Basics Big-O Notation The magnitude of the number of operations Less precise than the exact number More useful for comparing two algorithms as input grows larger Rough idea: “ term in the formula which grows most quickly ”

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 13 Lecture 2: Basics Big-O Notation Quadratic Time –largest term no more than –“ big-O of n-squared ” –doubling the input increases the number of operations approximately 4 times or less –e.g. Method 2(100) = 10,200 Method 2(200) = 40,400

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 14 Lecture 2: Basics Big-O Notation Linear Time –largest term no more than –“ big-O of n ” –doubling the input increases the number of operations approximately 2 times or less –e.g. Method 1(100) = 300 Method 1(200) = 600

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 15 Lecture 2: Basics Big-O Notation Logarithmic Time –largest term no more than –“ big-O of log n ” –doubling the input increases the running time by a fixed number of operations –e.g. Method 3(100) = 3 Method 3(1000) = 4

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 16 Lecture 2: Basics Summary Method 1: Method 2: Method 3: Run-time expressed with big-O is the order of the algorithm Constants ignored:

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 17 Lecture 2: Basics Summary Order allows us to focus on the algorithm and not on the speed of the processor Quadratic algorithms can be impractically slow

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 18 Lecture 2: Basics Comparison

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 19 Lecture 2: Basics Time Analysis of Java Methods Example: search method (p. 26) public static boolean search(double[] data, double target) { int i; for (i=0; i<data.length; i++) { if (data[i] == target) return true; } return false; }

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 20 Lecture 2: Basics Time Analysis of Java Methods Operations: assignment, arithmetic operators, tests –Loop start: two operations: initialization assignment, end test –Loop body: n times if input not found; assume constant k operations –Return: one operation –Total:

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 21 Lecture 2: Basics Time Analysis of Java Methods A loop that does a fixed number of operations n times is O(n)

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 22 Lecture 2: Basics Time Analysis of Java Methods worst-case: maximum number of operations for inputs of given size average-case: average number of operations for inputs of given size best-case: fewest number of operations for inputs of given size any-case: no cases to consider Pin the case down and think about n growing large – never small.

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 23 Lecture 2: Basics Object-Oriented Overview Slides from Main ’ s LectureSlides from Main ’ s Lecture