图的邻接矩阵表示、广度优先遍历和深度优先遍历

图的遍历算法
本文介绍了一种使用邻接矩阵表示图的方法,并通过一个具体的例子展示了如何实现图的深度优先遍历和广度优先遍历。文章提供了完整的Java代码实现,帮助读者理解这两种基本的图遍历算法。

如上如的所示,对图节点进行编号,每个节点又有相应的编号和值。因此图可以有一个二阶矩阵来记录各个节点的联通关系,由一个数组来记录各个节点的内容。图的广度优先遍历和深度优先遍历。

输出如下:
深度优先遍历:
1
2
4
8
5
6
3
7
广度优先遍历:
1
2
3
4
5
6
7
8

代码如下:
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 图的邻接矩阵表示
 * 图的广度优先遍历和深度优先遍历
 * @author liyiwen1
 * @date 2017/1/17
 */
public class Graph {
    //存储节点的连接关系
    int[][] connectRelation;
    //节点的内容
    int[] content;

    public static void main(String[] args) {
        int[][] connectRelation = new int[][]{
                {01100000},
                {10011000},
                {10000110},
                {01000001},
                {01000001},
                {00100001},
                {00100001},
                {00011110}
        };
        int[] content = new int[]{12345678};
        Graph graph1 = new Graph();
        graph1.setConnectRelation(connectRelation);
        graph1.setContent(content);
        System.out.println("深度优先遍历:");
        graph1.depthFirstSearch();
        System.out.println("广度优先遍历:");
        graph1.breadthFirstSearch();
    }

    //广度优先遍历
    public void breadthFirstSearch(){
        Deque deque = new ArrayDeque();
        deque.offer(0);
        boolean[] visited = new boolean[this.content.length];//记录节点是否已经被访问
        while (!deque.isEmpty()){
            int node = (Integer)deque.pollFirst();
            if (visited[node] == false){
                System.out.println(this.content[node]);
                visited[node] = true;
                for (int i = 0; i < this.content.length ; ++i){
                    if (this.connectRelation[node][i] == 1){
                        deque.offer(i);
                    }
                }
            }
        }

    }

    //深度优先遍历
    public void depthFirstSearch(){
        boolean[] visited = new boolean[this.content.length];//记录节点是否已经被访问
        depthFirstSearch(visited, 0);
    }

    public void depthFirstSearch(boolean[] visited, int node){
        if (visited[node] == false){//如果节点未被访问
            System.out.println(this.content[node]);
            visited[node] = true;
            for (int i = 0; i < this.content.length; ++i){
                if (this.connectRelation[node][i] == 1){
                    depthFirstSearch(visited, i);
                }
            }
        }
    }

    public int[][] getConnectRelation() {
        return connectRelation;
    }

    public void setConnectRelation(int[][] connectRelation) {
        this.connectRelation = connectRelation;
    }

    public int[] getContent() {
        return content;
    }

    public void setContent(int[] content) {
        this.content = content;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值