Discrete Mathematics (H) Lecture 6 Notes
Algorithms and Complexity
Asymptotic Notation
Big-
Notation
Definition: Big-
Let
(for more precise definition and applications see notes for lecture 2 of Data Structures and Algorithm Analysis (H))
Tip: Big-
Let
Proof. Assuming
Big-
for integer for integers for an integer
Tip: Big-
If
Proof. By definition, there exist constants
Tip: Big-
If
Proof. When
Big- Notation
Definition: Big-
Let
Tip
Big-
Big- Notation
Definition: Big-
Two functions
Algorithms
Definition: Algorithm
An algorithm is a finite sequence of precise instructions for performing a computation or for solving a problem.
Definition: Computational Problem
A computational problem is a specification of the desired input-output relationship.
Definition: Problem Instance
An instance of a problem is all the inputs needed to compute a solution to the problem.
A correct algorithm halts with the correct output for every input instance. We then say that the algorithm solves the problem.
Time and Space Complexity
Definition: Time Complexity and Space Complexity
The number of machine operations needed in an algorithm is the time complexity of the algorithm, and the amount of memory needed is the space complexity of the algorithm.
Example: Horner’s Algorithm
Consider the evaluation of
The
number of operations needed in this algorithm is clearly
Example: Insertion Sort

Best-case
To analyze for the best-case, we constrain the input so that is will result in the fastest possible running time for the given input size. For a sorting algorithm, the best-case is when the input array is already sorted.
In this case the number of comparison needed is
Worst-case
To analyze for the best-case, we constrain the input so that is will
result in the slowest possible running time for the given input size.
For a sorting algorithm, the best-case is when the input array is
already sorted in reverse order.
Average-case
We average running time over every possible type of input for the given size, this usually involve probabilities of different types of input.
The complexity of insertion sort is
Dealing with Hard Problems
Showing that a problem has an efficient algorithm is relatively easy, since all we need to do is to demonstrate an algorithm. But showing that there is no efficient algorithm exists for a particular problem is rather difficult, since in this case we need to prove the non-existence of such algorithm among the countless possible algorithms. A handy way to approach this question is through the NP-Complete problems.
The NP-Complete problems is a very large class of thousands of practical problems for which it is not known if the problems have efficient solutions. Or more precisely, have polynomial-time solutions. It is known that if any one of the NP-Complete problems has an efficient solution then all of the NP-Complete problems can be efficient solutions. Since the innumberable man-years trying to find the efficient solutions, NP-Complete problems are very likely to be hard.
Encoding the Input of Problems
Complexity of a problem is a measure w.r.t. the size of input. In order to formally discuss how hard a problem is, we need to be much more formal than before about the input size of a problem.
Definition: Input Size
The input size of a problem is the minimum number
of bits (
But the exact input size
For most problems, it is sufficient to choose some natural and
(usually) simple, encoding and use the size
Input Size Example: Composite
Example: Composite Number
Given a positive integer
Solution. Any integer
Input Size Example: Sorting
Example: Sorting
Sort
Solution. Using fixed length encoding, we write
Complexity in terms of Input Size
Example: Composite Number
The naive algorithm for determining whether
At the first glance, this algorithm makes
Definition: Type of Functions
Two positive functions
Input Size Example: Integer Multiplication
Example: Integer Multiplication
Given two integers
The minimum input size of this problem is
Complexity
Decision Problems and Optimization Problems
Example: Decision Problem
A decision problem is a question that has two possible answers: yes and no.
If
Example: Optimization Problem
An optimization problem requires an answer that is an optimal configuration.
An optimization problem usually has a corresponding decision problem.
Example: Knapsack vs. DKnapsack
We have a knapsack of capacity
Then
- Optimization problem (Knapsack):
- Find the largest value
of any subset that fits in the knapsack, i.e, .
- Find the largest value
- Decision problem (DKnapsack):
- Given
, is there a subset of the objects that fits in the knapsack and has total value at least ?
- Given
Relationship Between the two Problems
Given a subroutine for solving the optimization problem, solving the corresponding decision problem is usually trivial: First we solve the optimization problem, then check the decision problem. If it does, answer yes, otherwise no.
Thus, if we prove that a given decision problem is hard to solve efficiently, then it is obvious that the optimization problem must be (at least as) hard.
Complexity Classes
Definition: Polynomial-Time Algorithms
An algorithm is polynomial-time if its running time is
Tip
Whether we use
Definition: Nonpolynomial-Time Algorithms
An algorithm is nonpolynomial-time if the running time is
not
Nonpolynomial-time algorithms are very often impractical.
Example: Polynomial-Time Solvable Problems
A problem is solvable in polynomial time (or simply in polynomial time) if there exists an algorithm which solves the problem in polynomial time (a.k.a. tractable).
Definition: Complexity Class
The class
Definition: Certificate
A certificate is a specific object corresponding to a yes-input, such that it can be used to show that the input is indeed a yes-input.
Given a presumed yes-input and its corresponding certificate, by making used of the given certificate, we verify that the input is actually a yes-input.
Definition: Complexity Class
The class
We can intuitively observe that
Reduction
Definition: Reduction
We say that a problem
Example
Let
: multiplying two positive numbers : adding two numbers
Then
We say that
Definition: Polynomial-Time Reductions
Let
transform an input for into an input for s.t. a yes-input of maps to a yes-input of , and a no-input maps to a no-input of . is computable in polynomial time in .
If such an
Given two decision problems
Theorem
If
Proof.
Combining these, we get the following polynomial-time algorithm for
solving
- take input
for and compute - run algorithm
on input , and return the answer.
Both steps take polynomial time. So the combined algorithm takes
polynomial time. Hence
Lemma: Transitivity of Reduction
If
NP-Completeness and Its Properties
Definition: Complexity Class
The class
- for every
,
Intuitively,
Theorem
Let
- If there is a polynomial-time algorithm for
, then there is a polynomial-time algorithm for every . - If there is no polynomial-time algorithm for
, then there is no polynomial-time algorithm for every .
Either all NP-Complete problems are polynomial time solvable, or all NP-Complete problems are not polynomial time solvable.
Proof. For 1., if
For 2., assume that for some problem
- Title: Discrete Mathematics (H) Lecture 6 Notes
- Author: Zhige Chen
- Created at : 2025-10-15 21:46:09
- Updated at : 2025-10-15 22:25:30
- Link: https://nofe1248.github.io/2025/10/15/discrete-math-lec-6/
- License: This work is licensed under CC BY-NC-SA 4.0.