Time Complexity of modified dfs algorithm

此生再无相见时 提交于 2019-12-02 22:34:08

问题


I want to write an algorithm that finds an optimal vertex cover of a tree in linear time O(n), where n is the number of the vertices of the tree.

A vertex cover of a graph G=(V,E) is a subset W of V such that for every edge (a,b) in E, a is in W or b is in W.

In a vertex cover we need to have at least one vertex for each edge.

If we pick a non-leaf, it can cover more than one edge.

That's why I thought we can do it as follows:

We visit the root of the tree, then we visit one of its children, a child of the latter that we visited and so on.

Then if we have reached at a leaf, we check if we already have taken its father for the optimal vertex cover, and if not we pick it. Then, if the vertex that we picked for the optimal vertex cover has also other children, we pick the first of them and visit the leftmost children recursively and if we reach at the leaf and its father hasn't been chosen for the desired vertex cover, we choose it and so on.

I have written the following algorithm:

   DFS(node x){
      discovered[x]=1;
      for each (v in Adj(x)){
          if discovered[v]==0{
             DFS(v);
             if (v->taken==0){
                 x<-taken=1;
             }
          }
      }
    }

I thought that its time complexity is

(|V_i|, |E_i| are the number of vertices and edges respectively of the subtrees at the root of which we call DFS )

Is the time complexity I found right? Or have I calculated it wrong?

EDIT: Is the complexity of the algorithm described by the recurrence relation:

T(|V|)=E*T(|V|-1)+O(1)

? Or am I wrong?

来源:https://stackoverflow.com/questions/29871890/time-complexity-of-modified-dfs-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!