Lecture 1: Introduction - : COMP3011: Design and Analysis of Algorithms
Lecture 1: Introduction - : COMP3011: Design and Analysis of Algorithms
Lecturer:
– Lecture 1: Introduction – Dr. Jesper Jansson
Department of Computing, PolyU
Office: PQ825
Teaching assistant:
Lecturer: Dr. Jesper Jansson Mr. Yushi Li
Office: PQ503
The Hong Kong Polytechnic University
2018-09-06
COMP3011 Lecture 1 1 / 38
Schedule Textbook
Lectures:
T. H. Cormen, C. E. Leiserson,
Thursdays, 10:30 a.m. – 12:20 p.m., PQ306
R. L. Rivest, and C. Stein:
Tutorials: Introduction to Algorithms (Third
Tuesdays, 11:30 a.m. – 12:20 p.m., R503 Edition),
Thursdays, 9:30 a.m. – 10:20 a.m., Y415 The MIT Press, Cambridge,
Thursdays, 4:30 p.m. – 5:20 p.m., P308 Massachusetts, 2009.
PART I
Introduction to algorithms
Algorithms
Example: (from elementary school)
This course:
COMP3011: Design and Analysis of Algorithms
Note that:
It doesn’t matter which two numbers are given here...the same
method works for any input consisting of two positive integers.
The algorithm can be run by a human, an electronic computer, or ???
The running time, i.e., the number of operations in terms of the input
size, is important. (In fact, faster algorithms than the above exist for
multiplying large numbers! [“Toom-Cook multiplication”, etc.])
COMP3011 Lecture 1 6 / 38 COMP3011 Lecture 1 7 / 38
Algorithm descriptions Motivation
Algorithm = A method for solving a specific, well-defined problem.
People often use pseudocode to describe algorithms formally.
Question:
Sometimes people use text (and pictures) to describe an algorithm.
What are some important practical applications of algorithms?
Remark:
If one assumes another model of computation such as a quantum computer
or a DNA computer that allows certain other operations in 1 unit of time
⇒ Some computational problems can be solved much faster!
COMP3011 Lecture 1 13 / 38 COMP3011 Lecture 1 14 / 38
Course goals
What are the properties of a “good” algorithm? 1 To provide students with in-depth knowledge of algorithm design
Gives the correct answer. techniques and tools for measuring the efficiency of algorithms.
For randomized algorithms, we want to get the correct answer with high
probability or that the running time is low with high probability.
PART II
Example: (also called a “problem instance”)
Insertion-Sort Input h31, 41, 59, 26, 41, 58i
⇒ Output h26, 31, 41, 41, 58, 59i
Algorithm?
Start with an empty left hand and the cards face down on the table. Example:
Remove one card at a time from the table and insert it into the
correct position in the left hand.
To find the correct position for a card, compare it with each of the
cards already in the hand, from right to left.
Note that at all times, the cards held in the left hand are sorted and
these cards were originally the top cards of the pile on the table.
COMP3011 Lecture 1 19 / 38 COMP3011 Lecture 1 20 / 38
Insertion-Sort, cont. Insertion-Sort, correctness
1) Initialization:
j = 2 ⇒ A[1..j − 1] = A[1] is trivially sorted.
2) Maintenance:
key is inserted into the correct position and all larger elements are
shifted one step to the right. where ci = the (constant) time taken by line i and
tj = the number of times the while-loop’s test is executed for that j.
3) Termination: The outer for-loop ends when j = n + 1.
⇒ A[1..j − 1] = A[1..n] is sorted. T (n) = c1 n + c2 (n − 1) + c4 (n − 1) + c5 nj=2 tj + c6 nj=2 (tj − 1) +
P P
⇒P
c7 nj=2 (tj − 1) + c8 (n − 1)
Best case: The input is already sorted. Worst case: The input is in reverse sorted order.
⇒ A[i] ≤ key immediately in the while-loop’s test ⇒ tj = 1 for all j ⇒ Always has
c A[i]c> key in the while-loop’s test ⇒ tj = j for all j
⇒ T (n) = (c1 + c2 + c4 + c5 + c8 )n − (c2 + c4 + c5 + c8 ) 5 6 c7 2 c5 c6 c7
⇒ T (n) = + + n + c1 + c2 + c4 + − − + c8 n
2 2 2 2 2 2
⇒ T (n) = an + b for constants a, b −(c2 + c4 + c5 + c8 )
⇒ T (n) is a linear function of n. ⇒ T (n) = an2 + bn + c for constants a, b, c
⇒ T (n) is a quadratic function of n.
COMP3011 Lecture 1 23 / 38 COMP3011 Lecture 1 23 / 38
Result: The subarrays are merged into a single sorted array in A[p..r ].
Then:
Θ(1), if n = 1,
T (n) = “recurrence”
2T (n/2) + Θ(n), if n > 1.
Merge-Sort, summary
Continue expanding until the problem sizes reach 1:
In summary, Algorithm Merge-Sort is correct and its worst-case running
time is: Θ(n lg n)
[Each level has cost cn, Does Insertion-Sort have any advantages over Merge-Sort?
there are lg n + 1 levels]