Download presentation
Presentation is loading. Please wait.
Published byClifton Shaw Modified over 9 years ago
1
Introduction to Algorithms Jiafen Liu Sept. 2013
2
About me Teacher : Liu Jiafen Mail: jfliu@swufe.edu.cn Research Interest: –Information security –Formal Method and Verification of Correctness –Data Mining and Business Intelligence –Development of soft under iOS Homepage: –http://it.swufe.edu.cn/2011- 09/24/201109241343095261.html
3
Now it’s your turn Please introduce yourself –Name –Age –Graduated School –Specialty and Interest Background Investigation Have you studied data structure and other prerequisite courses of CS? Have you programmed in practice before?
4
About the Course Site: –http://fife.swufe.edu.cn/BILab/lectures/algo/al go.htmlLecture –Notes can be found here. Related Resources can also be found on http://ocw.mit.edu/courses, you can also watch lectures online http://ocw.mit.edu/courses No Textbook
5
About the Course 16 weeks (the last 2 is reserved for exam) Site: Tongbo Building 206 Time: Can we choose another proper time? –Tuesday afternoon ( 5-7 or 6-8 ) ? –Tuesday night ( 10-12 ) ? –Thursday night ( 10-12 ) –Or we have to split into 2 period(first 2 in classroom, and last 1 on computers)?
6
Grading 20%Attendance 50%Performance –1 presentation at least (10%) –assignments (30%) 30% Final paper
7
Course Objectives What is an algorithm? What’s the difference between algorithm and program? Why we need this course? How to evaluate a algorithm? How can we design a good algorithm? How to make an algorithm implemented?
8
What is an algorithm? Algorithm is a problem-solving method or process. It helps us to understand scalability. Algorithm satisfy the following properties: –Input –Output –Unambiguous –Finite –Feasible
9
Algorithm and Program Program is an implementation of algorithm using a programming language. Algorithm design takes the most important place in Software Engineering. Programmer VS Coder? How to become an excellent programmer?
10
What is important for a program? correctness functionality maintainablity robustness simplicity user- friendliness Performance scalability
11
Why we need performance? currencyPerformance is the currency of computing. Example: C family VS Java This course makes theoretical study of computer program performance and resource usage. –How to evaluate an algorithm? –How can we design a good algorithm? –How to make an algorithm implemented?
12
The problem of sorting InputInput: sequence 〈 a 1, a 2, …, a n 〉 of numbers. OutputOutput: permutation 〈 a 1 ', a 2 ', …, a n ' 〉 such that a 1 ' ≤ a 2 ' ≤…≤ a n '. Example: Input : 8 2 4 9 3 6 Output:2 3 4 6 8 9 How can we do that? –Recall the process of playing cards.
13
Insertion sort 8 2 4 9 3 6
14
Insertion sort 8 2 4 9 3 6 2 8 4 9 3 6
15
Insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6
16
Insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6
17
Insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6
18
Insertion sort 8 2 4 9 3 6 2 8 4 9 3 6 2 4 8 9 3 6 2 3 4 8 9 6 2 3 4 6 8 9 Done!
19
Insertion sort
20
Running time The running time depends on the input: an already sorted sequence is easier to sort. Since short sequences are easier to sort than long ones, we will parameterize the running time by the size of the input.
21
Kinds of analyses Worst-case: (usually) –T(n) =maximum time of algorithm on any input of size n. Average-case: (sometimes) –T(n) =expected time of algorithm over all inputs of size n. –Need assumption of statistical distribution of inputs. Best-case: (bogus) –Cheat with a slow algorithm that works fast on some input.
22
Machine-independent time Generally, we seek upper bounds on the running time, Why? –Because everybody likes a guarantee. What is insertion sort’s worst-case time? It depends on the speed of our computer: –relative speed (on the same machine) –absolute speed (on different machines)
23
Big Idea Asymptotic Analysis Ignore machine-dependent constants. growthLook at growth of T(n) as n→∞.
24
Θ-notation Math: Θ(g(n)) = { f(n) : there exist positive constants c 1, c 2, and n 0 such that 0 ≤ c 1 g(n) ≤ f (n) ≤ c 2 g(n) for all n ≥ n 0 } Engineering: Drop low-order terms; Ignore leading constants. Example: 3n 3 + 90n 2 –5n+ 6046 = Θ(n 3 )
25
Θ-notation Θ-notation is fabulous because it satisfies our issue of being able to compare both relative and absolute speed.
26
Asymptotic performance When n gets large enough, a Θ (n 2 ) algorithm always beats a Θ(n 3 ) algorithm. We shouldn’t ignore asymptotically slower algorithms, however. Real-world design situations often call for a careful balancing of engineering objectives. Asymptotic analysis is a useful tool to help to structure our thinking.
27
Insertion sort analysis Assume that every elemental operation is going to take some constant amount of time. Worst case : Input reverse sorted. [arithmetic series] Average case: All permutations equally likely.
28
About Θ-notation It is more of a descriptive notation than it is a manipulative notation. Is insertion sort a fast sorting algorithm? –Moderately so, for small n. –Not at all, for large n.
29
Another method : Merge sort Merging two sorted arrays 20 12 13 11 7 9 2 1 Time = Θ(n) to merge a total of n elements (linear time). 1 2 7 9 11 12 13 20
30
Merge Sort T(n) Θ(1) 2T(n/2) Θ(n) Θ(1) : Abuse, but we can ignore it. 2T(n/2) : Should be, but it turns out not to matter asymptotically.
31
Cost for merge sort We shall usually omit stating the base case when T(n) = Θ(1) because it has no effect on the asymptotic solution to the recurrence. Lecture 2 will provide several ways to find a good upper bound on T(n), for example, Recursion Tree.
32
Recursion Tree We can write as T(n) = 2T(n/2) + cn, where c > 0 and it is constant. T(n)
33
Recursion Tree We can write as T(n) = 2T(n/2) + cn, where c > 0 and it is constant. cn T(n/2) T(n/2)
34
Recursion Tree We can write as T(n) = 2T(n/2) + cn, where c > 0 and it is constant. cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4)
35
Recursion Tree We can write as T(n) = 2T(n/2) + cn, where c > 0 and it is constant. cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4) …… Θ(1) Height = ? cn #(leaves) = ? n logn+1 Θ(n) Total = cn*logn+ Θ(n)Θ (n logn)
36
Conclusion Θ(nlgn) grows more slowly than Θ(n 2 ). Therefore, merge sort asymptotically beats insertion sort in the worst case. In practice, merge sort beats insertion sort for n > 30 or so. Go test it out for yourself!
37
Homework Read Chapter1 and Chapter 2. Recall another sort method “Bubble sort” and express with pseudocode, then compute its cost using Θ -notation (Hand in paper edition the next class).
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.