算法导论22.2-8

</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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值