图的深度优先搜索(DFS),广度优先搜索(BFS)与最小生成树(MST)

DFS

package org.exam.ch13.dfs;

/**
 * Created by xin on 15.10.18.
 */
class StackX {
    private final int SIZE = 20;
    private int[] st;
    private int top;
    public StackX() {
        st = new int[SIZE];
        top = -1;
    }
    public void push(int j) {
        st[++top] = j;
    }
    public int pop() {
        return st[top--];
    }
    public int peek() {
        return st[top];
    }
    public boolean isEmpty() {
        return (top == -1);
    }
}
class Vertex{
    public char label;
    public boolean wasVisited;
    public Vertex(char label) {
        this.label = label;
    }
}
class Graph{
    private final int MAX_VERTS=20;
    private Vertex[] vertexList;
    private int[][] adjMat;
    private int nVerts;//默认值为0
    private StackX stackX;
    public Graph(){
        vertexList=new Vertex[MAX_VERTS];//里面元素为空
        adjMat=new int[MAX_VERTS][MAX_VERTS];//元素默认值都为0
        stackX=new StackX();
    }
    public void addVertex(char label){
        vertexList[nVerts++]=new Vertex(label);
    }
    public void addEdge(int start,int end){
        adjMat[start][end]=1;
        adjMat[end][start]=1;
    }
    public void displayVertex(int index){
        System.out.print(vertexList[index].label);
    }
    public int getAdjUnvisitedVertex(int v){
        for (int i = 0; i < nVerts; i++) {
            if (adjMat[v][i]==1&&vertexList[i].wasVisited==false){
                return i;
            }
        }
        return -1;
    }
    public void dfs(){
        vertexList[0].wasVisited=true;
        displayVertex(0);
        stackX.push(0);
        while (!stackX.isEmpty()){
            int curVertex=stackX.peek();
            int v=getAdjUnvisitedVertex(curVertex);
            if (v==-1){
                stackX.pop();
            }else{
                vertexList[v].wasVisited=true;
                displayVertex(v);
                stackX.push(v);
            }
        }
        for (int i = 0; i < nVerts; i++) {
            vertexList[i].wasVisited=false;
        }
    }
}
public class App {
    public static void main(String[] args) {
        Graph graph = new Graph();
        graph.addVertex('A');
        graph.addVertex('B');
        graph.addVertex('C');
        graph.addVertex('D');
        graph.addVertex('E');

        graph.addEdge(0,1);
        graph.addEdge(1,2);
        graph.addEdge(0,3);
        graph.addEdge(3,4);
        System.out.println("Visits:");
        graph.dfs();
        System.out.println();
    }
}

BFS

package org.exam.ch13.bfs;

/**
 * Created by xin on 15.10.20.
 */
class Queue {
    private final int SIZE = 20;
    private int[] queArray;
    private int front;
    private int rear;
    public Queue() {
        queArray = new int[SIZE];
        front = 0;
        rear = -1;
    }
    public void insert(int j) {
        if (rear == SIZE - 1)
            rear = -1;
        queArray[++rear] = j;
    }
    public int remove() {
        int temp = queArray[front++];
        if (front == SIZE)
            front = 0;
        return temp;
    }
    public boolean isEmpty() {
        return (rear + 1 == front || (front + SIZE - 1 == rear));
    }
}
class Vertex {
    public char label;        // label (e.g. 'A')
    public boolean wasVisited;

    public Vertex(char lab) {
        label = lab;
        wasVisited = false;
    }
}
class Graph{
    private final int MAX_VERTS=20;
    private Vertex[] vertexList;
    private int[][] adjMat;
    private int nVerts;//默认值为0
    private Queue queue;
    public Graph(){
        vertexList=new Vertex[MAX_VERTS];//里面元素为空
        adjMat=new int[MAX_VERTS][MAX_VERTS];//元素默认值都为0
        queue=new Queue();
    }
    public void addVertex(char label){
        vertexList[nVerts++]=new Vertex(label);
    }
    public void addEdge(int start,int end){
        adjMat[start][end]=1;
        adjMat[end][start]=1;
    }
    public void displayVertex(int index){
        System.out.print(vertexList[index].label);
    }
    public int getAdjUnvisitedVertex(int v){
        for (int i = 0; i < nVerts; i++) {
            if (adjMat[v][i]==1&&vertexList[i].wasVisited==false){
                return i;
            }
        }
        return -1;
    }
    public void bfs(){
        vertexList[0].wasVisited=true;
        displayVertex(0);
        queue.insert(0);
        int v2;

        while (!queue.isEmpty()) {     // until queue empty,
            int v1 = queue.remove();   // remove vertex at head
            // until it has no unvisited neighbors
            while ((v2 = getAdjUnvisitedVertex(v1)) != -1) {                                  // get one,
                vertexList[v2].wasVisited = true;  // mark it
                displayVertex(v2);                 // display it
                queue.insert(v2);               // insert it
            }   // end while
        }
        for (int i = 0; i < nVerts; i++) {
            vertexList[i].wasVisited=false;
        }
    }

}

public class App {
    public static void main(String[] args) {
        Graph graph = new Graph();
        graph.addVertex('A');
        graph.addVertex('B');
        graph.addVertex('C');
        graph.addVertex('D');
        graph.addVertex('E');

        graph.addEdge(0,1);
        graph.addEdge(1,2);
        graph.addEdge(0,3);
        graph.addEdge(3,4);
        System.out.println("Visits:");
        graph.bfs();
        System.out.println();
    }
}

MST

package org.exam.ch13.mst;

/**
 * Created by xin on 15.10.18.
 */
class StackX {
    private final int SIZE = 20;
    private int[] st;
    private int top;
    public StackX() {
        st = new int[SIZE];
        top = -1;
    }
    public void push(int j) {
        st[++top] = j;
    }
    public int pop() {
        return st[top--];
    }
    public int peek() {
        return st[top];
    }
    public boolean isEmpty() {
        return (top == -1);
    }
}

class Vertex{
    public char label;
    public boolean wasVisited;
    public Vertex(char label) {
        this.label = label;
    }
}

class Graph{
    private final int MAX_VERTS=20;
    private Vertex[] vertexList;
    private int[][] adjMat;
    private int nVerts;//默认值为0
    private StackX stackX;
    public Graph(){
        vertexList=new Vertex[MAX_VERTS];//里面元素为空
        adjMat=new int[MAX_VERTS][MAX_VERTS];//元素默认值都为0
        stackX=new StackX();
    }
    public void addVertex(char label){
        vertexList[nVerts++]=new Vertex(label);
    }
    public void addEdge(int start,int end){
        adjMat[start][end]=1;
        adjMat[end][start]=1;
    }
    public void displayVertex(int index){
        System.out.print(vertexList[index].label);
    }
    public int getAdjUnvisitedVertex(int v){
        for (int i = 0; i < nVerts; i++) {
            if (adjMat[v][i]==1&&vertexList[i].wasVisited==false){
                return i;
            }
        }
        return -1;
    }
    public void mst(){
        vertexList[0].wasVisited=true;
        stackX.push(0);
        while (!stackX.isEmpty()){
            int curVertex=stackX.peek();
            int v=getAdjUnvisitedVertex(curVertex);
            if (v==-1){
                stackX.pop();
            }else{
                vertexList[v].wasVisited=true;
                displayVertex(curVertex);
                displayVertex(v);
                System.out.print(" ");
                stackX.push(v);
            }
        }
        for (int i = 0; i < nVerts; i++) {
            vertexList[i].wasVisited=false;
        }
    }
}

public class App {
    public static void main(String[] args) {
        Graph graph = new Graph();
        graph.addVertex('A');
        graph.addVertex('B');
        graph.addVertex('C');
        graph.addVertex('D');
        graph.addVertex('E');

        graph.addEdge(0,1);
        graph.addEdge(0,2);
        graph.addEdge(0,3);
        graph.addEdge(0,4);
        graph.addEdge(1,2);
        graph.addEdge(1,3);
        graph.addEdge(1,4);
        graph.addEdge(2,3);
        graph.addEdge(2,4);
        graph.addEdge(3,4);

        System.out.print("Minimum spanning tree: ");
        graph.mst();   // minimum spanning tree
        System.out.println();
    }
}

    原文作者:DFS
    原文地址: https://blog.csdn.net/xiejx618/article/details/49363871
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞