SlideShare a Scribd company logo
DEPARTMENT OF COMPUTER SCIENCE &
INFORMATION TECHOLOGY
V.S PRIYADHARSHINI
I.MSC(CS)
DATA STRUCTURES AND ALGORITHMS
TOPIC: SUM OF SUBSET ,GRAPH COLORS
SUM OF SUBSET
Subset Sum Problem:
It is one of the most important problems in complexity
theory. The problem is given an A set of integers a1,
a2,…., an upto n integers. The question arises that is
there a non-empty subset such that the sum of the
subset is given as M integer?. For example, the set is
given as [5, 2, 1, 3, 9], and the sum of the subset is 9;
the answer is YES as the sum of the subset [5, 3, 1] is
equal to 9. This is an NP-complete problem again. It is
the special case of knapsack
SUM OF SUBSET
Let's understand this problem through an example:
 problem.
 We have a set of 5 integers given below:
N = 4, -2, 2, 3, 1
 We want to find out the subset whose sum is equal to 5. There
are many solutions to this problem.
 The naïve approach, i.e., brute-force search generates all the
possible subsets of the original array, i.e., there are 2n possible
states. Here the running time complexity would be exponential.
Then, we consider all these subsets in O(N) linear running time
and checks whether the sum of the items is M or not.
 The dynamic programming has pseudo-polynomial running
time.
SUM OF SUBSET
Statement: Given a set of positive integers, and a value sum, determine that the
sum of the subset of a given set is equal to the given sum.
Or
Given an array of integers and a sum, the task is to have all subsets of given
array with sum equal to the given sum.
Example 1:
Input: set[] = {4, 16, 5, 23, 12}, sum = 9
Output = true
Subset {4, 5} has the sum equal to 9.
Example 2:
Input: set[] = {2, 3, 5, 6, 8, 10}, sum = 10
Output = true
There are three possible subsets that have the sum equal to 10.
Subset1: {5, 2, 3}
Subset2: {2, 8}
Subset3: {10}
SUM OF SUBSET
There are two ways of solving the subset problem:
 Recursion
 Dynamic programming
Method 1: Recursion
 Before knowing about the recursive approach, we should know about two
things in a subset which are given below:
 Include: Here include means that we are selecting the element from the array.
 Exclude: Here, exclude means that we are rejecting the element from the array.
To implement the recursive approach, we consider the following two cases:
 Now we consider the first element and now the required sum is equal to the
difference between the target sum and value of first element. The number of
elements is equal to the difference between the total elements and 1.
 Leave the 'first' element and now the required sum = target sum. The number
of elements is equal to the difference between the total elements and 1.
Let's understand that how can we solve the problem
using recursion. Consider the array which is given
below:
arr = [3, 4, 5, 2]
sum = 9
result = []
In the above example, we have taken an array, and the
empty array named result that stores all the values
whose resultant sum is equal to 9.
First element in an array is 3. There are two
scenarios:
First scenario is select. The sum is equal to the target
sum - value of first element, i.e., 9 - 3 = 6 and the
first element, i.e., 3 gets stored in the result array, i.e.,
result[].
Second scenario is reject. The array arr contains the
elements 4, 5, 2, i.e., arr = [4, 5, 2] and sum would be
same as 9 as we are rejecting the element 3. The
result[] array would remain empty.
Now we perform the same select and reject operation
on element 4 as it is the first element of the array now.
Select the element 4 from the array. Since we are selecting 4
from the array so array arr would contain the elements 5, 2,
i.e., arr = [5, 2]. The sum is equal to the 6-4 = 2 and the
element 4 gets stored in the result arr. The result[] = {3, 4}.
Reject the element 4 from the array. Since we are rejecting
the 4 from the array so array arr would contain the
elements 5, 2, i.e., arr = [5, 2]. The sum would remain same
as 6 and the result array would be same as previous, i.e., {3}.
Now we perform the select and reject operation on element 5.
Select the element 5 from the array. Since we are selecting 5 from the
array so array arr would contain the elements 2, i.e., arr = [2]. The sum
is equal to the 2 - 5 equals to -3 and the element 5 gets stored in the
result arr. The result[] = {3, 4, 5}. Reject the element 5 from the array.
Since we are rejecting 5 from the array so array arr would contain the
element 2, i.e., arr = [2]. The sum would remain same as previous, i.e., 6
and the result array would be same as previous, i.e., {3, 4}.
 If we observe S-5, we can see
that the sum is negative that
returns false. It means that there
is no further subset available in
the set.
Consider R-5. It also has two
scenarios:
 Select the element 2 from the
array. Once the element 2 gets
selected, the array becomes
empty, i.e., arr[] = " ". The sum
would be 2-2 equals to 0 and the
element 2 gets stored in the
result array. The result[] = [3, 4,
2].
 Reject the element 2 from the
array. Once the element 2 gets
rejected, the array becomes
empty, i.e., arr[] = " ". The sum
would be same as previous, i.e.,
2 and the result array would also
be same as previous, i.e., [3, 4].
Consider R-4. It has two
scenarios:
 Select the element 5 from the
array. Since we are selecting 5
from the array so array arr
would contain the elements
2, i.e., arr = [2]. The sum
would be 6-5 equals to 1 and
the element 5 gets stored in
the result array. The result[] =
[3, 5].
 Reject the element 5 from the
array. Since we are rejecting 5
from the array so array arr
would contain the element 2,
i.e., arr = [2]. The sum would
remain same as previous, i.e.,
6 and the result array would
be same as previous, i.e., {3}.
Consider S-5. It has two
scenarios:
 Select the element 2 from
the array. Since we are
selecting 2 from the array
so array arr would be
empty, i.e., arr = " ". The
sum would be 1-2 equals to
-1 and the element 2 gets
stored in the result array.
The result[] = [3, 5, 2].
 Reject the element 2 from
the array. Since we are
rejecting 2 from the array
so array arr would become
empty. The sum would
remain same as previous,
i.e., 1 and the result array
would be same as previous,
i.e., {3, 5}.
Consider R-5. It has two
scenarios:
 Select the element 2 from the
array. Since we are selecting 2
from the array so array arr
would be empty, i.e., arr = " ".
The sum would be 6-2 equals
to 4 and the element 2 gets
stored in the result array. The
result[] = [3, 2].
 Reject the element 2 from the
array. Since we are rejecting 2
from the array so array arr
would become empty. The
sum would remain same as
previous, i.e., 6 and the result
array would be same as
previous, i.e., {3}.
Similarly, we get the reject case,
i.e., R-3 as shown as below:
Graph coloring
Graph:
A graph is a unique data structure in programming that
consists of finite sets of nodes or vertices and a set of edges that
connect these vertices to them. At this moment, adjacent
vertices can be called those vertices that are connected to the
same edge with each other. In simple terms, a graph is a visual
representation of vertices and edges sharing some connection or
relationship. Although there are plenty of graph algorithms that
you might have been familiar with, only some of them are put to
use. The reason for this is simple as the standard graph
algorithms are designed in such a way to solve millions of
problems with just a few lines of logically coded technique. To
some extent, one perfect algorithm is solely optimized to achieve
such efficient results.
Graph coloring
Types of Graphs:
There are various types of graph algorithms that you would be looking
at in this article but before that, let's look at some types of terms to
imply the fundamental variations between them.
 Order: Order defines the total number of vertices present in the graph.
 Size: Size defines the number of edges present in the graph.
 Self-loop: It is the edges that are connected from a vertex to itself.
 Isolated vertex: It is the vertex that is not connected to any other
vertices in the graph.
 Vertex degree: It is defined as the number of edges incident to a vertex
in a graph.
 Weighted graph: A graph having value or weight of vertices.
 Unweighted graph: A graph having no value or weight of vertices.
 Directed graph: A graph having a direction indicator.
 Undirected graph: A graph where no directions are defined.
Data structure algorithm
Graph coloring
Graph coloring:
Graph coloring algorithms follow the approach of
assigning colors to the elements present in the graph under
certain conditions. The conditions are based on the
techniques or algorithms. Hence, vertex coloring is a
commonly used coloring technique followed here. First, in
this method, you try to color the vertex using k color,
ensuring that two adjacent vertexes should not have the
same color. Other method includes face coloring and edge
coloring. Both of these methods should also ensure that no
edge or face should be inconsequent color. The coloring of
the graph is determined by knowing the chromatic
number, which is also the smaller number of colors
needed.
Data structure algorithm
Data structure algorithm

More Related Content

PPTX
Relations & functions.pps
PPTX
Relations and functions
PPT
Relations and Functions
PPT
Relations and functions
PDF
Relations, functions, and their graphs
PDF
PPTX
Relations
PPT
Relations and functions
Relations & functions.pps
Relations and functions
Relations and Functions
Relations and functions
Relations, functions, and their graphs
Relations
Relations and functions

What's hot (19)

PPTX
PPTX
Relations and functions
PPT
PPT
Discrete Math Ch5 counting + proofs
PDF
Calculus - Functions Review
PPTX
Discrete Math Chapter 2: Basic Structures: Sets, Functions, Sequences, Sums, ...
DOCX
Yun math
PPT
Relations and functions
PPTX
Discrete Structure Mathematics lecture 1
PPTX
Discrete mathematic
PPT
Array
PPTX
Lesson 11: Functions and Function Notation
PPTX
PPTX
Sets, functions and groups
PDF
Discrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
PPT
PPTX
Relations in Discrete Math
PPTX
Advance algebra
PDF
3.3 Zeros of Polynomial Functions
Relations and functions
Discrete Math Ch5 counting + proofs
Calculus - Functions Review
Discrete Math Chapter 2: Basic Structures: Sets, Functions, Sequences, Sums, ...
Yun math
Relations and functions
Discrete Structure Mathematics lecture 1
Discrete mathematic
Array
Lesson 11: Functions and Function Notation
Sets, functions and groups
Discrete Mathematics and Its Applications 7th Edition Rose Solutions Manual
Relations in Discrete Math
Advance algebra
3.3 Zeros of Polynomial Functions
Ad

Similar to Data structure algorithm (20)

PPT
Array 31.8.2020 updated
PPTX
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
PPTX
1. Real Numbers and Integer Exponent.pptx
PDF
PDF
Square Root Decomposition
PPT
PDF
Data structures arrays
PPTX
CSE115 C Programming Multidimensional Array Introduction
PDF
Introduction to Arrays in C
PPT
Computational skills
PDF
CE344L-200365-Lab2.pdf
PDF
IUA Unidad 3 - Actividad 4 - Niveyro - Alarcón
DOCX
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
PDF
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPTX
Practice exercises of online Judges related to array
PPTX
Marh algebra lesson
PDF
Unit ii data structure-converted
PPTX
Theory of Computation "Chapter 1, introduction"
Array 31.8.2020 updated
BASIC OF ALGORITHM AND MATHEMATICS STUDENTS
1. Real Numbers and Integer Exponent.pptx
Square Root Decomposition
Data structures arrays
CSE115 C Programming Multidimensional Array Introduction
Introduction to Arrays in C
Computational skills
CE344L-200365-Lab2.pdf
IUA Unidad 3 - Actividad 4 - Niveyro - Alarcón
PPS 7.7 RECURSION, AS A DIFFERENT WAY OF SOLVING PROBLEMS. EXAMPLE PROGRAMS
Comparative analysis of x^3+y^3=z^3 and x^2+y^2=z^2 in the Interconnected Sets
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Practice exercises of online Judges related to array
Marh algebra lesson
Unit ii data structure-converted
Theory of Computation "Chapter 1, introduction"
Ad

More from PriyadharshiniVS (20)

PPTX
R Ramya devi digital image processing
PPTX
R ramya devi cloud computing
PPTX
Ramya devi R women empowerment
PPTX
R Ramya devi artificial intelligence and machine learning
PPTX
Ramya devi R internet of things
PPTX
PPTX
PPTX
PPTX
PPTX
MOBILE APP DEVELOPMENT USING PYTHON
PPTX
Vanmathy python
PPTX
PriyaDharshini distributed operating system
PPTX
Vanmathy distributed operating system
PPTX
vanmathy cryptography network security
PPTX
vanmathy cryptography network security
PPTX
Vanmathy distributed operating system
PPTX
Vanmathy no sql
PPTX
Cryptography and network security
PPTX
PPTX
Graph, graph and network modeling, advantage of graph database
R Ramya devi digital image processing
R ramya devi cloud computing
Ramya devi R women empowerment
R Ramya devi artificial intelligence and machine learning
Ramya devi R internet of things
MOBILE APP DEVELOPMENT USING PYTHON
Vanmathy python
PriyaDharshini distributed operating system
Vanmathy distributed operating system
vanmathy cryptography network security
vanmathy cryptography network security
Vanmathy distributed operating system
Vanmathy no sql
Cryptography and network security
Graph, graph and network modeling, advantage of graph database

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PPTX
Institutional Correction lecture only . . .
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Classroom Observation Tools for Teachers
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
Institutional Correction lecture only . . .
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GDM (1) (1).pptx small presentation for students
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Supply Chain Operations Speaking Notes -ICLT Program
O7-L3 Supply Chain Operations - ICLT Program
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
Anesthesia in Laparoscopic Surgery in India
Chinmaya Tiranga quiz Grand Finale.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Classroom Observation Tools for Teachers
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx

Data structure algorithm

  • 1. DEPARTMENT OF COMPUTER SCIENCE & INFORMATION TECHOLOGY V.S PRIYADHARSHINI I.MSC(CS)
  • 2. DATA STRUCTURES AND ALGORITHMS TOPIC: SUM OF SUBSET ,GRAPH COLORS
  • 3. SUM OF SUBSET Subset Sum Problem: It is one of the most important problems in complexity theory. The problem is given an A set of integers a1, a2,…., an upto n integers. The question arises that is there a non-empty subset such that the sum of the subset is given as M integer?. For example, the set is given as [5, 2, 1, 3, 9], and the sum of the subset is 9; the answer is YES as the sum of the subset [5, 3, 1] is equal to 9. This is an NP-complete problem again. It is the special case of knapsack
  • 4. SUM OF SUBSET Let's understand this problem through an example:  problem.  We have a set of 5 integers given below: N = 4, -2, 2, 3, 1  We want to find out the subset whose sum is equal to 5. There are many solutions to this problem.  The naïve approach, i.e., brute-force search generates all the possible subsets of the original array, i.e., there are 2n possible states. Here the running time complexity would be exponential. Then, we consider all these subsets in O(N) linear running time and checks whether the sum of the items is M or not.  The dynamic programming has pseudo-polynomial running time.
  • 5. SUM OF SUBSET Statement: Given a set of positive integers, and a value sum, determine that the sum of the subset of a given set is equal to the given sum. Or Given an array of integers and a sum, the task is to have all subsets of given array with sum equal to the given sum. Example 1: Input: set[] = {4, 16, 5, 23, 12}, sum = 9 Output = true Subset {4, 5} has the sum equal to 9. Example 2: Input: set[] = {2, 3, 5, 6, 8, 10}, sum = 10 Output = true There are three possible subsets that have the sum equal to 10. Subset1: {5, 2, 3} Subset2: {2, 8} Subset3: {10}
  • 6. SUM OF SUBSET There are two ways of solving the subset problem:  Recursion  Dynamic programming Method 1: Recursion  Before knowing about the recursive approach, we should know about two things in a subset which are given below:  Include: Here include means that we are selecting the element from the array.  Exclude: Here, exclude means that we are rejecting the element from the array. To implement the recursive approach, we consider the following two cases:  Now we consider the first element and now the required sum is equal to the difference between the target sum and value of first element. The number of elements is equal to the difference between the total elements and 1.  Leave the 'first' element and now the required sum = target sum. The number of elements is equal to the difference between the total elements and 1.
  • 7. Let's understand that how can we solve the problem using recursion. Consider the array which is given below: arr = [3, 4, 5, 2] sum = 9 result = [] In the above example, we have taken an array, and the empty array named result that stores all the values whose resultant sum is equal to 9.
  • 8. First element in an array is 3. There are two scenarios: First scenario is select. The sum is equal to the target sum - value of first element, i.e., 9 - 3 = 6 and the first element, i.e., 3 gets stored in the result array, i.e., result[]. Second scenario is reject. The array arr contains the elements 4, 5, 2, i.e., arr = [4, 5, 2] and sum would be same as 9 as we are rejecting the element 3. The result[] array would remain empty.
  • 9. Now we perform the same select and reject operation on element 4 as it is the first element of the array now. Select the element 4 from the array. Since we are selecting 4 from the array so array arr would contain the elements 5, 2, i.e., arr = [5, 2]. The sum is equal to the 6-4 = 2 and the element 4 gets stored in the result arr. The result[] = {3, 4}. Reject the element 4 from the array. Since we are rejecting the 4 from the array so array arr would contain the elements 5, 2, i.e., arr = [5, 2]. The sum would remain same as 6 and the result array would be same as previous, i.e., {3}.
  • 10. Now we perform the select and reject operation on element 5. Select the element 5 from the array. Since we are selecting 5 from the array so array arr would contain the elements 2, i.e., arr = [2]. The sum is equal to the 2 - 5 equals to -3 and the element 5 gets stored in the result arr. The result[] = {3, 4, 5}. Reject the element 5 from the array. Since we are rejecting 5 from the array so array arr would contain the element 2, i.e., arr = [2]. The sum would remain same as previous, i.e., 6 and the result array would be same as previous, i.e., {3, 4}.
  • 11.  If we observe S-5, we can see that the sum is negative that returns false. It means that there is no further subset available in the set. Consider R-5. It also has two scenarios:  Select the element 2 from the array. Once the element 2 gets selected, the array becomes empty, i.e., arr[] = " ". The sum would be 2-2 equals to 0 and the element 2 gets stored in the result array. The result[] = [3, 4, 2].  Reject the element 2 from the array. Once the element 2 gets rejected, the array becomes empty, i.e., arr[] = " ". The sum would be same as previous, i.e., 2 and the result array would also be same as previous, i.e., [3, 4].
  • 12. Consider R-4. It has two scenarios:  Select the element 5 from the array. Since we are selecting 5 from the array so array arr would contain the elements 2, i.e., arr = [2]. The sum would be 6-5 equals to 1 and the element 5 gets stored in the result array. The result[] = [3, 5].  Reject the element 5 from the array. Since we are rejecting 5 from the array so array arr would contain the element 2, i.e., arr = [2]. The sum would remain same as previous, i.e., 6 and the result array would be same as previous, i.e., {3}.
  • 13. Consider S-5. It has two scenarios:  Select the element 2 from the array. Since we are selecting 2 from the array so array arr would be empty, i.e., arr = " ". The sum would be 1-2 equals to -1 and the element 2 gets stored in the result array. The result[] = [3, 5, 2].  Reject the element 2 from the array. Since we are rejecting 2 from the array so array arr would become empty. The sum would remain same as previous, i.e., 1 and the result array would be same as previous, i.e., {3, 5}.
  • 14. Consider R-5. It has two scenarios:  Select the element 2 from the array. Since we are selecting 2 from the array so array arr would be empty, i.e., arr = " ". The sum would be 6-2 equals to 4 and the element 2 gets stored in the result array. The result[] = [3, 2].  Reject the element 2 from the array. Since we are rejecting 2 from the array so array arr would become empty. The sum would remain same as previous, i.e., 6 and the result array would be same as previous, i.e., {3}.
  • 15. Similarly, we get the reject case, i.e., R-3 as shown as below:
  • 16. Graph coloring Graph: A graph is a unique data structure in programming that consists of finite sets of nodes or vertices and a set of edges that connect these vertices to them. At this moment, adjacent vertices can be called those vertices that are connected to the same edge with each other. In simple terms, a graph is a visual representation of vertices and edges sharing some connection or relationship. Although there are plenty of graph algorithms that you might have been familiar with, only some of them are put to use. The reason for this is simple as the standard graph algorithms are designed in such a way to solve millions of problems with just a few lines of logically coded technique. To some extent, one perfect algorithm is solely optimized to achieve such efficient results.
  • 17. Graph coloring Types of Graphs: There are various types of graph algorithms that you would be looking at in this article but before that, let's look at some types of terms to imply the fundamental variations between them.  Order: Order defines the total number of vertices present in the graph.  Size: Size defines the number of edges present in the graph.  Self-loop: It is the edges that are connected from a vertex to itself.  Isolated vertex: It is the vertex that is not connected to any other vertices in the graph.  Vertex degree: It is defined as the number of edges incident to a vertex in a graph.  Weighted graph: A graph having value or weight of vertices.  Unweighted graph: A graph having no value or weight of vertices.  Directed graph: A graph having a direction indicator.  Undirected graph: A graph where no directions are defined.
  • 19. Graph coloring Graph coloring: Graph coloring algorithms follow the approach of assigning colors to the elements present in the graph under certain conditions. The conditions are based on the techniques or algorithms. Hence, vertex coloring is a commonly used coloring technique followed here. First, in this method, you try to color the vertex using k color, ensuring that two adjacent vertexes should not have the same color. Other method includes face coloring and edge coloring. Both of these methods should also ensure that no edge or face should be inconsequent color. The coloring of the graph is determined by knowing the chromatic number, which is also the smaller number of colors needed.