SlideShare a Scribd company logo
Design and Analysis of
Algorithms
Mathematical foundations
Department of Computer Science
Mathematical Background
❖Analysis of algorithms requires knowledge of
mathematical tools such as:
 Methods for evaluating and bounding
summations
 Sets, relations, functions, graphs and trees
 Counting: permutations and combinations
 Probability and statistics
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 2
Sets
❖A set is a collection of distinguishable objects; called
members or elements
❖A set cannot contain the same element more than
once; a variation of this is called multiset
❖Given a set say S and element x then we can write:
❖ ( “x is in S”) OR (“x is not in S”)
❖Frequently used sets
 the empty set
 Z set of integers {…, -2,-1,0,1,2,…}
 R set of real numbers
 N set of natural numbers {0,1,2,…}
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 3
S
x S
x 
θ
Set relations / operations
❖Subset A is a subset of B
❖Proper subset if but
❖Intersection
❖Union
❖Difference A-B
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 4
B
A 
B
A  B
A  B
A 
B
A
B
A
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.
❖Speed is fun!
Monday, March 6, 2023
Design and Analysis of Computer Algorithm 5
Methods of Proof
❖Proof by Contradiction
 Assume a theorem is false; show that this assumption
implies a property known to be true is false -- therefore
original hypothesis must be true
❖Proof by Counterexample
 Use a concrete example to show an inequality cannot
hold
❖Mathematical Induction
 Prove a trivial base case, assume true for k, then show
hypothesis is true for k+1
 Used to prove recursive algorithms
Monday, March 6, 2023 6
Design and Analysis of Computer Algorithm
Review: Induction
❖Suppose
 S(k) is true for fixed constant k
▪ Often k = 0
 S(n) S(n+1) for all n >= k
❖Then S(n) is true for all n >= k
Monday, March 6, 2023 7
Design and Analysis of Computer Algorithm
Proof By Induction
❖Claim: S(n) is true for all n >= k
❖Basis:
 Show formula is true when n = k
❖Inductive hypothesis:
 Assume formula is true for an arbitrary n
❖Step:
 Show that formula is then true for n+1
Monday, March 6, 2023 8
Design and Analysis of Computer Algorithm
Induction Example:
Gaussian Closed Form
❖Prove 1 + 2 + 3 + … + n = n(n+1) / 2
 Basis:
▪ If n = 0, then 0 = 0(0+1) / 2
 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
Monday, March 6, 2023 9
Design and Analysis of Computer Algorithm
Induction Example:
Geometric Closed Form
❖Prove a0 + a1 + … + an = (an+1 - 1)/(a - 1) for
all 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+1 = a0 + a1 + … + an + an+1
= (an+1 - 1)/(a - 1) + an+1 = (an+1 – 1+ an+1+1– an+1)/(a - 1)
= (an+1+1 - 1)/(a - 1)
Monday, March 6, 2023 10
Design and Analysis of Computer Algorithm
Induction
❖We’ve been using weak induction
❖Strong induction also holds
 Basis: show S(0)
 Hypothesis: assume S(k) holds for arbitrary k <=
n
 Step: Show S(n+1) follows
❖Another variation:
 Basis: show S(0), S(1)
 Hypothesis: assume S(n) and S(n+1) are true
 Step: show S(n+2) follows
Monday, March 6, 2023 11
Design and Analysis of Computer Algorithm
Loop Invariants
❖ We use loop invariants to understand why an
algorithm is correct by showing 3 things:
1. Initialisation - it is true prior to the first iteration of
the loop
2. Maintenance – if it is true before an iteration of the
loop, it remains true before the next iteration
3. Termination – when the loop terminates, the
invariant gives us a useful property that helps us
show that the algorithm is correct
Monday, March 6, 2023 12
Design and Analysis of Computer Algorithm
Loop Invariants in Use
Insertion Sort
1 for j = 2 to n
2 x = A[j ]
3 i = j − 1
4 while i > 0 and A[i ] > x
5 A[i + 1] = A[i ];
6 i = i − 1
7 A[i +1] = x
Monday, March 6, 2023 13
Design and Analysis of Computer Algorithm
Correctness of insertion sort
❖ Each time execution reaches (2), the
elements A[1..j − 1] (i.e. s′) are in sorted
order, and they are the same elements as
were originally in A[1..j − 1] (permuted).
❖ The elements A[j ..n] (i.e. s) are unchanged.
(1) perm(A[1..j − 1],A0[1..j − 1])
(2) ordered(A[1..j − 1])
(3) A[j ..n] = A0[j ..n]
❖ This is a loop invariant for the outer loop.
Monday, March 6, 2023 14
Design and Analysis of Computer Algorithm
Using the invariant to prove correctness
❖ Initialization: Show that the loop invariant
holds prior to the first iteration.
 Here, j = 2, so A[1..j − 1] is just the singleton
A[1].
(1) A0[1] = A[1], so the permutation is trivial.
(2) {A[1]} is trivially ordered.
(3) A[2..n] = A0[2..n].
Monday, March 6, 2023 15
Design and Analysis of Computer Algorithm
Using the invariant to prove correctness
❖ Maintenance: Assume that the loop invariant holds
before some iteration. Show that it will hold again
before the next.
(1) Assume perm(A[1..j − 1],A0[1..j − 1]) and show
perm(A′[1..j ′ − 1],A0[1..j ′ − 1]), where j ′ = j + 1
and A′ is the array A after execution of lines 2–7.
(2) Assume ordered(A[1..j − 1]) and show ordered
(A′[1..j ′ − 1]).
(3) A[j ..n] = A0[j ..n] because we have not yet
touched this part of the array.
Monday, March 6, 2023 16
Design and Analysis of Computer Algorithm
Using the invariant to prove correctness
❖ Termination: Assume that the loop invariant
holds, and that the loop has terminated.
Show that the postcondition is satisfied.
❖ Here, the loop terminates with j = n + 1.
(1) perm(A[1..j − 1],A0[1..j − 1]) implies
perm(A[1..n],A0[1..n]).
(2) ordered(A[1..j − 1]) implies ordered(A[1..n]).
(3) A[j ..n] is empty.
Monday, March 6, 2023 17
Design and Analysis of Computer Algorithm
Using the invariant to prove correctness
❖ We should also show that the program actually
does terminate. In this this case, the outer loop
executes exactly n − 1 times.
❖ The inner loop starts with i = j − 1, which is a
positive value; at each iteration, i is decremented;
and the loop exits when i = 0 (or perhaps sooner,
depending on x and A). This guarantees
termination.
❖ The other operations in the algorithm are all
assignments or comparisons, which (we assume)
always terminate.
Monday, March 6, 2023 18
Design and Analysis of Computer Algorithm

More Related Content

PDF
Skiena algorithm 2007 lecture15 backtracing
PPTX
Mathematical Statistics Assignment Help
PDF
Lesson 13: Exponential and Logarithmic Functions (Section 021 slides)
PPT
PDF
Complex Analysis
PPT
Backtracking
PPTX
Mathematical Statistics Assignment Help
PPTX
MODULE_05-Matrix Decomposition.pptx
Skiena algorithm 2007 lecture15 backtracing
Mathematical Statistics Assignment Help
Lesson 13: Exponential and Logarithmic Functions (Section 021 slides)
Complex Analysis
Backtracking
Mathematical Statistics Assignment Help
MODULE_05-Matrix Decomposition.pptx

Similar to CCS 3102 Lecture 2_ Mathematical foundations.pdf (20)

PPTX
III_Data Structure_Module_1.pptx
PPTX
Complex differentiation contains analytic function.pptx
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
PPTX
MATRICES-MATHED204.pptx
PPT
III_Data Structure_Module_1.ppt
PDF
Senior_Project
PDF
PMED Transition Workshop - Non-parametric Techniques for Estimating Tumor Het...
PDF
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
PDF
Perspective in Informatics 3 - Assignment 1 - Answer Sheet
PDF
7-Backtracking.pdfsssssssssssssssssssssssssssssssssss
DOCX
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PDF
Machine learning (11)
PDF
Decoding BCH-Code.pdf
PPTX
01 FUNCTIONS.pptx
PPTX
Tutorial on EM algorithm – Part 1
PDF
10.1-introduction-to-complex-numbers.pdf
PDF
A PROBABILISTIC ALGORITHM FOR COMPUTATION OF POLYNOMIAL GREATEST COMMON WITH ...
PDF
A Probabilistic Algorithm for Computation of Polynomial Greatest Common with ...
PDF
A PROBABILISTIC ALGORITHM FOR COMPUTATION OF POLYNOMIAL GREATEST COMMON WITH ...
PDF
A coefficient inequality for the starlike univalent functions in the unit dis...
III_Data Structure_Module_1.pptx
Complex differentiation contains analytic function.pptx
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
MATRICES-MATHED204.pptx
III_Data Structure_Module_1.ppt
Senior_Project
PMED Transition Workshop - Non-parametric Techniques for Estimating Tumor Het...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Perspective in Informatics 3 - Assignment 1 - Answer Sheet
7-Backtracking.pdfsssssssssssssssssssssssssssssssssss
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
Machine learning (11)
Decoding BCH-Code.pdf
01 FUNCTIONS.pptx
Tutorial on EM algorithm – Part 1
10.1-introduction-to-complex-numbers.pdf
A PROBABILISTIC ALGORITHM FOR COMPUTATION OF POLYNOMIAL GREATEST COMMON WITH ...
A Probabilistic Algorithm for Computation of Polynomial Greatest Common with ...
A PROBABILISTIC ALGORITHM FOR COMPUTATION OF POLYNOMIAL GREATEST COMMON WITH ...
A coefficient inequality for the starlike univalent functions in the unit dis...
Ad

Recently uploaded (20)

PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PPT
Quality review (1)_presentation of this 21
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
Introduction to the R Programming Language
PDF
Business Analytics and business intelligence.pdf
PPT
ISS -ESG Data flows What is ESG and HowHow
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PDF
Fluorescence-microscope_Botany_detailed content
PDF
[EN] Industrial Machine Downtime Prediction
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
PPTX
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
PDF
Introduction to Data Science and Data Analysis
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Quality review (1)_presentation of this 21
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Supervised vs unsupervised machine learning algorithms
Introduction to the R Programming Language
Business Analytics and business intelligence.pdf
ISS -ESG Data flows What is ESG and HowHow
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
Fluorescence-microscope_Botany_detailed content
[EN] Industrial Machine Downtime Prediction
Galatica Smart Energy Infrastructure Startup Pitch Deck
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
STUDY DESIGN details- Lt Col Maksud (21).pptx
AI Strategy room jwfjksfksfjsjsjsjsjfsjfsj
Introduction to Basics of Ethical Hacking and Penetration Testing -Unit No. 1...
Introduction to Data Science and Data Analysis
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Miokarditis (Inflamasi pada Otot Jantung)
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Introduction-to-Cloud-ComputingFinal.pptx
Ad

CCS 3102 Lecture 2_ Mathematical foundations.pdf

  • 1. Design and Analysis of Algorithms Mathematical foundations Department of Computer Science
  • 2. Mathematical Background ❖Analysis of algorithms requires knowledge of mathematical tools such as:  Methods for evaluating and bounding summations  Sets, relations, functions, graphs and trees  Counting: permutations and combinations  Probability and statistics Monday, March 6, 2023 Design and Analysis of Computer Algorithm 2
  • 3. Sets ❖A set is a collection of distinguishable objects; called members or elements ❖A set cannot contain the same element more than once; a variation of this is called multiset ❖Given a set say S and element x then we can write: ❖ ( “x is in S”) OR (“x is not in S”) ❖Frequently used sets  the empty set  Z set of integers {…, -2,-1,0,1,2,…}  R set of real numbers  N set of natural numbers {0,1,2,…} Monday, March 6, 2023 Design and Analysis of Computer Algorithm 3 S x S x  θ
  • 4. Set relations / operations ❖Subset A is a subset of B ❖Proper subset if but ❖Intersection ❖Union ❖Difference A-B Monday, March 6, 2023 Design and Analysis of Computer Algorithm 4 B A  B A  B A  B A  B A B A
  • 5. 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. ❖Speed is fun! Monday, March 6, 2023 Design and Analysis of Computer Algorithm 5
  • 6. Methods of Proof ❖Proof by Contradiction  Assume a theorem is false; show that this assumption implies a property known to be true is false -- therefore original hypothesis must be true ❖Proof by Counterexample  Use a concrete example to show an inequality cannot hold ❖Mathematical Induction  Prove a trivial base case, assume true for k, then show hypothesis is true for k+1  Used to prove recursive algorithms Monday, March 6, 2023 6 Design and Analysis of Computer Algorithm
  • 7. Review: Induction ❖Suppose  S(k) is true for fixed constant k ▪ Often k = 0  S(n) S(n+1) for all n >= k ❖Then S(n) is true for all n >= k Monday, March 6, 2023 7 Design and Analysis of Computer Algorithm
  • 8. Proof By Induction ❖Claim: S(n) is true for all n >= k ❖Basis:  Show formula is true when n = k ❖Inductive hypothesis:  Assume formula is true for an arbitrary n ❖Step:  Show that formula is then true for n+1 Monday, March 6, 2023 8 Design and Analysis of Computer Algorithm
  • 9. Induction Example: Gaussian Closed Form ❖Prove 1 + 2 + 3 + … + n = n(n+1) / 2  Basis: ▪ If n = 0, then 0 = 0(0+1) / 2  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 Monday, March 6, 2023 9 Design and Analysis of Computer Algorithm
  • 10. Induction Example: Geometric Closed Form ❖Prove a0 + a1 + … + an = (an+1 - 1)/(a - 1) for all 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+1 = a0 + a1 + … + an + an+1 = (an+1 - 1)/(a - 1) + an+1 = (an+1 – 1+ an+1+1– an+1)/(a - 1) = (an+1+1 - 1)/(a - 1) Monday, March 6, 2023 10 Design and Analysis of Computer Algorithm
  • 11. Induction ❖We’ve been using weak induction ❖Strong induction also holds  Basis: show S(0)  Hypothesis: assume S(k) holds for arbitrary k <= n  Step: Show S(n+1) follows ❖Another variation:  Basis: show S(0), S(1)  Hypothesis: assume S(n) and S(n+1) are true  Step: show S(n+2) follows Monday, March 6, 2023 11 Design and Analysis of Computer Algorithm
  • 12. Loop Invariants ❖ We use loop invariants to understand why an algorithm is correct by showing 3 things: 1. Initialisation - it is true prior to the first iteration of the loop 2. Maintenance – if it is true before an iteration of the loop, it remains true before the next iteration 3. Termination – when the loop terminates, the invariant gives us a useful property that helps us show that the algorithm is correct Monday, March 6, 2023 12 Design and Analysis of Computer Algorithm
  • 13. Loop Invariants in Use Insertion Sort 1 for j = 2 to n 2 x = A[j ] 3 i = j − 1 4 while i > 0 and A[i ] > x 5 A[i + 1] = A[i ]; 6 i = i − 1 7 A[i +1] = x Monday, March 6, 2023 13 Design and Analysis of Computer Algorithm
  • 14. Correctness of insertion sort ❖ Each time execution reaches (2), the elements A[1..j − 1] (i.e. s′) are in sorted order, and they are the same elements as were originally in A[1..j − 1] (permuted). ❖ The elements A[j ..n] (i.e. s) are unchanged. (1) perm(A[1..j − 1],A0[1..j − 1]) (2) ordered(A[1..j − 1]) (3) A[j ..n] = A0[j ..n] ❖ This is a loop invariant for the outer loop. Monday, March 6, 2023 14 Design and Analysis of Computer Algorithm
  • 15. Using the invariant to prove correctness ❖ Initialization: Show that the loop invariant holds prior to the first iteration.  Here, j = 2, so A[1..j − 1] is just the singleton A[1]. (1) A0[1] = A[1], so the permutation is trivial. (2) {A[1]} is trivially ordered. (3) A[2..n] = A0[2..n]. Monday, March 6, 2023 15 Design and Analysis of Computer Algorithm
  • 16. Using the invariant to prove correctness ❖ Maintenance: Assume that the loop invariant holds before some iteration. Show that it will hold again before the next. (1) Assume perm(A[1..j − 1],A0[1..j − 1]) and show perm(A′[1..j ′ − 1],A0[1..j ′ − 1]), where j ′ = j + 1 and A′ is the array A after execution of lines 2–7. (2) Assume ordered(A[1..j − 1]) and show ordered (A′[1..j ′ − 1]). (3) A[j ..n] = A0[j ..n] because we have not yet touched this part of the array. Monday, March 6, 2023 16 Design and Analysis of Computer Algorithm
  • 17. Using the invariant to prove correctness ❖ Termination: Assume that the loop invariant holds, and that the loop has terminated. Show that the postcondition is satisfied. ❖ Here, the loop terminates with j = n + 1. (1) perm(A[1..j − 1],A0[1..j − 1]) implies perm(A[1..n],A0[1..n]). (2) ordered(A[1..j − 1]) implies ordered(A[1..n]). (3) A[j ..n] is empty. Monday, March 6, 2023 17 Design and Analysis of Computer Algorithm
  • 18. Using the invariant to prove correctness ❖ We should also show that the program actually does terminate. In this this case, the outer loop executes exactly n − 1 times. ❖ The inner loop starts with i = j − 1, which is a positive value; at each iteration, i is decremented; and the loop exits when i = 0 (or perhaps sooner, depending on x and A). This guarantees termination. ❖ The other operations in the algorithm are all assignments or comparisons, which (we assume) always terminate. Monday, March 6, 2023 18 Design and Analysis of Computer Algorithm