SlideShare a Scribd company logo
Algorithms Design Approach/Patterns
1. DIVIDE AND CONQUER
2. Greedy Algorithms
3. Dynamic programing
4. BackTracking
5. Branch and Bound By
Name: Ashwin Shiv
Roll No:181210013
CSE 2nd Year
NIT Delhi
DIVIDE AND CONQUER
DIVIDE AND CONQUER -3 Part TOP
DOWN APPROCH
 TOP down Approach to design Algorithms
1) Divide : Divide the problem into sub problem that is similar to original problem
2) Conquer Solve the problem recursively
3) Combine : Combine the solution of sub problem to create the solution of
original problem
APPLICATION OF D&C ALOGORATHIM
 Binary Search
 Finding Max and Min
 Merge Sort
 Quick Sort
 Strassen Matrix Multiplication
 Convex hull
DIVIDE and CONQUER – APPROCH
ALGO DANDC (P)
{
if small (p) then return S(p)
else
{
divide P in smaller p1, p2...pk where k >=1
apply DANDC to each sub problem Recursively
return combine ( DANDC(p1), DANDC(p1) ……………… DANDC(pk))
}
}
Greedy Algorithms
Greedy Algorithms
 A greedy algorithm is an algorithm that constructs an object X one step at a time,
at each step choosing the locally best option.
 In some cases, greedy algorithms construct the globally best object by repeatedly
choosing the locally best option.
Greedy Algorithms – Advantages
Greedy algorithms have several advantages over other algorithmic approaches:
● Simplicity: Greedy algorithms are often easier to describe and code up than other
algorithms.
● Efficiency: Greedy algorithms can often be implemented more efficiently than other
algorithms.
Greedy Algorithms – DIS Advantages
Greedy algorithms have several drawbacks:
● Hard to design: Once you have found the right greedy approach, designing greedy
algorithms can be easy. However, finding the right approach can be hard.
● Hard to verify: Showing a greedy algorithm is correct often requires a nuanced
argument.
Greedy Algorithms – APPROCH
ALGO GREDDY ( a, n)
{
solution = 0;
for i to n do
{
x = select (a) ;
if feasible (solution , x ) ;
then solution Union (solution , x) ;
}
return solution;
}
Applications OF Greedy Algorithms
 Activity Selection
 Huffman coding
 Job Sequencing
 Knap Snap
 Minimum Spanning tree
 Single Source shortest Path
 Bellman ford Algorithm
 Dijkstra’s Algorithm
Dynamic Programming
Dynamic Programming
 Used to solve optimization problems.
1. Breaks down the complex problems into simpler subproblems.
2. Find optimal solution to these subproblems.
3. Store the results of these subproblems so that it can be reused later.
4. Finally Calculates the result of complex sub-problem.
Dynamic Programming– Advantages
 Time Complexity will be always less because it reduces the line code
 It speeds up the processing as we use previously calculated intermediate values.
Dynamic Programming– Disadvantages
 Space Complexity would be more as dividing problem in sub problem and storing
intermediate results in consuming memory.
APPLICATION OF Dynamic Programming
 Cutting a rod into pieces to Maximize Profit
 Matrix Chain Multiplication
 Longest Common Subsequence
 Optimal Binary Search Tree
 Travelling Salesman Problem
 0/1 Knapsack Problem
BackTracking
BackTracking
 Backtracing uses bruit force approach to solve the problem.
 The Algorithmic Approach – Backtracking systematically try and search
possibilities to find the solution.
 Brute force approach say for any given problem generate all possible solution and
pick a desire solution.
 Generate depth first search to generate state space tree.
BackTracking– Advantages
 Comparison with the Dynamic Programming, Backtracking Approach is more effective
in some cases.
 Backtracking Algorithm is the best option for solving tactical problem.
 Also Backtracking is effective for constraint satisfaction problem.
 In greedy Algorithm, getting the Global Optimal Solution is a long procedure and
depends on user statements but in Backtracking It Can Easily getable.
 Backtracking technique is simple to implement and easy to code.
 Different states are stored into stack so that the data or Info can be usable anytime.
 The accuracy is granted.
BackTracking– DisAdvantages
– Backtracking Approach is not efficient for solving strategic Problem.
– The overall runtime of Backtracking Algorithm is normally slow
– To solve Large Problem Sometime it needs to take the help of other techniques like
Branch and bound.
– Need Large amount of memory space for storing different state function in the
stack for big problem.
– Thrashing is one of the main problem of Backtracking. – The Basic Approach
Detects the conflicts too late.
BackTracking– APPROCH
Backtrack (v1,Vi)
{
If (V1,……., Vi) is a Solution Then
Return (V1,…, Vi)
For each v DO
If (V1,…….,Vi) is acceptable vector THEN
Solution = try (V1,…,Vi, V);
If Solution != () Then
RETURN Solution ;
}
}
}
APPLICATION OF BackTracking
 N Queen Problem
 Sum of subset problem
 Graph Coloring problem
 Hamiltonian Circuit Problem
 Maze Problem
Branch and Bound
Branch and Bound
 Branch and bound is an algorithm design paradigm which is generally used for solving
combinatorial optimization problems.
 These problems are typically exponential in terms of time complexity and may require exploring all
possible permutations in worst case.
 The Branch and Bound Algorithm technique solves these problems relatively quickly.
 BFS (Brute-Force)We can perform a Breadth-first search on the state space tree. This always
finds a goal state nearest to the root. But no matter what the initial state is, the algorithm attempts
the same sequence of moves like Depth-first-search DFS.
 Branch and Bound. The search for an answer node can often be speeded by using an “intelligent”
ranking function, also called an approximate cost function to avoid searching in sub-trees that do
not contain an answer node. It is similar to the backtracking technique but uses BFS-like search
Branch and Bound– APPROCH
 FIFO Branch Bound ( First IN First OUT)
 LIFO Branch Bound ( Last IN First OUT)
 LC Branch Bound ( Least Cost )
APPLICATION OF Branch and Bound
 0/1 Knapsack Problem using Least Cost Branch and Bound Algorithm
 Travelling Salesman Problem using Branch and Bound
0/1 Knapsack Problem using Dynamic
Programming
• In these kinds of problems,we are given some items with weights and profits.
• The 0/1 denotes that either you will not pick the item(0) or you can pick the item completely(1).
• Cannot take a fractional amount of an item taken or take an item more than once.
• It cannot be solved by the Greedy Approach because it is enable to fill the knapsack to capacity.
• In simple words,0/1 Knapsack Problem means that we want to pack n items in a bag(knapsack):
1. The ith item is worth pi(profit) and weight wi kg.
2. Take as valuable a load as possible, but cannot exceed W pounds.
3. pi,wi,W are integers.
Why Dynamic Programming to solve 0/1
Knapsack Problem?
 It is a very helpful problem in combinatorics.
 Recomputation is avoided as it stores the result of previous subproblem so that it
can be used again in solving another subproblem.
Algorithm
Knapsack(n,cw)
1. int M[][] = new int[n + 1][cw + 1];//Build a memoization matrix in bottom up manner.
2. for i<-0 to n
3. for w<-0 to cw
4. if (i == 0 || w == 0) then
5. M[i][w] = 0;
6. else if(gwt[i - 1]<= w) then
7. M[i][w] = max(pro[i - 1] + M[i - 1][w - gwt[i - 1]], M[i - 1][w])
8. else
9. M[i][w] = M[i - 1][w]
10. return M[n][cw]
Code in Java to solve 0-1 Knapsack problem Solution
using Dynamic Programming to get Maximum Profit
My Java
Program
0/1 Knapsack Problem using Dynamic Programming
Algorithms Design Patterns

More Related Content

PPTX
An overview of gradient descent optimization algorithms
PPTX
Problem reduction AND OR GRAPH & AO* algorithm.ppt
PPTX
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
PPTX
Mycin presentation
PPTX
Alpha-beta pruning (Artificial Intelligence)
PPT
Backtracking
PPTX
Local search algorithms
PPT
Hill climbing
An overview of gradient descent optimization algorithms
Problem reduction AND OR GRAPH & AO* algorithm.ppt
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
Mycin presentation
Alpha-beta pruning (Artificial Intelligence)
Backtracking
Local search algorithms
Hill climbing

What's hot (20)

PPT
Artificial Intelligence -- Search Algorithms
PPT
SINGLE-SOURCE SHORTEST PATHS
PPTX
Relationship Between Cybercrime and Information Security
PPTX
Multi threaded programming
PPT
Fundamentals of the Analysis of Algorithm Efficiency
PPTX
Operator precedance parsing
PPT
Parallel Computing
PPTX
Travelling salesman dynamic programming
DOCX
15CS562 AI VTU Question paper
PPTX
Asymptotic Notation and Data Structures
PDF
Planning Agent
PPT
Branch and bound
PPTX
phases of algorithm
PDF
Adnan: Introduction to Natural Language Processing
PPT
Priority scheduling algorithms
PPTX
Dijkstra's Algorithm
PPTX
JDBC ppt
PPT
5.1 greedy
PPTX
Introduction to Soft Computing
Artificial Intelligence -- Search Algorithms
SINGLE-SOURCE SHORTEST PATHS
Relationship Between Cybercrime and Information Security
Multi threaded programming
Fundamentals of the Analysis of Algorithm Efficiency
Operator precedance parsing
Parallel Computing
Travelling salesman dynamic programming
15CS562 AI VTU Question paper
Asymptotic Notation and Data Structures
Planning Agent
Branch and bound
phases of algorithm
Adnan: Introduction to Natural Language Processing
Priority scheduling algorithms
Dijkstra's Algorithm
JDBC ppt
5.1 greedy
Introduction to Soft Computing
Ad

Similar to Algorithms Design Patterns (20)

PPTX
Applied Algorithms Introduction to Algorithms.pptx
PPTX
Dynamic programming, Branch and bound algorithm & Greedy algorithms
PDF
heuristic search Techniques and game playing.pdf
PPTX
Comparitive Analysis of Algorithm strategies
PPT
Optimization problems
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPT
Algorithm designs and its technique.ppt
PPT
ADT(Algorithm Design Technique Backtracking algorithm).ppt
PPTX
esign and Analysis of Algorithms Presentation.pptx
PPTX
Design Algorithms - - Backtracking.pptx
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPT
35 algorithm-types
PPT
35-algorithm-types.ppt
PPT
35 algorithm-types
PPT
algorithm-types.ppt
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPT
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
PPT
Algorithm types
PPT
Greedy_Backtracking graph coloring.ppt
PPTX
Types of algorithms
Applied Algorithms Introduction to Algorithms.pptx
Dynamic programming, Branch and bound algorithm & Greedy algorithms
heuristic search Techniques and game playing.pdf
Comparitive Analysis of Algorithm strategies
Optimization problems
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Algorithm designs and its technique.ppt
ADT(Algorithm Design Technique Backtracking algorithm).ppt
esign and Analysis of Algorithms Presentation.pptx
Design Algorithms - - Backtracking.pptx
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
35 algorithm-types
35-algorithm-types.ppt
35 algorithm-types
algorithm-types.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Parallel_Algorithms_In_Combinatorial_Optimization_Problems.ppt
Algorithm types
Greedy_Backtracking graph coloring.ppt
Types of algorithms
Ad

Recently uploaded (20)

PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
A Presentation on Artificial Intelligence
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Big Data Technologies - Introduction.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Machine Learning_overview_presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Encapsulation theory and applications.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
A Presentation on Artificial Intelligence
The AUB Centre for AI in Media Proposal.docx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
Big Data Technologies - Introduction.pptx
Spectral efficient network and resource selection model in 5G networks
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine Learning_overview_presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Encapsulation_ Review paper, used for researhc scholars
Encapsulation theory and applications.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx

Algorithms Design Patterns

  • 1. Algorithms Design Approach/Patterns 1. DIVIDE AND CONQUER 2. Greedy Algorithms 3. Dynamic programing 4. BackTracking 5. Branch and Bound By Name: Ashwin Shiv Roll No:181210013 CSE 2nd Year NIT Delhi
  • 3. DIVIDE AND CONQUER -3 Part TOP DOWN APPROCH  TOP down Approach to design Algorithms 1) Divide : Divide the problem into sub problem that is similar to original problem 2) Conquer Solve the problem recursively 3) Combine : Combine the solution of sub problem to create the solution of original problem
  • 4. APPLICATION OF D&C ALOGORATHIM  Binary Search  Finding Max and Min  Merge Sort  Quick Sort  Strassen Matrix Multiplication  Convex hull
  • 5. DIVIDE and CONQUER – APPROCH ALGO DANDC (P) { if small (p) then return S(p) else { divide P in smaller p1, p2...pk where k >=1 apply DANDC to each sub problem Recursively return combine ( DANDC(p1), DANDC(p1) ……………… DANDC(pk)) } }
  • 7. Greedy Algorithms  A greedy algorithm is an algorithm that constructs an object X one step at a time, at each step choosing the locally best option.  In some cases, greedy algorithms construct the globally best object by repeatedly choosing the locally best option.
  • 8. Greedy Algorithms – Advantages Greedy algorithms have several advantages over other algorithmic approaches: ● Simplicity: Greedy algorithms are often easier to describe and code up than other algorithms. ● Efficiency: Greedy algorithms can often be implemented more efficiently than other algorithms.
  • 9. Greedy Algorithms – DIS Advantages Greedy algorithms have several drawbacks: ● Hard to design: Once you have found the right greedy approach, designing greedy algorithms can be easy. However, finding the right approach can be hard. ● Hard to verify: Showing a greedy algorithm is correct often requires a nuanced argument.
  • 10. Greedy Algorithms – APPROCH ALGO GREDDY ( a, n) { solution = 0; for i to n do { x = select (a) ; if feasible (solution , x ) ; then solution Union (solution , x) ; } return solution; }
  • 11. Applications OF Greedy Algorithms  Activity Selection  Huffman coding  Job Sequencing  Knap Snap  Minimum Spanning tree  Single Source shortest Path  Bellman ford Algorithm  Dijkstra’s Algorithm
  • 13. Dynamic Programming  Used to solve optimization problems. 1. Breaks down the complex problems into simpler subproblems. 2. Find optimal solution to these subproblems. 3. Store the results of these subproblems so that it can be reused later. 4. Finally Calculates the result of complex sub-problem.
  • 14. Dynamic Programming– Advantages  Time Complexity will be always less because it reduces the line code  It speeds up the processing as we use previously calculated intermediate values.
  • 15. Dynamic Programming– Disadvantages  Space Complexity would be more as dividing problem in sub problem and storing intermediate results in consuming memory.
  • 16. APPLICATION OF Dynamic Programming  Cutting a rod into pieces to Maximize Profit  Matrix Chain Multiplication  Longest Common Subsequence  Optimal Binary Search Tree  Travelling Salesman Problem  0/1 Knapsack Problem
  • 18. BackTracking  Backtracing uses bruit force approach to solve the problem.  The Algorithmic Approach – Backtracking systematically try and search possibilities to find the solution.  Brute force approach say for any given problem generate all possible solution and pick a desire solution.  Generate depth first search to generate state space tree.
  • 19. BackTracking– Advantages  Comparison with the Dynamic Programming, Backtracking Approach is more effective in some cases.  Backtracking Algorithm is the best option for solving tactical problem.  Also Backtracking is effective for constraint satisfaction problem.  In greedy Algorithm, getting the Global Optimal Solution is a long procedure and depends on user statements but in Backtracking It Can Easily getable.  Backtracking technique is simple to implement and easy to code.  Different states are stored into stack so that the data or Info can be usable anytime.  The accuracy is granted.
  • 20. BackTracking– DisAdvantages – Backtracking Approach is not efficient for solving strategic Problem. – The overall runtime of Backtracking Algorithm is normally slow – To solve Large Problem Sometime it needs to take the help of other techniques like Branch and bound. – Need Large amount of memory space for storing different state function in the stack for big problem. – Thrashing is one of the main problem of Backtracking. – The Basic Approach Detects the conflicts too late.
  • 21. BackTracking– APPROCH Backtrack (v1,Vi) { If (V1,……., Vi) is a Solution Then Return (V1,…, Vi) For each v DO If (V1,…….,Vi) is acceptable vector THEN Solution = try (V1,…,Vi, V); If Solution != () Then RETURN Solution ; } } }
  • 22. APPLICATION OF BackTracking  N Queen Problem  Sum of subset problem  Graph Coloring problem  Hamiltonian Circuit Problem  Maze Problem
  • 24. Branch and Bound  Branch and bound is an algorithm design paradigm which is generally used for solving combinatorial optimization problems.  These problems are typically exponential in terms of time complexity and may require exploring all possible permutations in worst case.  The Branch and Bound Algorithm technique solves these problems relatively quickly.  BFS (Brute-Force)We can perform a Breadth-first search on the state space tree. This always finds a goal state nearest to the root. But no matter what the initial state is, the algorithm attempts the same sequence of moves like Depth-first-search DFS.  Branch and Bound. The search for an answer node can often be speeded by using an “intelligent” ranking function, also called an approximate cost function to avoid searching in sub-trees that do not contain an answer node. It is similar to the backtracking technique but uses BFS-like search
  • 25. Branch and Bound– APPROCH  FIFO Branch Bound ( First IN First OUT)  LIFO Branch Bound ( Last IN First OUT)  LC Branch Bound ( Least Cost )
  • 26. APPLICATION OF Branch and Bound  0/1 Knapsack Problem using Least Cost Branch and Bound Algorithm  Travelling Salesman Problem using Branch and Bound
  • 27. 0/1 Knapsack Problem using Dynamic Programming • In these kinds of problems,we are given some items with weights and profits. • The 0/1 denotes that either you will not pick the item(0) or you can pick the item completely(1). • Cannot take a fractional amount of an item taken or take an item more than once. • It cannot be solved by the Greedy Approach because it is enable to fill the knapsack to capacity. • In simple words,0/1 Knapsack Problem means that we want to pack n items in a bag(knapsack): 1. The ith item is worth pi(profit) and weight wi kg. 2. Take as valuable a load as possible, but cannot exceed W pounds. 3. pi,wi,W are integers.
  • 28. Why Dynamic Programming to solve 0/1 Knapsack Problem?  It is a very helpful problem in combinatorics.  Recomputation is avoided as it stores the result of previous subproblem so that it can be used again in solving another subproblem.
  • 29. Algorithm Knapsack(n,cw) 1. int M[][] = new int[n + 1][cw + 1];//Build a memoization matrix in bottom up manner. 2. for i<-0 to n 3. for w<-0 to cw 4. if (i == 0 || w == 0) then 5. M[i][w] = 0; 6. else if(gwt[i - 1]<= w) then 7. M[i][w] = max(pro[i - 1] + M[i - 1][w - gwt[i - 1]], M[i - 1][w]) 8. else 9. M[i][w] = M[i - 1][w] 10. return M[n][cw]
  • 30. Code in Java to solve 0-1 Knapsack problem Solution using Dynamic Programming to get Maximum Profit My Java Program 0/1 Knapsack Problem using Dynamic Programming