CS 302 Data Structures Algorithm Efficiency.

Slides:



Advertisements
Similar presentations
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Advertisements

Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 10 Algorithm Efficiency
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Data Structures Data Structures Topic #7. Today’s Agenda How to measure the efficiency of algorithms? Discuss program #3 in detail Review for the midterm.
© 2006 Pearson Addison-Wesley. All rights reserved6-1 More on Recursion.
Cmpt-225 Algorithm Efficiency.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ 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)
Matt Schierholtz. Method for solving problems with finite steps Algorithm example: Error Check for problem Solve problem Must terminate.
Analysis of Algorithms
Measuring the Efficiency of Algorithms Analysis of algorithms Provides tools for contrasting the efficiency of different methods of solution Time efficiency,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 9: Algorithm Efficiency and Sorting Data Abstraction &
Algorithm Efficiency Chapter 10 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Algorithm Efficiency and Sorting Data Structure & Algorithm.
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
CSC310 © Tom Briggs Shippensburg University Fundamentals of the Analysis of Algorithm Efficiency Chapter 2.
Efficiency of Algorithms. Node - data : Object - link : Node + createNode() + getData() + setData() + getLink() + setLink() + addNodeAfter() + removeNodeAfter()
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching Topics Sequential Search Binary Search.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
© 2017 Pearson Education, Hoboken, NJ. All rights reserved
Algorithm Analysis 1.
Chapter 10 Algorithm Efficiency
Algorithm Efficiency and Sorting
Chapter 22 Developing Efficient Algorithms
Introduction to Analysis of Algorithms
Design and Analysis of Algorithms Chapter -2
Analysis of Algorithms
Analysis of Algorithms
Searching – Linear and Binary Searches
Analysis of Algorithms
Introduction to Algorithms
Algorithm Analysis and Big Oh Notation
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Efficiency (Chapter 2).
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.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
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”),
Algorithm Efficiency Chapter 10.
Algorithm Efficiency and Sorting
Analysis of Algorithms
Algorithm Efficiency: Searching and Sorting Algorithms
Algorithm Efficiency Chapter 10
Algorithm Analysis Bina Ramamurthy CSE116A,B.
Algorithmic Complexity
Fundamentals of the Analysis of Algorithm Efficiency
Algorithm Efficiency and Sorting
Algorithm Analysis and Big Oh Notation
Fundamentals of the Analysis of Algorithm Efficiency
Algorithm Efficiency and Sorting
Analysis of Algorithms
Algorithm Efficiency and Sorting
Algorithms and data structures: basic definitions
Presentation transcript:

CS 302 Data Structures Algorithm Efficiency

This chapter will show you how to analyze the efficiency of algorithms. The basic mathematical techniques for analyzing algorithms are central to more advanced topics in Computer Science. As examples we will see analysis of some algorithms that you have studied before In addition, this chapter examines the important topic of sorting data.

Measuring an algorithms efficiency is quite important because your choice of algorithm for a given application often has a great impact. Suppose two algorithms perform the same task, such as searching. What does it mean to compare the algorithms and conclude that one is better?

The analysis of algorithms is the area of Computer Science that provides the tools for contrasting the efficiency of different methods of solution. How do you compare them? Implement them both in C++ and run them? There are a few difficulties with this How are the algorithms coded? What computer should you use? What data should the programs use? To overcome these (and other) difficulties, we will use some mathematical techniques to the algorithms independently of coding, computers, and data.

Execution Time of Algorithm Traversal of linked nodes – example: So the total time is A(n+1) + C(n+1) + W(n) Which is proportional to n Therefore a list of 100 takes longer than a list of 10

The Towers of Hanoi In the Chapter on Recursion we proved recursively that the solution to the towers of Hanoi problem with n disks requires 2n-1 moves If each move requires the same time, m, the solution requires (2n-1)*m time units. As you can see, this time requirement increases rapidly as the number of disks increases

Nested Loops Consider an algorithm that contains nested loops of the following form: for (i=1 to n) for (j=1 to i) for (k=1 to 5) taks T If task T requires t time units, the innermost loop (on k) requires 5*t time units, the loop on j requres 5*t*i time units, and the outermost loop on i requires

Algorithm Growth Rate The most important thing to learn is how quickly the algorithm’s time requirement grows as a function of the problem size. Algorithm A requires time proportional to n2 Algorithm B requires time proportional to n This is called the growth rate

Analysis and Big O Notation Definition: Algorithm A is order f ( n ) Denoted O( f ( n )) If constants k and n0 exist Such that A requires no more than k  f ( n ) time units to solve a problem of size n ≥ n0 .

Analysis and Big O Notation FIGURE 10-2 The graphs of 3  n2 and n2 - 3  n + 10

Analysis and Big O Notation FIGURE 10-3 A comparison of growth-rate functions: (a) in tabular form

Analysis and Big O Notation

Analysis and Big O Notation FIGURE 10-3 A comparison of growth-rate functions: (a) in graphical form

Analysis and Big O Notation Order of growth of some common functions

Properties of Growth-Rate Functions You can ignore low order terms in an algorithm’s growth rate function: O(n3+4n2+3n) is O(n3) You can ignore a multiplicative constant in the high-order term of an algorithm’s growth rate function O(5n3) is O(n3) You can combine growth rate functions O(f(n)) + O(g(n)) = O(f(n)+g(n)) O(n2) + O(n) = O(n2+n) = O(n2)

Worst-case and average-case analysis A particular algorithm might require different times to solve different problems of the same size. Worst-case analysis concludes that the algorithms is O(f(n)) if, in the worst case, A requires no more time than k*f(n) Average-case analysis attempts to determine the average amount of time that an algorithm requires to solve problems of size n This is more difficult than worst-case analysis.

The efficiency of Searching Algorithms Lets look at two algorithms sequential search and binary search Sequential Search Start at the beginning and look until you find it or run out of data. Best case: Worst case: Average case:

The efficiency of Searching Algorithms Binary Search Assumes a sorted array. The algorithm determines which half to look at and ignores the other half. If n = 2k, there are k divisions Therefore k = log2n Thus the algorithm is O(log2n) in the worst case So, is a binary search better than a sequential search? If n = 1,000,000 log2 1,000,000 = 19 Therefore at most 20 compares as opposed to at most 1,000,000 compares.

Efficiency of Searching Algorithms Sequential search Worst case O(n) Average case O(n) Best case O(1) Binary search of sorted array Worst case O(log2n) Remember required overhead for keeping array sorted

Keeping Your Perspective Array-based getEntry is O(1) Link-based getEntry is O(n) Consider how frequently particular ADT operations occur in given application Some seldom-used but critical operations must be efficient

Keeping Your Perspective If problem size always small, ignore an algorithm’s efficiency Weigh trade-offs between algorithm’s time and memory requirements Compare algorithms for both style and efficiency

End of Chapter 10