You are on page 1of 10

Analysis

A l i off
Algorithms
Instructor: Dr Ghulam Mustafa
Punjab University, Gujranwala Campus
What is an Algorithm?
•A An algorithm
l ith is i a computational
t ti l
procedure that takes some value, or
a sett off values,
l as input
i t and
d
produces some values, or a set of
values,
l as output.
t t
• So,, it is a sequence
q of computational
p
steps that transform the input into
the output.
p
Analysis
y Issues
• WHAT DATA STRUCTURES TO USE! (lists,
(lists
queues, stacks, heaps, trees, etc.)

• IS IT CORRECT! (all or only most of the time?)

• HOW EFFICIENT IS IT! (asymptotically fixed or does it


depend on the inputs?)

• IS THERE AN EFFICIENT ALGORITHM!!


• P NP
Why study algorithm?
• Internet
Internet. Web search,
search packet routing
routing, distributed file
sharing, ...
• Biology. Human genome project, protein folding, ...
• Computers.
C t Circuit
Ci it llayout,t fil
file system,
t compilers,
il ...
• Computer graphics. Movies, video games, virtual
reality, ...
• Security. Cell phones, e-commerce, voting machines,
...
• Multimedia.
u ed a MP3,3, JJPG,
G, DivX,, HDTV,, face
ace recognition,
ecog o ,
...
• Social networks. Recommendations, news feeds,
advertisements,, ...
• Physics. N-body simulation, particle collision
simulation, ...
Pseudo code Conventions
(cont )
(cont.)
We will be using pseudocode for our algorithm
implementations
p and will follow the conventions summarized
here.
• Indentation will indicate block structures (similar to
Python)
• Loops (for, while) and decisions (if/else) follow
standard C++/Java conventions, i.e. the loop
counter retains its value that caused the loop to
terminate (useful when proving the correctness of
an algorithm)
• // indicates comments
• = indicates assignment (and can be used for
multiple assignment)
• Variables are local to the p
procedure in which they y
are used
Pseudo code Conventions
• Array elements are accessed with bracketed indices as in
C++/Java, e.g. A[i]
• The notation ".." is used to indicate a (inclusive) subarray, e.g.
A[i..j] indicates the elements <A[i],A[i+1], ..., A[j]>
• Object attributes and methods are accessed using the standard "."
notation,
i e.g. A.length
Al h
• Object names are treated as pointers, thus B = A creates
an alias NOT a copy
• Variables are passed by value, except for objects and arrays
which are passed by reference.
p values may
• Multiple y be returned in a return statement
• Boolean expressions are short circuiting, i.e. are evaluated left to
right only until the value of the expression is known
Example: One Loop
Problem: Does arrayy A contain the
integer t? Given A (array of length n)
and t (an integer).
Algorithm 1
1: for i=1 to n
2
2: if A[i]==t
A[i] t
3
3: return TRUE
4: return FALSE
Example: Two Loop
Problem: Given A;B (arraysy of length
g
n) and t (an integer). [Does A or B
contain t?]
Algorithm 2
1
1: f i=1
for i 1 to n
2: if A[i]==t
3: return TRUE
4: for i=1 to n
5: if A[i]==t
6: return TRUE
7: return FALSE
Example: Two Nested Loop
Problem: Do arrays A;B have a
number in common? Given arrays A;
B off length
l h n.
Algorithm
g 3
1: for i=1 to n
2: for j=1 to n
3:: if A[i]==B[j]
A[i] B[j]
4: return TRUE
5: return FALSE
Example: Two Nested Loop (II)
Problem: Does array A have duplicate
entries? Given arrays A of length n.
Algorithm 4
1: for i=1 to n
2: for jj=i+1
i 1 to n
3:: if A[i]==B[j]
[] [j]
4: return TRUE
5: return FALSE

You might also like