列出连通集 - Java

给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。

输入格式:
输入第1行给出2个整数N(0

输出格式:
按照"{ v​1 v​2 … v​k }"的格式,每行输出一个连通集。先输出DFS的结果,再输出BFS的结果。

输入样例:
8 6
0 7
0 1
2 0
4 1
2 4
3 5
输出样例:
{ 0 1 4 2 7 }
{ 3 5 }
{ 6 }
{ 0 1 2 7 4 }
{ 3 5 }
{ 6 }

注:写的时候踩了个坑,想按照赋值一半的邻接矩阵写的,结果测试2死活过不去…后来才发现TAT

代码:

import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileReader;

public class Main {

    public static void main(String[] args){
        Main self = new Main();
        BufferedReader br = new BufferedReader(new FileReader(FileDescriptor.in));
        try {
            Graph graph = self.createGraph(br);
            self.DFS(graph);
            graph.resetVisited();

            self.BFS(graph);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //处理输入
    public Graph createGraph(BufferedReader br) throws Exception{
        String[] s = br.readLine().trim().split(" ");
        int n = Integer.parseInt(s[0]);
        int e = Integer.parseInt(s[1]);
        Graph graph = new Graph(n,e);
        for (int i = 0; i < graph.getE(); i++) {
            String[] s1 = br.readLine().trim().split(" ");
            int temp1 = Integer.parseInt(s1[0]);
            int temp2 = Integer.parseInt(s1[1]);
            graph.getGraph()[temp1][temp2] = 1;
            graph.getGraph()[temp2][temp1] = 1;
        }
        return graph;
    }

    //DFS
    public void DFS(Graph graph){
        for (int i = 0; i < graph.getN(); i++) {
            if (!graph.getVisited()[i]){
                System.out.print("{ ");
                DFS(graph,graph.getNodes()[i]);
                System.out.println("}");
            }
        }
    }
    //对图中的node节点进行深度优先搜索
    private void DFS(Graph graph, int node){
        graph.getVisited()[node] = true;
        System.out.print(node + " ");
        for (int i = 0; i < graph.getN(); i++) {
            if (graph.getGraph()[node][i] == 1 && !graph.getVisited()[i]){
                DFS(graph, i);
            }
        }
    }

    //BFS
    public void BFS(Graph graph){
        for (int i = 0; i < graph.getN(); i++) {
            if (!graph.getVisited()[i]){
                System.out.print("{ ");
                BFS(graph,graph.getNodes()[i]);
                System.out.println("}");
            }
        }
    }
    //对图中的node节点进行广度优先搜索
    private void BFS(Graph graph, int node) {
        Que que = new Que(graph.getN());
        que.addQue(node);
        graph.getVisited()[node] = true;
        System.out.print(node + " ");
        while (que.getLen() > 0){
            int temp = que.leaveQue();
            for (int i = 0; i < graph.getN(); i++) {
                if (graph.getGraph()[temp][i] == 1 && !graph.getVisited()[i]){
                    System.out.print(i + " ");
                    graph.getVisited()[i] = true;
                    que.addQue(i);
                }
            }
        }
    }
}

//图对象
class Graph{
    private int n;
    private int e;
    private int[] nodes;
    private int[][] graph;
    private boolean[] visited;

    public Graph(int n, int e) {
        this.n = n;
        this.e = e;
        this.graph = new int[this.n][this.n];
        this.visited = new boolean[this.n];
        init();
    }

    private void init(){
        this.nodes = new int[this.n];
        for (int i = 0; i < this.n; i++) {
            this.nodes[i] = i;
        }
    }

    public void resetVisited(){
        for (int i = 0; i < this.visited.length; i++) {
            this.visited[i] = false;
        }
    }

    public int getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }

    public int getE() {
        return e;
    }

    public void setE(int e) {
        this.e = e;
    }

    public int[][] getGraph() {
        return graph;
    }

    public void setGraph(int[][] graph) {
        this.graph = graph;
    }

    public boolean[] getVisited() {
        return visited;
    }

    public void setVisited(boolean[] visited) {
        this.visited = visited;
    }

    public int[] getNodes() {
        return nodes;
    }

    public void setNodes(int[] nodes) {
        this.nodes = nodes;
    }
}

class Que{
    private int[] que;
    private int top;
    private int rear;

    public Que(int len) {
        this.que = new int[len];
        this.top = -1;
        this.rear = -1;
    }

    public void addQue(int node){
        que[++top] = node;
    }

    public int leaveQue(){
        return que[++rear];
    }

    public int getLen(){
        return top-rear;
    }

}

你可能感兴趣的:(java,拼题)