Introduction to Algorithms
lecture 1
What is the Algorithms
A sequence of procedure input -> output
example – finding max value60 80 70 90 40 20 60 100
Expression
Natural language
flow chart
pseudo code
Programming language
Data structure
data structure is important in Algorithms
array Pros
Cons
linked list Pros
Cons … 10 50 30 40 …
10 50 30 40 20 15
Data structure
queueFIFO (First In First Out)
stackLIFO (Last In First Out)
In 40 30 20 10 Out
In Out 40
30 20 10
Insert 10, 20, 30, 40
Data structure
graph G=( V, E )
V: vertex, E: edge
undirected graph
directed graph
loop
degree, in-degree, out-degree
path
u v
u v
(u,v) = (v,u)
<u,v>
u v
Data structure
graph tree
rooted tree binary tree
root
leaf
ancestor descendant parent
child sibling depth height
Evaluation of an Algorithm
Accuracy
Efficiency
big O O(1) : constant
O(logn) : logarithmic
O(n) : linear
O(nlogn)
O(n2) :quadratic
O(n3) :cubic
O(nk) : polynomial
O(2n) :, exponential
O(n!)
Exchanging coins
Greedy Algorithm
Choose next step based on current state
greedy
Input: M= 37, c=(25, 20, 10, 5, 1), d=5Output: k =
Exchanging coins
Greedy Algorithm
O(Md)
O(d)
GreedyChange ( M, c, d ) GreedyChange2 ( M, c, d )
Evaluation
(correctness)
Coin change problemM=40, c = (25,20,10,5,1) greedy: k=
Correct answer: k=
Coin change problem
Exhaustive search/Brute force Algorithm) BruteForceChange ( M, c, d )
O(Md)
Examines every possible alternative to find one particular solution