C++ Program to Find the Size of Largest Independent Set (LIS) in a Binary Tree
Posted by Superadmin on August 09 2022 07:03:44

C++ Program to Find the Size of Largest Independent Set (LIS) in a Binary Tree

 

 

This is a C++ Program to find largest independent set in a binary tree. In graph theory, an independent set or stable set is a set of vertices in a graph, no two of which are adjacent. That is, it is a set I of vertices such that for every two vertices in I, there is no edge connecting the two. Equivalently, each edge in the graph has at most one endpoint in I. The size of an independent set is the number of vertices it contains. Independent sets have also been called internally stable sets. A maximal independent set is either an independent set such that adding any other vertex to the set forces the set to contain an edge or the set of all vertices of the empty graph.

 

Here is source code of the C++ Program to Find Size of the Largest Independent Set(LIS) in a Given a Binary Tree. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* Dynamic programming based program for Largest Independent Set problem */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. // A utility function to find max of two integers
  9. int max(int x, int y)
  10. {
  11.     return (x > y) ? x : y;
  12. }
  13.  
  14. /* A binary tree node has data, pointer to left child and a pointer to
  15.  right child */
  16. struct node
  17. {
  18.         int data;
  19.         int liss;
  20.         struct node *left, *right;
  21. };
  22.  
  23. // A memoization function returns size of the largest independent set in
  24. //  a given binary tree
  25. int LISS(struct node *root)
  26. {
  27.     if (root == NULL)
  28.         return 0;
  29.  
  30.     if (root->liss)
  31.         return root->liss;
  32.  
  33.     if (root->left == NULL && root->right == NULL)
  34.         return (root->liss = 1);
  35.  
  36.     // Caculate size excluding the current node
  37.     int liss_excl = LISS(root->left) + LISS(root->right);
  38.  
  39.     // Calculate size including the current node
  40.     int liss_incl = 1;
  41.     if (root->left)
  42.         liss_incl += LISS(root->left->left) + LISS(root->left->right);
  43.     if (root->right)
  44.         liss_incl += LISS(root->right->left) + LISS(root->right->right);
  45.  
  46.     // Return the maximum of two sizes
  47.     root->liss = max(liss_incl, liss_excl);
  48.  
  49.     return root->liss;
  50. }
  51.  
  52. // A utility function to create a node
  53. struct node* newNode(int data)
  54. {
  55.     struct node* temp = (struct node *) malloc(sizeof(struct node));
  56.     temp->data = data;
  57.     temp->left = temp->right = NULL;
  58.     temp->liss = 0;
  59.     return temp;
  60. }
  61.  
  62. // Driver program to test above functions
  63. int main()
  64. {
  65.     // Let us construct the tree given in the above diagram
  66.     struct node *root = newNode(20);
  67.     root->left = newNode(8);
  68.     root->left->left = newNode(4);
  69.     root->left->right = newNode(12);
  70.     root->left->right->left = newNode(10);
  71.     root->left->right->right = newNode(14);
  72.     root->right = newNode(22);
  73.     root->right->right = newNode(25);
  74.  
  75.     cout<<"Size of the Largest Independent Set is "<< LISS(root);
  76.  
  77.     return 0;
  78. }

Output:

$ g++ LargestIndependetSetBTree.cpp
$ a.out
 
Size of the Largest Independent Set is 5
------------------
(program exited with code: 0)
Press return to continue