![]() |
Independent sets are set of vertices or edges in which the pair of any two vertices or edges are not adjacent to each other. Assuming that Independent sets mean Independent sets of vertices, we have to find a set of such vertices in which any two pairs of vertexes are not adjacent to each other. Using graph coloring we can solve this problem. We will modify the graph coloring method as instead of using different colors we will only use two colors i.e 0,1. So we will assume that those vertices which are labeled 0 are a part of the set and others are not. So the vertices labeled with 0 don’t have any adjacent vertex labeled with 0. Approach: The basic idea about the functioning of call by references in java and concepts of the vector is a must. Also, we are assuming the index labeled as the vertex name and the value in the vector at that index as the color of that vertex (either 0 or 1). The initially found variable is set to false i.e no set of the desired size is found. We will be making use of the word removed and the color ‘0’ for any vertex. Both indicate the same thing that the particular vertex can be removed from the graph and can be included in the set. Procedure: It is illustrated along with the help of methods that are used in the program are explained for understanding purposes sequentially as follows:
So this approach proves extra beneficial for our approach.
Implementation: Till now if we have any solution then we will set found true otherwise will save the configuration of the color vector for further use. All this is done for each ‘ith’ vertex in the loop and the modified color vector is stored in the vector of vectors named set_found in the program. If the desired size is not found till now, then we will try our last case that we will be performing pairwise intersection of all the generated sets of configurations. In this, we will repeat the same procedure starting with the color vector again and maintaining the configurations generated. The only difference is that we will not begin by assigning ‘0’ to the ith vertex. Instead of that, we will check pairs of configurations (in the set_found) for those vertexes that are labeled with ‘0’ and are common to both sets. They will be labeled ‘0’ in the color vector and the best part, the above procedure will be the same, to check the maximum size of the set possible and the above case. Example Java
Output
The number of vertices in the graph is taken as 4 The minimum size of the set required is taken as 2 Searching for the set Independent set of size 1found 1 Independent set of size 1found 2 Independent set of size 1found 2 Independent set of size 1found 2 Independent set of size 1found 1 Independent set of size 1found 1 Independent set of size 1found 1 Independent set of size 1found 2 Independent set of size 1found 2 Independent set of size 1found 2 Couldn't find the set of least size given
|
Reffered: https://www.geeksforgeeks.org
Java Programs |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |