Analysis of Algorithms Desing Strategies
Analysis of Algorithms Desing Strategies
BSSE 4th
Instructor
Saqib Ameer
Algorithms Design Techniques
• No matter which programming language you use, it is important to
learn algorithm design techniques in data structures in order to be
able to build scalable systems.
• Selecting a proper design technique for algorithms is a complex but
important task. Following are some of the main algorithm design
techniques:
Design Techniques
• Brute-force or exhaustive search
• Divide and Conquer
• Greedy Algorithms
• Dynamic Programming
• Branch and Bound Algorithm
• Randomized Algorithm
• Backtracking
Brute force Approach
• Brute force is a straightforward approach to solving a problem ,
usually directly based on the problem statement and definitions of
the concept involved.
• In Simple Just do it
• The brute force method is by solving a particular problem by checking
all the possible cases which is slow.
Brute Force Approach
• For example, you are given a sorted numbers in an array and you have
to find a specific value. The brute force method is to make a for loop
and iterate through the elements of the array and eventually you will
see the digit you are looking for, however there are trade off cases
that in Computer Science we value.
• These are called worst case, best case, average case scenarios
Divide and Conquer
• In divide and conquer approach, the problem in hand, is divided into
smaller sub-problems and then each problem is solved independently.
When we keep on dividing the subproblems into even smaller sub-
problems, we may eventually reach a stage where no more division is
possible. Those "atomic" smallest possible sub-problem (fractions) are
solved. The solution of all sub-problems is finally merged in order to
obtain the solution of an original problem.
Divide and Conquer
Divide and Conquer
• Broadly, we can understand divide-and-conquer approach in a three-
step process.
Divide/Break
• This step involves breaking the problem into smaller sub-problems. Sub-
problems should represent a part of the original problem. This step
generally takes a recursive approach to divide the problem until no sub-
problem is further divisible.
Conquer/Solve
• This step receives a lot of smaller sub-problems to be solved. Generally,
at this level, the problems are considered 'solved' on their own.
Divide and Conquer
Merge/Combine
• When the smaller sub-problems are solved, this stage recursively combines them until
they formulate a solution of the original problem.
• Examples
The following computer algorithms are based on divide-and-conquer programming
approach −
• Merge Sort
• Quick Sort
• Binary Search
• Strassen's Matrix Multiplication
• Closest pair (points)
Greedy Approach
• An algorithm is designed to achieve optimum solution for a given
problem. In greedy algorithm approach, decisions are made from the
given solution domain. As being greedy, the closest solution that
seems to provide an optimum solution is chosen.
• Greedy algorithms try to find a localized optimum solution, which
may eventually lead to globally optimized solutions. However,
generally greedy algorithms do not provide globally optimized
solutions.
Examples of Greedy Approach
• Examples
• Most networking algorithms use the greedy approach. Here is a list of few of them −
1) Travelling Salesman Problem
2) Prim's Minimal Spanning Tree Algorithm
3) Kruskal's Minimal Spanning Tree Algorithm
4) Dijkstra's Minimal Spanning Tree Algorithm
5) Graph - Map Coloring
6) Graph - Vertex Cover
7) 0-1 Knapsack Problem
8) Job Scheduling Problem
Dynamic programming
• Dynamic programming approach is similar to divide and conquer in
breaking down the problem into smaller and yet smaller possible sub-
problems. But unlike, divide and conquer, these sub-problems are not
solved independently. Rather, results of these smaller sub-problems are
remembered and used for similar or overlapping sub-problems.
• Dynamic programming is used where we have problems, which can be
divided into similar sub-problems, so that their results can be re-used.
Mostly, these algorithms are used for optimization. Before solving the
in-hand sub-problem, dynamic algorithm will try to examine the results
of the previously solved sub-problems. The solutions of sub-problems
are combined in order to achieve the best solution.
Dynamic Programming
• In contrast to divide and conquer algorithms, where solutions are
combined to achieve an overall solution, dynamic algorithms use the
output of a smaller sub-problem and then try to optimize a bigger
sub-problem. Dynamic algorithms use Memoization to remember the
output of already solved sub-problems
Dynamic Programming Examples
• The following computer problems can be solved using dynamic
programming approach −
• Fibonacci number series 1 2 3 5 8 13 21 n = n -2 + n -1
• Knapsack problem bag = 20kg 10kg 12kg 2 kg 6kg 8kf 11kg 16kg