Calculate the diameter of a tree

  • Context: MHB 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Diameter Tree
Click For Summary

Discussion Overview

The discussion revolves around calculating the diameter of a tree using the Breadth-first search (BFS) algorithm. Participants explore the theoretical underpinnings of the diameter concept, its calculation methodology, and the implications of the BFS algorithm in determining the longest path within a tree structure.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants outline the definition of the diameter of a tree as the maximum distance between any two vertices, represented mathematically.
  • There is a proposed method using BFS to find the diameter, which involves two steps: first finding the furthest node from an arbitrary starting node, and then finding the furthest node from that node.
  • One participant questions the reasoning behind calculating the diameter as the sum of distances from the starting node to the furthest node and then to another node, suggesting it should be the sum of those distances.
  • Another participant suggests that the diameter can be understood as the height of the left and right subtrees combined, indicating a structural interpretation of the tree.
  • There are discussions about the implications of directional versus bidirectional edges in the tree structure and how that affects the BFS implementation.
  • Participants express uncertainty about the correct identification of nodes during the BFS process and the implications for calculating the diameter.

Areas of Agreement / Disagreement

Participants generally agree on the method of using BFS to calculate the diameter, but there is disagreement regarding the interpretation of the distances involved and whether the diameter should be calculated as a simple sum of distances or through a more nuanced understanding of tree structure.

Contextual Notes

Some assumptions about the tree structure, such as the nature of edges (directed vs. bidirectional), are not fully resolved. Additionally, there are unresolved questions about the identification of nodes during the BFS process and how that impacts the calculation of the diameter.

Who May Find This Useful

This discussion may be useful for students and practitioners interested in graph theory, particularly those looking to understand tree structures and algorithms for calculating properties like diameter using BFS.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hey! (Smile)

$$T=(V,E) \text{ tree }$$
$$\text{diameter of a tree } = \max_{u,v \in V} \delta(u,v)$$

$$\delta(u,v)=\text{the length of the shortest path from the vertex u to the vertex v}$$

How can we calculate the diameter of a tree,when we are given the algorithm of the Breadth-first-search ? (Wait)

Code:
    Breadthfirstsearch(G,s)
    for each u ∈ V \ {s}
         color[u]<-white
         d[u]<-oo
         p[u]<-Ø
    color[s]<-gray
    d[s]<-0
    p[s]<-Ø
    Q<-Ø 
    Insert(Q,s)
    while Q ≠ Ø
           u<-Del(Q)
           for each v  ∈ Adj(u)
              if color[v]=white then
                 color[v]<-gray
                 d[v]=d[u]+1
                 p[v]<-u
                 Insert(Q,v)
    
           color[u]<-black

According to my notes,we could do the following:

  1. We implement the Breadth-first search from a given initial node $s$.
    We calculate $d$ for all the vertices.
    Let $u$ the vertex,such that: $$\delta(s,u)=d=\max_{v \in V} d[v]$$

    [*] We implement the Breadth-first search with the vertex $u$,as the initial node.
    Let $w$ the vertex,such that:
    $$\delta(u,w)=d[w]=\max_{v \in V} d[v]$$


Then,the diameter of $T$ is equal to $\delta(u,w)$.

Could you explain me why we calculate the diameter in this way? (Thinking)
 
Physics news on Phys.org
evinda said:
Hey! (Smile)

$$T=(V,E) \text{ tree }$$
$$\text{diameter of a tree } = \max_{u,v \in V} \delta(u,v)$$

$$\delta(u,v)=\text{the length of the shortest path from the vertex u to the vertex v}$$

How can we calculate the diameter of a tree,when we are given the algorithm of the Breadth-first-search ? (Wait)

Code:
    Breadthfirstsearch(G,s)
    for each u ∈ V \ {s}
         color[u]<-white
         d[u]<-oo
         p[u]<-Ø
    color[s]<-gray
    d[s]<-0
    p[s]<-Ø
    Q<-Ø 
    Insert(Q,s)
    while Q ≠ Ø
           u<-Del(Q)
           for each v  ∈ Adj(u)
              if color[v]=white then
                 color[v]<-gray
                 d[v]=d[u]+1
                 p[v]<-u
                 Insert(Q,v)
    
           color[u]<-black

According to my notes,we could do the following:

  1. We implement the Breadth-first search from a given initial node $s$.
    We calculate $d$ for all the vertices.
    Let $u$ the vertex,such that: $$\delta(s,u)=d=\max_{v \in V} d[v]$$

    [*] We implement the Breadth-first search with the vertex $u$,as the initial node.
    Let $w$ the vertex,such that:
    $$\delta(u,w)=d[w]=\max_{v \in V} d[v]$$


Then,the diameter of $T$ is equal to $\delta(u,w)$.

Could you explain me why we calculate the diameter in this way? (Thinking)


Hi evinda! (Blush)

The diameter is the length of the longest path in the tree.
To find it, we need to find the ends of this longest path.

That is a 2-step process.
First we start at an arbitrary node ($s$) and find the furthest node from it ($u$).
This is one of those 2 ends of the longest path.
When we find the furthest node from $u$, we have found the longest path.
Its length is the diameter. (Mmm)
 
I like Serena said:
Hi evinda! (Blush)

The diameter is the length of the longest path in the tree.
To find it, we need to find the ends of this longest path.

That is a 2-step process.
First we start at an arbitrary node ($s$) and find the furthest node from it ($u$).
This is one of those 2 ends of the longest path.
When we find the furthest node from $u$, we have found the longest path.

Could you explain me further why we find the shortest distance from the node $s$ to the node $v$ and then the shortest distance from the node $v$ to the node $w$? (Thinking)

I like Serena said:
Its length is the diameter. (Mmm)

So,shouldn't the diameter be equal to $\delta(s,u)+ \delta(u,w)$ ? :confused:
 
evinda said:
Could you explain me further why we find the shortest distance from the node $s$ to the node $v$ and then the shortest distance from the node $v$ to the node $w$? (Thinking)

So,shouldn't the diameter be equal to $\delta(s,u)+ \delta(u,w)$ ? :confused:

Suppose we pick an example.

Say:
Binary-Search-Tree-144ef511.png


Suppose we pick (1) as node $s$.
What would $u$ and $w$ be? (Thinking)
 
I like Serena said:
Suppose we pick an example.

Say:Suppose we pick (1) as node $s$.
What would $u$ and $w$ be? (Thinking)

I wanted to implement now the Breadth-first search,with the node $(1)$ as node $(s)$.
At the beginning,we have:

View attachment 3082

and then:

View attachment 3083

But..then, does the queue remain empty,because of the fact that the node $1$ has no children or is it: $Adj(1)=\{ 3 \}$ ? (Thinking)
 

Attachments

  • q.png
    q.png
    343 bytes · Views: 126
  • q.png
    q.png
    290 bytes · Views: 118
evinda said:
I wanted to implement now the Breadth-first search,with the node $(1)$ as node $(s)$.
At the beginning,we have:

View attachment 3082

and then:

View attachment 3083

But..then, does the queue remain empty,because of the fact that the node $1$ has no children or is it: $Adj(1)=\{ 3 \}$ ? (Thinking)

With the edges directed as they are, you are quite right.
Good! (Smile)

So, to make the example more interesting, let's pretend that all those directional edges are really bidirectional edges.
What would you get then? (Wondering)
 
I like Serena said:
So, to make the example more interesting, let's pretend that all those directional edges are really bidirectional edges.
What would you get then? (Wondering)

Picking $(1)$ as node $s$,I got the following:

View attachment 3084

So, $u=13$ and $\delta(s,u)=5$

Then,picking $(13)$ as node $s$,I got:

View attachment 3085

So,the diameter is equal to $6$,since this is the length of the longest path that exists at the tree,right? (Thinking)
 

Attachments

  • node.png
    node.png
    3.4 KB · Views: 119
  • node.png
    node.png
    3.4 KB · Views: 125
evinda said:
Picking $(1)$ as node $s$,I got the following:

So, $u=13$ and $\delta(s,u)=5$

Then,picking $(13)$ as node $s$,I got:

So,the diameter is equal to $6$,since this is the length of the longest path that exists at the tree,right? (Thinking)

Right! (Smile)

Actually, in the 2nd step, you shouldn't have picked (13) as $s$.
It is still called $u$ and you're supposed to find $w$.
evinda said:
Could you explain me further why we find the shortest distance from the node $s$ to the node $v$ and then the shortest distance from the node $v$ to the node $w$? (Thinking)So,shouldn't the diameter be equal to $\delta(s,u)+ \delta(u,w)$ ? :confused:

Can you answer your own questions now? (Wondering)
 
I like Serena said:
Actually, in the 2nd step, you shouldn't have picked (13) as $s$.
It is still called $u$ and you're supposed to find $w$.

Oh yes,you are right! (Nod)

I like Serena said:
Can you answer your own questions now? (Wondering)

I will give it a try.. (Wasntme)

Could you explain me further why we find the shortest distance from the node s to the node v and then the shortest distance from the node v to the node w?

We choose an initial node $s$.
Then,we find the distance from $s$ to all the other nodes and call $u$,the node to which the distance from $s$ is the greatest.
Now,we start from $u$ and want to find a node,in order the length of the path to be the greatest possible.

So,the diameter of a tree is the height of the left subtree plus the height of the right subtree.
With $\delta(s,u)$ ,we find a node $u$, to which the distance from the root $s$ is the greatest .The node $u$ will be at the last level of one of the subtrees. Then,we are looking for a vertex $w$,that has the greatest distance from the vertex $v$,so it will be at the last level of the other subtree.

Now,we have found at least two nodes from the last level of both the left and right subtree and the length of the path from the node of the last level of the left subtree to the one of the right subtree will be the longest path,and also the diamater of the tree.

So,shouldn't the diameter be equal to δ(s,u)+δ(u,w) ?

No, adding $\delta(s,u)$ by $\delta(u,w)$ we add twice the length of the path from the vertex $s$ to the vertex $u$.Is my reasoning ok? (Blush)
 
  • #10
evinda said:
I will give it a try.. (Wasntme)

We choose an initial node $s$.
Then,we find the distance from $s$ to all the other nodes and call $u$,the node to which the distance from $s$ is the greatest.
Now,we start from $u$ and want to find a node,in order the length of the path to be the greatest possible.

Good! :)
So,the diameter of a tree is the height of the left subtree plus the height of the right subtree.

That doesn't sound quite right. It's not true for our example.
It would only be true for a node that is already on the longest path.
And then there might still be more than 2 sub trees.
With $\delta(s,u)$ ,we find a node $u$, to which the distance from the root $s$ is the greatest .The node $u$ will be at the last level of one of the subtrees. Then,we are looking for a vertex $w$,that has the greatest distance from the vertex $v$

Yes! (Yes)

so it will be at the last level of the other subtree.
Now,we have found at least two nodes from the last level of both the left and right subtree

No.

and the length of the path from the node of the last level of the left subtree to the one of the right subtree will be the longest path,and also the diamater of the tree.

Yes.

No, adding $\delta(s,u)$ by $\delta(u,w)$ we add twice the length of the path from the vertex $s$ to the vertex $u$.

You do add twice a length, but it's the length from $s$ to the first common node in both paths. (Thinking)
 
  • #11
I like Serena said:
That doesn't sound quite right. It's not true for our example.
It would only be true for a node that is already on the longest path.
And then there might still be more than 2 sub trees.
So,would it only be true for $s=(8)$ ? (Thinking)
I like Serena said:
No.

Why? (Thinking)

I like Serena said:
You do add twice a length, but it's the length from $s$ to the first common node in both paths. (Thinking)

A ok.. (Nod)
 
  • #12
evinda said:
So,would it only be true for $s=(8)$ ? (Thinking)

Node (8) is the root, which happens to be part of the longest path.
But this is not necessarily the case.

However, you could treat any node that is on the longest path as the root of the tree.
In that case the diameter is the sum of the height of 2 sub trees of this node. (Nerd)
Why? (Thinking)

Because your statement about a left and right subtree is not correct.
That's why what you write here about the left and right subtree is also not correct. (Wasntme)
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
23
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K