0% found this document useful (0 votes)
5 views37 pages

Lec 16

The document outlines the characteristics of algorithms, emphasizing their clarity, defined inputs and outputs, finiteness, feasibility, and language independence. It discusses methods for proving algorithm correctness, including mathematical induction and counterexamples, and introduces concepts of time complexity with examples of various time complexities such as constant, logarithmic, linear, and exponential. The document serves as a comprehensive guide to understanding algorithm correctness and efficiency.

Uploaded by

Mohd Iliyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views37 pages

Lec 16

The document outlines the characteristics of algorithms, emphasizing their clarity, defined inputs and outputs, finiteness, feasibility, and language independence. It discusses methods for proving algorithm correctness, including mathematical induction and counterexamples, and introduces concepts of time complexity with examples of various time complexities such as constant, logarithmic, linear, and exponential. The document serves as a comprehensive guide to understanding algorithm correctness and efficiency.

Uploaded by

Mohd Iliyas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Correctness of Algorithms, Time

Complexity

Prasad Chaugule

June 17, 2025


What is an algorithm?

• Clear and Unambiguous: The algorithm should be


unambiguous. Each of its steps should be clear in all aspects
and must lead to only one meaning.

• Well-Defined Inputs: If an algorithm says to take inputs, it


should be well-defined inputs. It may or may not take input.

• Well-Defined Outputs: The algorithm must clearly define


what output will be yielded, and it should be well-defined as
well. It should produce at least 1 output.

1 / 26
What is an algorithm?

• Finite-ness: The algorithm must be finite, i.e. it should


terminate after a finite time.

• Feasible: The algorithm must be simple, generic, and


practical, such that it can be executed with the available
resources. It must not contain some future technology or
anything.

• Language Independent: The Algorithm designed must be


language-independent, i.e., it must be just plain instructions
that can be implemented in any language, and yet the output
will be the same, as expected.

2 / 26
How to prove that your algorithm is correct (or incorrect)?

3 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉

4 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉

• Prove or Disprove: "Every positive integer is the sum of two


squares of integers"

4 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉

• Prove or Disprove: "Every positive integer is the sum of two


squares of integers"

• Prove of Disprove: ∀x∀y (xy ≥ x) (over all integers)

4 / 26
• Prove or Disprove: ⌈x + y ⌉ = ⌈x⌉ + ⌈y ⌉

• Prove or Disprove: "Every positive integer is the sum of two


squares of integers"

• Prove of Disprove: ∀x∀y (xy ≥ x) (over all integers)

4 / 26
5 / 26
Use Mathematical Induction to prove the correctness

• Show that it’s true for the base case.


• If it’s true for n then it’s true for n + 1

6 / 26
Mathematical Induction

Prove all prices p ≥ 8, the price p can be paid by only using 3


cents and 5 cents.

7 / 26
Solution

8 / 26
Algorithm - The cent problem

9 / 26
Loop invariant

10 / 26
Loop invariant examples

• Finding Maximum (or minimum) element from a given list of


numbers.
• Finding sum of the elements in a given list
• Binary search tree

11 / 26
Loop invariant: BST

12 / 26
Greedy Algorithm Example

13 / 26
One possible Algorithm?

14 / 26
Arguing its incorrectness by counterexample

15 / 26
Another attempt!

16 / 26
Correctness of greedy interval scheduling solution

17 / 26
Big O Notation

Definition
A function f (n) is O(g (n)), if for k ≥ k0 , f (n) ≤ c × g (n).

18 / 26
Big O Notation

Definition
A function f (n) is O(g (n)), if for k ≥ k0 , f (n) ≤ c × g (n).

18 / 26
Time complexity

19 / 26
Time complexity - continued

20 / 26
Time complexity– continued

21 / 26
Examples

• Constant time: Finding Minimum in a sorted list

22 / 26
Examples

• Constant time: Finding Minimum in a sorted list


• Logarithmic time: Searching in a sorted list

22 / 26
Examples

• Constant time: Finding Minimum in a sorted list


• Logarithmic time: Searching in a sorted list
• Linear time: Finding minimum in a list, not necessarily sorted

22 / 26
Examples

• Constant time: Finding Minimum in a sorted list


• Logarithmic time: Searching in a sorted list
• Linear time: Finding minimum in a list, not necessarily sorted
• Quasilinear: Mergesort

22 / 26
Examples

• Constant time: Finding Minimum in a sorted list


• Logarithmic time: Searching in a sorted list
• Linear time: Finding minimum in a list, not necessarily sorted
• Quasilinear: Mergesort
• Quadratic: Bubblesort

22 / 26
Examples

• Constant time: Finding Minimum in a sorted list


• Logarithmic time: Searching in a sorted list
• Linear time: Finding minimum in a list, not necessarily sorted
• Quasilinear: Mergesort
• Quadratic: Bubblesort
• Exponential: Finding all subsets

22 / 26
Examples

• Constant time: Finding Minimum in a sorted list


• Logarithmic time: Searching in a sorted list
• Linear time: Finding minimum in a list, not necessarily sorted
• Quasilinear: Mergesort
• Quadratic: Bubblesort
• Exponential: Finding all subsets
• Factorial: Finding all permutations

22 / 26
What is the complexity?

23 / 26
What is the complexity?

24 / 26
What is the complexity?

25 / 26
26 / 26

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy