Discrete Structure Li Tak Sing( 李德成 ) Lectures 20-22 1.

Slides:



Advertisements
Similar presentations
Dynamic Programming 25-Mar-17.
Advertisements

Introduction to Algorithms Quicksort
110/6/2014CSE Suprakash Datta datta[at]cse.yorku.ca CSE 3101: Introduction to the Design and Analysis of Algorithms.
MATH 224 – Discrete Mathematics
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.
Lower bound: Decision tree and adversary argument
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.
MS 101: Algorithms Instructor Neelima Gupta
Equivalence, Order, and Inductive Proof
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Chapter 19: Searching and Sorting Algorithms
Complexity 15-1 Complexity Andrei Bulatov Hierarchy Theorem.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
CS2420: Lecture 13 Vladimir Kulyukin Computer Science Department Utah State University.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
CPSC 411, Fall 2008: Set 2 1 CPSC 311 Analysis of Algorithms Sorting Lower Bound Prof. Jennifer Welch Fall 2009.
Tirgul 6 B-Trees – Another kind of balanced trees Problem set 1 - some solutions.
Algorithm Efficiency and Sorting
Chapter 11: Limitations of Algorithmic Power
Chapter 12 Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define trees as data structures Define the terms.
DAST 2005 Week 4 – Some Helpful Material Randomized Quick Sort & Lower bound & General remarks…
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Trees.
1.1 Chapter 1: Introduction What is the course all about? Problems, instances and algorithms Running time v.s. computational complexity General description.
Called as the Interval Scheduling Problem. A simpler version of a class of scheduling problems. – Can add weights. – Can add multiple resources – Can ask.
B-Tree. B-Trees a specialized multi-way tree designed especially for use on disk In a B-tree each node may contain a large number of keys. The number.
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:
CSCE350 Algorithms and Data Structure
CSCE350 Algorithms and Data Structure Lecture 17 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Sorting Fun1 Chapter 4: Sorting     29  9.
Discrete Structures Lecture 12: Trees Ji Yanyan United International College Thanks to Professor Michael Hvidsten.
Trees – Chapter 9 Slides courtesy of Dr. Michael P. Frank University of Florida Dept. of Computer & Information Science & Engineering.
Reading and Writing Mathematical Proofs Spring 2015 Lecture 4: Beyond Basic Induction.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
Review 1 Queue Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Asymptotic Behavior Algorithm : Design & Analysis [2]
1 Section 4.3 Order Relations A binary relation is an partial order if it transitive and antisymmetric. If R is a partial order over the set S, we also.
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 
Chapter 13 Backtracking Introduction The 3-coloring problem
Bushy Binary Search Tree from Ordered List. Behavior of the Algorithm Binary Search Tree Recall that tree_search is based closely on binary search. If.
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.
The NP class. NP-completeness Lecture2. The NP-class The NP class is a class that contains all the problems that can be decided by a Non-Deterministic.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Discrete Structures Li Tak Sing( 李德成 ) Lectures
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.
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
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,
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.
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
COMPS263F Unit 2 Discrete Structures Li Tak Sing( 李德成 ) Room A
CPSC 411 Design and Analysis of Algorithms
Presentation transcript:

Discrete Structure Li Tak Sing( 李德成 ) Lectures

Section Well-founded orders Well-founded  A poset is said to be well-founded if every descending chain of elements is finite. In this case, the partial order is called a well-founded order. 2

Examples of well-founded poset The < relations of N.  100>90>78>.. This chain must end with finite terms. aa shorter aab shorter abaaa 3

Examples of posets that are not well- founded. The < relation of Z.  30>28>21>2>-4> The < relation of R +  30.2>29.2>20.4>15.8>....>0.004> >.... 4

The minimal Element Property If A is a well-founded set, then every subset of A has a minimal element. Conversely, if every nonempty subset of A has a minimal element, then A is well- founded. 5

Lexicograhic or ordering of tuples 6

Example 7

Solution 1.Consider an arbitrary binary tree with n nodes. A descending chain starting from the tree cannot have more than n+1 elements. Therefore the descending chain must be finite. Therefore the poset is well-founded. 8

Solution 2.Consider an arbitrary binary tree with n nodes. A descending chain starting from the tree cannot have more than n+1 elements. Therefore the descending chain must be finite. Therefore the poset is well-founded. 9

Example 10

Solution 11

Chapter 5 Analysis Techniques We need to see if an algorithm is efficient or not. Least time and least space. Which one is more important? Generally, time is a more important factor because you can buy space but not time. 12

Worst-case running time The running time is measured by counting the number of operations instead of doing the real timing during the execution of a program. The number of operations performed by an algorithm usually depends on the size or structure of the input. Some algorithm also depends on the structure of the input. For example some sorting algorithms perform poorly when the original list is already sorted. 13

Worst-case running time An input of size n is a worst-case input if, when compared to all other inputs of size n, it causes an algorithm A to execute the largest number of operations. For any input I we'll denote its size by size(I), and we'll let time(I) denote the number of operations executed by A on I. Then, the worst- case function for A is defined as follows: W A (n)=max{time(I)|I is an input and size(I)=n} 14

Optimal in the worst case An algorithm A is optimal in the worst case for problem P, if for any algorithm B that exits, or ever will exist, the following relationship holds: W A (n)  W B (n) for all n>0 15

Steps to find an algorithm that is optimal (Find an algorithm) Find or design an algorithm A to solve P. Then do an analysis of A to find the worst-case function W A. (Find a lower bound) Find a function F such that F(n)  W B for all n>0 and for all alogrithms B that solve P. Compare F and W A. If F= W A, then A is optimal in the worst case. 16

Steps to find an algorithm that is optimal Suppose we know that F  W A. This means that F(n)<W A (n) for some n. In this case there are two possible courses of action to consider:  Try to build a new algorithm C such that W C (n)  W A (n) for all n>0  Try to find a new function G such that F(n)  G(n)  W B (n) for all n>0 and for all algorithms B that solve P. 17

Matrix Multiplication 18

Matrix Multiplication 19

Matrix Multiplication 20

Matrix Multiplication 21

In the last method, only 7 multiplications are needed. Multiplication of larger-size matrices is broken down into multiplying many 2 by 2 matrices. So the number of multiplication operations becomes less than n 3. The known lower bound for the number of multiplication operations needed to multiply two n by n matrices is n 2. Strassen showed how to multiply two matrices with n 2.81 multiplications. The number 2.81 is related to log

Finding the minimum Problem: to find the minimum number in an unsorted list of a numbers. We'll count the number of comparison operations that an algorithm makes between elements of the list. To find the minimum number in a list of n numbers, the minimum number must be compared with the other n-1 numbers. Therefore, n-1 is a lower bound on the number of comparisons needed to find the minimum number in a list of n numbers. 23

Finding the minimum The following algorithm is optimal because the operation  is executed exactly n-1 times. m:=a[1]; for i:=2 to n do m:=if m  a[i] then m else a[i] od 24

Decision trees A decision tree for an algorithm is a tree whose nodes represent decision points in the algorithm and whose leaves represent possible outcomes. Decision trees can be useful in trying to construct an algorithm or trying to find properties of a decision tree. 25

Decision trees If an algorithm makes decisions based on the comparison of two objects, then it can be represented by a binary decision tree. Each nonleaf node in the tree represents a pair of objects to be compared by the algorithm, and each branch from that node represents a path taken by the algorithm based on the comparison. Each leaf can represent an outcome of the algorithm. 26

Lower bounds for decision tree algorithms Suppose that a problem has n possible outcomes and it can be solved by a binary decision tree algorithm. What is the best binary decision tree algorithm? We may not know the answer, but we can find a lower bound for the depth of any binary decision tree to solve the problem. Since the program has n possible outcomes, it follows that any binary decision tree algorithm to solve the problem must have at least n leaves, one for each of the n possible outcomes. Recall that the number of leaves in a binary tree of depth d is at most 2 d. 27

Lower bounds for decision tree algorithms So if d is the depth of a binary decision tree to solve a problem with n possible outcomes, then we must have n  2 d. We can solve this inequality for d by taking log 2 of both sides to obtain log 2 n  d. Since d is a natural number, it follows that  log 2 n  d 28

Ternary decision tree We can do the same analysis for ternary decision trees. The number of leaves in a ternary tree of depth d is at most 3 d. If d is the depth of a ternary decision tree to solve a problem with n possible outcomes, then we must have n  3 d. Or:  log 3 n  d 29

Binary Search Suppose we search a sorted list in a binary fashion. That is, we check the middle element of the list to see whether it's the key we are looking for. If not, then we perform the same operation on either the left half or the right half of the list, depending on the value of the key. This algorithm has a nice representation as a decision tree. For example, suppose we have the following sorted list of 15 numbers: x 1, x 2, x 3, x 4, x 5, x 6, x 7, x 8, x 9, x 10, x 11, x 12, x 13, x 14, x 15, 30

Decision tree for binary search 31

Binary search Suppose we're given a number key K, and we must find whether it is in the list. The decision tree for a binary search of the list has the number x 8 at its root. This represents the comparison of K with x 8. If K=x 8, then we are successful in one comparison. If K<x 8, then we go to the left child of x 8 ; otherwise we go to the right child of x 8. The result is a ternary decision tree in which the leaves are labeled with either S, for successful search, or U, for unsuccessful search. 32

Binary search Since the depth of the tree is 4, it follows that there will be four comparisons in the worst case to find whether K is in the list. Is this an optimal algorithm? To see that the answer is yes, we can observe that there are 31 possible outcomes for the given program: 15 leaves labeled with S to represent successful searches; and 16 leaves labeled with U to represent the gaps where K<x 1, x i <K<x i+1 for 1  i<15, and x 15 <K. Therefore, a worst-case lower bound for the number of comparisons is  log 3 31  =4. Therefore the hyphen algorithm is optimal. 33

Examples A solution to a problem has x possible outcomes. An algorithm to solve the problem has a binary decision tree of depth 5. Draw a picture of the decision tree for an optimal algorithm to find the maximum number in the list x1, x2, x3 and x4. 34

Finding a bad coin Suppose we are asked to use a pan balance to find the heavy coin among eight coins with the assumption that they all look alike and the other seven all have the same weight. One way to proceed is to always place coins in the two pans that include the bad coin, so the pan will always go down. 35

This gives a binary decision tree, where each internal node of the tree represents the pan balance. If the left side goes down, then the heavy coin is on the left side of the balance. Otherwise, the heavy coin is on the right side of the balance. Each leaf represents one coin that is the heavy coin. Suppose we label the coins with the numbers 1, 2,..., 8. 36

The decision tree for one algorithm is shown in Figure 5.2, where the numbers on either side of a nonleaf node represent the coins on either side of the pan balance. This algorithm finds the heavy coin in three weighings. Can we do any better? Look at the next example. 37

An Optimal Solution We are asked to use a pan balance to find the heavy coin among eight coins with the assumption that they all look alike and the other seven all have the same weight. In this case we'll weigh coins with the possibility that the two pans are balanced. So a decision tree can have nodes with three children. 38

We don't have to use all eight coins on the first weighing. For example, the above shows the decision tree for one algorithm. Notice that there is no middle branch on the middle subtree because, at this point, one of the coins 7 or 8 must be the heavy one. This algorithm finds the heavy coin in two weighings. 39

This algorithm is an optimal pan-balance algorithm for the problem, where we are counting the number of weighings to find the heavy coin. To see this, notice that any one of the eight coins could be the heavy one. Therefore, there must be at least eight leaves on any algorithm's decision tree. 40

But a binary tree of depth d can have 2 d possible leaves. So to get eight leaves, we must have 2 d ≥ 8. This implies that d ≥ 3. But a ternary tree of depth d can have 3 d possible leaves. So to get eight leaves, we must have 3 d ≥ 8, or d ≥ 2. Therefore, 2 is a lower bound for the number of weighings. Since the algorithm solves the problem in two weighings, it is optimal. 41

A Lower Bound Computation Suppose we have a set of 13 coins in which at most one coin is bad and a bad coin may be heavier or lighter than the other coins. The problem is to use a pan balance to find the bad coin if it exists and say whether it is heavy or light. We'll find a lower bound on the heights of decision trees for pan-balance algorithms to solve the problem. 42

Any solution must tell whether a bad coin is heavy or light. Thus there are 27 possible conditions: no bad coin and the 13 pairs of conditions (ith coin light, ith coin heavy). Therefore, any decision tree for the problem must have at least 27 leaves. So a ternary decision tree of depth d must satisfy 3 d ≥ 27, or d ≥ 3. This gives us a lower bound of 3. 43

Now the big question: Is there an algorithm to solve the problem, where the decision tree of the algorithm has depth 3? The answer is no. Just look at the cases of different initial weighings, and note in each case that the remaining possible conditions cannot be distinguished with just two more weighings. Thus any decision tree for this problem must have depth 4 or more. 44

5.2 Finding Closed Forms A closed from is an expression that can be computed by applying a fixed number of familiar operations to the arguments. A closed form can't have an ellipsis because the number of operations to evaluate the form would not be fixed. For example, the expression n(n+1)/2 is a closed form, but the expression n is not a closed form. 45

Closed Forms of Elementary Finite Sums 46

Examples Evaluate each of the followings:

Solution 48

Examples Find a closed form for each of the following sums n (5n+2) (3 n ). 49

Solution 50

Simple sort a[i], i=1,...,n are n numbers. We want to sort these numbers in ascending order. min(a,i,n) is the index of the minimum number among the elements of a[i],..a[n]. exchange(a[i],a[j]) is the usual operation of swapping elements and does not use any comparison. 51

Simple sort The algorithm for i:=1 to n-1 do j:=min(a,i,n); exchange(a[i],a[j]) od The algorithm makes n-1 comparison in the first round, then n-2 comparison in the second round,... So the total number of comparisons made is: (n-1)+(n-2)+....1=n(n-1)/2 52

Simple sort Exactly n(n-1)/2 comparisons are made for all inputs. So every set of input corresponds to the worst case. 53

Example For the following algorithm, answer each question by giving a formula in terms of n: for i:=1 to n do for j:=1 to i do x:=x+f(x) od; x:=x+g(x) od  Find the number of times the assignment statement (:=) is executed during the running of the program. 54

Solution 55

Example For the following algorithm, answer each question by giving a formula in terms of n: i:=1; while i<n+1 do i:=i+2; for j:=1 to i do S od od  Find the number of times the statement S is executed during the running of the program. 56

Solution 57

Example Find a closed form, in terms of n, for the number of times S is executed in the following algorithm: for i:=1 to n do for j:=i to n do S od od 58

Solution 59

Example Find the exact number of times, in terms of the natural number n, that S is executed in the following algorithm: i:=0; while i<n do S; i:=i+2 od 60

Solution 61