Introduction To Algorithms
Introduction To Algorithms
Structures
Jaideep Srivastava
Schedule of topics
Lecture 1: Algorithm analysis
Concept – what is it?
Examples – lots of it
Formalism
Lecture 2: Recursion
Concept
Examples
Lecture 3: Trees
Tree algorithms
2
Introduction
3
Lecture 1 – Algorithm Analysis & Complexity
Objectives
The main focus of is to introduce you to a systematic study
of algorithms and data structures.
The two guiding principles of the course are: abstraction and
formal analysis.
Abstraction: We focus on topics that are broadly applicable
to a variety of problems.
Analysis: We want a formal way to compare two objects (data
structures or algorithms).
In particular, we will worry about "always correct"-ness, and
worst-case bounds on time and memory (space).
5
What is Algorithm Analysis For
Foundations of Algorithm Analysis and Data Structures.
Analysis:
How to predict an algorithm’s performance
Data Structures
How to efficiently store, access, manage data
6
Example Algorithms
Two algorithms for computing the Factorial
Which one is better?
7
Examples of famous algorithms
Constructions of Euclid
Newton's root finding
Fast Fourier Transform (signal processing)
Compression (Huffman, Lempel-Ziv, GIF, MPEG)
DES, RSA encryption (network security)
Simplex algorithm for linear programming (optimization)
Shortest Path Algorithms (Dijkstra, Bellman-Ford)
Error correcting codes (CDs, DVDs)
TCP congestion control, IP routing (computer networks)
Pattern matching (Genomics)
Search Engines (www)
8
Role of Algorithms in Modern World
geology)
Sensor networks. RFID tags
9
A real-world Problem
10
IP Prefixes and Routing
Each router is really a switch: it receives packets at several
input ports, and appropriately sends them out to output
ports.
Thus, for each packet, the router needs to transfer the
packet to that output port that gets it closer to its
destination.
Should each router keep a table: IP address x Output Port?
How big is this table?
When a link or router fails, how much information would need
to be modified?
A router typically forwards several million packets/sec!
11
Data Structures
The IP packet forwarding is a Data Structure problem!
Efficiency, scalability is very important.
12
Algorithms to Process these Data
13
Max Subsequence Problem
Given a sequence of integers A1, A2, …, An, find the maximum possible
value of a subsequence Ai, …, Aj.
Numbers can be negative.
You want a contiguous chunk with largest sum.
14
Algorithm 1 for Max Subsequence Sum
Given A1,…,An , find the maximum value of Ai+Ai+1+···+Aj
0 if the max value is negative
thisSum += a[ k ]; O (1)
if( thisSum > maxSum ) O (1)
maxSum = thisSum;
}
return maxSum;
15
Algorithm 2
Idea: Given sum from i to j-1, we can compute the
sum from i to j in constant time.
This eliminates one nested loop, and reduces the
into maxSum = 0;
16
Algorithm 3
This algorithm uses divide-and-conquer paradigm.
Suppose we split the input sequence at midpoint.
17
Algorithm 3 (cont.)
Example:
left half | right half
4 -3 5 -2 | -1 2 6 -2
Max subsequences in each half found by recursion.
How do we find the straddling max subsequence?
Key Observation:
Left half of the straddling sequence is the max
18
Algorithm 3: Analysis
T(1) = 1
T(n) = 2T(n/2) + O(n)
19
Algorithm 4
2, 3, -2, 1, -5, 4, 1, -3, 4, -1, 2
int maxSum = 0, thisSum = 0;
20
Proof of Correctness
becomes < 0
The max subsequence cannot start at any p between i and
21
Algorithm 4
int maxSum = 0, thisSum = 0;
22
Why Efficient Algorithms Matter
Suppose N = 106
A PC can read/process N records in 1 sec.
But if some algorithm does N*N computation, then it takes
1M seconds = 11 days!!!
23
How to Measure Algorithm Performance
Memory required
Running time
24
Abstraction
An algorithm may run differently depending on:
the hardware platform (PC, Cray, Sun)
25
Average, Best, and Worst-Case
Average case:
Real world distributions difficult to predict
Best case:
Seems unrealistic
Worst case:
Gives an absolute guarantee
26
Examples
Vector addition Z = A+B
for (int i=0; i<n; i++)
Z[i] = A[i] + B[i];
T(n) = c n
27
Examples
Vector (outer) multiplication Z = A*BT
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
Z[i,j] = A[i] * B[j];
T(n) = c2 n2;
28
Simplifying the Bound
10 or 20 terms
Do we really need that many terms?
29
Simplifications
Keep just one term!
the fastest growing term (dominates the runtime)
etc.
30
Simplification
Drop the constant coefficient
Does not effect the relative order
31
Simplification
The faster growing term (such as 2n) eventually will
outgrow the slower growing terms (e.g., 1000 n) no
matter what their coefficients!
32
Complexity and Tractability
T(n)
n n n log n n2 n3 n4 n10 2n
10 .01s .03s .1s 1s 10s 10s 1s
20 .02s .09s .4s 8s 160s 2.84h 1ms
30 .03s .15s .9s s 810s 6.83d 1s
40 .04s .21s 1.6s s 2.56ms 121d 18m
50 .05s .28s s s 6.25ms 3.1y 13d
100 .1s .66s 10s 1ms 100ms 3171y 41013y
103 1s 9.96s 1ms 1s 16.67m 3.171013y 3210283y
104 s 130s 100ms 16.67m 115.7d 3.171023y
105 s 1.66ms 10s 11.57d 3171y 3.171033y
106 ms 19.92ms 16.67m 31.71y 3.17107y 3.171043y
33
log n n n log n n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
4 16 64 256 4096 65,536
5 32 160 1,024 32,768 4,294,967,296
70000 2n n2 100000 2n n2
60000 n3
50000
10000 n log n
40000
n3 1000 n
30000
100
20000
n log n 10 log n
10000
0
n
1
n log n n
34
Another View
More resources (time and/or processing power) translate into
large problems solved if complexity is low
1000n 1 10 10
5n2 14 45 3.2
N3 10 22 2.2
2n 10 13 1.3
35
Asymptotics
3n2+4n+1 3 n2 n2
15 n2+6n 15 n2 n2
a n2+bn+c a n2 n2
36
Caveats
memory requirements
37
Asymptotic Notations
38
By Pictures
Big-Oh (most commonly used)
bounded above
Big-Omega
bounded below N0
Big-Theta
exactly
Small-o
N0
39
Example
T ( n ) n 2n
3 2
O (?) (?)
0
10
n n
5 2
n n
3 3
n n
40
Examples
f ( n) Asymptomic
c (1)
i 1 ci n
k i
( n )
k
in1 i ( n 2 )
in1 i 2 ( n 3 )
i 1i
n k
( n )
k 1
in0 r i ( r n )
n! (n(n /e) n )
i 11 / i
n
(log n)
41
Summary (Why O(n)?)
T(n) = ck nk + ck-1 nk-1 + ck-2 nk-2 + … + c1 n + co
Too complicated
O(nk )
matter asymptotically
Other criteria hard to quantify
42
Runtime Analysis
Useful rules
simple statements (read, write, assign)
O(1) (constant)
simple operations (+ - * / == > >= < <=
O(1)
sequence of simple statements/operations
rule of sums
for, do, while loops
rules of products
43
Runtime Analysis (cont.)
Two important rules
Rule of sums
44
Runtime Analysis (cont.)
if (cond) then O(1)
body1 T1(n)
else
body2 T2(n)
endif
45
Runtime Analysis (cont.)
Method calls
A calls B
B calls C
etc.
flattened
T(n) = max(TA(n), TB(n), TC(n))
46
Example
47
Example
48
Run Time for Recursive Programs
T(n) is defined recursively in terms of T(k), k<n
The recurrence relations allow T(n) to be “unwound”
Factorial
Hanoi towers
49
Example: Factorial
T ( n)
int factorial (int n) {
T (n 1) d
if (n<=1) return 1;
T (n 2) d d
else return n * factorial(n-1); T (n 3) d d d
} ....
T (1) (n 1) * d
factorial (n) = n*n-1*n-2* … *1 c (n 1) * d
O ( n)
n * factorial(n-1) T(n)
n-1 * factorial(n-2) T(n-1)
n-2 * factorial(n-3) T(n-2)
…
2 *factorial(1)
T(1)
50
Example: Factorial (cont.)
int factorial1(int n) {
if (n<=1) return 1;
else {
fact = 1;
O (1)
for (k=2;k<=n;k++)
fact *= k; O(n)
O (1)
return fact;
}
}
Both algorithms are O(n).
51
Example: Hanoi Towers
Hanoi(n,A,B,C) =
Hanoi(n-1,A,C,B)+Hanoi(1,A,B,C)+Hanoi(n-1,C,B,A)
T ( n)
2T (n 1) c
2 2 T (n 2) 2c c
23 T (n 3) 2 2 c 2c c
....
2 n 1T (1) (2 n 2 ... 2 1)c
(2 n 1 2 n 2 ... 2 1)c
O(2 n )
52
Worst Case, Best Case, and Average Case
template<class T>
void SelectionSort(T a[], int n)
{ // Early-terminating version of selection sort
bool sorted = false;
for (int size=n; !sorted && (size>1); size--) {
int pos = 0;
sorted = true;
// find largest
for (int i = 1; i < size; i++)
if (a[pos] <= a[i]) pos = i;
else sorted = false; // out of order
Swap(a[pos], a[size - 1]);
}
}
Worst Case
Best Case
53
c f(N)
n0 T(N)
f(N)
T(N)=O(f(N))
7N+4 = O(N)
15N+20 = O(N)
N10 = O(2N)?
N2=O(N)? 6N + 4 = W(N) ? 7N?
N log N = O(N)? N+4 ? N2? N log N?
N log N = O(N2)?
N log N = W(N2)?
3 = O(1)
1000000=O(1)
Sum i = O(N)?
54
An Analogy: Cooking Recipes
Algorithms are detailed and precise instructions.
Example: bake a chocolate mousse cake.
Convert raw ingredients into processed output.
New advances.
New models: clusters, Internet, workstations
55
Lecture 2 - Recursion
What is Recursion?
57
How does it work?
Functions are implemented using an internal stack of
activation records.
Each time a function is called a new activation record
is pushed on the stack.
When a function returns, the stack is popped and the
activation record of the calling method is on top of the
stack.
58
How does it work? (cont.)
59
Example
60
Recursive Function Call
A recursive call is a function call in which the
called function is the same as the one making
the call.
65
Indirect recursion
Receive( buffer )
while buffer is not filled up
If information is still incoming
get a char and store it in buffer;
else exit();
decode(buffer);
Decode(buffer)
decode information in buffer;
store(buffer);
Store(buffer)
transfer information from buffer to file;
receive(buffer);
66
Nested recursion
h(n) { 0 if n=0
h(n) ={ n if n>4
h(n) { h(2+h(2n)) if n <=4
67
Writing a recursive function to find
n factorial
DISCUSSION
if ( number == 0 )
return 1;
Writing a recursive function to find
Factorial(n)
or, n * Factorial(n - 1)
Recursively:
if n = 0, then n! = 1
if n > 0, then n! = n * (n-1)!
70
Factorial Function
int RecFactorial( /* in */ int n)
// Calculates n factorial, n!
// Precondition: n is a non-negative
// integer
{
if (n <= 0) then
return 1
else
return n * RecFactorial(n-1)
}
71
1int fact(int n)
2{
3 if (n <= 0) then
4 return 1;
5 else
6 return n*fact(n-1);
7 }
main(...) {
...
20 System.out.print( (fact(3));
...
returns 6 to main()
72
Exponentiation
baseexponent
e.g. 53
Could be written as a function
Power(base, exp)
73
Can we write it recursively?
be = b * b(e-1)
74
Another Recursive Function
main(...) {
...
20 cout << (Power(2,3));
75 ...
The Puzzle
A man has an infant male-female pair of rabbits in a hutch
entirely surrounded by a wall. We wish to know how many
rabbits can be bred from this pair in one year, if the nature of
the rabbits is such that every month they breed one other
male-female pair which begins to breed in the second month
after their birth. Assume that no rabbits die during the year.
76
A Tree Diagram for Fibonacci’s Puzzle
77
Observations
78
Fibonacci sequence
Recursive definition
f(1) = 1
f(2) = 1
f(n) = f(n-1) + f(n-2)
79
A More Complex Recursive Function
Fibonacci Number Sequence
if n = 1, then Fib(n) = 1
if n = 2, then Fib(n) = 1
if n > 2, then Fib(n) = Fib(n-2) + Fib(n-1)
80
Fibonacci Sequence Function
81
Tracing with Multiple Recursive Calls
82
Tracing with Multiple Recursive Calls
83
Tracing with Multiple Recursive Calls
84
Tracing with Multiple Recursive Calls
85
Tracing with Multiple Recursive Calls
86
Tracing with Multiple Recursive Calls
87
Tracing with Multiple Recursive Calls
88
Tracing with Multiple Recursive Calls
89
Tracing with Multiple Recursive Calls
90
Tracing with Multiple Recursive Calls
91
Tracing with Multiple Recursive Calls
92
Tracing with Multiple Recursive Calls
93
Tracing with Multiple Recursive Calls
94
Tracing with Multiple Recursive Calls
95
Tracing with Multiple Recursive Calls
96
Tracing with Multiple Recursive Calls
97
Tracing with Multiple Recursive Calls
98
Tracing with Multiple Recursive Calls
99
Tracing with Multiple Recursive Calls
100
Fib(5)
Fib(4) Fib(3)
Fib(1) Fib(0)
101
Excessive Recursion-Fibonacci recursion
Int iterativeFib ( int n )
if ( n < 2)
return n;
else {
int i=2, tmp,current = 1, last=0;
for ( ; i<=n; ++i) {
tmp=current;
current+=last;
last=tmp;
}
return current;
}
}
102
103
104
Rules of Recursion
105
Rules of Recursion
Third fundamental rule of recursion:
106
Rules of Recursion
Fourth fundamental rule of recursion:
107
Towers of Hanoi
The puzzle consisted of N disks and three poles:
A (the source), B (the destination), and C (the
spare)
108
Towers of Hanoi
A B C
109
Towers of Hanoi
A B C
110
Towers of Hanoi
A B C
111
Towers of Hanoi
A B C
112
Towers of Hanoi
A B C
113
Towers of Hanoi
A B C
114
Towers of Hanoi
A B C
115
Towers of Hanoi
A B C
116
Towers of Hanoi
void solveTowers(
int count, char source, char dest, char spare){
if (count == 1)
cout<<“Move disk from pole “ << source
<< " to pole " << destination <<endl;
else
{
towers(count-1, source, spare, destination);
towers(1, source, destination, spare);
towers(count-1, spare, destination, source);
}//end if
}//end solveTowers
118
Recall that . . .
Recursion occurs when a function calls itself
(directly or indirectly).
120
Pascal Triangle
The combinations of n items taken r at a time. For example:
three items: a b c
taken 2 at a time: ab ac bc
Thus there are three combinations of 3 items taken 2 at a time.
In General: C(n,r) = n!/(r!(n-r)!) Obviously you can calculate
C(n,r) using factorials.
121
Pascal Triangle
This leads to Pascal Triangle:
n 0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
123
Pascal Triangle
124
What is the value of rose(25)?
int rose (int n)
{
if ( n == 1 ) // base case
return 0;
else // general case
return ( 1 + rose ( n / 2 ) );
}
Finding the value of rose(25)
The parameter(s) in the recursive call cannot all be the same as the
formal parameters in the heading. Otherwise, infinite recursion
would occur.
struct ListType
{
int length ; // number of elements in the list
};
ListType list ;
Recursive function to determine if value is in list
PROTOTYPE
bool ValueInList( ListType list , int value , int startIndex )
74 36 ... 95 75 29 47 ...
list[0] [1] [startIndex] [length -1]
Already searched Needs to be searched
index
of
current
element
to
examine
bool ValueInList ( ListType list , int value , int startIndex )
132
“Why use recursion?”
int
int factorial[8]
factorial[8] == {1,
{1, 1,
1, 2,
2, 6,
6, 24,
24, 120,
120, 720,
720, 5040};
5040};
134
Use a recursive solution when:
Limitations of
Arrays
Linked lists
Stacks
Queues
137
Trees: Recursive Definition
138
Trees: Recursive Definition (cont.)
ROOT OF
TREE T
T1 T2 T3 T4 T5
SUBTREES
139
Trees: An Example
B C D E
G H
F
140
Trees: More Definitions
141
Trees: More Definitions (cont.)
142
Binary Trees – A Informal Definition
children:
struct TreeNode
{
Object element;
TreeNode *left_child;
TreeNode *right_child;
};
143
Binary Trees – Formal Definition
a root
a binary tree called its left subtree
144
Binary Trees: Recursive Definition
ROOT OF
TREE T
*left_child *right_child
T1 T2
SUBTREES
145
Differences Between A Tree & A Binary Tree
No node in a binary tree may have more than 2
children, whereas there is no limit on the number of
children of a node in a tree.
The subtrees of a binary tree are ordered; those of a
tree are not ordered.
146
Differences Between A Tree & A Binary Tree
(cont.)
The subtrees of a binary tree are ordered; those of a
tree are not ordered
a a
b b
147
Internal and External Nodes
internal node
external node
148
Most of concepts related to binary trees can be explained
Recursive definition of a Binary Tree
recursive
For instance, A binary tree is:
An external node, or
149
What is a binary tree? (cont.)
Property2: a unique path exists from the root to every
other node
150
Mathematical Properties of Binary Trees
151
Minimum Number Of Nodes
Minimum number of nodes in a binary tree
whose height is h.
At least one node at each level.
minimum number
of nodes is h + 1
152
Maximum Number Of Nodes
All possible nodes at first h levels are present
154
Relationship Between Number of Nodes
(Internal
A binary - NExternal)
tree with internal nodes has N+1 external nodes
155
Number of edges
156
Number of edges
157
Binary Tree Representation
Array representation
Linked representation
158
Binary Trees
159
Node Number Properties
1 Complete binary tree
2 3
4 5 6 7
8 9 10 11 12 13 14 15
160
Full Binary Tree With n Nodes
Start with a complete binary tree that has at least n
nodes.
Number the nodes as described earlier.
The binary tree defined by the nodes numbered 1
through n is the unique n node full binary tree.
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Full binary tree with 11 nodes
161
Array Representation
•Number the nodes using the numbering scheme
for a full binary tree
•Store the node numbered i in tree[i]
a1
2 3
b c
4 5 6 7
d e f g
8 9 10
h i j
tree[] a b c d e f g h i j
0 1 2 3 4 5 6 7 8 9 10
162
Right-Skewed Binary Tree
An n node binary tree needs an array whose length is between n+1
and 2n
If h = n-1 then skewed binary tree
1
a
b 3
7
c
15
d
a - b - - - c - - - - - - - d
tree[] 0 5 10 15
163
Array Representation (cont.)
Each tree node is represented as a struct
Struct TreeNode {
object element;
int child1;
int child2;
…
int childn;
};
164
Linked Representation
165
Trees: Linked representation Implementation 1
struct TreeNode
{ Each node contains a link to all
Object element; of its children.
TreeNode *child1;
TreeNode *child2; This isn’t a good idea, because
. a node can have an arbitrary
.
.
number of children!
TreeNode *childn;
};
166
Trees: Linked representation
Implementation 2
struct TreeNode
{
Object element;
TreeNode *child1;
TreeNode *sibling;
};
167
Implementation 2: Example
A /
B C / D E / /
F / / G / H /
I / /
The downward links are to the first
child; the horizontal links are to the
next sibling.
168
Binary Trees
A binary tree is a tree whose nodes have at most two
offspring
Example
struct nodeType {
object element;
struct nodeType *left, *right;
};
struct nodeType *tree;
169
Linked Representation Example
root a
b c
d e
g
f
leftChild
element h
rightChild
170
Some Binary Tree Operations
171
CS122 Algorithms and Data Structures
173
A Property of Binary Search Trees
The key of the root is larger than any key in the left
subtree
The key of the root is smaller than any key in the right
subtree
Note: Duplicated keys are not allowed
174
A Property of Binary Search Tree
ROOT OF
TREE T
*left_child *right_child
X
T1 T2
175
Binary Search Trees in C++
We will use two classes:
The class BinaryNode simply constructs individual
176
Search Operation
177
FindMin Operation
BinaryNode* findMin (BinaryNode *t) {
if ( t == NULL )
return NULL;
if ( t -> left == NULL )
return t;
return findMin (t -> left);
}
178
FindMax Operation
179
Insert Operation
180
Insert Operation (cont.)
181
Removal Operation
182
Removal Operation (cont.)
If the node to be removed has two children, the
general strategy is to replace the data of this node
with the smallest key of the right subtree.
Then the node with the smallest data is now removed
(this case is easy since this node cannot have two
children).
183
Removal Operation (cont.)
void remove (const int &x, BinaryNode* &t) const {
if ( t == NULL ) return; // key is not found; do nothing
if ( t->key == x) {
if( t->left != NULL && t->right != NULL ) { // Two children
t->key = findMin( t->right )->key;
remove( t->key, t->right );
}
else { // One child
BinaryNode *oldNode = t;
t = ( t->left != NULL ) ? t->left : t->right;
delete oldNode;
}
}
else { // Two recursive calls
if ( x < t->key ) remove( x, t->left );
else if( t->key < x ) remove( x, t->right );
}
}
184
Deleting by merging
185
Deleting by merging
186
Deleting by copying
187
Balancing a binary tree
A binary tree is height-balanced or simply balanced if
the difference in height of both the subtrees is either
zero or one
Perfectly balanced if all leaves are to be found on one
level or two levels.
188
Balancing a binary tree
189
Analysis
The running time of these operations is O(lv), where lv
is the level of the node containing the accessed item.
What is the average level of the nodes in a binary
search tree? It depends on how well balanced the tree
is.
190
Average Level of Nodes
Data Order: 10, 5, 1, 8, 20, 13, 34
10
5 20 N=7
1 8 13 34
192
Effect of Data Order
Obtained if data is 1, 2, 3, 4
Obtained if data is 4, 3, 2 1
193
Depth of Nodes
In the best case the depth will be about O(log N).
In the worst case, if the data are already ordered, the
depth will be about O(N).
194
Effects of Data Order…
So, if the input data are randomly ordered, what is the
average depth of the nodes?
The analysis is beyond the scope of this course, but it
can be shown that the average depth is O(log N),
which is a very nice result.
195
Summary
196
Uses for Binary Trees…
-- trees
Binary Binaryare a good way
Expression to express arithmetic
Trees
expressions.
The leaves are operands and the other nodes are
operators.
The left and right subtrees of an operator node
197
Binary Expression Trees: Examples
+ -
a+b
a b a
-a
//
* +
e f
+ -
(a + b) * (c – d) / (e + f)
a b c d
198
Merits of Binary Tree Form
Left and right operands are easy to visualize
Code optimization algorithms work with the binary tree
form of an expression
Simple recursive evaluation of expression
* +
e f
+ -
a b c d
199
Levels Indicate Precedence
‘*’
‘+’ ‘3’
‘4’ ‘2’
( 4 + 2 ) * 3 = 18
Inorder Traversal: (A + H) / (M - Y)
Print second
tree
‘/’
‘+’ ‘-’
+ *
+ g
a *
b c * f
d e
Print first
tree
‘/’
‘+’ ‘-’
+ *
+ g
a *
b c * f
d e
‘/’
‘+’ ‘-’
+ *
+ g
a *
b c * f
d e
208
Constructing an Expression Tree
There is a simple O(N) stack-based algorithm to
convert a postfix expression into an expression tree.
Recall we also have an algorithm to convert an infix
expression into postfix, so we can also convert an infix
expression into an expression tree without difficulty (in
O(N) time).
209
Expression Tree Algorithm
Read the postfix expression one symbol at at time:
If the symbol is an operand, create a one-node tree and
T2 from the stack, and form a new tree whose root is the
operator, and whose children are T1 and T2.
Push the new tree pointer on the stack.
210
Example
ab+ :
a b
+
Note: These stacks are depicted horizontally.
a b
211
Example
ab+cde+ :
+ c d e
a b
+ c +
a b d e
212
Example
ab+cde+* :
+
*
a b c +
d e
213
Example
ab+cde+**:
+
*
a b c +
d e
214