0% found this document useful (0 votes)
4 views21 pages

Lecture1 Intro

The document outlines a course on algorithms, detailing the textbook references, grading distribution, and key topics such as algorithm performance analysis, design paradigms, and specific algorithms like Fibonacci and Insertion Sort. It emphasizes the importance of correct algorithms, the concept of NP-complete problems, and methods for analyzing algorithm efficiency using asymptotic notation. The document also includes examples and running time comparisons for different algorithms.

Uploaded by

Huzaifa Siam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views21 pages

Lecture1 Intro

The document outlines a course on algorithms, detailing the textbook references, grading distribution, and key topics such as algorithm performance analysis, design paradigms, and specific algorithms like Fibonacci and Insertion Sort. It emphasizes the importance of correct algorithms, the concept of NP-complete problems, and methods for analyzing algorithm efficiency using asymptotic notation. The document also includes examples and running time comparisons for different algorithms.

Uploaded by

Huzaifa Siam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Inroduction

Course Overview
❑ Textbook and References
❑ Ellis Horowitz, and Sartaj Sahni, Fundamentals of Computer Algorithms,
Galgotia Publications (P) Ltd.
❑ Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein,
Introduction to Algorithms, Prentice-Hall of India Private Limited.
❑ Handouts that I will provide
❑ Grading Distribution
◼ Attendance – 10%
◼ Class Test / Quizz – 20%
◼ Mid Term Exam – 30%
◼ Final Exam – 40%
What is this Course About?
❑ Analyze the asymptotic performance of algorithms.
❑ Apply important algorithmic design paradigms and methods
including :
▪ Divide and Conquer
▪ Dynamic Programming
▪ Greedy Algorithms
▪ Backtracking
▪ And many more
Algorithm

❑ An algorithm is any well-defined computational


procedure that takes some value, or set of values, as
input and produces some value, or set of values, as
output.

Sequence of
Input computational Output
steps
Example of Algorithm: Sorting Problem
❑ Input: A sequence of n numbers: a1, a2 ,..., an

❑ Output: A permutation (reordering) a1, a2 ,..., an


of the input sequence such that a1  a2  ...  an

❑ Example: Input: sequence 31, 41, 59, 26, 41, 58


Output: sequence 26, 31, 41, 41, 58, 59
Correct Algorithms
❑ An algorithm is said to be correct if, for every input
instance, it halts with the correct output. We say that a
correct algorithm solves the given computational problem.
❑ An incorrect algorithm might not halt at all on some input
instances, or it might halt with an answer other than the
desired one.
❑ Incorrect algorithms can sometimes be useful, if their
error rate can be controlled. (An example of this when we
study algorithms for finding large prime numbers.)
Hard problems
❑ There are some problems for which no efficient solution is
known, which are known as NP-complete:
▪ it is unknown whether or not efficient algorithms exist for NP-
complete problems.
▪ the set of NP-complete problems has the remarkable property that
if an efficient algorithm exists for any one of them, then efficient
algorithms exist for all of them.
▪ a small change to the problem statement can cause a big change
to the efficiency of the best known algorithm.
Choosing Algorithms
❑ Example: Fibonacci sequence is defined as follows:
F(0) = 0, F(1) = 1, and
F(n) = F(n-1) + F(n-2) for n > 1.

❑ Write an algorithm to computer F(n).

❑ There many algorithms, but what is the most efficient ?


Algorithms 1 and 2 for Fibonacci
❑ Algorithm 1 for Fibonacci
function fib1(n){
if n < 2 then return n;
else return fib1(n-1) + fib1(n-2);
}

❑ Algorithm 2 for Fibonacci


function fib2(n){
i= 1; j = 0;
for k = 1 to n do { j = i+j; i = j- i;}
return j;
}
Algorithms 3 for Fibonacci
Algorithm 1 for Fibonacci
function fib3(n){
i = 1; j = 0; k = 0; h = 1;
while n>0 do {
if (n odd) then { t = jh;
j = ih + jk +t;
i = ik +t;}
t = h^2;
h = 2kh+t;
k = k^2+t;
n = n div 2;}
return j;
}
Example of Running Times for Fibonacci

n 10 20 30 50 100 10000 1000000 100000000

fib1 8 ms 1s 2 min 21 days

fib2 1/6 ms 1/3 ms ½ ms ¾ ms 3/2 ms 150 ms 15 s 25 min

fib3 1/3 ms 2/5 ms ½ ms ½ ms ½ ms 1 ms 3/2 ms 2 ms


Insertion Sort
❑ Efficient algorithm for sorting a small
number of elements

❑ We start with an empty left hand and the


cards face down on the table.
❑ We then 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, we
compare it with each of the cards already
in the hand, from right to left.
Insertion Sort (Cont..)
INSERTION-SORT(A)
for j = 2 to length[A] do
key = A[j]
//Insert A[j] into the sorted sequence A[1.. j - 1].
i = j-1
while i>0 and A[i]>key
A[i + 1] = A[i]
i = i-1
end while
A[i+1] = key
end for
Analyzing Algorithms
❑ Analyzing an algorithm: for an input size,
❑ measure memory (space)

❑ measure computational time (running time).


❑ Input size: depends on the problem:
❑ Sorting: number of items in the input; array size,… O(n)
❑ Big integer (multiplying, …): number of bits to represent the input in binary notation
O(log n)
❑ Two number: input of a graph can be O(n, m), number of vertices and number of
edges.
❑ Running time:
❑ A constant amount of time is required to execute each line
❑ Each execution of the ith line takes time ci , where ci is a constant.
Analysis of Insertion Sort
Why?

❑ What are the best and


worst-case running?
❑ How about average-case?

tj is the number of times the while loop test in line 5 is executed


for that value of j.

T(n) = c1n + c2(n-1) + c4(n-1) + c5 ∑j=2..n tj + c6 ∑j=2..n (tj-1)


+ c7 ∑j=2..n (tj-1) + c8(n-1)
Where are c6 &
c7 !!
Analysis of Insertion Sort (Cont..)
❑ Best case: the array is already sorted

𝑇 𝑛 = 𝑐1𝑛 + 𝑐2 𝑛 − 1 + 𝑐4 𝑛 − 1 + 𝑐5 𝑛 − 1 + 𝑐8 𝑛 − 1

𝑇 𝑛 = 𝑐1 + 𝑐2 + 𝑐4 + 𝑐5 + 𝑐8 𝑛 − 𝑐2 + 𝑐4 + 𝑐5 + 𝑐8

𝑇 𝑛 = 𝑎𝑛 + 𝑏

Linear function of n
Analysis of Insertion Sort (Cont..)
❑ Worst case: the array is already in reverse sorted order
𝑛 𝑛+1
𝑇 𝑛 = 𝑐1𝑛 + 𝑐2 𝑛 − 1 + 𝑐4 𝑛 − 1 + 𝑐5 𝑛 − 1 + 𝑐5 −1
2
𝑛 𝑛−1 𝑛 𝑛−1
+ 𝑐6 + 𝑐7 + 𝑐8 𝑛 − 1
2 2

𝑇 𝑛 = 𝑛2 + 𝑐1 + 𝑐2 + 𝑐4 + − − + 𝑐8 n
2 2 2 2 2 2
− 𝑐2 + 𝑐4 + 𝑐5 + 𝑐8
Quadratic
𝑇 𝑛 = 𝑎𝑛2 + 𝑏𝑛 + 𝑐
function of n
Growth Function
❑ Asymptotic notation
❑ The order of growth of the running time of an algorithm
gives a simple characterization of the algorithm's
efficiency.

❑ For
input sizes large enough, we make only the order of
growth of the running time relevant, so we study the
asymptotic efficiency of algorithms.
Asymptotic notation
❑ Θ notation
❑ Θ(g(n)) = {f(n) : there exist positive constants c1, c2, and n0 such
that 0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n) for all n ≥ n0}.
❑ We usually write f(n)= Θ(g(n)) to express the same notation
❑ We say that g(n) is an asymptotically tight bound for f(n)
❑ Ο notation
❑ O(g(n)) = {f(n): there exist positive constants c and n0 such that
0≤ f(n) ≤ cg(n) for all n ≥ n0}
❑ We write f(n)= Ο(g(n)) to indicate that a function f(n) is a member of
the set Ο(g(n))
❑ We use Ο-notation when we have an asymptotic upper bound
Asymptotic notation (Cont..)
❑ Ω notation
❑ Ω(g(n)) = {f(n): there exist positive constants c and n0 such that
0 ≤ cg(n) ≤ f(n) for all n ≥ n0}.
❑ We write f(n)= Ω (g(n)) to indicate that a function f(n) is a member of
the set Ω (g(n))
❑ Ω-notation provides an asymptotic lower bound
Thanks for your Attention

Q&A

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy