</pre>题目:<p></p><p><span style="font-family:Times-Roman; font-size:11pt; color:rgb(35,31,32)">The<span style="font-family:Times-BoldItalic; font-size:11pt">diameter <span style="font-family:Times-Roman; font-size:11pt">of a tree <span style="font-family:MT2MIT; font-size:11pt">T <span style="font-family:MT2SYT; font-size:11pt">D <span style="font-family:MT2MIT; font-size:11pt">.V; E/ <span style="font-family:Times-Roman; font-size:11pt">is defined as <img src="https://img-blog.csdn.net/20160219163034925?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><span style="font-family:MT2MIS; font-size:7pt"><span style="font-family:MT2SYS; font-size:7pt"><span style="font-family:MT2MIS; font-size:7pt"><span style="font-family:MT2MIT; font-size:11pt"><span style="font-family:Times-Roman; font-size:11pt">, that is, the<span style="font-size:11pt">largest of all shortest-path distances in the tree. Give an efficient algorithm to<span style="font-size:11pt">compute the diameter of a tree, and analyze the running time of your algorithm.</span></span><br style="orphans:2; widows:2" /></span></span></span></span></span></span></span></span></span></span></span></span></p><p>这题在BFS,想着用BFS,最后放弃了</p><p>DFS做倒是容易</p><p></p><p>下面是DFS解决的代码</p><p>树是用邻接矩阵表示的,如果用邻接表能降低时间复杂度,表达麻烦,所以用矩阵了</p><p></p><p></p><pre name="code" class="cpp">#include<iostream>
using namespace std;
const int N = 8;
int G[ N ][ N ] = {
//0, 1, 2, 3, 4, 5, 6, 7
{ 0, 1, 0, 0, 0, 0, 0, 0 }, // 0
{ 1, 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 0, 1, 0, 0, 0, 1, 0, 0 }, // 2
{ 0, 1, 0, 0, 1, 0, 0, 0 }, // 3
{ 0, 0, 0, 1, 0, 0, 0, 0 }, // 4
{ 0, 0, 1, 0, 0, 0, 1, 4 }, // 5
{ 0, 0, 0, 0, 0, 1, 0, 0 }, // 6
{ 0, 0, 0, 0, 0, 4, 0, 0 }, // 7
};
/*
0-1-2-5-6
| |
3 7
|
4
*/
class Vertex
{
public:
int diameter;
//diameter = max{ son[i].longestWayToLeaf + son[j].longestWayToLeaf + toSon[i] + toSon[j] }(i != j)
int longestWayToLeaf;
//longestWayToLeaf = max{ son[i].longestWayToLeaf + toSon[i] }
Vertex():diameter(0), longestWayToLeaf(0)
{
}
};
//p is the index of v in G
void setLongestWayToLeafAndDiameter(Vertex * vs, int p)
{
if (vs[ p ].diameter > 0 && vs[ p ].longestWayToLeaf > 0)
return;
for (int i = 0; i < N; ++i)
{
if (G[ p ][ i ] > 0)
{
G[ i ][ p ] = 0;// forbidden return back
setLongestWayToLeafAndDiameter(vs, i);
}
}
for (int i = 0; i < N; ++i)
{
for (int j = i + 1; j < N; ++j)
{
if (G[ p ][ i ] > 0 && G[ p ][ j ] > 0)
{
int temp = vs[ i ].longestWayToLeaf + vs[ j ].longestWayToLeaf + G[ p ][ i ] + G[ p ][ j ];
if (vs[ p ].diameter < temp)
{
vs[ p ].diameter = temp;
}
}
}
}
for (int i = 0; i < N; ++i)
{
if (G[ p ][ i ] > 0)
{
int temp = vs[ i ].longestWayToLeaf + G[ p ][ i ];
if (vs[ p ].longestWayToLeaf < temp)
{
vs[ p ].longestWayToLeaf = temp;
}
}
}
}
int main()
{
Vertex * vs = new Vertex[ N ];
setLongestWayToLeafAndDiameter(vs, 0);
for (int i = 0; i < N; ++i)
{
cout << i << "\t(" << vs[ i ].diameter << "\t, " << vs[ i ].longestWayToLeaf << ")\t" << endl;
}
delete[] vs;
}
算法导论22.2-8
最新推荐文章于 2022-03-23 08:43:34 发布