I know how to find the diameter of a tree in linear time, prove the validity of the algorithm used to do so (successive BFS's), and prove why it doesn't work for non-tree connected graphs.

However, I need an algorithm/approach that has better complexity than O(N^2) to find the diameter of a relatively large WEIGHTED pseudotree (a tree with one extra edge).

Please help, and thanks in advance!

Kelements, wherea_{i}is the depth ofi-th tree. You have to find suchiandjthata_{i}+a+j+min(|i-j|,K- |i-j|) is maximized.iand assume that correspondingjcan be found on the right ofi(i.e. betweeni+ 1 and something like ). Now you have a bunch of requests of the form "find maximalj+a_{j}on this subsegment". It can be solved with arbitrary RMQ solution. That would result if for the whole solution.How would I decompose the pseudotree cycle into trees?

After you find the cycle, remove its edges from the graph (e.g. mark its edges are "shall not go here"). You're left with a forest, its components of connectivity are trees growing from the cycle (some trees may be degenerate — i.e. containing a single vertex only).

@yeputons I see! Then step 5 is finding the diameter of each tree in the new forest?

Yes, exactly. I'm sorry if that wasn't clear from my initial message.

Nevermind, my solution was wrong.

Was printing my answer too slow (my english skill is so weak). However, decide to post it.

How does a tree with one extra egde look like? It's a simple cycle with trees rooted at this cycle's nodes. It can be two cases:

1) Simple — diameter lies inside one of this rooted trees. Easy to compute the answer.

2) Main one — diameter enters cycle at vertex

uand leaves atv. Then the answer is the shortest of two pathes in cycle betweenuandv, plush_{u}, plush_{v}, where'sh_{u}is max height ofu's rooted tree.Let's enumerate nodes in our cycle as 0, 1, ...

c- 1 (c— cycle length). Now suppose that diameter enters cycle at node 0. What's the answer in this case? It'sh_{0}+max(h_{1}+ 1,h_{2}+ 2,h_{3}+ 3, ...h_{x}+x) wherexis number around and thenh_{0}+max(h_{x + 1}+ (x- 1),h_{x + 2}+ (x- 2), ...h_{c - 1}+ 1).Let's put values for one half of the cycle (

h_{1}+ 1,h_{2}+ 2, ...h_{x}+x) in some data structure (and the second half's values to another copy of this structure). What does change when we switch from node 0 to node 1? We need to remove one value from this structrure (h_{1}+ 1), then decrease all values by 1, add new value (h_{x + 1}+x) and find maximum of them.So we need a data structure which provides following operations: add/remove elements, decrease all elements by the same number and get maximum element. It's not hard data structure's task and can be done via simple heap/set with some additional easy manipulations to support decreasing. The key observation is — if all elements are decreased at once — maximum element remains the same, only his value is decreased.

The complexity for each DS operation will be

O(log c), so overall complexity will beO(n + c log c). It can be futher reduced toO(1)for operation (and overallO(n)) with using queue that supports its maximum instead of heap/set (because we remove only head elements and add tail ones, not random ones).P.S. Can you provide a link for this task?

I don't know how to prove this but I heard that the same approach for trees can be used to find the diameter of a tree with an extra edge. (Run BFS from an arbitrary vertex, find the farthest vertex and run BFS from this vertex). This works in O(n). I think I heard about this from tehqin's Algorithms Live.

It definitely does not work by running an ordinary BFS. Would using Dijkstra's algorithm in place of a BFS work? I am not able to prove this case.

EDIT: The tree is weighted, as now clarified in the original post.

If this pseudotree is unweighted then it should work.

Counterexample. We start at node 1. Two farthest nodes for it are 3 and 5. We choose 3. For 3 the farthest node is at distance 2. However, diameter is 3 (path from node 2 to node 5).

In episode 1, I ask if this algorithm still holds with one extra edge but don't reveal the answer. Finding a counterexample to the problem was the exercise. The two BFS method doesn't even work on unweighted pseudotrees. I plan to cover this problem in a future episode to teach applications of max queue. :)

I usually use this exercise to argue the two DFS diameter search on trees is unintuitive (until you prove it). At the very least, the two DFS method was unintuitive to me when I was first told it works.

Auto comment: topic has been updated by vamaddur (previous revision, new revision, compare).