码迷,mamicode.com
首页 > 其他好文 > 详细

图的建立及两种优先搜索实现

时间:2015-10-08 20:02:35      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

利用邻接矩阵法建立一个简单的图,然后利用广度优先搜索(BFS)和深度优先搜索(DFS)测试代码,并实现了深度优先搜索的非递归形式。需要注意的是,由于每次测试前都要初始化图,故每种方法只能单独测试。

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

class GraphVertex { //图的顶点。
    char vertex;
    boolean isVisited;
    public GraphVertex(char vt) {
        vertex = vt; isVisited = false;
    }
    @Override
    public boolean equals(Object obj) {
        return this.vertex == ((GraphVertex)obj).vertex;
    }
    @Override
    public int hashCode() {
        return (this.vertex - ‘0‘);
    }
    @Override
    public String toString() {
        return "" + vertex + ‘\t‘ + isVisited;
    }
}
class Graph {
    int vertexNum;
    GraphVertex[] gVertex; //顶点
    int[][] adjMat; //邻接矩阵
    public Graph(int vertexNum) {
        this.vertexNum = vertexNum;
        gVertex = new GraphVertex[vertexNum];
        adjMat = new int[vertexNum][vertexNum]; //全部元素初始化为0
    }
    public void addVertexs(GraphVertex[] nodes) { //添加顶点
        System.arraycopy(nodes, 0, gVertex, 0, nodes.length);
    }
    public void addEdge(GraphVertex ch1, GraphVertex ch2) { //构造邻接矩阵
        int index1 = getIndex(ch1);
        int index2 = getIndex(ch2);
        adjMat[index1][index2] = 1;
        adjMat[index2][index1] = 1;
    }
    public void BFS(GraphVertex ch) { //广度优先搜索
        Queue<GraphVertex> queue = new LinkedList<GraphVertex>();
        int index = getIndex(ch);
        GraphVertex gv = gVertex[index];
        System.out.print(gv.vertex);
        gv.isVisited = true;
        queue.offer(gv);
        while (!queue.isEmpty()) {
            GraphVertex tmp = queue.poll();
            int i = getIndex(tmp);
            for (int j = 0; j < adjMat[i].length; j++) {
                if(adjMat[i][j] == 1 && gVertex[j].isVisited == false) {
                    System.out.println(gVertex[j].vertex);
                    gVertex[j].isVisited = true;
                    queue.offer(gVertex[j]);
                }
            }
        }
    }
    public void DFS(GraphVertex ch) { // 深度优先搜索(递归实现)
        int index = getIndex(ch);
        GraphVertex gv = gVertex[index];
        System.out.println(gv.vertex);
        gv.isVisited = true;
        for (int j = 0; j < adjMat[index].length; j++) {
            if(adjMat[index][j] == 1 && gVertex[j].isVisited == false) {
                DFS(gVertex[j]);
            }
        }
    }
    public void NonDFS(GraphVertex ch) { //深度优先搜索(非递归实现)
        Stack<GraphVertex> stack = new Stack<GraphVertex>();
        int index = getIndex(ch);
        GraphVertex gv = gVertex[index];
        System.out.println(gv.vertex);
        gv.isVisited = true;
        stack.push(gv);
        while (!stack.isEmpty()) {
            GraphVertex tmp = stack.peek();
            int nextIndex = nextNotVisitVertex(tmp);
            if(nextIndex == -1) 
                stack.pop();
            else {
                GraphVertex vtex = gVertex[nextIndex];
                System.out.println(vtex.vertex);
                vtex.isVisited = true;
                stack.push(vtex);
            }
        }
    }
    
    public int nextNotVisitVertex(GraphVertex tmp) {
        int index = getIndex(tmp);
        for (int j = 0; j < adjMat[index].length; j++) {
            if(adjMat[index][j] == 1 && gVertex[j].isVisited == false) {
                return j;
            }
        }
        return -1;
    }
    public int getIndex(GraphVertex gv) {
        for (int i = 0; i < gVertex.length; i++) {
            if(gv.equals(gVertex[i]))
                return i;
        }
        return Integer.MAX_VALUE;
    }
}
public class TestClass { //测试类
    public static void addEdges(Graph graph, String[] edges) {
        for (int i = 0; i < edges.length; i++) {
            char ch1 = edges[i].charAt(0);
            char ch2 = edges[i].charAt(1);
            graph.addEdge(new GraphVertex(ch1), new GraphVertex((ch2)));
        }
    }
    public static void main(String[] args) {
        char[] vertexs = {‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘};
        String[] edges = {"AB", "AE", "BC", "BD", "BE", "CD", "DE"};
        Graph graph = new Graph(vertexs.length);
        GraphVertex[] nodes = new GraphVertex[vertexs.length];
        for (int i = 0; i < nodes.length; i++) {
            nodes[i] = new GraphVertex(vertexs[i]);
        }
        graph.addVertexs(nodes);
        addEdges(graph, edges);
        
//        graph.BFS(new GraphVertex(‘A‘));
//        graph.DFS(new GraphVertex(‘A‘));
        graph.NonDFS(new GraphVertex(‘A‘));
    }
}

 

图的建立及两种优先搜索实现

标签:

原文地址:http://www.cnblogs.com/lasclocker/p/4861814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!