Vertex Cover Problem (Dynamic Programming Solution for Tree)

A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either āuā or āvā is in vertex cover. Although the name is Vertex Cover, the set covers all edges of the given graph.Ā
The problem to find minimum size vertex cover of a graph is NP complete. But it can be solved in polynomial time for trees. In this post a solution for Binary Tree is discussed. The same solution can be extended for n-ary trees.
For example, consider the following binary tree. The smallest vertex cover is {20, 50, 30} and size of the vertex cover is 3.Ā
The idea is to consider following two possibilities for root and recursively for all nodes down the root.Ā
1) Root is part of vertex cover: In this case root covers all children edges. We recursively calculate size of vertex covers for left and right subtrees and add 1 to the result (for root).
2) Root is not part of vertex cover: In this case, both children of root must be included in vertex cover to cover all root to children edges. We recursively calculate size of vertex covers of all grandchildren and number of children to the result (for two children of root).
Below are implementation of above idea.Ā
C
// A naive recursive C implementation for vertex cover problem for a tree#include <stdio.h>#include <stdlib.h>Ā
// A utility function to find min of two integersint min(int x, int y) { return (x < y)? x: y; }Ā
/* A binary tree node has data, pointer to left child and a pointer toĀ Ā Ā right child */struct node{Ā Ā Ā Ā int data;Ā Ā Ā Ā struct node *left, *right;};Ā
// The function returns size of the minimum vertex coverint vCover(struct node *root){Ā Ā Ā Ā // The size of minimum vertex cover is zero if tree is empty or thereĀ Ā Ā Ā // is only one nodeĀ Ā Ā Ā if (root == NULL)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā if (root->left == NULL && root->right == NULL)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is part of itĀ Ā Ā Ā int size_incl = 1 + vCover(root->left) + vCover(root->right);Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is not part of itĀ Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā if (root->left)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->left->left) + vCover(root->left->right);Ā Ā Ā Ā if (root->right)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->right->left) + vCover(root->right->right);Ā
Ā Ā Ā Ā // Return the minimum of two sizesĀ Ā Ā Ā return min(size_incl, size_excl);}Ā
// A utility function to create a nodestruct node* newNode( int data ){Ā Ā Ā Ā struct node* temp = (struct node *) malloc( sizeof(struct node) );Ā Ā Ā Ā temp->data = data;Ā Ā Ā Ā temp->left = temp->right = NULL;Ā Ā Ā Ā return temp;}Ā
// Driver program to test above functionsint main(){Ā Ā Ā Ā // Let us construct the tree given in the above diagramĀ Ā Ā Ā struct node *rootĀ Ā Ā Ā Ā Ā Ā Ā = newNode(20);Ā Ā Ā Ā root->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(8);Ā Ā Ā Ā root->left->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā = newNode(4);Ā Ā Ā Ā root->left->rightĀ Ā Ā Ā Ā Ā Ā Ā = newNode(12);Ā Ā Ā Ā root->left->right->leftĀ Ā = newNode(10);Ā Ā Ā Ā root->left->right->rightĀ = newNode(14);Ā Ā Ā Ā root->rightĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(22);Ā Ā Ā Ā root->right->rightĀ Ā Ā Ā Ā Ā Ā = newNode(25);Ā
Ā Ā Ā Ā printf ("Size of the smallest vertex cover is %d ", vCover(root));Ā
Ā Ā Ā Ā return 0;} |
C++
#include <iostream>#include <cstdlib>Ā
// A utility function to find min of two integersint min(int x, int y) { return (x < y)? x: y; }Ā
/* A binary tree node has data, pointer to left child and a pointer toĀ Ā Ā right child */struct node{Ā Ā Ā Ā int data;Ā Ā Ā Ā node *left, *right;};Ā
// The function returns size of the minimum vertex coverint vCover(node *root){Ā Ā Ā Ā // The size of minimum vertex cover is zero if tree is empty or thereĀ Ā Ā Ā // is only one nodeĀ Ā Ā Ā if (root == nullptr)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā if (root->left == nullptr && root->right == nullptr)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is part of itĀ Ā Ā Ā int size_incl = 1 + vCover(root->left) + vCover(root->right);Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is not part of itĀ Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā if (root->left)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->left->left) + vCover(root->left->right);Ā Ā Ā Ā if (root->right)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->right->left) + vCover(root->right->right);Ā
Ā Ā Ā Ā // Return the minimum of two sizesĀ Ā Ā Ā return min(size_incl, size_excl);}Ā
// A utility function to create a nodenode* newNode( int data ){Ā Ā Ā Ā node* temp = new node;Ā Ā Ā Ā temp->data = data;Ā Ā Ā Ā temp->left = temp->right = nullptr;Ā Ā Ā Ā return temp;}Ā
// Driver program to test above functionsint main(){Ā Ā Ā Ā // Let us construct the tree given in the above diagramĀ Ā Ā Ā node *rootĀ Ā Ā Ā Ā Ā Ā Ā = newNode(20);Ā Ā Ā Ā root->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(8);Ā Ā Ā Ā root->left->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā = newNode(4);Ā Ā Ā Ā root->left->rightĀ Ā Ā Ā Ā Ā Ā Ā = newNode(12);Ā Ā Ā Ā root->left->right->leftĀ Ā = newNode(10);Ā Ā Ā Ā root->left->right->rightĀ = newNode(14);Ā Ā Ā Ā root->rightĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(22);Ā Ā Ā Ā root->right->rightĀ Ā Ā Ā Ā Ā Ā = newNode(25);Ā
Ā Ā Ā Ā std::cout << "Size of the smallest vertex cover is " << vCover(root) << std::endl;Ā
Ā Ā Ā Ā return 0;} |
Java
// A naive recursive Java implementation // for vertex cover problem for a treeĀ
class GFG{Ā Ā Ā Ā // A utility function to find min of two integersĀ Ā Ā Ā static int min(int x, int y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return (x < y) ? x : y;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā /*Ā Ā Ā Ā * A binary tree node has data, pointerĀ Ā Ā Ā to left child and a pointer to rightĀ Ā Ā Ā * childĀ Ā Ā Ā */Ā Ā Ā Ā static class node Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int data;Ā Ā Ā Ā Ā Ā Ā Ā node left, right;Ā Ā Ā Ā };Ā
Ā Ā Ā Ā // The function returns size Ā Ā Ā Ā // of the minimum vertex coverĀ Ā Ā Ā static int vCover(node root) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // The size of minimum vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // is zero if tree is empty or thereĀ Ā Ā Ā Ā Ā Ā Ā // is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if (root == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == null && root.right == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_incl = 1 + vCover(root.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.left.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right);Ā Ā Ā Ā Ā Ā Ā Ā if (root.right != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.right.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the minimum of two sizesĀ Ā Ā Ā Ā Ā Ā Ā return Math.min(size_incl, size_excl);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // A utility function to create a nodeĀ Ā Ā Ā static node newNode(int data) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā node temp = new node();Ā Ā Ā Ā Ā Ā Ā Ā temp.data = data;Ā Ā Ā Ā Ā Ā Ā Ā temp.left = temp.right = null;Ā Ā Ā Ā Ā Ā Ā Ā return temp;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void main(String[] args) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Let us construct tree given in the above diagramĀ Ā Ā Ā Ā Ā Ā Ā node root = newNode(20);Ā Ā Ā Ā Ā Ā Ā Ā root.left = newNode(8);Ā Ā Ā Ā Ā Ā Ā Ā root.left.left = newNode(4);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right = newNode(12);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.left = newNode(10);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.right = newNode(14);Ā Ā Ā Ā Ā Ā Ā Ā root.right = newNode(22);Ā Ā Ā Ā Ā Ā Ā Ā root.right.right = newNode(25);Ā
Ā Ā Ā Ā Ā Ā Ā Ā System.out.printf("Size of the smallest vertex" + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "cover is %d ", vCover(root));Ā
Ā Ā Ā Ā }}Ā
// This code is contributed by 29AjayKumar |
Python3
# A naive recursive Python3 implementation # for vertex cover problem for a treeĀ
# A utility function to find min of two integersĀ
# A binary tree node has data, pointer to# left child and a pointer to right child class Node:Ā Ā Ā Ā Ā Ā Ā Ā Ā def __init__(self, x):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā self.data = xĀ Ā Ā Ā Ā Ā Ā Ā self.left = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.right = NoneĀ
# The function returns size of # the minimum vertex coverdef vCover(root):Ā Ā Ā Ā Ā Ā Ā Ā Ā # The size of minimum vertex cover Ā Ā Ā Ā # is zero if tree is empty or thereĀ Ā Ā Ā # is only one nodeĀ Ā Ā Ā if (root == None):Ā Ā Ā Ā Ā Ā Ā Ā return 0Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == None andĀ Ā Ā Ā Ā Ā Ā root.right == None):Ā Ā Ā Ā Ā Ā Ā Ā return 0Ā
Ā Ā Ā Ā # Calculate size of vertex cover when Ā Ā Ā Ā # root is part of itĀ Ā Ā Ā size_incl = (1 + vCover(root.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right))Ā
Ā Ā Ā Ā # Calculate size of vertex cover Ā Ā Ā Ā # when root is not part of itĀ Ā Ā Ā size_excl = 0Ā Ā Ā Ā if (root.left):Ā Ā Ā Ā Ā Ā size_excl += (1 + vCover(root.left.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right))Ā Ā Ā Ā if (root.right):Ā Ā Ā Ā Ā Ā size_excl += (1 + vCover(root.right.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right))Ā
Ā Ā Ā Ā # Return the minimum of two sizesĀ Ā Ā Ā return min(size_incl, size_excl)Ā
# Driver Codeif __name__ == '__main__':Ā Ā Ā Ā Ā Ā Ā Ā Ā # Let us construct the tree Ā Ā Ā Ā # given in the above diagramĀ Ā Ā Ā rootĀ = Node(20)Ā Ā Ā Ā root.left = Node(8)Ā Ā Ā Ā root.left.left = Node(4)Ā Ā Ā Ā root.left.right = Node(12)Ā Ā Ā Ā root.left.right.left = Node(10)Ā Ā Ā Ā root.left.right.right = Node(14)Ā Ā Ā Ā root.rightĀ = Node(22)Ā Ā Ā Ā root.right.right = Node(25)Ā
Ā Ā Ā Ā print("Size of the smallest vertex cover is", vCover(root))Ā
# This code is contributed by mohit kumar 29 |
C#
// A naive recursive C# implementation // for vertex cover problem for a treeusing System;Ā
class GFG{Ā Ā Ā Ā // A utility function to find Ā Ā Ā Ā // min of two integersĀ Ā Ā Ā static int min(int x, int y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return (x < y) ? x : y;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā /*Ā Ā Ā Ā * A binary tree node has data, pointerĀ Ā Ā Ā to left child and a pointer to rightĀ Ā Ā Ā * childĀ Ā Ā Ā */Ā Ā Ā Ā public class node Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā public int data;Ā Ā Ā Ā Ā Ā Ā Ā public node left, right;Ā Ā Ā Ā };Ā
Ā Ā Ā Ā // The function returns size Ā Ā Ā Ā // of the minimum vertex coverĀ Ā Ā Ā static int vCover(node root) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // The size of minimum vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // is zero if tree is empty or thereĀ Ā Ā Ā Ā Ā Ā Ā // is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if (root == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == null && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā root.right == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_incl = 1 + vCover(root.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.left.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right);Ā Ā Ā Ā Ā Ā Ā Ā if (root.right != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.right.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Return the minimum of two sizesĀ Ā Ā Ā Ā Ā Ā Ā return Math.Min(size_incl, size_excl);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // A utility function to create a nodeĀ Ā Ā Ā static node newNode(int data) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā node temp = new node();Ā Ā Ā Ā Ā Ā Ā Ā temp.data = data;Ā Ā Ā Ā Ā Ā Ā Ā temp.left = temp.right = null;Ā Ā Ā Ā Ā Ā Ā Ā return temp;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void Main(String[] args) Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Let us construct tree given Ā Ā Ā Ā Ā Ā Ā Ā // in the above diagramĀ Ā Ā Ā Ā Ā Ā Ā node root = newNode(20);Ā Ā Ā Ā Ā Ā Ā Ā root.left = newNode(8);Ā Ā Ā Ā Ā Ā Ā Ā root.left.left = newNode(4);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right = newNode(12);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.left = newNode(10);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.right = newNode(14);Ā Ā Ā Ā Ā Ā Ā Ā root.right = newNode(22);Ā Ā Ā Ā Ā Ā Ā Ā root.right.right = newNode(25);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write("Size of the smallest vertex" + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "cover is {0} ", vCover(root));Ā Ā Ā Ā }}Ā
// This code is contributed by 29AjayKumar |
Javascript
<script>// A naive recursive Javascript implementation// for vertex cover problem for a treeĀ Ā Ā Ā Ā Ā Ā Ā Ā // A utility function to find min of two integersĀ Ā Ā Ā function min(x,y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā return (x < y) ? x : y;Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā /*Ā Ā Ā Ā * A binary tree node has data, pointerĀ Ā Ā Ā to left child and a pointer to rightĀ Ā Ā Ā * childĀ Ā Ā Ā */Ā Ā Ā Ā class NodeĀ Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā constructor(d)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.data=d;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.left=null;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā this.right=null;Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // The function returns sizeĀ Ā Ā Ā // of the minimum vertex coverĀ Ā Ā Ā function vCover(root)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // The size of minimum vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // is zero if tree is empty or thereĀ Ā Ā Ā Ā Ā Ā Ā // is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if (root == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == null && root.right == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā let size_incl = 1 + vCover(root.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā let size_excl = 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.left.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right);Ā Ā Ā Ā Ā Ā Ā Ā if (root.right != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.right.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Return the minimum of two sizesĀ Ā Ā Ā Ā Ā Ā Ā return Math.min(size_incl, size_excl);Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā // Let us construct tree given in the above diagramĀ Ā Ā Ā root = new Node(20);Ā Ā Ā Ā root.left = new Node(8);Ā Ā Ā Ā root.left.left = new Node(4);Ā Ā Ā Ā root.left.right = new Node(12);Ā Ā Ā Ā root.left.right.left = new Node(10);Ā Ā Ā Ā root.left.right.right = new Node(14);Ā Ā Ā Ā root.right = new Node(22);Ā Ā Ā Ā root.right.right = new Node(25);Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write("Size of the smallest vertex" +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "cover is ", vCover(root));Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // This code is contributed by unknown2108</script> |
Size of the smallest vertex cover is 3
Time complexity of the above naive recursive approach is exponential. It should be noted that the above function computes the same subproblems again and again. For example, vCover of node with value 50 is evaluated twice as 50 is grandchild of 10 and child of 20.
Since same subproblems are called again, this problem has Overlapping Subproblems property. So Vertex Cover problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, re-computations of same subproblems can be avoided by storing the solutions to subproblems and solving problems in bottom up manner.
Following is the implementation of Dynamic Programming based solution. In the following solution, an additional field āvcā is added to tree nodes. The initial value of āvcā is set as 0 for all nodes. The recursive function vCover() calculates āvcā for a node only if it is not already set.
C
/* Dynamic programming based program for Vertex Cover problem for Ā Ā Ā a Binary Tree */#include <stdio.h>#include <stdlib.h>Ā
// A utility function to find min of two integersint min(int x, int y) { return (x < y)? x: y; }Ā
/* A binary tree node has data, pointer to left child and a pointer toĀ Ā Ā right child */struct node{Ā Ā Ā Ā int data;Ā Ā Ā Ā int vc;Ā Ā Ā Ā struct node *left, *right;};Ā
// A memoization based function that returns size of the minimum vertex cover.int vCover(struct node *root){Ā Ā Ā Ā // The size of minimum vertex cover is zero if tree is empty or thereĀ Ā Ā Ā // is only one nodeĀ Ā Ā Ā if (root == NULL)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā if (root->left == NULL && root->right == NULL)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā // If vertex cover for this node is already evaluated, then return itĀ Ā Ā Ā // to save recomputation of same subproblem again.Ā Ā Ā Ā if (root->vc != 0)Ā Ā Ā Ā Ā Ā Ā Ā return root->vc;Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is part of itĀ Ā Ā Ā int size_incl = 1 + vCover(root->left) + vCover(root->right);Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is not part of itĀ Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā if (root->left)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->left->left) + vCover(root->left->right);Ā Ā Ā Ā if (root->right)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->right->left) + vCover(root->right->right);Ā
Ā Ā Ā Ā // Minimum of two values is vertex cover, store it before returningĀ Ā Ā Ā root->vc =Ā min(size_incl, size_excl);Ā
Ā Ā Ā Ā return root->vc;}Ā
// A utility function to create a nodestruct node* newNode( int data ){Ā Ā Ā Ā struct node* temp = (struct node *) malloc( sizeof(struct node) );Ā Ā Ā Ā temp->data = data;Ā Ā Ā Ā temp->left = temp->right = NULL;Ā Ā Ā Ā temp->vc = 0; // Set the vertex cover as 0Ā Ā Ā Ā return temp;}Ā
// Driver program to test above functionsint main(){Ā Ā Ā Ā // Let us construct the tree given in the above diagramĀ Ā Ā Ā struct node *rootĀ Ā Ā Ā Ā Ā Ā Ā = newNode(20);Ā Ā Ā Ā root->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(8);Ā Ā Ā Ā root->left->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā = newNode(4);Ā Ā Ā Ā root->left->rightĀ Ā Ā Ā Ā Ā Ā Ā = newNode(12);Ā Ā Ā Ā root->left->right->leftĀ Ā = newNode(10);Ā Ā Ā Ā root->left->right->rightĀ = newNode(14);Ā Ā Ā Ā root->rightĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(22);Ā Ā Ā Ā root->right->rightĀ Ā Ā Ā Ā Ā Ā = newNode(25);Ā
Ā Ā Ā Ā printf ("Size of the smallest vertex cover is %d ", vCover(root));Ā
Ā Ā Ā Ā return 0;} |
C++
#include <iostream>#include <stdlib.h>Ā
// A utility function to find min of two integersint min(int x, int y) { return (x < y)? x: y; }Ā
/* A binary tree node has data, pointer to left child and a pointer toĀ Ā Ā right child */struct node{Ā Ā Ā Ā int data;Ā Ā Ā Ā int vc;Ā Ā Ā Ā struct node *left, *right;};Ā
// A memoization based function that returns size of the minimum vertex cover.int vCover(struct node *root){Ā Ā Ā Ā // The size of minimum vertex cover is zero if tree is empty or thereĀ Ā Ā Ā // is only one nodeĀ Ā Ā Ā if (root == NULL)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā if (root->left == NULL && root->right == NULL)Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā // If vertex cover for this node is already evaluated, then return itĀ Ā Ā Ā // to save recomputation of same subproblem again.Ā Ā Ā Ā if (root->vc != 0)Ā Ā Ā Ā Ā Ā Ā Ā return root->vc;Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is part of itĀ Ā Ā Ā int size_incl = 1 + vCover(root->left) + vCover(root->right);Ā
Ā Ā Ā Ā // Calculate size of vertex cover when root is not part of itĀ Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā if (root->left)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->left->left) + vCover(root->left->right);Ā Ā Ā Ā if (root->right)Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root->right->left) + vCover(root->right->right);Ā
Ā Ā Ā Ā // Minimum of two values is vertex cover, store it before returningĀ Ā Ā Ā root->vc =Ā min(size_incl, size_excl);Ā
Ā Ā Ā Ā return root->vc;}Ā
// A utility function to create a nodestruct node* newNode( int data ){Ā Ā Ā Ā struct node* temp = (struct node *) malloc( sizeof(struct node) );Ā Ā Ā Ā temp->data = data;Ā Ā Ā Ā temp->left = temp->right = NULL;Ā Ā Ā Ā temp->vc = 0; // Set the vertex cover as 0Ā Ā Ā Ā return temp;}Ā
// Driver program to test above functionsint main(){Ā Ā Ā Ā // Let us construct the tree given in the above diagramĀ Ā Ā Ā struct node *rootĀ Ā Ā Ā Ā Ā Ā Ā = newNode(20);Ā Ā Ā Ā root->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(8);Ā Ā Ā Ā root->left->leftĀ Ā Ā Ā Ā Ā Ā Ā Ā = newNode(4);Ā Ā Ā Ā root->left->rightĀ Ā Ā Ā Ā Ā Ā Ā = newNode(12);Ā Ā Ā Ā root->left->right->leftĀ Ā = newNode(10);Ā Ā Ā Ā root->left->right->rightĀ = newNode(14);Ā Ā Ā Ā root->rightĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā = newNode(22);Ā Ā Ā Ā root->right->rightĀ Ā Ā Ā Ā Ā Ā = newNode(25);Ā
Ā Ā Ā Ā std::cout << "Size of the smallest vertex cover is " << vCover(root) << std::endl;Ā
Ā Ā Ā Ā return 0;} |
Java
/* Dynamic programming based program for Vertex Cover problem for a Binary Tree */Ā
class GFG{Ā Ā Ā Ā // A utility function to find min of two integersĀ Ā Ā Ā static int min(int x, int y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return (x < y) ? x : y;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā /*Ā Ā Ā Ā * A binary tree node has data, pointer Ā Ā Ā Ā to left child and a pointer to rightĀ Ā Ā Ā * childĀ Ā Ā Ā */Ā Ā Ā Ā static class node Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā int data;Ā Ā Ā Ā Ā Ā Ā Ā int vc;Ā Ā Ā Ā Ā Ā Ā Ā node left, right;Ā Ā Ā Ā };Ā
Ā Ā Ā Ā // A memoization based function that returnsĀ Ā Ā Ā // size of the minimum vertex cover.Ā Ā Ā Ā static int vCover(node root)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // The size of minimum vertex cover is zeroĀ Ā Ā Ā Ā Ā Ā Ā //Ā if tree is empty or there is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if (root == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == null && root.right == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If vertex cover for this node is Ā Ā Ā Ā Ā Ā Ā Ā // already evaluated, then return itĀ Ā Ā Ā Ā Ā Ā Ā // to save recomputation of same subproblem again.Ā Ā Ā Ā Ā Ā Ā Ā if (root.vc != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return root.vc;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex cover Ā Ā Ā Ā Ā Ā Ā Ā // when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_incl = 1 + vCover(root.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.left.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right);Ā Ā Ā Ā Ā Ā Ā Ā if (root.right != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.right.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Minimum of two values is vertex cover, Ā Ā Ā Ā Ā Ā Ā Ā // store it before returningĀ Ā Ā Ā Ā Ā Ā Ā root.vc = Math.min(size_incl, size_excl);Ā
Ā Ā Ā Ā Ā Ā Ā Ā return root.vc;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // A utility function to create a nodeĀ Ā Ā Ā static node newNode(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā node temp = new node();Ā Ā Ā Ā Ā Ā Ā Ā temp.data = data;Ā Ā Ā Ā Ā Ā Ā Ā temp.left = temp.right = null;Ā Ā Ā Ā Ā Ā Ā Ā temp.vc = 0; // Set the vertex cover as 0Ā Ā Ā Ā Ā Ā Ā Ā return temp;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Let us construct tree given in the above diagramĀ Ā Ā Ā Ā Ā Ā Ā node root = newNode(20);Ā Ā Ā Ā Ā Ā Ā Ā root.left = newNode(8);Ā Ā Ā Ā Ā Ā Ā Ā root.left.left = newNode(4);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right = newNode(12);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.left = newNode(10);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.right = newNode(14);Ā Ā Ā Ā Ā Ā Ā Ā root.right = newNode(22);Ā Ā Ā Ā Ā Ā Ā Ā root.right.right = newNode(25);Ā
Ā Ā Ā Ā Ā Ā Ā Ā System.out.printf("Size of the smallest vertex" + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "cover is %d ", vCover(root));Ā Ā Ā Ā }}Ā
// This code is contributed by PrinciRaj1992 |
C#
/* Dynamic programming based program for Vertex Cover problem for a Binary Tree */using System;Ā
class GFG{Ā Ā Ā Ā // A utility function to find Ā Ā Ā Ā // min of two integersĀ Ā Ā Ā static int min(int x, int y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā return (x < y) ? x : y;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā /*Ā Ā Ā Ā * A binary tree node has data, pointer Ā Ā Ā Ā to left child and a pointer to rightĀ Ā Ā Ā * childĀ Ā Ā Ā */Ā Ā Ā Ā class node Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā public int data;Ā Ā Ā Ā Ā Ā Ā Ā public int vc;Ā Ā Ā Ā Ā Ā Ā Ā public node left, right;Ā Ā Ā Ā };Ā
Ā Ā Ā Ā // A memoization based function that returnsĀ Ā Ā Ā // size of the minimum vertex cover.Ā Ā Ā Ā static int vCover(node root)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // The size of minimum vertex cover is zeroĀ Ā Ā Ā Ā Ā Ā Ā // if tree is empty or there is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if (root == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == null && Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā root.right == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // If vertex cover for this node is Ā Ā Ā Ā Ā Ā Ā Ā // already evaluated, then return itĀ Ā Ā Ā Ā Ā Ā Ā // to save recomputation of same subproblem again.Ā Ā Ā Ā Ā Ā Ā Ā if (root.vc != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return root.vc;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex cover Ā Ā Ā Ā Ā Ā Ā Ā // when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_incl = 1 + vCover(root.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā int size_excl = 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.left.left) + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right);Ā Ā Ā Ā Ā Ā Ā Ā if (root.right != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.right.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // Minimum of two values is vertex cover, Ā Ā Ā Ā Ā Ā Ā Ā // store it before returningĀ Ā Ā Ā Ā Ā Ā Ā root.vc = Math.Min(size_incl, size_excl);Ā
Ā Ā Ā Ā Ā Ā Ā Ā return root.vc;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // A utility function to create a nodeĀ Ā Ā Ā static node newNode(int data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā node temp = new node();Ā Ā Ā Ā Ā Ā Ā Ā temp.data = data;Ā Ā Ā Ā Ā Ā Ā Ā temp.left = temp.right = null;Ā Ā Ā Ā Ā Ā Ā Ā temp.vc = 0; // Set the vertex cover as 0Ā Ā Ā Ā Ā Ā Ā Ā return temp;Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // Driver codeĀ Ā Ā Ā public static void Main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā // Let us construct tree given in the above diagramĀ Ā Ā Ā Ā Ā Ā Ā node root = newNode(20);Ā Ā Ā Ā Ā Ā Ā Ā root.left = newNode(8);Ā Ā Ā Ā Ā Ā Ā Ā root.left.left = newNode(4);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right = newNode(12);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.left = newNode(10);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.right = newNode(14);Ā Ā Ā Ā Ā Ā Ā Ā root.right = newNode(22);Ā Ā Ā Ā Ā Ā Ā Ā root.right.right = newNode(25);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Console.Write("Size of the smallest vertex" + Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "cover is {0} ", vCover(root));Ā Ā Ā Ā }}Ā
// This code is contributed by PrinciRaj1992 |
Javascript
<script>Ā
/* Dynamic programming based program forVertex Cover problem for a Binary Tree */Ā
// A utility function to find min of two integersfunction min(x,y){Ā Ā Ā Ā return (x < y) ? x : y;}Ā
/*Ā Ā Ā Ā * A binary tree node has data, pointerĀ Ā Ā Ā to left child and a pointer to rightĀ Ā Ā Ā * childĀ Ā Ā Ā */class Node{Ā Ā Ā Ā constructor(data)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā this.vc=0;Ā Ā Ā Ā Ā Ā Ā Ā this.data=data;Ā Ā Ā Ā Ā Ā Ā Ā this.left=this.right=null;Ā Ā Ā Ā }}Ā
// A memoization based function that returns// size of the minimum vertex cover.function vCover(root){Ā Ā Ā Ā // The size of minimum vertex cover is zeroĀ Ā Ā Ā Ā Ā Ā Ā //Ā if tree is empty or there is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if (root == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left == null && root.right == null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // If vertex cover for this node isĀ Ā Ā Ā Ā Ā Ā Ā // already evaluated, then return itĀ Ā Ā Ā Ā Ā Ā Ā // to save recomputation of same subproblem again.Ā Ā Ā Ā Ā Ā Ā Ā if (root.vc != 0)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return root.vc;Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā let size_incl = 1 + vCover(root.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Calculate size of vertex coverĀ Ā Ā Ā Ā Ā Ā Ā // when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā let size_excl = 0;Ā Ā Ā Ā Ā Ā Ā Ā if (root.left != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.left.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.left.right);Ā Ā Ā Ā Ā Ā Ā Ā if (root.right != null)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + vCover(root.right.left) +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā vCover(root.right.right);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Minimum of two values is vertex cover,Ā Ā Ā Ā Ā Ā Ā Ā // store it before returningĀ Ā Ā Ā Ā Ā Ā Ā root.vc = Math.min(size_incl, size_excl);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return root.vc;}Ā
// Driver code// Let us construct tree given in the above diagramĀ Ā Ā Ā Ā Ā Ā Ā let root = new Node(20);Ā Ā Ā Ā Ā Ā Ā Ā root.left = new Node(8);Ā Ā Ā Ā Ā Ā Ā Ā root.left.left = new Node(4);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right = new Node(12);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.left = new Node(10);Ā Ā Ā Ā Ā Ā Ā Ā root.left.right.right = new Node(14);Ā Ā Ā Ā Ā Ā Ā Ā root.right = new Node(22);Ā Ā Ā Ā Ā Ā Ā Ā root.right.right = new Node(25);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā document.write("Size of the smallest vertex " +Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "cover is ", vCover(root));Ā
Ā
Ā
// This code is contributed by rag2127Ā
</script> |
Python3
# A binary tree node has data, pointer to left child and a pointer to#Ā Ā right childĀ
Ā
class node:Ā
Ā Ā Ā Ā def __init__(self):Ā Ā Ā Ā Ā Ā Ā Ā # instance fields found by C++ to Python Converter:Ā Ā Ā Ā Ā Ā Ā Ā self.data = 0Ā Ā Ā Ā Ā Ā Ā Ā self.vc = 0Ā Ā Ā Ā Ā Ā Ā Ā self.left = NoneĀ Ā Ā Ā Ā Ā Ā Ā self.right = NoneĀ
Ā
# Driver program to test above functionsdef main():Ā Ā Ā Ā # Let us construct the tree given in the above diagramĀ Ā Ā Ā root = Globals.newNode(20)Ā Ā Ā Ā root.left = Globals.newNode(8)Ā Ā Ā Ā root.left.left = Globals.newNode(4)Ā Ā Ā Ā root.left.right = Globals.newNode(12)Ā Ā Ā Ā root.left.right.left = Globals.newNode(10)Ā Ā Ā Ā root.left.right.right = Globals.newNode(14)Ā Ā Ā Ā root.right = Globals.newNode(22)Ā Ā Ā Ā root.right.right = Globals.newNode(25)Ā
Ā Ā Ā Ā print("Size of the smallest vertex cover is ", end='')Ā Ā Ā Ā print(Globals.vCover(root), end='')Ā Ā Ā Ā print("\n", end='')Ā
Ā
class Globals:Ā Ā Ā Ā # A utility function to find min of two integersĀ Ā Ā Ā @staticmethodĀ Ā Ā Ā def min(x, y):Ā Ā Ā Ā Ā Ā Ā Ā if(x < y):Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return xĀ Ā Ā Ā Ā Ā Ā Ā else:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return yĀ
Ā Ā Ā Ā # A memoization based function that returns size of the minimum vertex cover.Ā
Ā Ā Ā Ā @staticmethodĀ Ā Ā Ā def vCover(root):Ā Ā Ā Ā Ā Ā Ā Ā # The size of minimum vertex cover is zero if tree is empty or thereĀ Ā Ā Ā Ā Ā Ā Ā # is only one nodeĀ Ā Ā Ā Ā Ā Ā Ā if root is None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0Ā Ā Ā Ā Ā Ā Ā Ā if root.left is None and root.right is None:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return 0Ā
Ā Ā Ā Ā Ā Ā Ā Ā # If vertex cover for this node is already evaluated, then return itĀ Ā Ā Ā Ā Ā Ā Ā # to save recomputation of same subproblem again.Ā Ā Ā Ā Ā Ā Ā Ā if root.vc != 0:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return root.vcĀ
Ā Ā Ā Ā Ā Ā Ā Ā # Calculate size of vertex cover when root is part of itĀ Ā Ā Ā Ā Ā Ā Ā size_incl = 1 + Globals.vCover(root.left) + Globals.vCover(root.right)Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Calculate size of vertex cover when root is not part of itĀ Ā Ā Ā Ā Ā Ā Ā size_excl = 0Ā Ā Ā Ā Ā Ā Ā Ā if root.left:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + \Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Globals.vCover(root.left.left) + \Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Globals.vCover(root.left.right)Ā Ā Ā Ā Ā Ā Ā Ā if root.right:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā size_excl += 1 + \Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Globals.vCover(root.right.left) + \Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Globals.vCover(root.right.right)Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Minimum of two values is vertex cover, store it before returningĀ Ā Ā Ā Ā Ā Ā Ā root.vc = Globals.min(size_incl, size_excl)Ā
Ā Ā Ā Ā Ā Ā Ā Ā return root.vcĀ
Ā Ā Ā Ā # A utility function to create a nodeĀ Ā Ā Ā @staticmethodĀ Ā Ā Ā def newNode(data):Ā Ā Ā Ā Ā Ā Ā Ā temp = node()Ā Ā Ā Ā Ā Ā Ā Ā temp.data = dataĀ Ā Ā Ā Ā Ā Ā Ā temp.left = temp.right = NoneĀ Ā Ā Ā Ā Ā Ā Ā temp.vc = 0Ā # Set the vertex cover as 0Ā Ā Ā Ā Ā Ā Ā Ā return tempĀ
Ā
if __name__ == "__main__":Ā Ā Ā Ā main() |
Size of the smallest vertex cover is 3
Time Complexity:
The time complexity of the vCover function is O(n), where n is the number of nodes in the binary tree. This is because each node is visited only once and its vertex cover size is calculated in constant time.
Space Complexity:
The space complexity of the program is O(n), where n is the number of nodes in the binary tree. This is because the space required for the binary tree is proportional to the number of nodes in the tree. Additionally, the memoization technique used in the vCover function requires additional space to store the vertex cover sizes of already evaluated nodes. However, since the depth of the recursion tree is limited by the height of the binary tree, the space complexity of the program is also O(h), where h is the height of the binary tree. In the worst case scenario where the binary tree is skewed, the height of the tree can be as large as n, which would result in a space complexity of O(n).
References:Ā
http://courses.csail.mit.edu/6.006/spring11/lectures/lec21.pdf
Exercise:Ā
Extend the above solution for n-ary trees.Ā
This article is contributed by Udit Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Ā
Approach for any general tree :
1. Approach will be same dynamic programming approach as discussed.
2. For every node, if we exclude this node from vertex cover than we have to include its neighbouring nodes,
Ā Ā and if we include this node in the vertex cover than we will take the minimum of the two possibilities of taking its neighbouring
Ā Ā nodes in the vertex cover to get minimum vertex cover.Ā
3. We will store the above information in the dp array.
C++
// C++ implementation for the above approach#include <bits/stdc++.h>using namespace std;Ā
// An utility function to add an edge in the treevoid addEdge(vector<int> adj[], int x, int y){Ā Ā Ā Ā adj[x].push_back(y);Ā Ā Ā Ā adj[y].push_back(x);}Ā
void dfs(vector<int> adj[], vector<int> dp[], int src,Ā Ā Ā Ā Ā Ā Ā Ā Ā int par){Ā Ā Ā Ā for (auto child : adj[src]) {Ā Ā Ā Ā Ā Ā Ā Ā if (child != par)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, child, src);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā for (auto child : adj[src]) {Ā Ā Ā Ā Ā Ā Ā Ā if (child != par) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // not including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][0] += dp[child][1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][1] += min(dp[child][1], dp[child][0]);Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }}Ā
// function to find minimum size of vertex covervoid minSizeVertexCover(vector<int> adj[], int N){Ā Ā Ā Ā vector<int> dp[N + 1];Ā
Ā Ā Ā Ā for (int i = 1; i <= N; i++) {Ā Ā Ā Ā Ā Ā Ā Ā // 0 denotes not included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā dp[i].push_back(0);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // 1 denotes included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā dp[i].push_back(1);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā dfs(adj, dp, 1, -1);Ā
Ā Ā Ā Ā // printing minimum size vertex coverĀ Ā Ā Ā cout << min(dp[1][0], dp[1][1]) << endl;}Ā
// Driver Codeint main(){Ā Ā Ā Ā Ā Ā /*Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 2Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 7Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 3Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 6Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā |Ā Ā Ā Ā Ā Ā Ā \Ā Ā Ā Ā 4Ā Ā Ā Ā Ā Ā Ā Ā Ā 8Ā Ā Ā Ā Ā Ā Ā Ā Ā 5Ā Ā Ā Ā Ā */Ā Ā Ā Ā Ā Ā Ā Ā Ā // number of nodes in the treeĀ Ā Ā Ā int N = 8;Ā
Ā Ā Ā Ā // adjacency list representation of the treeĀ Ā Ā Ā vector<int> adj[N + 1];Ā
Ā Ā Ā Ā addEdge(adj, 1, 2);Ā Ā Ā Ā addEdge(adj, 1, 7);Ā Ā Ā Ā addEdge(adj, 2, 3);Ā Ā Ā Ā addEdge(adj, 2, 6);Ā Ā Ā Ā addEdge(adj, 3, 4);Ā Ā Ā Ā addEdge(adj, 3, 8);Ā Ā Ā Ā addEdge(adj, 3, 5);Ā
Ā Ā Ā Ā minSizeVertexCover(adj, N);Ā
Ā Ā Ā Ā return 0;} |
Java
// Java implementation for the above approachimport java.util.*;Ā
class GFG {Ā Ā Ā Ā // An utility function to add an edge in the treeĀ Ā Ā Ā static void addEdge(List<List<Integer> > adj, int x,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā adj.get(x).add(y);Ā Ā Ā Ā Ā Ā Ā Ā adj.get(y).add(x);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā static void dfs(List<List<Integer> > adj,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā List<List<Integer> > dp, int src,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int par)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā for (Integer child : adj.get(src)) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (child != par)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, child, src);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (Integer child : adj.get(src)) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (child != par) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // not including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(src).set(0,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(child).get(1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + dp.get(src).get(0));Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(src).set(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(src).get(1)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā + Math.min(dp.get(child).get(1),Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(child).get(0)));Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // function to find minimum size of vertex coverĀ Ā Ā Ā static void minSizeVertexCover(List<List<Integer> > adj,Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int N)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā List<List<Integer> > dp = new ArrayList<>();Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i <= N; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.add(new ArrayList<>());Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= N; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 0 denotes not included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(i).add(0);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 1 denotes included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.get(i).add(1);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, 1, -1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // printing minimum size vertex coverĀ Ā Ā Ā Ā Ā Ā Ā System.out.println(Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Math.min(dp.get(1).get(0), dp.get(1).get(1)));Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public static void main(String[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā /*Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 2Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 7Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 3Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 6Ā
Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā |Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā 4Ā Ā Ā Ā Ā Ā Ā Ā Ā 8Ā Ā Ā Ā Ā Ā Ā Ā Ā 5Ā
Ā Ā Ā Ā */Ā
Ā Ā Ā Ā Ā Ā Ā Ā // number of nodes in the treeĀ Ā Ā Ā Ā Ā Ā Ā int N = 8;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // adjacency list representation of the treeĀ Ā Ā Ā Ā Ā Ā Ā List<List<Integer> > adj = new ArrayList<>();Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i <= N; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā adj.add(new ArrayList<>());Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 1, 2);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 1, 7);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 2, 3);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 2, 6);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 4);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 8);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 5);Ā
Ā Ā Ā Ā Ā Ā Ā Ā minSizeVertexCover(adj, N);Ā Ā Ā Ā }} |
Python3
# Python3 implementation for the above approachĀ
Ā
def addEdge(adj, x, y):Ā Ā Ā Ā adj[x].append(y)Ā Ā Ā Ā adj[y].append(x)Ā
Ā
def dfs(adj, dp, src, par):Ā Ā Ā Ā for child in adj[src]:Ā Ā Ā Ā Ā Ā Ā Ā if child != par:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, child, src)Ā
Ā Ā Ā Ā for child in adj[src]:Ā Ā Ā Ā Ā Ā Ā Ā if child != par:Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # not including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][0] = dp[child][1] + dp[src][0]Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā # including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][1] = dp[src][1] + min(dp[child][1], dp[child][0])Ā
Ā
def minSizeVertexCover(adj, N):Ā Ā Ā Ā dp = [[0 for j in range(2)] for i in range(N+1)]Ā Ā Ā Ā for i in range(1, N+1):Ā Ā Ā Ā Ā Ā Ā Ā # 0 denotes not included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā dp[i][0] = 0Ā
Ā Ā Ā Ā Ā Ā Ā Ā # 1 denotes included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā dp[i][1] = 1Ā
Ā Ā Ā Ā dfs(adj, dp, 1, -1)Ā
Ā Ā Ā Ā # printing minimum size vertex coverĀ Ā Ā Ā print(min(dp[1][0], dp[1][1]))Ā
Ā
# Driver Code"""Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā \Ā Ā Ā Ā Ā Ā 2Ā Ā Ā Ā Ā Ā 7Ā Ā Ā Ā Ā / \Ā Ā Ā Ā 3Ā Ā 6Ā Ā Ā /|\Ā Ā Ā 4 8 5"""# number of nodes in the treeN = 8Ā
# adjacency list representation of the treeadj = [[] for i in range(N+1)]addEdge(adj, 1, 2)addEdge(adj, 1, 7)addEdge(adj, 2, 3)addEdge(adj, 2, 6)addEdge(adj, 3, 4)addEdge(adj, 3, 8)addEdge(adj, 3, 5)Ā
minSizeVertexCover(adj, N) |
C#
using System;using System.Collections.Generic;Ā
class GFG{Ā Ā Ā Ā // An utility function to add an edge in the treeĀ Ā Ā Ā static void addEdge(List<List<int>> adj, int x, int y)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā adj[x].Add(y);Ā Ā Ā Ā Ā Ā Ā Ā adj[y].Add(x);Ā Ā Ā Ā }Ā
Ā Ā Ā Ā static void dfs(List<List<int>> adj, List<List<int>> dp, int src, int par)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā foreach (int child in adj[src])Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (child != par)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, child, src);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā foreach (int child in adj[src])Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (child != par)Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // not including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][0] = dp[child][1] + dp[src][0];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][1] = dp[src][1] + Math.Min(dp[child][1], dp[child][0]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā }Ā
Ā Ā Ā Ā // function to find minimum size of vertex coverĀ Ā Ā Ā static void minSizeVertexCover(List<List<int>> adj, int N)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā List<List<int>> dp = new List<List<int>>();Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i <= N; i++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp.Add(new List<int>());Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 1; i <= N; i++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 0 denotes not included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i].Add(0);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 1 denotes included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i].Add(1);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, 1, -1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // printing minimum size vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(Math.Min(dp[1][0], dp[1][1]));Ā Ā Ā Ā }Ā
Ā Ā Ā Ā public static void Main(string[] args)Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā /*Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 2Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 7Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 3Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 6Ā
Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā |Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā 4Ā Ā Ā Ā Ā Ā Ā Ā Ā 8Ā Ā Ā Ā Ā Ā Ā Ā Ā 5Ā
Ā Ā Ā Ā */Ā
Ā Ā Ā Ā Ā Ā Ā Ā // number of nodes in the treeĀ Ā Ā Ā Ā Ā Ā Ā int N = 8;Ā
Ā Ā Ā Ā Ā Ā Ā Ā // adjacency list representation of the treeĀ Ā Ā Ā Ā Ā Ā Ā List<List<int>> adj = new List<List<int>>();Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i <= N; i++)Ā Ā Ā Ā Ā Ā Ā Ā {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā adj.Add(new List<int>());Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 1, 2);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 1, 7);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 2, 3);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 2, 6);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 4);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 8);Ā Ā Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 5);Ā
Ā Ā Ā Ā Ā Ā Ā Ā minSizeVertexCover(adj, N);Ā Ā Ā Ā }} |
Javascript
// Javascript implementation for the above approachĀ
Ā Ā Ā Ā Ā Ā // An utility function to add an edge in the treeĀ Ā Ā Ā Ā Ā function addEdge(adj, x, y) {Ā Ā Ā Ā Ā Ā Ā Ā adj[x].push(y);Ā Ā Ā Ā Ā Ā Ā Ā adj[y].push(x);Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā function dfs(adj, dp, src, par) {Ā Ā Ā Ā Ā Ā Ā Ā for (let child of adj[src]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (child != par) dfs(adj, dp, child, src);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (let child of adj[src]) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (child != par) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // not including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][0] += dp[child][1];Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // including source in the vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[src][1] += Math.min(dp[child][1], dp[child][0]);Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā Ā Ā }Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā // function to find minimum size of vertex coverĀ Ā Ā Ā Ā Ā function minSizeVertexCover(adj, N) {Ā Ā Ā Ā Ā Ā Ā Ā let dp = Array.from(Array(N + 1), () => new Array());Ā
Ā Ā Ā Ā Ā Ā Ā Ā for (var i = 1; i <= N; i++) {Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 0 denotes not included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i].push(0);Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // 1 denotes included in vertex coverĀ Ā Ā Ā Ā Ā Ā Ā Ā Ā dp[i].push(1);Ā Ā Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā Ā Ā dfs(adj, dp, 1, -1);Ā
Ā Ā Ā Ā Ā Ā Ā Ā // printing minimum size vertex coverĀ Ā Ā Ā Ā Ā Ā Ā console.log(Math.min(dp[1][0], dp[1][1]));Ā Ā Ā Ā Ā Ā }Ā
Ā Ā Ā Ā Ā Ā // Driver CodeĀ
Ā Ā Ā Ā Ā Ā /*Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 1Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 2Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 7Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 3Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 6Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā /Ā Ā Ā Ā Ā Ā Ā |Ā Ā Ā Ā Ā Ā Ā \Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 4Ā Ā Ā Ā Ā Ā Ā Ā Ā 8Ā Ā Ā Ā Ā Ā Ā Ā Ā 5Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā */Ā
Ā Ā Ā Ā Ā Ā // number of nodes in the treeĀ Ā Ā Ā Ā Ā var N = 8;Ā
Ā Ā Ā Ā Ā Ā // adjacency list representation of the treeĀ Ā Ā Ā Ā Ā let adj = Array.from(Array(N + 1), () => new Array());Ā
Ā Ā Ā Ā Ā Ā addEdge(adj, 1, 2);Ā Ā Ā Ā Ā Ā addEdge(adj, 1, 7);Ā Ā Ā Ā Ā Ā addEdge(adj, 2, 3);Ā Ā Ā Ā Ā Ā addEdge(adj, 2, 6);Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 4);Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 8);Ā Ā Ā Ā Ā Ā addEdge(adj, 3, 5);Ā
Ā Ā Ā Ā Ā Ā minSizeVertexCover(adj, N); |
3
Time Complexity : O(N)
Auxiliary space : O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




