Skip to content

Tree Algorithm Added #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added src/Graph/Graph.md
Empty file.
46 changes: 46 additions & 0 deletions src/Graph/Tree/Diameter/diameter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

vector<vector<int>> tree ;
vector<int> visited, toLeaf, path_length;

void dfs(int node){
visited[node] = true;
vector<int> length = {-1};
for(int child : tree[node]){
if(visited[child])
continue;
dfs(child);
toLeaf[node] = max(toLeaf[node], 1 + toLeaf[child]);
length.push_back(toLeaf[child]);
}
int n = length.size(), m = min((int)length.size(),2);
for(int i = 0; i < m; i++){
for(int j = i+1; j < n; j++){
if(length[i] < length[j])
swap(length[i], length[j]);
}
path_length[node] += length[i] + 1;
}
}

int main(){
int n;
cin >> n;
int m = n - 1;
tree.resize(n+1), toLeaf.resize(n+1,0), path_length.resize(n+1,0), visited.resize(n+1,false);
while(m--){
int x, y;
cin >> x >> y;
tree[x].push_back(y);
tree[y].push_back(x);
}
int root = 1;
dfs(root);
int diameter = *max_element(path_length.begin(), path_length.end());
cout << diameter << "\n";
return 0;
}
97 changes: 97 additions & 0 deletions src/Graph/Tree/Diameter/diameter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
## **Diameter of a tree**
The Diameter of tree is the *maximum* length between two nodes. For example : <br>
Consider the following tree of 7 nodes

<div align = "center">
<img height = "100" src = "https://user-images.githubusercontent.com/58760297/99883654-e1a9b980-2c4e-11eb-979c-02dce3dd276d.png"/>
</div><br>

Here, *Diameter* = 4 .

## **Algorithm**

First, root the tree arbitarily.
<div align = "center">
<img height = "175" src = "https://user-images.githubusercontent.com/58760297/99886616-fe042100-2c63-11eb-9334-81907b4c1ba6.png"/>
</div><br>

For each *node*, we calculate *toLeaf(node)* which denotes
*maximum* length of a path from the *node* to any *leaf*.<br>

```
if node is leaf :
toLeaf[node] = 0
else
toLeaf[node] = 1 + max(toLeaf[child]) | for all child of node
```

<br>

We can use DFS to calculate *toLeaf(node)*.<br>
```cpp
vector<int> toLeaf(n+1, 0); // n is no. of nodes
void dfs(int node){
visited[node] = true;
for(int child : tree[node]){
if(visited[child])
continue;
dfs(child);
toLeaf[node] = max(toLeaf[node], 1 + toLeaf[child]);
}
}
```
<br>
<div align = "center">
<img height = "175" src = "https://user-images.githubusercontent.com/58760297/99886436-8da8d000-2c62-11eb-8c39-27906df824e5.png"/>
</div><br>



Now calculate *path_length(node)* which denotes *maximum* length of a path whose highest point is node.

```
if node is leaf :
path_length[node] = 0
else if node has only 1 child :
path_length[node] = toLeaf[child] + 1
else
Take two distinct child a,b such that (toLeaf[a] + toLeaf[b]) is maximum, then
path_length[node] = (toLeaf[a] + 1) + (toLeaf[b] + 1)

```
Here is the implementation .
```cpp
vector<int> toLeaf(n+1, 0), path_length(n+1, 0);
void dfs(int node){
visited[node] = true;
vector<int> length = {-1}; // allows us to handle the cases when node has less than 2 children
for(int child : tree[node]){
if(visited[child])
continue;
dfs(child);
toLeaf[node] = max(toLeaf[node], 1 + toLeaf[child]);
length.push_back(toLeaf[child]);
}
int s = length.size(), m = min((int)length.size(),2);
for(int i = 0; i < m; i++){
for(int j = i+1; j < s; j++){
if(length[i] < length[j])
swap(length[i], length[j]);
}
path_length[node] += length[i] + 1;
}
}
```

<br>
<div align = "center">
<img height = "175" src = "https://user-images.githubusercontent.com/58760297/99886528-3a834d00-2c63-11eb-8671-4d7eb16560c4.png"/>
</div><br>

Finally, *Diameter = maximum* of all lengths in path_length. Therefore here, *Diameter* = 4.

## *Problems*
- [Tree Diameter](https://cses.fi/problemset/task/1131/)
## *Reference*
- Competitive Programmer's Handbook by Antii Laaksonen.

9 changes: 9 additions & 0 deletions src/Graph/Tree/Tree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## **Tree**

We can define *Tree* as a connected undirected graph with *no cycles* .

There are some more ways we can define Tree . Here are some equivalent definitions :

- connected undirected graph with N nodes and N-1 edges.
- connected undirected graph with only unique paths i.e there is one and only path from one node to another.
- connected undirected graph where if you remove 1 edge it no longer remains connected.
6 changes: 4 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
- [Introduction](./introduction.md)
- [Persistent Data Structure](./PersistentDS/README.md)
- [Persistent Segment Trees](./PersistentDS/persistentST.md)


- [Graph](./Graph/Graph.md)
- [Tree](./Graph/Tree/Tree.md)
- [Diameter](./Graph/Tree/Diameter/diameter.md)