RainX_69

Print all nodes which are the endpoint of the diameter of a tree (IMPORTANT)

Jan 19th, 2023 (edited)
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | Source Code | 0 0
  1. Print all nodes which are the endpoint of the diameter of a tree
  2. Given a tree with n nodes, Print all nodes which are the endpoint of the diameter. 1<= n <=10^9.
  3. E.g. edges={{0,1},{2,1},{1,3},{3,4},{3,5},{3,6},{6,7},{6,8}};
  4.      Answer is -> Maximum diameter is -> 4
  5.                   Answer Nodes -> 0 , 2 , 7 , 8
  6.  
  7. ---------------------------------------------------------------------------------------------------------------------------------------
  8.  
  9.  
  10. -> Summary of below algorithm
  11.  
  12. 1) Using 2 DFS method, found out two nodes(say u,v) that serves as the end points of a longest diameter. Our task now is to find other diameters with this same length
  13.  
  14. 2) DFS from both the nodes to obtain distance vector from the said node to all other nodes
  15.  
  16. 3) Traverse both vectors and store indices which hosts largest diameter. There might be cases of duplicate nodes, in such cases, use a set inorder to eliminate duplicates. An example where duplicates are - [[1,4],[1,2],[1,5],1,3]], or you can say any star graph will print duplicates. So for that use a set.
  17.  
  18. ---------------------------------------------------------------------------------------------------------------------------------------
  19.  
  20.  
  21. # include <bits/stdc++.h>
  22. using namespace std;
  23.  
  24. vector<int> *adj;
  25.  
  26. void dfs(int node, int par, vector<int> &d){
  27.     for(auto nei: adj[node]){
  28.         if(nei==par){
  29.             continue;
  30.         }
  31.         if(d[nei]>d[node]+1){
  32.             d[nei]=d[node]+1;
  33.             dfs(nei,node,d);
  34.         }
  35.     }
  36. }
  37.  
  38. vector<int> getDiaInfo(int n){
  39.     vector<int> dist1(n,INT_MAX);
  40.     vector<int> dist2(n,INT_MAX);
  41.    
  42.     dist1[0]=0;
  43.     dfs(0,-1,dist1);
  44.    
  45.     int u=max_element(dist1.begin(),dist1.end())-dist1.begin();
  46.    
  47.     dist2[u]=0;
  48.     dfs(u,-1,dist2);    
  49.    
  50.     int v=max_element(dist2.begin(),dist2.end())-dist2.begin();
  51.    
  52.     return {u,v,dist2[v]};
  53. }
  54.  
  55. int main(){
  56.     int n;
  57.     vector<vector<int>> edges;
  58.    
  59.     // TEST CASE: 1
  60.    
  61.     n=9;
  62.     edges={{0,1},{2,1},{1,3},{3,4},{3,5},{3,6},{6,7},{6,8}};
  63.    
  64.    
  65.     // n=7;
  66.     // edges={{0,2},{1,2},{2,3},{3,4},{4,5},{4,6}};
  67.    
  68.     adj=new vector<int>[n];
  69.  
  70.     for(auto edge: edges){
  71.         adj[edge[0]].push_back(edge[1]);
  72.         adj[edge[1]].push_back(edge[0]);
  73.     }
  74.    
  75.     auto info=getDiaInfo(n); // returns two nodes who are ends of the largest diameter and the diameter
  76.    
  77.     int node1=info[0];
  78.     int node2=info[1];
  79.     int diameter=info[2];
  80.    
  81.    
  82.     vector<int> d1(n,INT_MAX);
  83.     vector<int> d2(n,INT_MAX);
  84.    
  85.     d1[node1]=0;
  86.     dfs(node1,-1,d1);
  87.    
  88.     d2[node2]=0;
  89.     dfs(node2,-1,d2);
  90.    
  91.    
  92.     set<int> nodes;
  93.    
  94.     for(int i=0;i<n;i++){
  95.         if(d1[i]==diameter){
  96.             nodes.push_back(i);
  97.         }
  98.         if(d2[i]==diameter){
  99.             nodes.push_back(i);
  100.         }
  101.     }
  102.    
  103.     cout<<"Maximum diameter is -> "<<diameter<<endl;
  104.     cout<<"Answer Nodes -> ";
  105.     for(auto v: nodes){
  106.         cout<<v<<" , ";
  107.     }
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment