Anda di halaman 1dari 49

Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr.

Muhammad Wasif Nisar Advanced Analysis of Algorithms


Introduction to Algorithms
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
So, what is
Computer Science?
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
Does a Computer
Scientist study
Computers?
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
But then computer
science must be about
writing computer
programs...
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
One definition...
Computer Science is the science of problem solving.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
A computer scientist can
Identify problems
Give an abstract, mathematical model
Analyze the problem
Finding a solution
Verify that the solution is correct
You need to be creative, precise, and like puzzles.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
Example...
Your boss tells you: We need to start selling
sandwiches. I can tell you how expensive bread,
cheese, and pickle are at different shops. Can you
work out the cheapest way for us to buy
ingredients?
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
So what do you do?
The solution may be in the form of a computer
program...
... but you need to start thinking about the problem
... and not hacking up something that might just
work.
A degree in Computer Science will give you all the
mental tools to solve this kind of problem, from the
maths you need to the programming skills.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
So what is an algorithm?
An algorithm is a mathematical recipe for solving a
problem.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
You are using all
kinds of algorithms
every day!
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
Algorithms
An algorithm is any well defined computational
procedure that takes some values or set of values as
input and produces some value or set of values as
output.
An algorithm is thus a sequence of computational steps
that transforms the input into output.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
How do you learn algorithms to cook?
To become good chef, you dont learn lots of recipes
by heart.
You study existing recipes.
You practice making them.
You experiment with your own variations.
Until you can invent your own recipes.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
How do you learn about algorithms?
A good computer scientist doesnt need to memorize
a big book of algorithms.
We will begin studying famous algorithms.
You can practice executing them by hand.
Once you recognize more general patterns, you can
experiment with your own variations.
At the end of the course, I expect you to invent
algorithms yourself.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
Analysis of Algorithms
The theoretical study of computer-program performance
and resource usage. Whats more important than
performance?
modularity
correctness
maintainability
functionality
robustness
user-friendliness
programmer time
simplicity
extensibility
reliability

Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
Why study algorithms and performance?
Algorithms help us to understand scalability.
Performance often draws the line between what is
feasible and what is impossible.
Algorithmic mathematics provides a language for
talking about program behavior.
Performance is the currency of computing.
The lessons of program performance generalize to
other computing resources.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
16
Origin of word: Algorithm
The word Algorithm comes from the name of the muslim author Abu Jafar
Mohammad ibn Musaal-Khowarizmi. He was born in the eighth century at
Khwarizm (Kheva), a town south of river Oxus inpresent Uzbekistan.
Uzbekistan, a Muslim country for over a thousand years, was taken over by
the Russians in 1873.
His year of birth is not known exactly. Al-Khwarizmi parents migrated to a
place south of Baghdad when he was a child. It has been established from
his contributions that he flourished under Khalifah Al-Mamun at Baghdad
during 813 to 833 C.E. Al-Khwarizmi died around 840 C.E.
Much of al-Khwarizmis work was written in a book titled al Kitab al-
mukhatasar fi hisab al-jabrwal-muqabalah (The Compendious Book on
Calculation by Completion and Balancing). It is from the titles of these
writings and his name that the words algebra and algorithm are derived. As
a result of his work, al-Khwarizmi is regarded as the most outstanding
mathematician of his time
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
17
Algorithm: Informal Definition
An algorithm is any well-defined computational
procedure that takes some values, or set of values, as
input and produces some value, or set of values, as
output. An algorithm is thus a sequence of
computational steps that transform the input into
output.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
18
Algorithms, Programming
A good understanding of algorithms is essential for a good
understanding of the most basic element of computer
science: programming. Unlike a program, an algorithm is a
mathematical entity, which is independent of a specific
programming language, machine, or compiler. Thus, in some
sense, algorithm design is all about the mathematical theory
behind the design of good programs.
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 19
Why Study Algorithms?
Necessary in any computer programming problem
Improve algorithm efficiency: run faster, process more data, do something that
would otherwise be impossible
Solve problems of significantly large sizes
Compare algorithms
Algorithms as a field of study
Learn about a standard set of algorithms
New discoveries arise
Numerous application areas
Learn techniques of algorithm design and analysis
how does one coordinate the efforts of many programmers working on a
single piece of software
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
20
Applications
Multimedia
CD player, DVD, MP3, JPG, DivX
Internet
Packet routing, data retrieval (Google)
Communication
Cell-phones, e-commerce
Computers
Circuit layout, file systems
Science
Human genome
Transportation
Airline crew scheduling, UPS deliveries
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
21
Roadmap
Different problems
Sorting
Searching
String processing
Graph problems
Geometric problems
Numerical problems
Different design paradigms
Divide-and-conquer
Incremental
Dynamic programming
Greedy algorithms
Randomized/probabilistic
NP Completeness
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
22
Analyzing Algorithms
Predict the amount of resources required:
memory: how much space is needed?
computational time: how fast the algorithm runs?
FACT: running time grows with the size of the input
Input size (number of elements in the input)
Size of an array, polynomial degree, # of elements in a matrix, # of bits in the
binary representation of the input, vertices and edges in a graph
Def: Running time = the number of primitive operations (steps) executed
before termination
Arithmetic operations (+, -, *), data movement, control, decision making (if,
while), comparison
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
23
Algorithm Efficiency vs. Speed
E.g.: sorting n numbers

Friends computer = 10
9
instructions/second
Friends algorithm = 2n
2
instructions

Your computer = 10
7
instructions/second
Your algorithm = 50nlgn instructions

Your friend =

You =

( )
seconds 2000
second / ns instructio 10
ns instructio 10 2
9
2
6
=
-
( )
seconds 100
second / ns instructio 10
ns instructio lg10 10 50
7
6 6
~
-
20 times better!!
Sort 10
6
numbers!
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
24
Algorithm Analysis: Example
Alg.: MIN (a[1], , a[n])
m a[1];
for i 2 to n
if a[i] < m
then m a[i];
Running time:
the number of primitive operations (steps) executed
before termination
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
Order (rate) of growth:
The leading term of the formula
Expresses the asymptotic behavior of the algorithm
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
25
Typical Running Time Functions
1 (constant running time):
Instructions are executed once or a few times
logN (logarithmic)
A big problem is solved by cutting the original problem in smaller sizes,
by a constant fraction at each step
N (linear)
A small amount of processing is done on each input element
N logN
A problem is solved by dividing it into smaller problems, solving them
independently and combining the solution
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
26
Typical Running Time Functions
N
2
(quadratic)
Typical for algorithms that process all pairs of data items (double
nested loops)
N
3
(cubic)
Processing of triples of data (triple nested loops)
N
K
(polynomial)
2
N
(exponential)
Few exponential algorithms are appropriate for practical use
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
27
Why Faster Algorithms?
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
28
Asymptotic Notations
A way to describe behavior of functions in the limit
Abstracts away low-order terms and constant factors
How we indicate running times of algorithms
Describe the running time of an algorithm as n grows to
O notation:
O notation:
O notation:
asymptotic less than and equal: f(n) g(n)
asymptotic greater than and equal:f(n) g(n)
asymptotic equality: f(n) = g(n)
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
29
Asymptotic Notations - Examples
O notation
n
2
/2 n/2
(6n
3
+ 1)lgn/(n + 1)
n vs. n
2

O notation
n
3
vs. n
2

n vs. logn
n vs. n
2

= O(n
2
)
n O(n
2
)
= O(n
2
lgn)
O notation
2n
2
vs. n
3

n
2
vs. n
2

n
3
vs. nlogn
n
3
= O(n
2
)
n = O(logn)
n = O(n
2
)
2n
2
= O(n
3
)
n
2
= O(n
2
)
n
3
= O(nlgn)
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
30
Mathematical Induction
Used to prove a sequence of statements (S(1), S(2), S(n))
indexed by positive integers
Proof:
Basis step: prove that the statement is true for n = 1
Inductive step: assume that S(n) is true and prove that S(n+1) is true for
all n 1
The key to proving mathematical induction is to find case n
within case n+1
( )

=
+
=
n
i
n n
i
1
2
1
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 31
Recursive Algorithms
Binary search: for an ordered array A, finds if x is in the array A*lohi+

Alg.: BINARY-SEARCH (A, lo, hi, x)

if (lo > hi)
return FALSE
mid (lo+hi)/2
if x = A[mid]
return TRUE
if ( x < A[mid] )
BINARY-SEARCH (A, lo, mid-1, x)
if ( x > A[mid] )
BINARY-SEARCH (A, mid+1, hi, x)
12

11

10

9

7

5

3

2

1 2 3 4 5 6 7 8
mid
lo hi
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
32
Recurrences
Def.: Recurrence = an equation or inequality that
describes a function in terms of its value on smaller
inputs, and one or more base cases
E.g.: T(n) = T(n-1) + n
Useful for analyzing recurrent algorithms
Methods for solving recurrences
Substitution method
Recursion tree method
Master method
Iteration method
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
33
Sorting
Iterative methods:
Insertion sort
Bubble sort
Selection sort
2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A
Divide and conquer
Merge sort
Quicksort
Non-comparison methods
Counting sort
Radix sort
Bucket sort
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
34
Types of Analysis
Worst case
Provides an upper bound on running time
An absolute guarantee that the algorithm would not run
longer, no matter what the inputs are
Best case
Input is the one for which the algorithm runs the fastest
Average case
Provides a prediction about the running time
Assumes that the input is random
(e.g. cards reversely ordered)
(e.g., cards already ordered)
(general case)
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
35
Specialized Data Structures
Problem:
Schedule jobs in a computer
system
Process the job with the highest
priority first
Solution: HEAPS
all levels are full, except possibly
the last one, which is filled from
left to right
for any node x
Parent(x) x
Operations:
Build
Insert
Extract max
Increase key
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
36
Specialized Data Structures
Problem:
Keeping track of customer account
information at a bank or flight
reservations
This applications requires fast search,
insert/delete, sort
Solution: binary search trees
If y is in left subtree of x,
then key [y] key [x]
If y is in right subtree of x,
then key [y] key [x]
Red-black trees, interval trees,
OS-trees
Left child Right child
L R
parent
key data
3
2 4
6
7
13
15
18
17 20
9
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
37
Hash Tables
Many applications require a dynamic set that supports only
INSERT, SEARCH, and DELETE
Example: a symbol table in a compiler
Scenario:
Each element has a key drawn
from a large universe of keys (U)
The set of keys is small compared to U
A hash table is a generalization of an ordinary array
U
(universe of keys)
K
(actual
keys)
k
1

k
4
k
2

k
5

k
3

Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
38
Hash Tables
Idea:
Use a function h to compute the slot for each key
Store the element in slot h(k)
A hash function h transforms a key into an index in a hash
table T[0m-1]:
h : U {0, 1, . . . , m - 1}
Time space tradeoff
Search and Insert take O(1), under some reasonable assumptions
However, worst-case search time is O(n)
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
39
Dynamic Programming
An algorithm design technique (like divide and conquer)
Richard Bellman, optimizing decision processes
Applicable to problems with overlapping subproblems
E.g.: Fibonacci numbers:
Recurrence: F(n) = F(n-1) + F(n-2)
Boundary conditions: F(1) = 0, F(2) = 1
Compute: F(5) = 3, F(3) = 1, F(4) = 2

Solution: store the solutions to subproblems in a table
Applications:
Assembly line scheduling, matrix chain multiplication, longest
common sequence of two strings, 0-1 Knapsack problem
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 40
Greedy Algorithms
Start End Activity
1 8:00am 9:15am Numerical methods class
2 8:30am 10:30am Movie presentation (refreshments served)
3 9:20am 11:00am Data structures class
4 10:00am noon Programming club mtg. (Pizza provided)
5 11:30am 1:00pm Computer graphics class
6 1:05pm 2:15pm Analysis of algorithms class
7 2:30pm 3:00pm Computer security class
8 noon 4:00pm Computer games contest (refreshments served)
9 4:00pm 5:30pm Operating systems class
Problem
Schedule the largest possible set of non-overlapping activities
for SEM 234
\
\
\
\
\
\
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
41
Greedy Algorithms
Similar to dynamic programming, but simpler approach
Also used for optimization problems
Idea: When we have a choice to make, make the one that
looks best right now
Make a locally optimal choice in hope of getting a globally optimal
solution
Greedy algorithms dont always yield an optimal solution
Applications:
Activity selection, fractional knapsack, Huffman codes

Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 42
Graphs
Applications that involve not only a set of items, but also the
connections between them
Computer networks
Circuits
Schedules
Hypertext
Maps
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
43
Searching in Graphs
Graph searching = systematically follow the edges of
the graph so as to visit the vertices of the graph
Two basic graph methods:
Breadth-first search
Depth-first search
The difference between them is in the order in which they
explore the unvisited edges of the graph
Graph algorithms are typically elaborations of the
basic graph-searching algorithms

u v w
x y z
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 44
Strongly Connected Components
Read in a 2D image and find regions of pixels that have the
same color
Original Labeled
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 45
Minimum Spanning Trees
A connected, undirected graph:
Vertices = houses, Edges = roads
A weight w(u, v) on each edge (u, v) e E
a
b c d
e
h g f
i
4
8
7
8
11
1 2
7
2
4
14
9
10
6
Find T _ E such that:
1. T connects all vertices
2. w(T) =
(u,v)eT
w(u, v) is
minimized
Algorithms: Kruskal and Prim
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms 46
Shortest Path Problems
Input:
Directed graph G = (V, E)
Weight function w : E R
Weight of path p = (v
0
, v
1
, . . . , v
k
)

Shortest-path weight from u to v:
(u, v) = min w(p) : u v if there exists a path from u to v
otherwise

=

=
k
i
i i
v v w p w
1
1
) , ( ) (
p
3
6
5
7
6
2
2 1
4
3
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
47
Variants of Shortest Paths
Single-source shortest path (Bellman-Ford, DAG shortest paths,
Disjkstra)
G = (V, E) find a shortest path from a given source vertex s to each vertex v
e V
Single-destination shortest path
Find a shortest path to a given destination vertex t from each vertex v
Reverse the direction of each edge single-source
Single-pair shortest path
Find a shortest path from u to v for given vertices u and v
Solve the single-source problem
All-pairs shortest-paths (Matrix multiplication, Floyd-Warshall)
Find a shortest path from u to v for every pair of vertices u and v
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
48
Number Theoretic Algorithms
Secured communication: RSA public-key
cryptosystem
Easy to find large primes
Hard to factor the product of large primes
Secure Message
Authenticated Message
Bob Alice
eavesdropper
Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms Dr. Muhammad Wasif Nisar Advanced Analysis of Algorithms
49
NP-Completeness
Not all problems can be solved in polynomial time
Some problems cannot be solved by any computer no
matter how much time is provided (Turings Halting
problem) such problems are called undecidable
Some problems can be solved but not in O(n
k
)
Can we tell if a problem can be solved ?
NP, NP-complete, NP-hard

Anda mungkin juga menyukai