MHB What is the Time Complexity of this Vertex Cover Algorithm?

AI Thread Summary
The discussion focuses on developing a linear time algorithm for finding an optimal vertex cover of a tree, aiming for a complexity of O(n). A vertex cover requires at least one vertex from each edge in the tree, and the proposed algorithm utilizes a depth-first search (DFS) approach. The algorithm involves recursively visiting nodes and selecting vertices based on whether their parent has been included in the cover.However, concerns are raised regarding the time complexity calculation. The initial complexity analysis suggests O(|V| * |E|), which is incorrect. The correct time complexity for DFS is O(|V| + |E|), as all vertices and edges should be visited only once. To achieve the desired linear time complexity, the algorithm needs modifications to ensure each vertex and edge is processed a single time. Suggestions include refining the DFS implementation to track visited vertices and their connections more effectively.
evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello! (Wave)

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:

Code:
   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;
             }
          }
      }
    }

Its time complexity is $T(n)=\sum_{i=1}^{|V|} O(|V_i|+|E_i|) \leq \sum_{i=1}^{|V|} O(|E_i|) \leq \sum_{i=1}^{|V|} O(|E|)=O(|V| \cdot |E|)$.

($|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? (Thinking)
 
Technology news on Phys.org


Hello there!

Your proposed algorithm looks like a good start. However, there are a few things that need to be clarified in order to accurately determine the time complexity.

Firstly, it is important to note that the time complexity for DFS is typically represented as $O(|V| + |E|)$, where $|V|$ is the number of vertices and $|E|$ is the number of edges. This is because in the worst case scenario, we would have to visit all of the vertices and edges in the graph.

In your algorithm, you have defined $|V_i|$ and $|E_i|$ as the number of vertices and edges in the subtrees at the root of which you call DFS. However, it is not clear how these values are determined and how they relate to the overall time complexity.

Additionally, in your algorithm, you have a nested for loop where you are iterating through all of the adjacent vertices for each vertex. This would result in a time complexity of $O(|V| \cdot |E|)$, which is different from the linear time complexity that you are aiming for.

To achieve a linear time complexity, you would need to modify your algorithm so that it only visits each vertex and edge once. One approach could be to use a modified version of depth-first search (DFS) where you keep track of the vertices that have been visited and their adjacent vertices. This would allow you to only visit each vertex and edge once, resulting in a time complexity of $O(|V| + |E|)$.

I hope this helps clarify the time complexity for your algorithm. Let me know if you have any further questions or concerns. Keep up the good work!
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
1
Views
2K
Replies
22
Views
5K
Replies
4
Views
2K
Replies
22
Views
2K
Replies
3
Views
2K
Replies
6
Views
2K
Back
Top