01-algo
01-algo
01-algo
Algorithm?
Design?
Analysis?
Course Objectives
Example: sorting
input: A sequence of numbers.
output: An ordered permutation of the input.
issues: correctness, efficiency, storage, etc.
Exception
Randomization (uniform distribution of
choice)
Approximation (Square root of 2)
Strengthening the
Informal Definiton
Analgorithm is a finite sequence of
unambiguous instructions for solving a
well-specified computational problem.
Important Features:
Algorithm by 3 ways
1. Using Natural language
2. Pseudocode;
3. flowchart.
Pseudocode
Natural language + programming language
construct
For e.g.to perform addition of two numbers we
write in pseudo-code
//this algo perform addition of two integers
//input two integers a and b
// output of two integers
Read a,b
C=a+b
Write c
Flow Chart
Is it recent, to build better algorithm?
Is it recent, to build better algorithm?
Resources to build ??
Ease of Implementation ??
Analysis of an
Algorithm
American
A la russe
algorithm 7000
6000
Time (ms)
Run the program with 5000
3000
and composition 2000
0
get an accurate 0 50 100
running time
Plot the results
Limitations of Experiments
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<int> distribution(1, 1000);
const int SIZE = 10000;
// Adjust this value to change the size of the array
std::vector<int> data(SIZE);
for (int i = 0; i < SIZE; ++i) { data[i] =
distribution(generator); }
C++ chrono
auto startTime =
std::chrono::high_resolution_clock::now();
selectionSort(data);
auto endTime =
std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds
>(endTime - startTime).count();
Python time library
34
Bad Programmer and Fast
Computer
Suppose n=106 numbers:
Merge sort: c n (lg n)
2
Bad programmer (c =50), high-language, ten
2
million/second computer B.
50 (106 lg 106) instructions/107 instructions
per second 100 seconds.
Thus, merge sort on B is 20 times faster than
insertion sort on A!
If sorting ten million numbers, 2.3 days VS.
20 minutes.
Conclusions:
Algorithms for solving the same problem
can differ dramatically in their efficiency.
much more significant than the
differences due to hardware and
software.
Theoretical Analysis
Uses a high-level description of the algorithm instead of an
implementation
Characterizes running time as a function of the input size, n.
Takes into account all (maximum) possible inputs
Evaluate the speed of an algorithm independent of the
hardware/software environment
Evaluation before implementation
Running Time
size of an array
# of elements in a matrix
- notation
n n
n0 n0
f(n) = ( g(n)) f(n) = O( g(n))
There exist positive constants c
such that there is a positive constant n0
such that …
f(n)
cg(n)
n0 n
f(n) = ( g(n)) 64
Notes on o-notation
if and only if
f (n) O( g (n)) and f (n) ( g (n)).
Example 2.
Because :
3n 2 2n 5 (n 2 )
3n 2 2n 5 O(n 2 )
Example 3.
Algorithm 1 Algorithm 2
Cost Cost
arr[0] = 0; c1 for(i=0; i<N; i++) c2
arr[1] = 0; c1 arr[i] = 0; c1
arr[2] = 0; c1
... ...
arr[N-1] = 0; c1
----------- -------------
c1+c1+...+c1 = c1 x N (N+1) x c2 + N x c1
=
78
(c2 + c1) x N + c2
Another Example
Algorithm 3 Cost
sum = 0; c1
for(i=0; i<N; i++) c2
for(j=0; j<N; j++) c2
sum += arr[i][j]; c3
------------
c1 + c2 x (N+1) + c2 x N x (N+1) + c3 x N2
79
Another Example
Rate of Growth
*
Million operations per second.
Decision
Only one solution
Answer in Yes / No
Optimization
Optimal
Multiple solutions
Maximization or Minimization problem
Finding Optimal
Conversion of optimal problem to
decision problem by bound
Applications: Resource allocation, scheduling, route
planning, etc.
Constraints: Real-world limitations that define feasible
solutions.
Optimality Problem
Maximality:
Concept: Finding the largest or most
desirable outcome.
Examples: Maximizing profit, efficiency, or
output.
Challenges:Avoiding local maxima, dealing
with multiple objectives.
Minimization
Minimality:
Concept: Finding the smallest or least costly
outcome.
Examples: Minimizing cost, time, or resource
usage.
Challenges: Balancing minimization with other
constraints or objectives.
Techniques for Solving
Optimization Problems:
Linear Programming
Dynamic Programming
Greedy Algorithms
Genetic Algorithms
Simulated Annealing
Important Points
Trade-offs:
Balancing multiple objectives (e.g., cost vs.
quality).
Pareto optimality: Solutions where improving one
objective worsens another.
Global vs. Local Optima:
Global optimum: Best solution across entire
solution space.
Local optimum: Best solution within a neighboring
set of solutions.
Challenge: Avoiding getting stuck in local optima.
Feasibility
Computationally feasible
For relative Large input, will algorithm stops in
reasonable time
Prime factorization
Tower of Hanoi
Slow problem
NP-hard problems: Many optimization problems are
computationally intensive.
How to solve them?
Chess
Cont..
Distributed Algorithm
Quntum computing
Approximation algorithms: Finding near-optimal
solutions efficiently.
Randomization