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

图的表示与遍历

时间:2014-09-11 17:03:02      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   java   ar   for   art   div   

图的表示:连接矩阵,连接链表。

图的遍历:dfs(递归、非递归),bfs.

 

连接矩阵下各种遍历:

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

public class Graph {
    int v;
    int e;
    int[][] mat;

    public Graph(int v) {
        this.v = v;
        this.e = 0;
        mat = new int[v][v];
    }

    public void addEdge(int a, int b) {
        if (mat[a][b] == 0)
            e++;
        mat[a][b] = 1;
        mat[b][a] = 1;
    }

    public Iterable<Integer> adj(int a) {
        List<Integer> res = new ArrayList<Integer>();
        for (int i = 0; i < v; i++) {
            if (mat[a][i] != 0) {
                res.add(i);
            }
        }
        return res;
    }

    public void dfsNonRecur() {
        boolean[] vis = new boolean[v];
        for (int i = 0; i < v; i++) {
            if (!vis[i]) {
                dfsNonRecur(i, vis);
            }
        }
        System.out.println();

    }

    public void dfs() {
        boolean[] vis = new boolean[v];
        for (int i = 0; i < v; i++) {
            if (!vis[i]) {
                dfs(i, vis);
            }
        }
        System.out.println();

    }

    private void dfs(int i, boolean[] vis) {
        vis[i] = true;
        System.out.print(i + " ");
        for (Integer each : adj(i)) {
            if (!vis[each])
                dfs(each, vis);
        }
    }

    private void dfsNonRecur(int i, boolean[] vis) {
        Stack<Integer> stack = new Stack<Integer>();
        vis[i] = true;
        stack.push(i);
        System.out.print(i + " ");

        while (!stack.isEmpty()) {
            Integer top = stack.peek();
            boolean finished = true;
            for (Integer each : adj(top)) {
                if (!vis[each]) {
                    System.out.print(each + " ");
                    vis[each] = true;
                    stack.push(each);
                    finished = false;
                    break;
                }
            }
            if (finished) {
                if (!stack.isEmpty())
                    stack.pop();
            }

        }

    }

    public void bfs() {
        boolean[] vis = new boolean[v];
        for (int i = 0; i < v; i++) {
            if (!vis[i]) {
                bfs(i, vis);
            }
        }
        System.out.println();

    }

    public void bfs(int start, boolean[] vis) {
        Queue<Integer> queue = new LinkedList<Integer>();
        queue.add(start);
        vis[start] = true;
        System.out.print(start + " ");
        while (!queue.isEmpty()) {
            Integer out = queue.remove();
            for (Integer each : adj(out)) {
                if (!vis[each]) {
                    vis[each] = true;
                    queue.add(each);
                    System.out.print(each + " ");
                }
            }

        }

    }

    public static void main(String[] args) {
        Graph g = new Graph(5);
        g.addEdge(0, 1);
        g.addEdge(0, 3);
        g.addEdge(1, 2);
        g.addEdge(3, 4);

        g.dfs();
        g.dfsNonRecur();
        g.bfs();
    }

}

 

参考资料:

http://algs4.cs.princeton.edu/41undirected/AdjMatrixGraph.java.html

http://algs4.cs.princeton.edu/41undirected/Graph.java.html

http://blog.csdn.net/lalor/article/details/6845788

图的表示与遍历

标签:style   blog   http   color   java   ar   for   art   div   

原文地址:http://www.cnblogs.com/jdflyfly/p/3966761.html

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