SlideShare a Scribd company logo
2
Most read
4
Most read
7
Most read
Willing is not enough, we must do
Bruce lee
Problem Statement:
In the subset-sum problem, we are given a finite set S of positive
integers and an integer target t > 0. We ask whether there exists a
subset S`⊆ S whose elements sum to t.
TRUE FALSE
t = 138457 TRUE
How to Design an Algorithm to Solve this Problem??
Brute Force??
Making all possible Subsets
A={1,2,4}
What will be its Complexity???
All possible subsets???
1. A’={}
2. A’={1}
3. A’={1,2}
4. A’={1,4}
5. A’={1,2,4}
6. A’={2}
7. A’={2,4}
8. A’={4}
SumOFSubset=2n=8
Brute Force Subset Sum Solution
Running time Q(n 2n )
What does this tell us about the time
complexity of the subset sum problem?
bool SumSub (w, i, j)
{
if (i == 0) return (j == 0);
else
if (SumSub (w, i-1, j)) return true;
else if ((j - wi) >= 0) return SumSub (w, i-1, j - wi);
else return false;
}
Complexity
0( 2n N)
All possible subsets and time required to sum them.
Brute Force??? No, thanks
Why is this Failing???
• Sometimes, the divide and conquer
approach seems appropriate but fails
to produce an efficient algorithm.
• One of the reasons is that D&Q
produces overlapping sub problems.
Dynamic Approach
Divide and conquer with Tabular form
Solves a sub-problem by making use of previously stored
solutions for all other sub problems.
HOW?????
Lets take an example
0 1 2 3 4 5 6 7 8 9 10 11
2
3
7
8
10
A={2,3,7,8,10} t=11
Any subset of { 2} making sum=0 ???
Lets take an example
0 1 2 3 4 5 6 7 8 9 10 11
2 T
3
7
8
10
A={2,3,7,8,10} t=11
Lets take an example
0 1 2 3 4 5 6 7 8 9 10 11
2 T F
3
7
8
10
A={2,3,7,8,10} t=11
Any subset of { 2} making sum=1 ???
Lets take an example
0 1 2 3 4 5 6 7 8 9 10 11
2 T F T
3
7
8
10
A={2,3,7,8,10} t=11
Any subset of { 2} making sum=3 ???
Lets take an example
0 1 2 3 4 5 6 7 8 9 10 11
2 T F T F F F F F F F F F
3 T F T T F T F F F F F F
7 T F T T F T F T F T T F
8 T F T T F T F T T T T T
10 T F T T F T F T T T T T
A={2,3,7,8,10} t=11
How to get the Answer??
8 is one of the Element
3 is one of the Element
As we have reached the Last row..
A’={8,3} is our Answer 
18
Dynamic Programming Algorithm
bool SumSub (w , n , m)
{
1. t[0,0] = true; for (j = 1 to m) t[0,j] = false;
2. for (i = 1 to n)
3. for (j = 0 to m)
4. t[i,j] = t[i-1,j];
5. if ((j-wi) >= 0)
6. t[i,j] = t[i-1,j] || t[i-1, j – wi];
7. return t[n,m];
}
i-1,
j - wi
i-1,
j
i , j
Its Complexity???
Time complexity is T(n) = O(m) + O(nm) = O(nm)
Application
Traveling Salesperson Problem
–Input: a graph of cities and roads
with distance connecting them and a
minimum total distant
–Output: either a path that visits each
with a cost less than the minimum,
or “no”.
• If given a path, easy to check if it
visits every city with less than
minimum distance travelled.
Drug Discovery Problem
–Input: a set of
proteins, a desired
3D shape
–Output: a sequence
of proteins that
produces the shape
(or impossible)
If given a sequence, easy to
check if sequence has the right shape.
THE END

More Related Content

PPT
0/1 knapsack
PPT
Floyd Warshall Algorithm
PDF
5. Basic Structure of SQL Queries.pdf
PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
PPTX
A presentation on prim's and kruskal's algorithm
PPTX
Birch Algorithm With Solved Example
PDF
Time and Space Complexity
PPTX
database Normalization
0/1 knapsack
Floyd Warshall Algorithm
5. Basic Structure of SQL Queries.pdf
Unit 1 chapter 1 Design and Analysis of Algorithms
A presentation on prim's and kruskal's algorithm
Birch Algorithm With Solved Example
Time and Space Complexity
database Normalization

What's hot (20)

PPT
Minimum spanning tree
PPT
Lecture 1 data structures and algorithms
PPT
Formal Specification in Software Engineering SE9
PDF
Lecture Notes-Finite State Automata for NLP.pdf
PPTX
Student result mamagement
PPTX
Component Based Software Engineering
PPTX
Functional dependancy
PPTX
Code Optimization
PPTX
Dijkstra's Algorithm
PDF
dbms lab manual
PPTX
All pair shortest path
PDF
Data base management system LAB MANUAL KCS 551.pdf
PPTX
library management system in SQL
DOC
Unit 3 daa
PPTX
Cursors, triggers, procedures
PPT
Graph traversal-BFS & DFS
PPTX
Daa unit 1
PPTX
SQL(DDL & DML)
PDF
Graph Based Clustering
PPT
BINARY TREE REPRESENTATION.ppt
Minimum spanning tree
Lecture 1 data structures and algorithms
Formal Specification in Software Engineering SE9
Lecture Notes-Finite State Automata for NLP.pdf
Student result mamagement
Component Based Software Engineering
Functional dependancy
Code Optimization
Dijkstra's Algorithm
dbms lab manual
All pair shortest path
Data base management system LAB MANUAL KCS 551.pdf
library management system in SQL
Unit 3 daa
Cursors, triggers, procedures
Graph traversal-BFS & DFS
Daa unit 1
SQL(DDL & DML)
Graph Based Clustering
BINARY TREE REPRESENTATION.ppt
Ad

Viewers also liked (20)

PPT
01 knapsack using backtracking
PPT
Backtracking
PPTX
Back tracking and branch and bound class 20
PDF
backtracking algorithms of ada
PPTX
8 queens problem using back tracking
PPTX
Backtracking
PDF
Solution 3.
PDF
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
PDF
Physical Computing and IoT
PPTX
Seminar report on android os
PPTX
(floyd's algm)
PDF
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
PPTX
Dynamic programming
PPT
lecture 30
PPT
Chap08alg
PDF
Solving The Shortest Path Tour Problem
PPT
21 backtracking
PDF
PPT
5.5 back track
PPTX
Matrix chain multiplication 2
01 knapsack using backtracking
Backtracking
Back tracking and branch and bound class 20
backtracking algorithms of ada
8 queens problem using back tracking
Backtracking
Solution 3.
All Pair Shortest Path Algorithm – Parallel Implementation and Analysis
Physical Computing and IoT
Seminar report on android os
(floyd's algm)
Randomized Algorithms in Linear Algebra & the Column Subset Selection Problem
Dynamic programming
lecture 30
Chap08alg
Solving The Shortest Path Tour Problem
21 backtracking
5.5 back track
Matrix chain multiplication 2
Ad

Similar to Subset sum problem Dynamic and Brute Force Approch (20)

PDF
Solution Manual for Data Structures and Algorithm Analysis in C++, 4/E 4th Ed...
PPT
Mergesort
PPT
Time complexity.ppt
PPT
Time complexity.pptr56435 erfgegr t 45t 35
PPT
how to calclute time complexity of algortihm
PPT
How to calculate complexity in Data Structure
PPTX
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
PDF
Data Structure: Algorithm and analysis
PPT
3. convolution fourier
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
PDF
Hull White model presentation
PDF
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
PDF
X01 Supervised learning problem linear regression one feature theorie
PDF
C1 - Insertion Sort
PPTX
Tutorial on EM algorithm – Part 2
PPTX
PPTX
Introduction to Algorithms
PDF
TableDrivenProg_20160714
PPT
Algorithm.ppt
PDF
0-1 knapsack problem
Solution Manual for Data Structures and Algorithm Analysis in C++, 4/E 4th Ed...
Mergesort
Time complexity.ppt
Time complexity.pptr56435 erfgegr t 45t 35
how to calclute time complexity of algortihm
How to calculate complexity in Data Structure
Time complexity.pptxghhhhhhhhhhhhhhhjjjjjjjjjjjjjjjjjjjjjjjjjj
Data Structure: Algorithm and analysis
3. convolution fourier
Algorithm And analysis Lecture 03& 04-time complexity.
Hull White model presentation
DOUBLE INTEGRAL.Introduction Numerical Problem Based on Lagrange’s Method of ...
X01 Supervised learning problem linear regression one feature theorie
C1 - Insertion Sort
Tutorial on EM algorithm – Part 2
Introduction to Algorithms
TableDrivenProg_20160714
Algorithm.ppt
0-1 knapsack problem

Recently uploaded (20)

PDF
Foundation of Data Science unit number two notes
PPTX
IB Computer Science - Internal Assessment.pptx
PPTX
Introduction to machine learning and Linear Models
PPTX
Introduction to Knowledge Engineering Part 1
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PDF
annual-report-2024-2025 original latest.
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
1_Introduction to advance data techniques.pptx
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PPTX
Database Infoormation System (DBIS).pptx
PPTX
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
PPTX
Business Acumen Training GuidePresentation.pptx
PPTX
Computer network topology notes for revision
PPTX
STUDY DESIGN details- Lt Col Maksud (21).pptx
PPTX
oil_refinery_comprehensive_20250804084928 (1).pptx
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Foundation of Data Science unit number two notes
IB Computer Science - Internal Assessment.pptx
Introduction to machine learning and Linear Models
Introduction to Knowledge Engineering Part 1
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
annual-report-2024-2025 original latest.
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Introduction-to-Cloud-ComputingFinal.pptx
1_Introduction to advance data techniques.pptx
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Database Infoormation System (DBIS).pptx
Microsoft-Fabric-Unifying-Analytics-for-the-Modern-Enterprise Solution.pptx
Business Acumen Training GuidePresentation.pptx
Computer network topology notes for revision
STUDY DESIGN details- Lt Col Maksud (21).pptx
oil_refinery_comprehensive_20250804084928 (1).pptx
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Reliability_Chapter_ presentation 1221.5784
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg

Subset sum problem Dynamic and Brute Force Approch

  • 1. Willing is not enough, we must do Bruce lee
  • 2. Problem Statement: In the subset-sum problem, we are given a finite set S of positive integers and an integer target t > 0. We ask whether there exists a subset S`⊆ S whose elements sum to t.
  • 3. TRUE FALSE t = 138457 TRUE How to Design an Algorithm to Solve this Problem??
  • 4. Brute Force?? Making all possible Subsets
  • 5. A={1,2,4} What will be its Complexity??? All possible subsets??? 1. A’={} 2. A’={1} 3. A’={1,2} 4. A’={1,4} 5. A’={1,2,4} 6. A’={2} 7. A’={2,4} 8. A’={4} SumOFSubset=2n=8
  • 6. Brute Force Subset Sum Solution Running time Q(n 2n ) What does this tell us about the time complexity of the subset sum problem? bool SumSub (w, i, j) { if (i == 0) return (j == 0); else if (SumSub (w, i-1, j)) return true; else if ((j - wi) >= 0) return SumSub (w, i-1, j - wi); else return false; }
  • 7. Complexity 0( 2n N) All possible subsets and time required to sum them. Brute Force??? No, thanks Why is this Failing???
  • 8. • Sometimes, the divide and conquer approach seems appropriate but fails to produce an efficient algorithm. • One of the reasons is that D&Q produces overlapping sub problems.
  • 9. Dynamic Approach Divide and conquer with Tabular form Solves a sub-problem by making use of previously stored solutions for all other sub problems. HOW?????
  • 10. Lets take an example 0 1 2 3 4 5 6 7 8 9 10 11 2 3 7 8 10 A={2,3,7,8,10} t=11 Any subset of { 2} making sum=0 ???
  • 11. Lets take an example 0 1 2 3 4 5 6 7 8 9 10 11 2 T 3 7 8 10 A={2,3,7,8,10} t=11
  • 12. Lets take an example 0 1 2 3 4 5 6 7 8 9 10 11 2 T F 3 7 8 10 A={2,3,7,8,10} t=11 Any subset of { 2} making sum=1 ???
  • 13. Lets take an example 0 1 2 3 4 5 6 7 8 9 10 11 2 T F T 3 7 8 10 A={2,3,7,8,10} t=11 Any subset of { 2} making sum=3 ???
  • 14. Lets take an example 0 1 2 3 4 5 6 7 8 9 10 11 2 T F T F F F F F F F F F 3 T F T T F T F F F F F F 7 T F T T F T F T F T T F 8 T F T T F T F T T T T T 10 T F T T F T F T T T T T A={2,3,7,8,10} t=11 How to get the Answer??
  • 15. 8 is one of the Element
  • 16. 3 is one of the Element
  • 17. As we have reached the Last row.. A’={8,3} is our Answer 
  • 18. 18 Dynamic Programming Algorithm bool SumSub (w , n , m) { 1. t[0,0] = true; for (j = 1 to m) t[0,j] = false; 2. for (i = 1 to n) 3. for (j = 0 to m) 4. t[i,j] = t[i-1,j]; 5. if ((j-wi) >= 0) 6. t[i,j] = t[i-1,j] || t[i-1, j – wi]; 7. return t[n,m]; } i-1, j - wi i-1, j i , j Its Complexity??? Time complexity is T(n) = O(m) + O(nm) = O(nm)
  • 19. Application Traveling Salesperson Problem –Input: a graph of cities and roads with distance connecting them and a minimum total distant –Output: either a path that visits each with a cost less than the minimum, or “no”. • If given a path, easy to check if it visits every city with less than minimum distance travelled.
  • 20. Drug Discovery Problem –Input: a set of proteins, a desired 3D shape –Output: a sequence of proteins that produces the shape (or impossible) If given a sequence, easy to check if sequence has the right shape.