Lecture13 131010algorithms2 4up
Lecture13 131010algorithms2 4up
Prof. Rodger
Solution: Assume n = 2k elements. Note that k = log n. Solution: n passes through list. pass n i comparisons
• Two comparisons are made at each stage; i < j, and x > am .
• Size of list is 2k , then 2k-1. then 2k-2 , … then 21 = 2.
• At the last step, list size is 20 = 1 and single last element compared.
• Hence, at most 2k + 2 = 2 log n + 2 comparisons are made. Θ(n2) since
• Therefore, the time complexity is Θ (log n), better than linear search. .
13 14
Stooge Sort
Worst case of BogoSort
• n elements are in an array
• If the value at the end is smaller than the first
• Also known as “StupidSort”
element, swap them
• If there are three of more elements then:
– Stooge sort the first 2/3 of the array • n elements in an array.
– Stooge sort the last 2/3 of the array • While (not in order)
– Stooge sort the first 2/3 of the array again – Shuffle array
• Else
– Done • Worst case:
.
• Worst case time is O( ) 19
• Average Case: 20
Matrix Multiplication Algorithm
Worst case of BogoSort • matrix multiplication algorithm; C = A B where C is an
m n matrix that is the product of the m k matrix A and
• Also known as “StupidSort” the k n matrix B.
Example: How many additions of integers and Example: How many additions of integers and
multiplications of integers are used by the multiplications of integers are used by the
matrix multiplication algorithm to multiply matrix multiplication algorithm to multiply
two n n matrices. two n n matrices.
Solution: There are n2 Solution: There are n2
n n n n
3 2
n n n n3 n2 n
O(n3). O(n3).
23 24
Matrix-Chain Multiplication Matrix-Chain Multiplication
• Compute matrix-chain A1A2∙ ∙ ∙An with fewest • Compute matrix-chain A1A2∙ ∙ ∙An with fewest
multiplications, where A1 , A2 , ∙ ∙ ∙ , An are , multiplications, where A1 , A2 , ∙ ∙ ∙ , An are ,
,∙∙∙ integer matrices. Matrix ,∙∙∙ integer matrices. Matrix
multiplication is associative. multiplication is associative.
Example: In which order should the integer matrices Example: In which order should the integer matrices
A1A2A3 - where A1 is 30 20 , A2 20 40, A3 40 10 - be A1A2A3 - where A1 is 30 20 , A2 20 40, A3 40 10 - be
multiplied? Solution: two possible ways for A1A2A3. multiplied? Solution: two possible ways for A1A2A3.
– A1(A2A3): A2A3 takes 20 ∙ 40 ∙ 10 8000 mults.. A1 – A1(A2A3): A2A3 takes 20 ∙ 40 ∙ 10 8000 mults.. A1
by the 20 10 matrix A2A3 takes 30 ∙ 20 ∙ 10 6000 by the 20 10 matrix A2A3 takes 30 ∙ 20 ∙ 10 6000
mults. Total number is 8000 6000 14,000. mults. Total number is 8000 6000 14,000.
– (A1A2)A3: A1A2 takes 30 ∙ 20 ∙ 40 24,000 mults. – (A1A2)A3: A1A2 takes 30 ∙ 20 ∙ 40 24,000 mults.
A1A2 by A3 takes 30 ∙ 40 ∙ 10 12,000 mults. Total A1A2 by A3 takes 30 ∙ 40 ∙ 10 12,000 mults. Total
is 24,000 12,000 36,000. is 24,000 12,000 36,000.
So the first method is best. So the first method is best.
25 26
• The P versus NP problem asks whether the class P = NP? Are there problems
• Tractable Problem: There exists a polynomial time algorithm to whose solutions can be checked in polynomial time, but can not be solved in
solve this problem. These problems are said to belong to the polynomial time?
Class P. – Note that just because no one has found a polynomial time algorithm is
• Intractable Problem: There does not exist a polynomial time different from showing that the problem can not be solved by a
algorithm to solve this problem polynomial time algorithm.
• Unsolvable Problem : No algorithm exists to solve this problem, • If a polynomial time algorithm for any of the problems in the NP complete
e.g., halting problem. class were found, then that algorithm could be used to obtain a polynomial
• Class NP: Solution can be checked in polynomial time. But no time algorithm for every problem in the NP complete class.
polynomial time algorithm has been found for finding a solution – Satisfiability (in Section 1.3) is an NP complete problem.
to problems in this class. • It is generally believed that P NP since no one has been able to ind a
• NP Complete Class: If you find a polynomial time algorithm for polynomial time algorithm for any of the problems in the NP complete
one member of the class, it can be used to solve all the problems class.
in the class. • The problem of P versus NP remains one of the most famous unsolved
problems in mathematics including theoretical computer science . The
29
Clay Mathematics Institute has offered a prize of $1,000,000 for a 30
solution.