图论系列(六)——图的广度优先遍历及应用2

1. 二分图检测

import java.util.LinkedList;
import java.util.Queue;

public class BipartitionDetection {

    private Graph G;

    private boolean[] visited;
    private int[] colors;		//colors[i]:节点i的颜色:0、1
    private boolean isBipartite = true;

    public BipartitionDetection(Graph G){

        this.G = G;
        visited = new boolean[G.V()];
        colors = new int[G.V()];
        for(int i = 0; i < G.V(); i ++)
            colors[i] = -1;

        for(int v = 0; v < G.V(); v ++)
            if(!visited[v])
                if(!bfs(v)){
                    isBipartite = false;
                    break;
                }
    }

    private boolean bfs(int s){

        Queue<Integer> queue = new LinkedList<>();
        queue.add(s);
        visited[s] = true;
        colors[s] = 0;

        while(!queue.isEmpty()){
            int v = queue.remove();

            for(int w: G.adj(v))
                if(!visited[w]){
                    queue.add(w);
                    visited[w] = true;
                    colors[w] = 1 - colors[v];
                }
                else if(colors[v] == colors[w])	//当前节点与访问过的邻接节点颜色一致
                    return false;
        }
        return true;
    }

    public boolean isBipartite(){
        return isBipartite;
    }
}

2. 无权图的最短路径

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;

// Unweighted Single Source Shortest Path
public class USSSP {
    private Graph G;
    private boolean[] visited;
    private int[] pre;      //保存到达某个点的上个节点的编号
    private int s;          //source:从哪个源头开始
    private int[] dis;      //dis[i]:源点到i节点的距离

    public USSSP(Graph g, int s){
        this.G = g;
        visited = new boolean[g.V()];
        pre = new int[g.V()];
        dis = new int[g.V()];
        for (int i = 0; i < g.V(); i++) {
            pre[i] = -1;
            dis[i] = -1;
        }
        this.s = s;

        bfs(s);
    }

    private void bfs(int s){
        Queue<Integer> queue = new LinkedList<>();
        queue.add(s);
        visited[s] = true;
        pre[s] = s;
        dis[s] = 0;

        while(!queue.isEmpty()){
            int v = queue.remove();

            for(int w:G.adj(v)){
                if(!visited[w]){
                    queue.add(w);
                    visited[w] = true;
                    pre[w] = v;
                    dis[w] = dis[v] + 1;
                }
            }
        }
    }

    public boolean isConnectedTo(int t){
        G.validateVertex(t);
        return visited[t];      //return pre[t] != -1;
                                //return dis[t] != -1;
    }

    public Iterable<Integer> path(int t){
        ArrayList<Integer> res = new ArrayList<>();
        if(!isConnectedTo(t))   return res;

        int cur = t;
        while(cur != s){
            res.add(cur);
            cur = pre[cur];
        }
        res.add(s);

        Collections.reverse(res);
        return res;
    }

    public int dis(int t){
        G.validateVertex(t);
        return dis[t];
    }

    public static void main(String[] args) {
        Graph graph = new Graph("g_5.txt");
        USSSP usssp = new USSSP(graph, 0);
        System.out.println(usssp.isConnectedTo(6));
        System.out.println(usssp.path(6));
        System.out.println(usssp.dis(6));
    }
}

3. BFS与DFS联系

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值