-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDiameter.cpp
More file actions
80 lines (64 loc) · 1.73 KB
/
getDiameter.cpp
File metadata and controls
80 lines (64 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
vector<bool> visited;
vector<vi> AdjList;
//finds the diameter of a tree
//and returns a vi of the path this diameter follows
//you can get the root vertex by calling
// r = diameter[diameter.size()/2]
// d = diameter.size() whereas s is the diameter
vector<int> get_diameter(int start_node) {
queue<int> q;
visited.assign(AdjList.size(), false);
q.push(start_node);
visited[start_node] = true;
int far = start_node;
while (!q.empty()) {
int u = q.front();
q.pop();
far = u;
for (const auto &item : AdjList[u]) {
if (max(u, item) == max(b1, b2) && min(u, item) == min(b1, b2)) {
continue;
}
int v = item;
if (!visited[v]) {
visited[v] = true;
q.push(v);
}
}
}
vector<int> parent(AdjList.size());
q.push(far);
visited.assign(AdjList.size(), false);
visited[far] = true;
parent[far] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
far = u;
for (const auto &item : AdjList[u]) {
if (min(b1, b2) == min(item, u) && max(b1, b2) == max(item, u)) {
continue;
}
int v = item;
if (!visited[v]) {
visited[v] = true;
parent[v] = u;
q.push(v);
}
}
}
vector<int> path;
while (far != -1) {
path.push_back(far);
far = parent[far];
}
return path;
}
int main() {
vi diameter = get_diameter(0); //start vertex is 0
int root = diameter[diameter.size()/2]
int size = diameter.size()
return 0;
}