Sei sulla pagina 1di 32

Analysis and Design of Algorithms

Ghada Hassan 2010


 Instructor: Dr. Ghada Hassan
◦ PhD. Computer Science Department , University
College London, 2010.
◦ M.Sc. Computer Science, AUC, 1999
◦ B.Sc. Computer Science, AUC, 1995

 Text Book: Thomas Cormen, Charles


Leiserson, Ron Rivest and Cliff Stein.
Introduction to Algorithms, MIT press, 2nd
edition, 2002.

Analysis and Design of Algorithms


Ghada Hassan 2010
 Office: Staff room, first floor, El-Learning
building

 Office hours:
◦ Sunday 10:00 -12:00
◦ Wednesday 1:00 -3:00

 Outside of office hours


◦ By appointment
◦ Please email: ghnasr@link.net

Analysis and Design of Algorithms


Ghada Hassan 2010
 Course Attendance

 Lecture Participation

 Assignments

 Academic misconduct (plagiarism, cheating


on exams) will not be tolerated

Analysis and Design of Algorithms


Ghada Hassan 2010
 Grading policy:
◦ Semester Work 10 %
◦ Oral Examination 10 %
◦ Practical Examination 15 %
◦ Final-Term Examination 65 %
◦ --------------------------------------
◦ Total 100 %

Analysis and Design of Algorithms


Ghada Hassan 2010
 Grading policy:
◦ Course work 10 %
◦ Programming project 15 %
◦ Mid term exam 25 %
◦ Final-Term Examination 50 %
◦ --------------------------------------
◦ Total 100 %

Analysis and Design of Algorithms


Ghada Hassan 2010
◦ One lecture per week
◦ One lab per week
◦ Assignments
 Problem sets
 Programming assignments
◦ Mid-Term test (Week 8)
◦ Final exam

Analysis and Design of Algorithms


Ghada Hassan 2010
 A rigorous introduction to the analysis and
design of algorithms

◦ Analysis and Design?


◦ Programming
◦ Math

Analysis and Design of Algorithms


Ghada Hassan 2010
 Covers fundamentals of analysis of algorithms and
design.

 The main techniques for algorithms deign are


described and analyzed:

 Techniques for performance analysis through


complexity computation are explained.

 Concepts of NP-completeness are explained.

Analysis and Design of Algorithms
Ghada Hassan 2010
 Introduction

 Asymptotic Notation

 Asymptotic Performance

 Example: Insertion sort

 Quick Mathematical Revision

Analysis and Design of Algorithms


Ghada Hassan 2010
 What is an ALGORITHM?

 Example: Find the minimum number of


elements from the following set {1, 2, 3, 4} to
add to get a six.

 Techniques
◦ Brute force?
◦ Greedy technique?
◦ ....

Analysis and Design of Algorithms


Ghada Hassan 2010
Analysis and Design of Algorithms
Ghada Hassan 2010
Start with
x, y

Temp = y
Y=0 y=x%y
x = temp
YES
Output
x

Analysis and Design of Algorithms


Ghada Hassan 2010
 An algorithm is a well defined computational
procedure that takes a set of values as input,
and produces a set of values as output. An
algorithm is the sequence of computational
steps that transform the input into the
output.

 Analysing an algorithm means predicting the


resources that that the algorithm requires.

Analysis and Design of Algorithms


Ghada Hassan 2010
 To Design and Algorithm, we:
◦ Define Input
◦ Define Required Output
◦ Define steps in pseudo-code to arrive from input to
output [What is pseudo-code?]

 To Analyse an Algorithm:
◦ Prove that it works
◦ Prove its correctness
◦ Derive its complexity = performance analysis

Analysis and Design of Algorithms


Ghada Hassan 2010
 Analysis is performed with respect to a
computational model

 We usually use a generic uniprocessor


random-access machine (RAM)
◦ All memory equally expensive to access
◦ No concurrent operations
◦ All reasonable instructions take unit time
 What about functions/methods?

Analysis and Design of Algorithms


Ghada Hassan 2010
 Number of primitive steps that are executed
◦ Except for time of executing a function call most
statements roughly require the same amount of
time
 y=m*x+b
 c = 5 / 9 * (t - 32 )
 z = f(x) + g(y)

Analysis and Design of Algorithms


Ghada Hassan 2010
 In this course, we care most about asymptotic
performance

 How does the algorithm behave as the


problem size gets very large?
 Running time
 Memory/storage requirements
 Bandwidth/power requirements/logic gates/etc.

Analysis and Design of Algorithms


Ghada Hassan 2010
 Worst case
◦ Provides an upper bound on running time
◦ An absolute guarantee

 Average case
◦ Provides the expected running time
◦ Very useful, but treat with care: what is “average”?
 Random (equally likely) inputs
 Real-life inputs

Analysis and Design of Algorithms


Ghada Hassan 2010
Computer A: Computer B:
- One billion instruction per second -Ten million instruction per second
- Running insertion sort (2 n^2) - Running merge Sort (50 n log n)
HOW LONG WILL IT TAKE TO SORT 1 MILLION NUMBERS?

Analysis and Design of Algorithms


Ghada Hassan 2010
10^6 numbers: 2000 seconds 100 seconds
10^7 numbers: 2.3 DAYS! 20 minutes

Analysis and Design of Algorithms


Ghada Hassan 2010
250

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Analysis and Design of Algorithms


Ghada Hassan 2010
500

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Analysis and Design of Algorithms


Ghada Hassan 2010
1000

f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n

0
1 3 5 7 9 11 13 15 17 19

Analysis and Design of Algorithms


Ghada Hassan 2010
5000

4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000 f(n) = n^3
f(n) = 2^n

1000

0
1 3 5 7 9 11 13 15 17 19

Analysis and Design of Algorithms


Ghada Hassan 2010
10000000

1000000

100000

10000

1000

100

10

1
1 4 16 64 256 1024 4096 16384 65536

Analysis and Design of Algorithms


Ghada Hassan 2010
 Simplifications
◦ Ignore actual and abstract statement costs
◦ Order of growth is the interesting measure:
 Highest-order term is what counts
 Remember, we are doing asymptotic analysis
 As the input size grows larger it is the high order term
that dominates

Analysis and Design of Algorithms


Ghada Hassan 2010
 We say a certain algorithm’s run time is O(n2)
◦ Properly we should say run time is in O(n2)
◦ Read O as “Big-O” (you’ll also hear it as “order”)
 In general a function
◦ f(n) is O(g(n)) if there exist positive constants c and
n0 such that f(n)  c  g(n) for all n  n0
 Formally
◦ O(g(n)) = { f(n):  positive constants c and n0 such
that f(n)  c  g(n)  n  n0

Analysis and Design of Algorithms


Ghada Hassan 2010
 We say Insertion Sort’s run time is (n)

 In general a function
◦ f(n) is (g(n)) if  positive constants c and n0 such
that 0  cg(n)  f(n)  n  n0

Analysis and Design of Algorithms


Ghada Hassan 2010
 A function f(n) is (g(n)) if  positive
constants c1, c2, and n0 such that

c1 g(n)  f(n)  c2 g(n)  n  n0

 Theorem
◦ f(n) is (g(n)) iff f(n) is both O(g(n)) and (g(n))

Analysis and Design of Algorithms


Ghada Hassan 2010
 A function f(n) is o(g(n)) if  positive
constants c and n0 such that
f(n) < c g(n)  n  n0
 A function f(n) is (g(n)) if  positive
constants c and n0 such that
c g(n) < f(n)  n  n0
 Intuitively,

 o() is like <  () is like >  () is like =


 O() is like   () is like 
Analysis and Design of Algorithms
Ghada Hassan 2010
 Chapter 1: 1.2-2, 1.2-3
 Chapter 2: 2.2.-3

Analysis and Design of Algorithms


Ghada Hassan 2010

Potrebbero piacerti anche