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

PTA上的题目答案绝大多数都是c或者c++的,很少有Java的,这是我和一个学C的朋友一起写出来的,分享给大家
先给大家看看结果,Java解决给定一个有N个顶点和E条边的无向图,请用DFS和BFS分别列出其所有的连通集。假设顶点从0到N−1编号。进行搜索时,假设我们总是从编号最小的顶点出发,按编号递增的顺序访问邻接点。_第1张图片
代码如下,我也有些不太理解的地方,欢迎大家留言,一起讨论
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("}");
        }
    }
}
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);
        }
    }
}

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;
}

}

你可能感兴趣的:(PTA)