Professional Documents
Culture Documents
Lec3 (DONE)
Lec3 (DONE)
Lec3 (DONE)
1
Prof. Ossama Ismail
Introduction to Algorithms - II
2
Prof. Ossama Ismail
Introduction to Algorithms
Lecture 1-2
Problem Solving
&
Mathematical Tools
3
Prof. Ossama Ismail
PROBLEM SOLVING
DEFINITION
4
Prof. Ossama Ismail
OBJECTIVE: Learn how to solve problems
• PURPOSE:
5
Prof. Ossama Ismail
How do we solve problems?
• We "just do"
• Guesswork-and-luck
• Trial-and-error
• Experience (possibly someone else's)
• "Scientifically"
6
Prof. Ossama Ismail
The Problem-solving Process
Analysis
Problem
specification
Design
Algorithm
Implementation
Program
Compilation
Executable
(solution)
7
Prof. Ossama Ismail
Creative Problem Solving
8
Prof. Ossama Ismail
Problem Solving
Steps
– Goal Formulation
• limiting the objectives
– Problem Formulation
• deciding what actions and states to consider
– Search
• looking for the possible action sequence
– Execution
9
Prof. Ossama Ismail
TIPS TO PRACTICAL PROBLEM
SOLVING
11
Prof. Ossama Ismail
Example 1 : The 8-queens problem
Problem formulation
– State : any arrangement of 0 to 8 queens on board
– Operators: add a queen to any square
– Goal test: 8 queens on board, none attacked
12
Prof. Ossama Ismail
Example 2 : Number of rectangles
13
Prof. Ossama Ismail
Example 2 : Number of rectangles – cont’d
• Ask yourself ??
16
Prof. Ossama Ismail
Example 3 : Move 3 sticks
17
Prof. Ossama Ismail
Example 4 :Water Jug Problem
Given a full 5-gallon jug
and an empty 2-gallon
Operation table
jug, the goal is to fill Name Con Transition Effect
the 2-gallon jug with d.
exactly one gallon of Empty5 – (x,y)→(0,y) Empty 5-
water. gal. jug
19
Prof. Ossama Ismail
Example 5 : Nine Dots Problem –cont’d
20
Prof. Ossama Ismail
Example 5 : Nine Dots Problem –cont’d
Examples :
❖ Internet Searching (e.g. Google –Yahoo)
❖ Route finding
❖ Touring and travelling salesperson problems
❖ VLSI layout
❖ Robot navigation
❖ Assembly sequencing
22
Prof. Ossama Ismail
Problem solving tools and techniques
23
Prof. Ossama Ismail
Creative problem solving process
The real problem may not surface until facts have been
gathered and analyzed. Therefore, start with what you
assume to be the problem, that can later be confirmed
or corrected.
24
Prof. Ossama Ismail
Creative problem solving process – cont’d
25
Prof. Ossama Ismail
Creative problem solving process – cont’d
26
Prof. Ossama Ismail
Creative problem solving process – cont’d
27
Prof. Ossama Ismail
Creative problem solving process – cont’d
28
Prof. Ossama Ismail
Creative problem solving process – cont’d
29
Prof. Ossama Ismail
Creative problem solving process – cont’d
30
Prof. Ossama Ismail
Mathematical Tools
31
Prof. Ossama Ismail
Mathematical Tools
• Floor and ceiling
• Polynomial of degree n
• Factorials
– n! = 1 * 2 * 3 * … * n
– 0! = 1
• Exponentials
a0 = 1
a1 = a
a-1 = 1/a
(am)n = amn
(am)n = (an)m
aman = am+n
ex = 1 + x + x2/2! + x3/3! + …
32
Prof. Ossama Ismail
Mathematical Tools cont’d
• Summations
• Logarithms
lg n = log2 n
ln n = loge n
lg kn = (lg n)k
lg lgn = lg(lg n)
a = b(logba)
logb(1/a) = -logba
logba = 1/logab
alogbn = nlogba
33
Prof. Ossama Ismail
Mathematical Tools cont’d
• Terminology
Function Name
c constant
a1n + a0 linear
a2n2 + a1n + a0 quadratic
a3n3 + a2n2 + a1n + a0 cubic
cn exponential
logbn logarithmic
nlogbn log linear
36
Prof. Ossama Ismail
Algorithm Analysis cont’d
Algorithm Sum(A, n):
input: The list A of integers
output: The sum of the integers in A
count
1. sum 0; 1
2. for i = 1 to n do n+1
3. sum sum + A[i] n
4. return sum; 1
Running time ::
T(n) = 1 + (n + 1) + n + 1
= 2n + 3
= a linear function or takes “linear time” = O(n) 37
Prof. Ossama Ismail
Algorithm Analysis cont’d
• Nested for loops
count
1. for i =1 to n do // n + 1
2. for j =1 to n do // (n )(n+1)
3. k k + 1; // (n)(n)
• Running time ::
Running time:
43
Prof. Ossama Ismail
Example:
Search Algorithms
44
Prof. Ossama Ismail
Search Algorithms
45
Prof. Ossama Ismail
Search Algorithms – cont’d
Many interesting problems can be solved using search algorithms: A
A search problem is defined by:
❖ A search space: –
▪ The set of objects among which we search for the solution
Examples: n-queens configuration, or routes between cities.
❖ A goal condition : –
▪ Characteristics of the object we want to find in the search
space? –
Example: Path between cities A and B
46
Prof. Ossama Ismail
Search Algorithms – cont’d
Types of searching Algorithms :
48
Prof. Ossama Ismail
Search Algorithms – cont’d
The Simplest Search: Linear Serial
49
Prof. Ossama Ismail
Search Algorithms – cont’d
Pseudo code for Serial Search
// Find x in an array A of length n
int i=1
boolean found = false
while ((i < n) && !found) {
if (x == A[i])
found = true
else i++
} >>>> print -- 50
Prof. Ossama Ismail
Search Algorithms – cont’d
Analysis for Serial Search
• Best case: Requires one array access: Θ(1)
• Worst case: Requires n array accesses: Θ(n)
• Average case:
To access an item, assuming position is random
(assume uniform search):
53
Prof. Ossama Ismail
Search Algorithms – cont’d
Binary Search
54
Prof. Ossama Ismail
Search Algorithms – cont’d
// Pseudo code for Serial Search
4 5 11 23 31 43 48 50 61 66 70
4 5 11 23 31 43 48 50 61 66 70
4 5 11 23 31 43 48 50 61 66 70
4 5 11 23 31 43 48 50 61 66 70
4 5 11 23 31 43 48 50 61 66 70
4 5 11 23 31 43 48 50 61 66 70 56
Prof. Ossama Ismail
Search Algorithms – cont’d
Analysis for Binary Search
1 for n = 1
T(n) = {
O(1) + T(n/2) for n > 1
Above equation is example of recurrence relation, this can be
solved by direct Substitution.
T(n) = T(n/2) +1
= T(n/22)+1+1
= T(n/23)+1+1+1
……
= T(n/2k) + k = log n // n = n/2k
T(n) = O(log n) --- order of log(n) 57
Prof. Ossama Ismail
Search Algorithms – cont’d
Search for 50 in a list of 11-elements
58
Prof. Ossama Ismail
Run-time Growth Rate
• We will be concerned about the growth rate of the
run-time function
– As n gets very large, what portion of the run-time
equation is dominant?
– See the plots in the following slide
• n+3
– The linear portion, or n, is dominant
• n2 + 3n + 1
– The quadratic portion, or n2, is dominant
59
Prof. Ossama Ismail
Growth Rates of Functions
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Why we care?
Because We worry about the speed of our algorithms for large input sizes “n”
60
Prof. Ossama Ismail
Growth Rates of Functions cont’d
62
Prof. Ossama Ismail
Big Oh Notation
• We will be concerned a lot about the
worst-case run-time (or upper bound)
– What is the dominant growth rate in the worst-
case?
• f(n) = O(g(n)),
– if there exists c and n0 > 0 such that
f(n) <= cg(n)
– f(n) grows at most as fast as g(n)
63
Prof. Ossama Ismail
Big Oh Notation
65
Prof. Ossama Ismail
Properties of Big Oh Cont’d
66
Prof. Ossama Ismail
Big Oh Notation cont’d
➢ 2n + 10 = O(n)
➢ 7n – 2 = O(n)
➢ n2 + 5n – 3 = O(n2)
➢ 3n3 + 20n2 + 5 = O(n3)
➢ 3logn + log logn = O(logn)
67
Prof. Ossama Ismail
Tight Upper Bound
• n2 is not in O(n)
To prove this assume that n2 <= cn
Then n <= c
==> Does not hold since “n” grows infinitely
• We typically say,
• f(n) is O(g(n)) or f(n) = O(g(n))
• But O(g(n)) is really a set of functions.
• It might be more clear to say,
• f(n) є O(g(n))
• But I don’t make the rules.
• Crystal clear: “f(n) is order (g(n))”
70
Prof. Ossama Ismail
Intuitively …
• To say f(n) is O(g(n)) is to say that
• f(n) is “less than or equal to” g(n)
• We also have
71
Prof. Ossama Ismail
Big Omega Notation
• f(n) = Omega(g(n)),
– if there exists c and n0 > 0 such that
f(n) >= cg(n)
– f(n) grows at least as fast as g(n)
72
Prof. Ossama Ismail
Big-Omega and Big-Theta
73
Prof. Ossama Ismail
Big Theta Notation
• f(n) = Theta(g(n)),
– if there exists c0, c1, and n0 > 0 such that
c0g(n) <= f(n) <= c1g(n)
– f(n) grows at the same rate as g(n)
74
Prof. Ossama Ismail
Big Theta Notation cont’d
– Simple example
for i <- 1 to n do
for j <- 1 to n do
k <- k + 1;
O(n2) and Omega(n2) → Theta(n2)
– Insertion sort
• O(n2) and Omega(n) → Theta(n2)
75
Prof. Ossama Ismail
Mappings for n 2
Ω (n2 )
Θ(n2)
O(n2 )
76
Prof. Ossama Ismail
Comparing Growth Rates
n = 1000, 1 ms/op
n 1 second
n log2 n 10 seconds
n2 17 minutes
n3 12 days
n4 32 years
n10 3.17 1019 years
2n 1.07 10301 years
78
Prof. Ossama Ismail
RECURRENCE RELATIONS
79
Prof. Ossama Ismail
Recurrence Relations
• A Recurrence is an equation or inequality that
describes a function in terms of its value on
smaller inputs.
=c for n = 1
T(n) = {
= 2 T(n/n) + c n for n > 1
81
Prof. Ossama Ismail
1. The substitution method
1. Guess the form of the solution.
2. Use mathematical induction to find the
constants and show that the solution works.
Binary Search
85
Prof. Ossama Ismail
Example: Binary Search
BinarySearch( sortedArray[], first, last, key)
{ // sortedArray in array of sorted (ascending) values.
// first, last in lower and upper subscript bounds & key value to search for.
// returns: index of key, or -insertion_position -1 if key is not in the array.
count
if (first <= last) 1
{ mid = (first + last) / 2; // compute mid point. 1
if (key == sortedArray[mid]) return mid; // found it. 1
else if (key < sortedArray[mid]) 1
// recursively call BinarySearch for the lower part
BinarySearch(sortedArray, first, mid-1, key); T(n/2)
else
// recursively call BinarySearch for the upper part
BinarySearch(sortedArray, mid+1, last, key); } T(n/2)
}
return -(first + 1); // failed to find key 1
} 86
Prof. Ossama Ismail
Recurrence -
Example 1:Binary Search Analysis
T(n)=O(1) + T(n/2)
T(1)=1
Above equation is example of recurrence relation, this can be
solved by by direct Substitution.
T(n) = T(n/2) +1
= T(n/22)+1+1
= T(n/23)+1+1+1
……
= T(n/2k) + k = log n // n = n/2k
T(n) = O(log n)
87
Prof. Ossama Ismail
Recurrence - Example 2
Quick Sort
88
Prof. Ossama Ismail
EXAMPLE 2: QUICK SORT
Run time
T(n) = 2T(n/2) + O(n)
T(1)= O(1)
92
Prof. Ossama Ismail
Proof By Induction
Gaussian Closed Form
Prove 1 + 2 + 3 + … + n = n(n+1) / 2
• Basis:
▪ If n = 0, then 0 = 0(0+1) / 2 = 0
• Inductive hypothesis:
▪ Assume 1 + 2 + 3 + … + n = n(n+1) / 2
• Step (show true for n+1):
▪ 1 + 2 + … + n + n+1 = (1 + 2 + …+ n) + (n+1)
▪ = n(n+1)/2 + n+1 = [n(n+1) + 2(n+1)]/2
▪ = (n+1)(n+2)/2 = (n+1)(n+1 + 1) / 2
Prof. Ossama Ismail
93
Proof By Induction
Geometric Closed Form
Prove a0 + a1 + … + an = (an+1 - 1)/(a - 1) for a ≠ 1
• Basis: show that a0 = (a0+1 - 1)/(a - 1)
a0 = 1 = (a1 - 1)/(a - 1)
• Inductive hypothesis:
▪ Assume a0 + a1 + … + an = (an+1 - 1)/(a - 1)
• Step (show true for n+1):
▪ a0 + a1 + … + an = (an+1 - 1)/(a - 1) + an+1
▪ = (an+1 - 1)/(a - 1) + an+1 = (an+1+1 - 1)/(a - 1)
94
Prof. Ossama Ismail
Reading
95
Prof. Ossama Ismail
Summary
97
Prof. Ossama Ismail