Academic presentation for college course (globe design)

Transkript

Academic presentation for college course (globe design)
CS473 - Algorithms I
Lecture 1
Introduction to Analysis of Algorithms
View in slide-show mode
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
1
Grading




Midterm: 20%
Final: 20%
Classwork: 54%
Attendance: 6%
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
2
Classwork (54% of the total grade)

Like small exams, covering the most recent material

There will be 7 classwork sessions

Thursdays: 17:40 – 19:30?


Open book (clean and unused). No notes. No slides.
See the syllabus for details.
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
3
Algorithm Definition


Algorithm: A sequence of computational steps that
transform the input to the desired output
Procedure vs. algorithm


An algorithm must halt within finite time with the right output
Example:
a sequence of
n numbers
CS 473 – Lecture 1
Sorting
Algorithm
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
sorted permutation
of input sequence
4
Many Real World Applications

Bioinformatics


Internet


Search and access information in large data
Security


Manage/manipulate/route data
Information retrieval


Determine/compare DNA sequences
Encode & decode personal/financial/confidential data
Computer Aided Design

Minimize human effort in chip-design process
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
5
Course Objectives

Learn basic algorithms & data structures
Gain skills to design new algorithms

Focus on efficient algorithms

Design algorithms that




are fast
use as little memory as possible
are correct!
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
6
Outline of Lecture 1

Study two sorting algorithms as examples
Insertion sort: Incremental algorithm
 Merge sort: Divide-and-conquer


Introduction to runtime analysis
Best vs. worst vs. average case
 Asymptotic analysis

CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
7
Sorting Problem
Input: Sequence of numbers
a1, a2,…,an
Output: A permutation
=   (1), (2),…,  (n)
such that
a(1)a(2)  …  a(n)
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
8
Insertion Sort
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
9
Insertion Sort: Basic Idea


Assume input array: A[1..n]
Iterate j from 2 to n
already sorted
j
iter j
insert into sorted array
j
after
iter j
CS 473 – Lecture 1
sorted subarray
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
10
Pseudo-code notation

Objective: Express algorithms to humans in a clear
and concise way

Liberal use of English

Indentation for block structures

Omission of error handling and other details
 needed in real programs
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
11
Algorithm: Insertion Sort (from Section 2.2)
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
12
Algorithm: Insertion Sort
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
Iterate over array elts j
Loop invariant:
The subarray A[1..j-1]
is always sorted
already sorted
j
key
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
13
Algorithm: Insertion Sort
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
Shift right the entries
in A[1..j-1] that are > key
already sorted
< key
j
> key
j
< key
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
> key
14
Algorithm: Insertion Sort
Insertion-Sort (A)
1. for j  2 to n do
key
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
j
< key
> key
do
5.
A[i+1]  A[i];
now sorted
6.
i  i - 1;
endwhile
Insert key to the correct location
7.
A[i+1]  key;
End of iter j: A[1..j] is sorted
endfor
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
15
Insertion Sort - Example
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
5
2
4 6
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
1
3
16
Insertion Sort - Example: Iteration j=2
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
5
j
key=2
2
4 6
1
3
initial
4
1
3
shift
3
insert
key
sorted
>2 j
5 2
6
sorted
2
5
4 6
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
1
17
Insertion Sort - Example: Iteration j=3
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
j
2
5
key=4
4 6
1
3
initial
sorted
What are the entries at the
end of iteration j=3?
?
?
? ?
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
?
?
18
Insertion Sort - Example: Iteration j=3
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
j
2
5
key=4
4 6
1
3
initial
1
3
shift
3
insert
key
sorted
<4 >4
2
5
j
4
6
sorted
2 4
5 6
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
1
19
Insertion Sort - Example: Iteration j=4
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
key=6
j
2
4
5 6
1
3
initial
1
3
shift
3
insert
key
sorted
<6 j
2
4
5
6
sorted
2
4
5 6
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
1
20
Insertion Sort - Example: Iteration j=5
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
key=1
j
2
4
5 6
1
3
initial
sorted
What are the entries at the
end of iteration j=5?
?
?
? ?
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
?
?
21
Insertion Sort - Example: Iteration j=5
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
key=1
j
2
4
5 6
1
3
initial
3
shift
3
insert
key
sorted
>1 >1 >1 >1
j
2
1
4
5
6
sorted
1
2
4 5
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
6
22
Insertion Sort - Example: Iteration j=6
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
j key=3
1
2
4 5
6
3
initial
<3 >3 >3 >3
j
2
3
shift
6
insert
key
sorted
1
4
5
6
sorted
1
2 3
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
4
5
23
Insertion Sort Algorithm - Notes

Items sorted in-place
Elements rearranged within array
 At most constant number of items stored outside the array
at any time (e.g. the variable key)
 Input array A contains sorted output sequence when the
algorithm ends


Incremental approach

Having sorted A[1..j-1], place A[j] correctly so that A[1..j]
is sorted
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
24
Running Time

Depends on:
Input size (e.g., 6 elements vs 6M elements)
 Input itself (e.g., partially sorted)


Usually want upper bound
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
25
Kinds of running time analysis
Worst Case (Usually)
T(n) = max time on any input of size n
 Average Case (Sometimes)
T(n) = average time over all inputs of size n
Assumes statistical distribution of inputs
 Best Case (Rarely)
T(n) = min time on any input of size n

BAD*: Cheat with slow algorithm that works fast on some inputs
GOOD: Only for showing bad lower bound
*Can modify any algorithm (almost) to have a low best-case running time

Check whether input constitutes an output at the very beginning of the algorithm
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
26
Running Time

For Insertion-Sort, what is its worst-case time?


Depends on speed of primitive operations
 Relative speed (on same machine)
 Absolute speed (on different machines)
Asymptotic analysis
Ignore machine-dependent constants
 Look at growth of T(n) as n

CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
27
 Notation


Drop low order terms
Ignore leading constants
e.g.
2n2+5n + 3 = (n2)
3n3+90n2-2n+5= (n3)

Formal explanations in the next lecture.
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
28
• As n gets large, a (n2) algorithm runs faster
than a (n3) algorithm
T(n)
Runtime larger
asymptotically
n
min value for n0
CS473 – Lecture 1
Cevdet Aykanat - Bilkent University
Computer Engineering Department
29
Insertion Sort – Runtime Analysis
Cost
Insertion-Sort (A)
c1
c2
c3
c4
c5
c6
c7
CS 473 – Lecture 1
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
tj: The number of
times while loop
test is executed for j
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
30
How many times is each line executed?
# times
Insertion-Sort (A)
n
n-1
n-1
k4
k5
k6
n-1
CS 473 – Lecture 1
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
n
k4 = å t j
j=2
n
k5 = å (t j -1)
j=2
n
k6 = å (t j -1)
j=2
31
Insertion Sort – Runtime Analysis

Sum up costs:
n
T n   c1n  c2 (n  1)  c3 (n  1)  c4  t j 
n
n
j=2
j=2
j 2
c5 å(t j -1) + c6 å(t j -1) + c7 (n -1)

What is the best case runtime?

What is the worst case runtime?
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
32
Question: If A[1...j] is already sorted, tj = ?
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
key=6
j
2
4
5 6
1
3
initial
3
shift
none
sorted
<6 j
2
4
5
6
1
tj = 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
33
Insertion Sort – Best Case Runtime

Original function:
n
T n   c1n  c2 (n  1)  c3 (n  1)  c4  t j 
n
n
j=2
j=2
j 2
c5 å(t j -1) + c6 å(t j -1) + c7 (n -1)

Best-case: Input array is already sorted
tj = 1 for all j
T ( n) = (c1 + c2 + c3 + c4 + c7 )n - (c2 + c3 + c4 + c7 )
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
34
Q: If A[j] is smaller than every entry in A[1..j-1], tj = ?
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
key=1
j
2
4
5 6
1
3
initial
3
shift
all
sorted
>1 >1 >1 >1
j
2
1
4
5
6
tj = j
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
35
Insertion Sort – Worst Case Runtime

Worst case: The input array is reverse sorted
tj = j for all j

After derivation, worst case runtime:
T ( n) = 12 (c4 + c5 + c6 )n2 +
(c1 + c2 + c3 + 12 (c4 - c5 - c6 )+ c7 )n - (c2 + c3 + c4 + c7 )
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
36
Insertion Sort – Asymptotic Runtime Analysis
Insertion-Sort (A)
1. for j  2 to n do
2.
key  A[j];
3.
i  j - 1;
4.
while i > 0 and A[i] > key
do
5.
A[i+1]  A[i];
6.
i  i - 1;
endwhile
7.
A[i+1]  key;
endfor
CS 473 – Lecture 1
(1)
(1)
(1)
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
37
Asymptotic Runtime Analysis of Insertion-Sort
• Worst-case (input reverse sorted)
– Inner loop is (j)
 n 
T n     j     j    n 2
j 2
 j 2 
 
n
• Average case (all permutations equally likely)
– Inner loop is (j/2)
n
n
j 2
j 2
 
T n     j 2    j    n 2
• Often, average case not much better than worst case
• Is this a fast sorting algorithm?
– Yes, for small n. No, for large n.
CS473 – Lecture 1
Cevdet Aykanat - Bilkent University
Computer Engineering Department
38
Merge Sort
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
39
Merge Sort: Basic Idea
Input array A
Divide
sort this half
sort this half
Conquer
merge two sorted halves
Combine
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
40
Merge-Sort (A, p, r)
if p = r then return;
else
q   (p+r)/2;
Merge-Sort (A, p, q);
Merge-Sort (A, q+1, r);
Merge (A, p, q, r);
endif
(Divide)
(Conquer)
(Conquer)
(Combine)
• Call Merge-Sort(A,1,n) to sort A[1..n]
• Recursion bottoms out when subsequences have length 1
CS473 – Lecture 1
Cevdet Aykanat - Bilkent University
Computer Engineering Department
41
Merge Sort: Example
Merge-Sort (A, p, r)
p
if p = r then
return
else
q   (p+r)/2
5
q
2
p
r
4 6
1
q
3
r
2
4
5
1
3
6
1
2
3 4
5
6
Merge-Sort (A, p, q)
Merge-Sort (A, q+1, r)
Merge(A, p, q, r)
endif
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
42
How to merge 2 sorted subarrays?
A[p..q]
2
4
5
1
A[q+1..r]


1
3
2
3 4
5
6
6
HW: Study the pseudo-code in the textbook (Sec. 2.3.1)
What is the complexity of this step?
(n)
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
43
Merge Sort: Correctness
Merge-Sort (A, p, r)
if p = r then
return
else
q   (p+r)/2
Merge-Sort (A, p, q)
Merge-Sort (A, q+1, r)
Merge(A, p, q, r)
endif
CS 473 – Lecture 1
Base case: p = r
 Trivially correct
Inductive hypothesis: MERGE-SORT
is correct for any subarray that is a
strict (smaller) subset of A[p, r].
General Case: MERGE-SORT is
correct for A[p, r].
 From inductive hypothesis and
correctness of Merge.
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
44
Merge Sort: Complexity
Merge-Sort (A, p, r)
if p = r then
return
else
q   (p+r)/2
Merge-Sort (A, p, q)
Merge-Sort (A, q+1, r)
Merge(A, p, q, r)
endif
CS 473 – Lecture 1
T(n)
(1)
(1)
T(n/2)
T(n/2)
(n)
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
45
Merge Sort – Recurrence

Describe a function recursively in terms of itself
To analyze the performance of recursive algorithms

For merge sort:

T(n) =
CS 473 – Lecture 1
(1)
if n=1
2T(n/2) + (n)
otherwise
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
46
How to solve for T(n)?
T(n) =
(1)
if n=1
2T(n/2) + (n)
otherwise

Generally, we will assume T(n) = (1) for sufficiently small n

The recurrence above can be rewritten as:
T(n) = 2 T(n/2) + (n)

How to solve this recurrence?
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
47
Solve Recurrence: T(n) = 2T (n/2) + Θ(n)
Θ(n)
T(n/2)
CS 473 – Lecture 1
T(n/2)
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
48
Solve Recurrence: T(n) = 2T (n/2) + Θ(n)
Θ(n/2)
T(n/4)
CS 473 – Lecture 1
Θ(n/2)
T(n/2)
T(n/4) T(n/4)
T(n/4)
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
each size
halved
2x
subprobs
Θ(n)
49
Solve Recurrence: T(n) = 2T (n/2) + Θ(n)
Θ(lgn)
Θ(n)
Θ(n/2)
T(n/4)
Θ(n)
Θ(n/2)
T(n/4) T(n/4)
Θ(n)
T(n/4)
Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1) Θ(1)
Θ(n)
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
Θ(n)
Total: Θ(nlgn)
50
Merge Sort Complexity

Recurrence:
T(n) = 2T(n/2) + Θ(n)

Solution to recurrence:
T(n) = Θ(nlgn)
CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
51
Conclusions: Insertion Sort vs. Merge Sort


(nlgn) grows more slowly than (n2)
Therefore Merge-Sort beats Insertion-Sort in the
worst case
In practice, Merge-Sort beats Insertion-Sort for n>30
or so.

CS 473 – Lecture 1
Cevdet Aykanat and Mustafa Ozdal
Computer Engineering Department, Bilkent University
52

Benzer belgeler

CS473-Algorithms I - Bilkent University Computer Engineering

CS473-Algorithms I - Bilkent University Computer Engineering T(n) = average time over all inputs of size n Assumes statistical distribution of inputs

Detaylı

j - Bilkent University Computer Engineering Department

j - Bilkent University Computer Engineering Department Hoare’s Partitioning Algorithm • It is important that  A[p] is chosen as the pivot element  If A[r] is used as pivot then • may yield a trivial split (termination i  j  r) • occurs when A[p…r...

Detaylı

Bilkent University Computer Engineering Department

Bilkent University Computer Engineering Department Total cost = å(depth(i) +1)× freq(i) i Note: The BST will be static. Only search operations will be performed. No insert, no delete, etc. CS 473 – DP Examples

Detaylı

Mustafa Ozan Karsavuran - Bilkent University Computer

Mustafa Ozan Karsavuran - Bilkent University Computer Publications  M. O. Karsavuran, K. Akbudak and C. Aykanat, “Locality-Aware Parallel Sparse MatrixVector and Matrix-Transpose-Vector Multiplication on Many-Core Processors,” in IEEE Transactions on...

Detaylı

n - Bilkent University Computer Engineering Department

n - Bilkent University Computer Engineering Department Cevdet Aykanat - Bilkent University Computer Engineering Department

Detaylı

Kelime İşleme Algoritmala

Kelime İşleme Algoritmala A subsequence of a string S, is a set of characters that appear in left -toright order, but not necessarily consecutively. Example ACTTGCG • ACT, ATTC, T, ACTTGC are all subsequences. • TTA is not ...

Detaylı