relation between degrees of vertices and edge removal - algorithm

I'm looking for help to prove the next question:
given an undirected tree with n vertices with each one's degree <= 3,
(1) prove that there exists an edge that if we remove we'll have two trees with number of vertices in each one - maximum (2*n/3).
(2) suggest a linear algorithm that finds such an edge in the above given tree

Choose an arbitrary root. Do a post order traversal to compute the size of each subtree. By descending from the root via children with subtrees at least as large as their siblings, find a subtree of size between (n-1)/3 inclusive and 2(n-1)/3 + 1 exclusive (the degree bound keeps the size from decreasing by more than minus one divided by two). Sever its parent edge.

Related

Algorithm to find the longest path in binary tree?

Question:
You are given a rooted binary tree (each node has at most two children).
For a simple path p between two nodes in the tree, let mp be the node on the path that is highest
(closest to the root). Define the weight of a path w(p) = Σ_u∈p d(u, mp), where d denotes the distance
(number of edges on the path between two nodes). That is, every node on the path is weighted by the
distance to the highest node on the path.
The question asks an algorithm that finds the maximum weight among all simple paths in the tree. I'm not sure if I interpreted correctly, but can I just find the longest path from mp to the farthest node? I haven't figure out which algorithm is appropriate for this question, but I think recursive is one way to do it. Again, I don't understand the question very well, it would be better if someone could "translate" it for me and guide me to the solution.
Let's assume we know mp. Then the highest-weight path must start in the left subtree and end in the right subtree (or vice versa). Otherwise, the path would not be simple. To find the start and end node, we would go as deep as possible into the respective subtrees as each level adds depth to the weight. Therefore, we can compute the weight of this path directly from the heights of the two subtrees (by using the analytic solution of the arithmetic progression):
max_weight = height_left * (height_left + 1) / 2 + height_right * (height_right + 1) / 2
To find the maximum weight path across the entire tree (without prescribing mp), simply check this value for all nodes. I.e., take a recursive algorithm that calculates the height for each subtree. When you have the two subtree heights for a node, calculate the maximum weight. Of all these weights, take the maximum. This requires time linear in the number of nodes.
And to answer your question: No, it is not necessarily the longest path in the tree. The path can have one branch that goes very deep but a very shallow branch on the other side. This is because adding one level deeper to the path does not just increase the weight by 1 but by the depth of that node.
This problem is the diameter of a binary tree. In this case, the node that at the lower level has a greater weight because it's far from the root. Therefore, to find the longest path is to find the diameter of the binary tree. We can use brute force algorithm to solve it, by traveling all leaf-to-leaf paths, then arriving at the diameter.
Method: naive approach
Find the height of left and right subtree, and then find the left and right diameter. Return the Maximum(Diameter of left subtree, Diameter of right subtree, Longest path between two nodes which passes through the root.)
Time Complexity: Since when calculating the diameter, every iteration for every node, is calculating height of tree separately in which we iterate the tree from top to bottom and when we calculate diameter recursively so its O(N2)
Improve:
If you notice at every node to find the diameter we are calling a separate function to find the height. We can improve it by finding the height of tree and diameter in the same iteration.Every node will return the two information in the same iteration , height of that node and diameter of tree with respect to that node. Running time is O(N)

Algorithm For Vertex Removal From Tree

When a vertex and its incident edges are removed from a tree, a collection of
subtrees remains. Write an algorithm that, given a graph that is a tree with n
vertices, finds a vertex v whose removal leaves no subtree with more than n/2
vertices.
I have attempted this problem using a modified DFS approach as well as a bridge finding algorithm. Any help would be much appreciated.
Create a recursive function that does a post-order traversal of the tree.
The function returns the subtree size and a vertex, or the vertex can be global (in which case you just set it instead of returning it).
Call the function for the root.
Call the function on each child's subtree.
If one of those calls returned a vertex, return that vertex.
Return the current vertex if these conditions hold:
Each child's subtree has less than or equal to n/2 vertices.
The sum of children's subtree sizes is greater than or equal to (n-1)/2, i.e. the subtree 'above' the current has less than or equal to n/2 nodes.
Return the sum of children's subtree sizes + 1 as the subtree size.
Running time: O(n).
I'm assuming you've already got the size of the tree - n - if not, you'll need to start off with another traversal to get this size (which doesn't affect the O(n) running time).

Updating a tree and keeping track of the change in the nodes of some subtree

Problem:
You are given a rooted tree where each node is numbered from 1 to N. Initially each node contains some positive value, say X. Now we are to perform two type of operations on the tree. Total 100000 operation.
First Type:
Given a node nd and a positive integer V, you need to decrease the value of all the nodes by some amount. If a node is at a distance of d from the given node then decrease its value by floor[v/(2^d)]. Do this for all the nodes.
That means value of node nd will be decreased by V (i.e, floor[V/2^0]). Values of its nearest neighbours will be decreased by floor[V/2] . And so on.
Second Type:
You are given a node nd. You have to tell the number of nodes in the subtree rooted at nd whose value is positive.
Note: Number of nodes in the tree may be upto 100000 and the initial values, X, in the nodes may be upto 1000000000. But the value of V by which the the decrement operation is to performed will be at most 100000.
How can this be done efficiently? I am stuck with this problem for many days. Any help is appreciated.
My Idea : I am thinking to solve this problem offline. I will store all the queries first. then, if somehow I can find the time[After which operation] when some node nd's value becomes less than or equal to zero(say it death time, for each and every node. Then we can do some kind of binary search (probably using Binary Indexed Trees/ Segment Trees) to answer all the queries of second type. But the problem is I am unable to find the death time for each node.
Also I have tried to solve it online using Heavy Light Decomposition but I am unable to solve it using it either.
Thanks!
Given a tree with vertex weights, there exists a vertex that, when chosen as the root, has subtrees whose weights are at most half of the total. This vertex is a "balanced separator".
Here's an O((n + k) polylog(n, k, D))-time algorithm, where n is the number of vertices and k is the number of operations and D is the maximum decrease. In the first phase, we compute the "death time" of each vertex. In the second, we count the live vertices.
To compute the death times, first split each decrease operation into O(log(D)) decrease operations whose arguments are powers of two between 1 and 2^floor(lg(D)) inclusive. Do the following recursively. Let v be a balanced separator, where the weight of a vertex is one plus the number of decrease operations on it. Compute distances from v, then determine, for each time and each power of two, the cumulative number of operations on v with that effective argument (i.e., if a vertex at distance 2 from v is decreased by 2^i, then record a -1 change in the 2^(i - 2) coefficient for v). Partition the operations and vertices by subtree. For each subtree, repeat this cumulative summary for operations originating in the subtree, but make the coefficients positive instead of negative. By putting the summary for a subtree together with v's summary, we determine the influence of decrease operations originating outside of the subtree. Finally, we recurse on each subtree.
Now, for each vertex w, we compute the death time using binary search. The decrease operations affecting w are given in a logarithmic number of summaries computed in the manner previously described, so the total cost for one vertex is log^2.
It sounds as though you, the question asker, know how the next part goes, but for the sake of completeness, I'll describe it. Do a preorder traversal to assign new labels to vertices and also compute for each vertex the interval of labels that comprises its subtree. Initialize a Fenwick tree mapping each vertex to one (live) or zero (dead), initially one. Put the death times and queries in a priority queue. To process a death, decrease the value of that vertex by one. To process a query, sum the values of vertices in the subtree interval.

algorithm to find max independent set in a tree

I need an algorithm to find max independent set in a tree. I'm thinking start from all leaf nodes, and then delete the direct parent nodes to these leaf nodes, then choose the parent nodes of the parent nodes we deleted, repeat this procedure recursively until we get to root. and is this done in O(n) time? any reply is appreciated. thanks.
And could anyone please point me an algorithm to find the max dominating set in a tree.
MAXIMUM INDEPENDENT SET
You can compute the maximum independent set by a depth first search through the tree.
The search will compute two values for each subtree in the graph:
A(i) = The size of the maximum independent set in the subtree rooted at i with the constraint that node i must be included in the set.
B(i) = The size of the maximum independent set in the subtree rooted at i with the restriction that node i must NOT be included in the set.
These can be computed recursively by considering two cases:
The root of the subtree is not included.
B(i) = sum(max(A(j),B(j)) for j in children(i))
The root of the subtree is included.
A(i) = 1 + sum(B(j) for j in children(i))
The size of the maximum independent set in the whole tree is max(A(root),B(root)).
MAXIMAL DOMINATING SET
According to the definition of dominating set in wikipedia the maximum dominating set is always trivially equal to including every node in the graph - but this is probably not what you mean?
Simple and fast approach:
As long as the graph is not empty, iteratively add a leaf v (degree 1) to the independent set V and remove v and its neighbor.
This should work, since
A tree always has a degree-1-vertex,
Adding a degree-1-vertex to V preserves optimality and
The result of such a step again gives a tree or a forest which is a union of trees.
To find the maximal independent set of vertices, we can use an important property of a tree: Every tree is Bipartite i.e. We can color the vertices of a tree using just two colors such that no two adjacent vertices have the same color.
Do a DFS traversal and start coloring the vertices with BLACK and WHITE.
Pick the set of vertices (either BLACK or WHITE) which are more in number. This will give you the maximal independent set of vertices for a tree.
Some Intuition behind the why this algorithm works:
Let us first revisit the definition of the maximal independent set of vertices. We have to pick just one end point of an edge and we have to cover every edge of the tree satisfying this property. We are not allowed to choose both end points of an edge.
Now what does bicoloring of a graph do? It simply divides the set of vertices into two subsets (WHITE and BLACK) and WHITE colored vertices are directly connected to BLACK ones. Thus if we choose either all WHITE or all BLACK ones we are inherently choosing an independent set of vertices. Thus to choose maximal independent
set, go for the subset whose size is larger.

Min cost edge deletions in tree to separate all leaves in a tree

This is from a larger problem, that I have boiled down to the following problem. Given a weighted tree with positive edge weights, and having k leaves. A leaf is a node which has exactly one adjacent node in the tree. I need to delete some edges from the tree so that the tree splits up into k components, with each component containing exactly one of the leaf nodes from the original tree. In other words, I need to delete edges so that all the leaves in the original tree are separated/disconnected from every other leaf of the original tree.
I need to do this in such a way that the sum of weights (cost) of the deleted edges is minimized. It is trivial to show that k-1 edges need to be deleted. So I need to minimize the sum of weights of these k-1 edges.
What is the optimal way to do this? Any hints would be appreciated. Thanks!
I think a greedy algorithm works here.
i.e. delete the lowest weight edge that generates a new component and repeat k-1 times.
Note that you have to be careful for a graph such as:
D<-A->B->C
If you delete B->C first, then deleting A->B does not generate a new component because B was not a leaf so does not need to be separated.
In other words, when selecting the lowest weight edge, do not include any edges that do not still lead to a leaf node.

Resources