Algorithm Design Methods
Algorithm Design Methods
Methods
So you set all the numbers back to 0 and try them one by
one: 0001, 0002, 0003, and so on until it opens. In the
worst case scenario, it would take 104, or 10,000 tries to
find your combination.
To construct the solution in an optimal way, this algorithm creates two sets where one
set contains all the chosen items, and another set contains the rejected items.
A Greedy algorithm makes good local choices in the hope that the solution should be
either feasible or optimal.
A space state tree is a tree representing all the possible states (solution or nonsolution)
of the problem from the root as an initial state to the leaf as a terminal state.
Backtracking Algorithm
Backtrack(x)
if x is not a solution
return false
if x is a new solution
add to list of solutions
backtrack(expand x)
Problem: You want to find all the possible ways of arranging B1 and B2 and G on 3
benches. Constraint: G should not be on the middle bench.
Solution: There are a total of 3! = 6 possibilities. We will try all the possibilities and
get the possible solutions. We recursively try all the possibilities.
Randomized Quicksort
Atlantic City Algorithms
Las Vegas Algorithms
Computational Complexity
π Approximation
If any problem can be divided into sub-problems, which in turn are divided into smaller
sub-problems, and if there are overlapping among these sub-problems, then the
solutions to these sub-problems can be saved for future reference.
In this way, efficiency of the CPU can be enhanced. This method of solving a solution is
referred to as dynamic programming.
Such problems involve repeatedly calculating the value of the same sub-problems to
find the optimum solution.
Let's find the Fibonacci sequence up to 5th term. A Fibonacci series is the sequence of
numbers in which each number is the sum of the two preceding ones.
For example, 0,1,1, 2, 3. Here, each number is the sum of the two preceding numbers.
1. If n <= 1, return 1.
2. Else, return the sum of two preceding numbers.
Dynamic programming works by storing the result of sub-problems so that when their
solutions are required, they are at hand and we do not need to recalculate them.
var m = map(0 → 0, 1 → 1)
function fib(n)
if key n is not in map m
m[n] = fib(n − 1) + fib(n − 2)
return m[n]
Assignment