Professional Documents
Culture Documents
CSE3004-Lecture 0-Merged-Pages-Deleted
CSE3004-Lecture 0-Merged-Pages-Deleted
CSE3004-Lecture 0-Merged-Pages-Deleted
Definition 1 (Algorithm)
An algorithm is a sequence of unambiguous instructions for solving
An algorithm has five characteristics [1]: a problem, i.e., for obtaining a required output for any legitimate
1. Input: An algorithm has zero or more inputs: quantities that are given input in a finite amount of time [2].
to it initially before the algorithm begins, or dynamically as the algorithm
runs.
2. Output: An algorithm has one or more output.
3. Finiteness: Total number of steps used in algorithm should be finite. In
other words, the algorithm must always terminate after a finite number of
steps.
4. Definiteness: Each step of the algorithm must be clear and unambiguous.
5. Effectiveness: Every step must be basic and essential.
6/25 7/25
Example of Algorithm: Finding largest of n numbers Example of Algorithm: Greatest Common Divisor (GCD)
Input: List L = {a1 , a2 , . . . , an } of n positive integers. For e.g. {5, 2, 4, 6, 1, 3}. In mathematics, the Euclidean algorithm, or Euclid’s algorithm, is an effi-
Output: number {x|x ≥ ai , 1 ≤ i ≤ n} and x ∈ L. For e.g. 6. cient method for computing the GCD of two integers (numbers), the largest
number that divides them both without a remainder.
Algorithm 1: max (L) Algorithm 2: max (L) Input: Two n-digit nonnegative integers, m and n.
Output: GCD of m and n.
◦ Step 1: Start. ◦ Step 1: Start.
◦ Step 2: If (|L| == 1) ◦ Step 2: If (|L| == 1)
◦ Step 3: return (a1 ) ◦ Step 3: return (a1 ) Approach 1: Euclid’s Algorithm (m, n) [2] Example illustrating Euclid algorithm
10/25 11/25
Example of Algorithm: Sorting a set of Natural Numbers Fundamentals of Algorithmic Problem Solving
1
Input: A sequence of n numbers ha1 , a2 , . . . , an i. For e.g. h5, 2, 4, 6, 1, 3i
Output: A permutation (reordering) ha1′ , a2′ , . . . , an′ i of the input sequence such
that a1′ ≤ a2′ ≤ . . . ≤ an′ . For e.g. h1, 2, 3, 4, 5, 6i.
1
Such an input sequence is called an instance of the sorting problem. 12/25 3/11
• The running time of the algorithm is the sum of running times for each
statement executed.
8/11 3/7
O notation Ω notation
We denote by O(g (n)) (pronounced “big-oh of g of n” or somtimes We denote by Ω(g (n)) (pronounced “big-omega of g of n” or som-
just “oh of g of n”) the set of functions times just “omega of g of n”) the set of functions
5/9 6/9
o notation ω notation
• We formally define o(g (n)) (pronounced “little-oh of g of n” as • We formally define ω(g (n)) (pronounced “little-ω of g of n” as
the set the set
• Some authors use this limit as a definition of the o-notation • Some authors use this limit as a definition of the ω-notation
f (n) f (n)
lim =0 lim =∞
n→∞ g (n) n→∞ g (n)
8/13 9/13
Relational properties Relational properties. . .
• Assume that f (n) and g (n) are asymptotically positive.
1. Transitivity:
2. Reflexivity:
3. Symmetry:
10/13 11/13
Mathematical Analysis of Mathematical Analysis of
Non-recursive Algorithms Non-recursive Algorithms
General Plan for Analyzing the Time Efficiency
We systematically apply the general 1. Decide on a parameter (or parameters) indicating an
input’s size.
framework (discussed earlier) to
2. Identify the algorithm’s basic operation. (As a rule, it
analyzing the time efficiency of non- is located in the innermost loop.)
recursive algorithms. 3. Check whether the number of times the basic
operation is executed depends only on the size of an
input. If it also depends on some additional property, the
worst-case, average-case, and, if necessary, best-case
efficiencies have to be investigated separately.
This problem can be solved by the following input’s size here is again n, the number of
straightforward algorithm. elements in the array.
◼ 3. Check whether the number of times the basic ◼ 5. Solve the recurrence or, at least, ascertain the
operation is executed can vary on different inputs order of growth of its solution.
of the same size; if it can, the worst-case,
average-case, and best-case efficiencies must
be investigated separately.
Example 1
◼ Compute the factorial function F(n) = n! for
an arbitrary nonnegative integer n.
◼ 3. Check whether the number of times the basic ◼ 5. Solve the recurrence or, at least, ascertain the
operation is executed can vary on different inputs order of growth of its solution.
of the same size; if it can, the worst-case,
average-case, and best-case efficiencies must
be investigated separately.
◼ With the initial condition, we get the recurrence for n = 1, which is achieved for i = n − 1, we
relation as
◼ The problem asks to find the shortest tour ◼ Then the problem can be stated as the
through a given set of n cities that visits
problem of finding the shortest
each city exactly once before returning to
Hamiltonian circuit/cycle of the graph.
the city where it started.
◼ The problem can be conveniently modeled
by a weighted graph, with the graph’s ◼ A Hamiltonian circuit/cycle is defined as a
vertices representing the cities and the edge cycle that passes through all the vertices of
weights specifying the distances. the graph exactly once.
approach impractical for all but very small ◼ A transport plane that has to deliver the
most valuable set of items to a remote
values of n. location without exceeding the plane’s capacity.
Knapsack Problem- Types Knapsack Problem- Types
Example: Example:
NP-Hard Problems Assignment Problem
◼ Thus, for both the traveling salesman and ◼ There are n people (agents) who need to
be assigned to execute n tasks, one person
knapsack problems considered above,
per job. (That is, each person is assigned to
exhaustive search leads to algorithms that are
exactly one job and each job is assigned to
extremely inefficient on every input. exactly one person.)
◼ In fact, these two problems are the best-known ◼ The cost that would accrue if the ith person
examples of so called NP-hard problems. is assigned to the jth job is a known
[Non-deterministic Polynomial-time Hard] quantity C[i,j] for each pair i,j = 1, 2, …, n.
◼ No polynomial-time algorithm is known for any ◼ The problem is to find an assignment with
NP-hard problem. the minimum total cost.
◼ It is easy to see that an instance of the ◼ For example, we cannot select the
assignment problem is completely specified smallest element in each row, because the
by its cost matrix C.
smallest elements may happen to be in the
◼ In terms of this matrix, the problem is to same column.
select one element in each row of the
matrix so that all selected elements are in ◼ In fact, the smallest element in the entire
different columns and the total sum of the matrix need not be a component of an
selected elements is the smallest possible. optimal solution.
◼ Note that no obvious strategy for finding a
◼ Thus, opting for the exhaustive search may
solution works here.
appear as an unavoidable evil.
Assignment Problem Assignment Problem
Assignment Problem
Brute-Force Approach
◼ Since the number of permutations to be
considered for the general case of the
assignment problem is
IDEA:
1. Break the problem into several subproblems that are similar to the original
problem but smaller in size,
2. Solve the subproblems recursively, and
3. then combine these solutions to create a solution to the original problem.
The divide-and-conquer paradigm involves three steps at each level of the recursion:
• Divide the problem into a number of subproblems that are smaller instances of
the same problem.
• Conquer the subproblems by solving them recursively. If the subproblem sizes
are small enough, however, just solve the subproblems in a straightforward
manner.
• Combine the solutions to the subproblems into the solution for the original
problem.
MERGE SORT:
ALGORITHM:
EXAMPLE: