Computational Complexity
(Continued)
15-150
1
Computational Complexity (Continued) 15-150 1 Story so far We - - PowerPoint PPT Presentation
Computational Complexity (Continued) 15-150 1 Story so far We need to model the efficiency of our algorithms. Complexity models (usually) follow the structure of the algorithm. Accounting for the most important operations are
(Continued)
15-150
1
the algorithm.
usually sufficient.
to recurrences.
expansions.
2
recursive algorithms.
3
4
5
6
7
In general the two sorted input lists can be of different sizes. merge ([1,3,5],[2,4,6,8]) βͺ [1,2,3,4,5,6,8]
8
Note that this not splitting a list in the middle!
9
Why? Why? π
!"#$%(π) = π(π)
π!"#$%(π) = π(π) split ([1,2,3,4,5] βͺ ([1,3,5],[2,4])
10
π
&'()'(π) = π(π)
π&'()'(π) = π(π)
11
Why?
12
( n 2* ) ( n 2* ) π¦ = log+ π 2* = π
13
Why? *All logs are log+ Why? /
$,- &./
π $ = π & β 1 π β 1
Geometric Series
2012 3 β 1 2 β 1 = π β 1 π
&!4(% π = π+π log π + π- + π/ π β π/ = O(n log n)
π+π log π
Lower order terms
14
split l
msort l1
msort l2 merge π‘πππ!"#$%(π) max(π‘πππ&!4(%
3 + , π‘πππ&!4(%( 3 +))
π‘πππ&'()'(π)
15
msort l2 split l merge msort l1 π!"#$%(π) max(π&!4(%
3 + , π&!4(%( 3 +))
π&'()'(π)
17
This split is purely sequential. βΉ π!"#$% π = π(π) This merge is purely sequential. βΉ π&'()' π = π(π)
18
19
) )
20
/
$,- 012! 3 ./ 1
2$ < /
$,- 5 1
2$ = 2 βΉ π&!4(% π < π- + π/ log+ π + 2π+π = π(π) Observation: Although we can run the two recursive calls to msort in parallel, the sequential algorithms for split and merge limit the overall parallelism!
21
π
!"#$% π = π(π log π)
π!"#$% π = π(π)
for sorting has to make at least π π log π comparisons.
22
23
It turns out we can improve the spans for split and merge to π?@ABC π = π(log π ) πDEFGE π = π(log π )
These will give us a π!"#$% π = π(log& π )
Think about why we have the square of the log.
(BST) is
value in each node
subtree < value in the root
subtree > value in the root
hold at each node.
24
25
7 6 9 3 1 8 6 I can not just attach these two subtrees to 6? I have to make sure everything on the left < 6 and everything on the right > 6 I need to keep track of the max value of the left and the min value of the right!!
26
What is the minimum work needed? π(π) Why?
27
π π = H π/ ππ π = 0 π+ + 2 π π 2 π > 0 Assumptions
size at each level
28
π π = H π/ ππ π = 0,1 π+ + 2 π π 2 π > 1 Assumptions
size at each level π π 2 β€ π π 2 βΉ π π β€ π+ +2 π π 2 for n > 1 Assumptions
the same size at each level π π β€ π+ + 2 π π 2 = π+ + 2π+ + 4π
3 6
β¦ = 1 + 2 + 4 + β― 2"./ π+ + 2"π
3 +"
π₯βππ π = π = 1 + 2 + 4 + β― 2&./ π+ + ππ 1 π π β€ π β 1 π+ + π/π = π(π)
29
Intuitively β the span should be proportional to the depth of the tree! Parallel
30
π π β€ π+ + π π 2 πππ π > 1 βΉ π π = π(log π) π 1 = π/ Left as an exercise.
solve a given problem?
the problem?
31
Miami, Houston, Pittsburgh, Chicago, Baltimore, Dallas, Houston, San Francisco, Seattle, Denver, Austin, Atlanta, St. Louis, Las Vegas, San Diego, Albuquerque, Philadelphia, Washington DC,β¦β¦}
32
immediately.
33
alphabetical order.
Chicago, Baltimore, Dallas, Houston, San Francisco, Seattle, Denver, Austin, Atlanta, St. Louis, Las Vegas, San Diego, Albuquerque, Philadelphia, Washington DC,β¦β¦}
34
alphabetical order.
immediately, it is βharderβ than the previous problem.
do it.
35
cities.
36
such that
Washington DC, β¦., Boston, New York.
37
such that starting with New York, you visit each city exactly once and return back to New York.
problem.
(you can fly if you want)
38
39
* 50! has Β» 85 digits Β» 1084 * Let us assume we can check 109 (1 billion) tours every second using a fast computer * We need Β» 1084/109 = 1075 seconds
* Β» 1070 days * Β» 2.5 * 1067 years * Β» 2.5 * 1065 centuries
* For reference: Big Bang is estimated to be about 1.5 *107 centuries ago.
40
41
any exponential e.g. π πY , π(π!)
42
Stack of n 2βs