Complete Responsive Online Company Website Design Free Download HTML CSS JS

**Minimum vertex cover problem**

Minimum vertex cover is a classic graph problem. Let’s say there are some roads in a city, now we want to put a guard at every intersection. If a guard is placed on a node, he can guard the roads connected to the node alone. The nodes in the above image are the intersections of the roads. How many guards are required to guard all the roads now? The red nodes in the image are sentinels.

This is an NP-hard problem, meaning that the problem has no polynomial time solution. But if the graph is a tree, i.e. there are n-1 edges and no cycles, then the problem can be solved with the help of dynamic programming or max flow/bipartite matching.

I am writing the dynamic programming solution in detail, then how to do it with max flow/bipartite matching.

In the DP solution we have to consider 2 cases:

1. If a node is not guarded, all nodes connected to it must be guarded, and not all roads will be covered. That is, if u and v are connected, then if u is not guarded, v must be guarded.

2. Placing a guard on a node is not mandatory but can be beneficial. So if you put a guard in u, then put a guard in v and see which one is profitable.

As with all DP problems, I will define a recursive function here. Our state will be which node we are currently on, and whether a guard has been placed on that node.

## Complete Responsive Online Company Website Design Free Download HTML CSS JS

F(u,1) = I am currently at node u and there is a guard at this node. f(u,1) will return the total number of guards in the remaining nodes.

F(u,0) = Currently at node u and there is no guard at this node. f(u,0) will return the total number of guards in the remaining nodes.

Suppose node number 1 is connected to node number 2,3,4.

It is understood that if the guard is not placed on the node number 1, then the guard must be placed on 2, 3, 4 all. Then we can say:

F(1,0)=F(2,1)+F(3,1)+F(4,1) + 0 , ie the total number of guards required to guard all nodes connected to 1.

Adding 0 at the end because the current node is not guarded.

Now find the value of F(1,1). If the guard is placed on node 1, the connected nodes may or may not be guarded, but we will take the one that gives the optimal result:

F(1,1)=1+min ( F(2,1),F(2,0) ) +min ( F(3,1),F(3,0) ) +min ( F(4,1 ),F(4,0) )

I am putting a guard on the node number 1, so finally 1 is added, by putting a guard once on each node, without putting it again, I see which one is optimal.

One thing to note is that parent nodes are never calculated. In the above image, if we go from 1 to 2, parent[2]=1, so we will not go from 2 to node 1 again.

Now come to the base case. 1 or 0 should be returned if a node cannot be moved from a node to a new node, 1 if the guard is installed, 0 if not installed. 1 should be returned if there is only one node in a tree (may also return 0 in some problems).

Spoj’s PT07X(vertex cover) problem is a straight forward problem. My code for this is:

#define MAXN 100002

int dp[MAXN][5];

int par[MAXN];

vectoredges[MAXN];

int f(int u, int isGuard)

{

if (edges[u].size() == 0)

return 0;

if (dp[u][isGuard] != -1)

return dp[u][isGuard];

int sum = 0;

for (int i = 0; i < (int)edges[u].size(); i++) {

int v = edges[u][i];

if (v != par[u]) {

par[v] = u;

if (isGuard == 0)

sum += f(v, 1);

else

sum += min(f(v, 1), f(v, 0));

}

}

return dp[u][isGuard] = sum + isGuard;

}

int main()

{

memset(dp, -1, sizeof(dp));

int n;

scanf(“%d”, &n);

for (int i = 1; i < n; i++) {

int u, v;

scanf(“%d%d”, &u, &v);

edges[u].push_back(v);

edges[v].push_back(u);

}

int ans = 0;

ans = min(f(1, 1), f(1, 0));

printf(“%d\n”;, ans);

return 0;

}

I wrote the code to always have the root of the tree as 1. In line 38, I am taking the optimal result by placing the guard once and not once in the root in the main function.

In the function u is the current node, isguard indicates whether the current node has a guard or not.

**Complete Responsive Online Company Website Design Free Download HTML CSS JS**

In line 10, if the size of the tree is 1, I have returned 1.

Line 13 goes from the current node to all the child nodes inside the loop. If there is no guard in the current node, I am installing it in the next one, and if there is, I am trying both ways. Not allowing to go to the parent node with the condition of line 15.

Finally returning sum+isGuard. That is, adding 1 if the current node has a guard, otherwise 0.

Roughly this is the DP solution. Since there are no cycles in the tree, it is necessarily a bipartite graph. In 1931, Dénes Kőnig proved that maximum matching=minimum vertex cover for a bipartite graph. This is one of the many min-max theorems in graph theory where maximizing something minimizes something else.

If you know the algorithm of maximum matching, then if you bicolor the tree and find out the matching, the vertex cover will come out. Even if the code is simple, the complexity will increase, so don’t work if there are more nodes. Again, since maximum matching is a variation of max-flow, it can also be solved using flow.

Another such problem is uva-10243 (fire fire fire). I solved it first and then spoj.

## Before Download

## You must Join our Facebook Group and Subscribe YouTube Channel

**All Links in Below:**

**Join Our FreeWebsiteCreate Facebook Group to get an instant update for projects, templates, design resources, and solutions.**

**Join Our YouTube Channel & Subscribe with Bell Icon for New Video:**

## Join Our Official Facebook Page For the Latest updates All Code Projects are Free:

## Visit our service page to get **premium services.**

Free Website Create – HTML CSS, PHP, JavaScript Programming Projects For Free

**Follow Us**

**Thank You,**

## Before Download

## You must Join our Facebook Group and Subscribe YouTube Channel

FreeWebsiteCreate.net tries to provide HTML, CSS, SCSS, JavaScript, React, Android Studio, Java, PHP, Laravel, Python, Django, C#(C Sharp), and ASP.net-related projects 100% free. We try to make learning easier. Free Website Create always tries to give free projects to new learners. Free projects and source code will help to learn quickly.

They can save time and learn more. In this post, we share a free portfolio project website code with HTML and CSS. This free code portfolio contains a single landing page with a responsive design. In this post, we get a free best carpenter and craftsman service website designed by FreeWebsiteCreate with HTML, CSS, Bootstrap, and JavaScript.