1 Section 5.1 Analyzing Algorithms Let P be a problem and A an algorithm to solve P. The running time of A can be analyzed by counting the number of certain.

Slides:



Advertisements
Similar presentations
110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
Advertisements

Fake coin detection. Fake coins Suppose we have a number of coins, at most one is fake (i.e. either one is fake or none is fake). You have a pair of scales.
Today’s topics Decision Trees Reading: Sections 9.1 CompSci 102.
22C:19 Discrete Structures Trees Spring 2014 Sukumar Ghosh.
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
Chapter 19: Searching and Sorting Algorithms
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Introduction to Analysis of Algorithms
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
© 2004 Goodrich, Tamassia Sorting Lower Bound1. © 2004 Goodrich, Tamassia Sorting Lower Bound2 Comparison-Based Sorting (§ 10.3) Many sorting algorithms.
CPSC 411, Fall 2008: Set 2 1 CPSC 411 Design and Analysis of Algorithms Set 2: Sorting Lower Bound Prof. Jennifer Welch Fall 2008.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Chapter 11: Limitations of Algorithmic Power
Sorting Lower Bound Andreas Klappenecker based on slides by Prof. Welch 1.
Backtracking Reading Material: Chapter 13, Sections 1, 2, 4, and 5.
Data Structures and Algorithms1 Basics -- 2 From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University: Harper.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
The Complexity of Algorithms and the Lower Bounds of Problems
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Sorting Lower Bound1. 2 Comparison-Based Sorting (§ 4.4) Many sorting algorithms are comparison based. They sort by making comparisons between pairs of.
Chapter 11 Limitations of Algorithm Power. Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples:
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Chapter 19: Searching and Sorting Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Sorting Fun1 Chapter 4: Sorting     29  9.
1 Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples: b number of comparisons needed to find the.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Princeton University COS 423 Theory of Algorithms Spring 2001 Kevin Wayne Average Case Analysis.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
Asymptotic Behavior Algorithm : Design & Analysis [2]
LIMITATIONS OF ALGORITHM POWER
CSCE 411H Design and Analysis of Algorithms Set 10: Lower Bounds Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 10 1 * Slides adapted.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Sorting Lower Bounds n Beating Them. Recap Divide and Conquer –Know how to break a problem into smaller problems, such that –Given a solution to the smaller.
Lecture 3 Sorting and Selection. Comparison Sort.
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.
1 Chapter 8-1: Lower Bound of Comparison Sorts. 2 About this lecture Lower bound of any comparison sorting algorithm – applies to insertion sort, selection.
Decision Trees DEFINITION: DECISION TREE A decision tree is a tree in which the internal nodes represent actions, the arcs represent outcomes of an action,
David Kauchak cs062 Spring 2010
Sorting Lower Bound 4/25/2018 8:49 PM
Decision trees Polynomial-Time
CPSC 411 Design and Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
(2,4) Trees 11/15/2018 9:25 AM Sorting Lower Bound Sorting Lower Bound.
Lower Bound Theory.
The Complexity of Algorithms and the Lower Bounds of Problems
Analysis and design of algorithm
CSCE 411 Design and Analysis of Algorithms
(2,4) Trees 12/4/2018 1:20 PM Sorting Lower Bound Sorting Lower Bound.
Chapter 11 Limitations of Algorithm Power
The Lower Bounds of Problems
(2,4) Trees 2/28/2019 3:21 AM Sorting Lower Bound Sorting Lower Bound.
Topic 5: Heap data structure heap sort Priority queue
CPSC 411 Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
The Selection Problem.
The Pigeonhole Principle
Presentation transcript:

1 Section 5.1 Analyzing Algorithms Let P be a problem and A an algorithm to solve P. The running time of A can be analyzed by counting the number of certain operations that are performed during its execution. This count may also depend on the size of an input, which depends on the problem. Example. If P asks whether a given item is in a given list, then we might count the comparison operations executed by A, which will depend on the length of the list. A worst-case input of size n is an input that causes A to execute the largest number of operations. Example. A worst-case input for an algorithm to search a list for an item would be a list without the item. Let W A (n) denote the maximum running time of A over all inputs of size n. W A is called the worst-case function for A. An algorithm A to solve P is optimal in the worst case if every algorithm B to solve P satisfies the inequality W A (n) ≤ W B (n) for all n > 0. Method to find optimal algorithm in the worst case: 1.Find an algorithm A to solve P and do some analysis to find W A (n). 2.(lower bound) Find a function F where F(n) ≤ W B (n) for all n > 0 and all B to solve P. 3. Compare F and W A. If F(n) = W A (n) for all n, then A is optimal in the worst case. Otherwise try to find a better lower bound or a better algorithm.

2 A decision tree for an algorithm is a tree whose nodes represent decision points in the algorithm and whose leaves represent outcomes. Example/Quiz. Given a set of nine coins, one of which is heavier than the others, use a pan-balance to find the heavy coin. A Solution. Let the coins be 1, 2, …, 9. The numbers on either side of each internal node of the decision tree represent coins on the pan balance. The 9 outcomes are 1H, …, 9H H H 2H 3H 4H 5H 6H 7H 8H The worst-case performance of this algorithm is 3 weighings. Is the algorithm optimal in the worst case? A ternary decision tree with depth d has at most 3 d leaves. Since there are 9 possible outputs, it follows that 3 d ≥ 9. Take log 3 to get d ≥ log 3 9. Since d is a natural number, d ≥  log 3 9  = 2. So a lower bound is 2. Is there a better lower bound or a better algorithm? Here’s an optimal worst-case algorithm: H 2H 3H 7H 8H 9H 4H 5H 6H 4 6

3 Example. Given a set of nine coins, one of which is bad, meaning that it is heavier or lighter than the others. Find a good lower bound for a pan-balance algorithm that finds the bad coin and states whether it is heavy or light. A Solution. A lower bound: If the coins are numbered 1, 2, …, 9 and we let H mean heavy and let L mean light, then there are 18 possible outputs: 1H, 1L, …, 9H, 9L. A ternary decision tree with depth d has at most 3 d leaves. It follows that 3 d ≥ 18. Take log 3 to obtain d ≥ log Since d is a natural number, we have d ≥  log 3 18  = 3. So 3 is a good lower bound on the number of weighings. Quiz. Find an optimal worst-case pan-balance algorithm to detect the one bad coin among a set of nine coins, where the output states whether the bad coin is heavy or light. A Solution: 1H 2H 3H 6L 5L 4L 7H 8L 9L 9H 8H 7L 4H 5H 6H 3L 2L 1L