LeetCode 261. Graph Valid Tree

Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

For example:

Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.

Hint:

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], what should your return? Is this case a valid tree?
According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
Note: 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.

思路:下面的方法有漏洞!不是充分必要条件!
1. 图的问题比较少,本题需要遍历节点,判断是否所有点相连,且没有loop。用DFS可以判断是否有loop。如何判断是否相连呢?利用数学知识,如果edge数小于n-1,则一定没有完全相连。所以最后思路是,先判断是否edge>=n-1,然后用dfs保证没有loop,从而确定是否是树!!
2. 参考http://www.cnblogs.com/grandyang/p/5257919.html 判定图是树的充分必要条件是:从某一个点入手,遍历一遍临接的点,如果没有loop,同时所有的节点都访问过,那么我们可以说是valid tree。这样做,才符合定义!
2. 无向图,例如[0,1],0的 adjacent list 包含1;1的adjacent list 包含0。DFS的思路是:从0开始,把0标记为visit,然后遍历临接list,然后对list中的节点继续遍历,但是要注意,有可能遍历又回到0去,为了防止遍历回到来的地方,可以设置一个pre变量表明来的地方,当临接list中的点和pre相同,则跳过!

//方法1:DFS
bool validTree(int n, vector<pair<int, int>>& edges) {
    //dfs
   //unordered_map<int,vector<int>> mm;由于是连续数,用vector比用map好!
    vector<vector<int>> mm;
    vector<bool> visited(n,0);
    for(auto&edge:edges){
        mm[edge.first].push_back(edge.second);
        mm[edge.second].push_back(edge.first);
    }
    if(!dfs(mm,visited,0,-1)) return false;
    for(auto&visit:visited)
        if(visit==0) return false;
    return true;
}

bool dfs(vector<vector<int>>& mm,vector<bool>&visited,int i,int pre){
    if(visited[i]) return false;
    visited[i]=1;
    for(auto&k:mm[i]){
        if(k==pre) continue;
        if(!dfs(mm,visited,k,i)) return false;//    
    }
    return true;

}

//方法2:bfs:使用queue,不够简洁。不用计算出真正的距离。计算了反而显得多余!
bool validTree(int n, vector<pair<int, int>>& edges) {
    queue<int> qq;
   //unordered_map<int,vector<int>> mm;由于是连续数,用vector比用map好!
    vector<vector<int>> mm;
    vector<int> distance(n,-1);
    distance[0]=0;
    for(auto&edge:edges){
        mm[edge.first].push_back(edge.second);
        mm[edge.second].push_back(edge.first);
    }
    qq.push(0);
    while(!qq.empty()){
        int cur=qq.top();
        qq.pop();
        int tmp=distance[cur];
        for(auto&k:mm[cur]){
            if(distance[k]){
                if(tmp-distance[k]==1) continue;
                return false;
            }
            distance[k]=tmp+1;
            qq.push(k);     
        }
    }
    for(auto&d:distance)
        if(d==-1) return false;
    return true;
}

//方法3:bfs:使用queue。不用计算距离,只需要标记一个节点是完全没访问(0),访问中(1),所有临接点都访问完(2)
bool validTree(int n, vector<pair<int, int>>& edges) {
    queue<int> qq;
    vector<vector<int>> mm;
    vector<int> visited(n,0);
    visited[0]=1;
    for(auto&edge:edges){
        mm[edge.first].push_back(edge.second);
        mm[edge.second].push_back(edge.first);
    }
    qq.push(0);
    while(!qq.empty()){
        int cur=qq.top();
        qq.pop();
        for(auto&k:mm[cur]){
            if(visited[k]==0){visited[k]=1;qq.push(k);}
            if(visited[k]==1) return false;//遇到cycle    
        }
        visited[cur]=2;//访问完全!
    }
    for(auto&d:visited)
        if(d==0) return false;
    return true;
}


//方法4:参考 ,用union find来判断undirect graph是否有cycle
bool validTree(int n, vector<pair<int, int>>& edges) {
    //新建一个array,存储每个node所在集合的根节点或parent节点
    vector<int> root(n,0);
    for(int i=0;i<n;i++) root[i]=i;//初始化,每个节点代表一个集合,根节点就是自己
    for(auto&edge:edges){
        int f=edge.first;
        int s=edge.second;
        while(root[f]!=f) f=root[f];//回溯查找每条edge开始点的根节点,只有根节点才满足root[f]==f
        while(root[s]!=s) s=root[s];//回溯查找每条edge结束点的根节点,只有根节点才满足root[s]==s
        if(root[s]==root[f]) //查找一条边的两端,发现这条边的两个端点已经属于同一个集合,即:有一条path连接这两点,故:存在cycle
            return false;
        root[s]=f;//更新一个节点的root,让两个集合union:即有相同的root节点
    }
    return edges.size()==n-1;//这个条件充分吗?还是需要去判断root里面的根节点是否全部相等。
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值