150 likes | 161 Views
Explore algorithm concepts and complexity in computing. Learn about integers and division properties, with examples and exercises to enhance understanding.
E N D
CMSC 203 / 0201Fall 2002 Week #4 – 16/18/20 September 2002 Prof. Marie desJardins
TOPICS • Algorithms • Algorithmic complexity • Integers
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
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
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.
WED 9/18ALGORITHM COMPLEXITY (2.2) ** Homework #2 due today! **
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
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.
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.
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.
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].
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.
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).]