无向图dfs判断是否有环和是否二分图

1:判断无向图是否有环: 如果图g有环,用dfs遍历图g,
会访问到已经访问过的顶点。 2:判断无向图是否为二分图:
利用gfs遍历图给图上色(2种颜色),然后判断上色顶点间是否冲突。若冲突,则为非二分图。

package Graph;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class Graph {
    private ArrayList<LinkedList<Element>> tableGrap;
    private int v;//顶点数
    private int e;//边数
     
    private boolean[] isVisited;//顶点的访问状态
    private boolean hasCycle;//是否有环
     
    private boolean[] color;//给图上色(两种颜色),用以判断是否二分图
    private boolean isTwoColorable = true;//是否二分图
     
    public Graph(int v,LinkedList<Element> list){
        this.v = v;
        isVisited = new boolean[v];
         
        tableGrap = new ArrayList<>();
        for(int i=0;i<v;i++)
            tableGrap.add(new LinkedList<Element>());
         
        create(list);
    }
 
    private void create(LinkedList<Element> list) {
        // TODO Auto-generated method stub
        for (Element ele : list) {
            tableGrap.get(ele.u).add(ele);
            tableGrap.get(ele.v).add(new Element(ele.v,ele.u));
            e++;
        }
    }
     
    //判断是否二分图
    public void isTwoGraph(){
        isVisited = new boolean[v];
        color = new boolean[v];
         
        for(int s=0;s<v;s++){
            if(!isVisited[s])
                dfs(s);
        }
    }
    //用dfs给顶点上色判断是否二分图
    private void dfs(int u){
        isVisited[u] =  true;
        for(Element ele : tableGrap.get(u)){
            if(!isVisited[ele.v]){
                color[ele.v] = !color[u];
                dfs(ele.v);
            }
            else if(color[ele.v] == color[v]) {
                System.out.println("非二分图")
                isTwoColorable = false;
            }
        }
    }
     
    //是否有环
    public void isCycle(){
        isVisited = new boolean[v];
        for(int s=0;s<v;s++){
            if(!isVisited[s])
                dfs(this,s,s);
        }
    }
     
    //dfs判断是否有环
    private void dfs(Graph graph, int v, int u) {
        // TODO Auto-generated method stub
        isVisited[v] = true;
        for(Element ele : tableGrap.get(v)){
            if(!isVisited[ele.v]){
                dfs(this,ele.v,v);
            }else{
                if(ele.v != u) {
                    hasCycle = true;
                    System.out.println("有环");//访问已经访问过的顶点,说明有环
                }
            }
        }
    }
 
    public static void main(String[] args){
        LinkedList<Element> list = new LinkedList<>();
        list.add(new Element(0,2));
        list.add(new Element(0, 1));
        list.add(new Element(0, 5));
        list.add(new Element(2,1));
        list.add(new Element(2,3));
        list.add(new Element(2,4));
        list.add(new Element(3,4));
        list.add(new Element(5,3));
         
        Graph graph = new Graph(6, list);
        graph.isCycle();
        graph.isTwoGraph();
    }
     
}
 
//
class Element{
    int u;
    int v;
    int weight;
     
    public Element(int u,int v){
        this.u = u;
        this.v = v;
//      this.weight = we
    }
}
    原文作者:DFS
    原文地址: https://blog.csdn.net/haiboself/article/details/51892375
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞