CMSC 203 / 0201 Fall 2002

15
September1999 CMSC 203 / 0201 Fall 2002 Week #4 – 16/18/20 September 2002 Prof. Marie desJardins

description

CMSC 203 / 0201 Fall 2002. Week #4 – 16/18/20 September 2002 Prof. Marie desJardins. TOPICS. Algorithms Algorithmic complexity Integers. MON 9/16 ALGORITHMS (2.1). CONCEPTS / VOCABULARY. Algorithm: “Finite set of precise instructions” - PowerPoint PPT Presentation

Transcript of CMSC 203 / 0201 Fall 2002

Page 1: CMSC 203 / 0201 Fall 2002

September1999

CMSC 203 / 0201Fall 2002

Week #4 – 16/18/20 September 2002

Prof. Marie desJardins

Page 2: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

TOPICS

Algorithms Algorithmic complexity

Integers

Page 3: CMSC 203 / 0201 Fall 2002

September1999

MON 9/16ALGORITHMS (2.1)

Page 4: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

CONCEPTS / VOCABULARY

Algorithm: “Finite set of precise instructions” Properties: Input, output, definiteness (preciseness),

correctness, finiteness, effectiveness, generality

Pseudocode Searching algorithms

Linear (sequential) search Binary search

Page 5: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples

Exercise 2.1.2: Which characteristics of an algorithm does each of these procedures have or not have? procedure double (n:positive integer)

while n > 0 n := 2n

procedure divide (n: positive integer)while n >= 0begin m := 1/n n := n – 1end

Page 6: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples II

Exercise 2.1.9: Describe an algorithm that inserts an integer x in the appropriate position into the list a1, a2, …, an of integers that are in increasing order.

Exercise 2.1.13: Describe an algorithm that produces the maximum, median, mean, and minimum of a set of three integers.

Page 7: CMSC 203 / 0201 Fall 2002

September1999

WED 9/18ALGORITHM COMPLEXITY (2.2)

** Homework #2 due today! **

Page 8: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

CONCEPTS / VOCABULARY

Computational complexity, time complexity, (space complexity) Worst-case, average-case Exponential complexity, polynomial complexity, linear

complexity, logarithmic complexity Tractability, intractability, unsolvability (halting problem,

Alan Turing), NP vs. P, NP-complete

Page 9: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples

Find the worst-case and average-case number of steps and big-O time complexity of the algorithms developed in the last class: Exercise 2.1.9: Describe an algorithm that inserts an

integer x in the appropriate position into the list a1, a2, …, an of integers that are in increasing order.

Exercise 2.1.13: Describe an algorithm that produces the maximum, median, mean, and minimum of a set of three integers.

Page 10: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples II

Exercise 2.2.13: Analyze the average-case performance of the linear search algorithm, if exactly half the time element x is not in the list, and if x is in the list, it is equally likely to be in any position.

Page 11: CMSC 203 / 0201 Fall 2002

September1999

FRI 9/20INTEGERS AND DIVISION (2.3)

Page 12: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

CONCEPTS / VOCABULARY

Fundamental Theorem of Arithmetic Primes, composite numbers Factoring Division (quotient, remainder) a | b Greatest common divisor, least common multiple,

relative primes Modular arithmetic, congruence

“The Division Algorithm” Note: We’ll revisit pp. 120-125 when we get to

Section 2.5.

Page 13: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples

Example 2.3.2: Let n and d be positive integers. How many positive integers not exceeding n are divisible by d?

Theorem 2.3.3: If n is a composite integer, then n has a prime divisor less than or equal to n.

Example 2.3.5: Show that 101 is prime [using Theorem 2.3.3].

Page 14: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples II

Write an algorithm to compute the prime factorization of n using the procedure on p. 115: Divide n by successive primes, starting with 2. If no prime factor n is found, then n is prime. If a prime factor p is found, continue by factoring n/p.

Start with p instead of 2.

Page 15: CMSC 203 / 0201 Fall 2002

September1999October 1999October 1999

Examples III

Exercise 2.3.19: Show that if 2n-1 is prime, then n is prime. [Hint: Use the identity 2ab-1 = (2a-1) (2a(b-1) + 2a(b-2) + … + 2a + 1).]