《算法分析与设计》Week 4

310. Minimum Height Trees


Description:

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.

Format
The graph contains n nodes which are labeled from 0 to n - 1. You will be given the number n and a list of undirected edges (each edge is a pair of labels).

You can assume that no duplicate edges will appear in edges. Since all edges are undirected, [0, 1] is the same as [1, 0] and thus will not appear together in edges.

Example 1:

Given n = 4edges = [[1, 0], [1, 2], [1, 3]]

        0
        |
        1
       / \
      2   3

return [1]

Example 2:

Given n = 6edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

     0  1  2
      \ | /
        3
        |
        4
        |
        5

return [3, 4]


Solution:

一、题意理解

     给一个有树的特征的无向图,求以哪个节点(可能不只一个)为root,可以使得树的高度最小。


二、分析

     1、可以依次选取节点使用BFS广搜,求得每一个节点做root时树的高度,取其中的最小值,但时间复杂度明显很高,特别是递归做广搜的时候。

     2、讨论区有一个perfect的分析,这里必须要引用一下。

     3、首先我们看一些图的理论:

          (1)一棵树是一个任意两个点都只被一条路径连通的无向图。

          (2)任何有n个节点和n-1条边的连通图是一棵树。

          (3)图中节点的度数是与这个顶点相连的边的个数。

          (4)叶子节点的度数为1。内部节点的度数至少为2.

          (5)路径图是指有两个及以上的节点,并且没有分叉的树。

          (6)如果树中一个节点被指定为根,则这样的一棵树被称为有根树。

          (7)有根树的高度是从根到所有叶子节点的路径的最大值。

      4、回到这个问题上,我们的问题是找到有最小高度的树,并返回他们的,首先我们可以考虑一个简单的例子----路径图,对于一个有n个节点的路径图,找到最小高度的树很简单------只需要将中间的节点设置为根即可。

      5、更一般地,假设我们不知道这个n,而且我们不能随机访问节点,那么我们就得去遍历。这样的话,我们可以从路径的两端开始,以相同的速度向中间移动。当他们相遇或者只差一步(n是奇数还是偶数的区别),我们就得到了想要的节点。

      6、对于一棵无根,我们可以做一些相似的事情。我们用指针从每一个叶节点开始,以相同的速度向内移动。当两个指针相遇时,保存其中的一个,直到最后两个指针相遇或者差一步,我们就找到了想要的根节点

      7、很显然最后相遇的两个指针是图中来自相隔最远的两个叶节点。

      8、实际的实现有点类似BFS拓扑排序。删除叶节点,更新内部顶点的度数,然后删除新的叶节点。这样一层一层的做,直到只剩下2个或1个节点。

      9、空间复杂度和时间复杂度都是O(n)。代码如下:

class Solution {
public:
    vector<int> findMinHeightTrees(int n, vector<pair<int, int>>& edges) {
        vector<int> leaves;
        // 只有一个节点,直接返回0
        if(n == 1) {
            leaves.push_back(0);
            return leaves;
        }
        vector<int> res;
        vector< unordered_set<int> > adj;
        
        // 以邻接表的方式存储图
        for(int i = 0; i < n; ++i) {
            adj.push_back(unordered_set<int>());
        }
        
        // 读入数据,存储图
        for(int i = 0; i < edges.size(); ++i) {
            adj[edges[i].first].insert(edges[i].second);
            adj[edges[i].second].insert(edges[i].first);
        }
        
        // 找到所有的叶子节点
        for(int i = 0; i < n; ++i) {
            if(1 == adj[i].size()) 
                leaves.push_back(i);
        }
        
        // 循环操作
        while(n > 2) {
            n -= leaves.size(); // 删除叶子节点个数
            vector<int> newleaves;
            // 遍历叶子节点表
            for(int i = 0; i < leaves.size(); ++i) {
                // 叶子节点只会跟一个节点相连,所以取邻接表的size()必等于一,取第一个元素即可。
                unordered_set<int>::iterator it = adj[ leaves[i] ].begin();
                int j = *it;
                adj[j].erase( leaves[i] ); // 更新与叶子节点相连的内部节点的邻接表
                if( 1 == adj[j].size())   // 如果因此内部节点变为了叶子节点,则记录
                    newleaves.push_back(j);
            }
            leaves.swap(newleaves); // 更新叶子节点表
        }
        
        // 返回最后剩下的1个或2个叶子节点
        return leaves;
    }

};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值