Professional Documents
Culture Documents
Assignment1 Algorithm Shariar (沙里尔)
Assignment1 Algorithm Shariar (沙里尔)
Introduction to algorithms
Classwork 1
Answer:
An Algorithm is a sequence of steps that describe how a problem can be solved.
Every computer program that ends with a result is basically based on an Algorithm.
Algorithms, however, are not just confined for use in computer programs, these can
also be used to solve mathematical problems and on many matters of day-to-day
life. Based on how they function, we can divide Algorithms into multiple types.
Types of algorithm
Well there are many types of algorithm but the most fundamental types of algorithm
are:
1. Recursive algorithms
2. Dynamic programming algorithm
3. Backtracking algorithm
4. Divide and conquer algorithm
5. Greedy algorithm
6. Brute Force algorithm
7. Randomized algorithm
1.Recursive algorithms:
This is one of the most interesting Algorithms as it calls itself with a smaller value as
inputs which it gets after solving for the current inputs. In more simpler words, It’s an
Algorithm that calls itself repeatedly until the problem is solved.
Example:
Fact(x)
return 1
These algorithms work by remembering the results of the past run and using them to
find new results. In other words, dynamic programming algorithm solves complex
problems by breaking it into multiple simple subproblems and then it solves each of
them once and then stores them for future use.
Example:
Fib(n)
if n=0
return 0
else
prev_Fib=0,curr_Fib=1
next_Fib=prev_Fib+curr_Fib
prev_Fib=curr_Fib
curr_Fib=new_Fib
return curr_Fib
3.Backtracking algorithm:
This is another effective way of solving many problems. In Divide and Conquer
algorithms, divide the algorithm into two parts, the first parts divides the problem on
hand into smaller subproblems of the same type. Then on the second part, these
smaller problems are solved and then added together (combined) to produce the
final solution of the problem.
5.Greedy algorithm
These algorithms are used for solving optimization problems. In this algorithm, we
find a locally optimum solution (without any regard for any consequence in future)
and hope to find the optimal solution at the global level.
Example:
This is one of the simplest algorithms in the concept. A brute force algorithm blindly
iterates all possible solutions to search one or more than one solution that may solve
a function. Think of brute force as using all possible combinations of numbers to
open a safe.
Example:
7.Randomized algorithm:
A randomized algorithm uses a random number at least once during the computation
to make a decision.
Exercise 2. Asymptotic Growth
Sort all the functions below in decreasing order of asymptotic (Big-O) growth. If some
have the same asymptotic growth, then be sure to indicate that. As usual, lg means
base 2.
1. n4
2. 4 log n
3. √𝑛 log4n
4. 5n
5. 4 log log n
6. (log n)5 log n
n
7. 5
8. nlog n
4
9. 4 n
n
10. 55
n
11. 44
n
𝑛
12. 4
4
4
13. 4 n
14. nlog n
n
15. 44
Solution:
𝑛
n n n 4 4
(𝑛⁄4) 4 > 5n > nlog n = nlog n > (log n)5 log n =
55 > 44 = 44 > 4n = 4n > (log
n)5 log n > n4 > 5n > √𝑛 log4n > 4 log n > 4 log log n
THE END