Xguo9@student.gsu.edu Xuan Guo Lab 6 xguo9@student.gsu.edu Xuan Guo.

Slides:



Advertisements
Similar presentations
THE WELL ORDERING PROPERTY Definition: Let B be a set of integers. An integer m is called a least element of B if m is an element of B, and for every x.
Advertisements

Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Discrete Mathematics CS 2610 February 26, part 1.
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
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 Growth of Functions
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Sorting CS 202 – Fundamental Structures of Computer Science II Bilkent.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
Algorithms Chapter 3 With Question/Answer Animations.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
O, , and  Notations Lecture 39 Section 9.2 Wed, Mar 30, 2005.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
ALGORITHMS.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Complexity Analysis (Part I)
Algorithms Chapter 3.
Chapter 2 Algorithm Analysis
CS212: Data Structures and Algorithms
Growth of Functions & Algorithms
Introduction to Analysis of Algorithms
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
The Growth of Functions
ET 2006 : Data Structures & Algorithms
The Growth of Functions
Enough Mathematical Appetizers!
CS 3343: Analysis of Algorithms
Computation.
CS 2210 Discrete Structures Algorithms and Complexity
Techniques for Finding Derivatives
Algorithm design and Analysis
Asymptotic Notations Algorithms Lecture 9.
ICS 353: Design and Analysis of Algorithms
Analysis Algorithms.
ITEC 2620M Introduction to Data Structures
CS 2210 Discrete Structures Algorithms and Complexity
Applied Discrete Mathematics Week 6: Computation
Programming and Data Structure
Sorting … and Insertion Sort.
ICS 353: Design and Analysis of Algorithms
Programming and Data Structure
ICS 353: Design and Analysis of Algorithms
Algorithmic Complexity
Counting Discrete Mathematics.
Applied Combinatorics, 4th Ed. Alan Tucker
CMSC 203, Section 0401 Discrete Structures Fall 2004 Matt Gaston
Enough Mathematical Appetizers!
Lower bound for sorting, radix sort
Analysis of Algorithms
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Enough Mathematical Appetizers!
Algorithmic complexity
Application: Efficiency of Algorithms II
Algorithms Chapter 3 With Question/Answer Animations.
CS210- Lecture 3 Jun 6, 2005 Announcements
Algorithm/Running Time Analysis
Discrete Mathematics CS 2610
ICS 353: Design and Analysis of Algorithms
Complexity Analysis (Part I)
Complexity Analysis (Part I)
CS 2210 Discrete Structures Algorithms and Complexity
Presentation transcript:

xguo9@student.gsu.edu Xuan Guo Lab 6 xguo9@student.gsu.edu Xuan Guo

Contents Section 3.1 Section 3.2 Section 3.3 Question 3, 5, 7, 35, 39

Question 3 section 3.1 Devise an algorithm that finds the sum of all the integers in a list.

Question 3 section 3.1 Devise an algorithm that finds the sum of all the integers in a list. (call the procedure AddEmUp, mimic the structure of Algorithm 1, the list is not empty) Input: a list of integers

Question 3 section 3.1 Devise an algorithm that finds the sum of all the integers in a list. Procedure AddEmUp(a1, ..., an: integers) sum := a1 for i :=2 to n sum := sum +ai return sum

Question 5 Section 3.1 Describe an algorithm that takes as input a list of n integers in non decreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is non decreasing if each integer in the list is at least as large as the previous integer in the list.)

Question 5 Section 3.1 Describe an algorithm that takes as input a list of n integers in non decreasing order and produces the list of all values that occur more than once. (Recall that a list of integers is non decreasing if each integer in the list is at least as large as the previous integer in the list.) Find the cases when ai=ai+1 Need to skip repeated duplicates form and return a list

Question 5 Section 3.1 procedure duplicates (a1, a2, ..., an: integers in non decreasing order) k:= 0 {this counts the duplicates} j:= 2 while j≤n if aj =aj−1 then k:= k+1 ck := aj while j≤n and aj = ck j:= j+1 {c1, c2, ..., ck is the desired list}

Question 7 Section 3.1 Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list.

Question 7 Section 3.1 Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list. (Go through the list, and record the index of the last even integer seen)

Question 7 Section 3.1 Describe an algorithm that takes as input a list of n integers and finds the location of the last even integer in the list or returns 0 if there are no even integers in the list. procedure last even location(a1, a2, . . . , an: integers) k := 0 for i := 1 to n if ai is even then k := i return k {k = 0 if there are no evens}

Question 35 Section 3.1 Use the bubble sort to sort 3, 1, 5, 7, 4, showing the lists obtained at each step.

Question 35 Section 3.1 Use the bubble sort to sort 3, 1, 5, 7, 4, showing the lists obtained at each step. Four passes 1) 1, 3, 5, 4, 7 2) 1, 3, 4, 5, 7 3) 1, 3, 4, 5, 7 4) 1, 3, 4, 5, 7

Question 39 Section 3.1 Use the insertion sort to sort the list in Exercise 35, showing the lists obtained at each step.

Question 39 Section 3.1 Use the insertion sort to sort the list in Exercise 35, showing the lists obtained at each step. 1) inserts 1, get 1, 3, 5, 7, 4 2) 5 inserted into 1, 3, get 1, 3, 5, 7, 4 3) 7 inserted into 1, 3, 5, 4) 4 is inserted

Question 9 Section 3.2 how that x2+4x+17 is O(x3) but that x3 is not O(x2+4x+17).

Question 9 Section 3.2 how that x2+4x+17 is O(x3) but that x3 is not O(x2+4x+17). Proof: On the one hand we have x2+4x+17 <= x2+x2+x2 = 3x2 <= 3x3 for all x > 17, so x2+4x+17 is O(x3) with witness C = 3 and k = 17.

Question 9 Section 3.2 how that x2+4x+17 is O(x3) but that x3 is not O(x2+4x+17). Proof: On the one hand we have x2+4x+17 <= x2+x2+x2 = 3x2 <= 3x3 for all x > 17, so x2+4x+17 is O(x3) with witness C = 3 and k = 17. On the other hand, if x3 were O(x2+4x+17), then we would have x3 <= C(x2+4x+17) <= 3Cx2 for all sufficiently large x. But this says than x <= 3C, clearly impossible for the constant C to satisfy for all large x. Therefore x3 is not O(x2+4x+17).

Question 27 Section 3.2 Give a big-O estimate for each of these functions. For the function g in your estimate that f(x) is O(g(x)), use a simple function g of the smallest order. a) nlog(n2+1)+n2logn b)(nlogn+1)2+(logn+1)(n2+1)

Question 27 Section 3.2 Give a big-O estimate for each of these functions. For the function g in your estimate that f(x) is O(g(x)), use a simple function g of the smallest order. a) nlog(n2+1)+n2logn O(n2logn) logn2 = 2logn

Question 27 Section 3.2 Give a big-O estimate for each of these functions. For the function g in your estimate that f(x) is O(g(x)), use a simple function g of the smallest order. b)(nlogn+1)2+(logn+1)(n2+1) O(n2(logn)2) throw away the smaller order term

Question 13 Section 3.3 The conventional algorithm for evaluating a polynomial anxn+an−1xn−1+ ··· +a1x+a0 at x=c can be expressed in pseudocode by procedure polynomial(c, a0, a1, ..., an: real numbers) power:=1 y:=a0 for i :=1 to n power := power ∗ c y := y + ai ∗ power return y{y=ancn+an−1cn−1+ ··· +a1c+a0} where the final value of y is the value of the polynomial at x=c. a) Evaluate 3x2+x+1 at x=2 by working through each step of the algorithm showing the values assigned at each assignment step. b)Exactly how many multiplications and additions are used to evaluate a polynomial of degree n at x=c?(Do not count additions used to increment the loop variable.)

Question 13 Section 3.3 procedure polynomial(c, a0, a1, ..., an: real numbers) power:=1 y:=a0 for i :=1 to n power := power ∗ c y := y + ai ∗ power return y {y=ancn+an−1cn−1+ ··· +a1c+a0} where the final value of y is the value of the polynomial at x=c. a) Evaluate 3x2+x+1 at x=2 n = 2, a1 = 1, a2 = 3, and c = 2

Question 13 Section 3.3 procedure polynomial(c, a0, a1, ..., an: real numbers) power:=1 y:=a0 for i :=1 to n power := power ∗ c y := y + ai ∗ power return y{y=ancn+an−1cn−1+ ··· +a1c+a0} where the final value of y is the value of the polynomial at x=c. b)Exactly how many multiplications and additions are used to evaluate a polynomial of degree n at x=c?(Do not count additions used to increment the loop variable.) 2 multiplication and 1 addition per pass 2n multiplications and n additions