基于JAVA的广度优先遍历

该博客展示了如何实现Java中的广度优先搜索(BFS)算法,用于遍历给定的图。代码中定义了一个BFS类,包含一个基于队列的BFS方法,该方法接受一个二维整数数组表示的图和一个起始节点,然后按BFS顺序输出所有可达节点。此外,还提供了一个单元测试方法,从每个节点开始进行BFS并打印结果。运行结果显示了从每个节点开始的BFS路径。
摘要由CSDN通过智能技术生成

算法分析课程作业(仅供参考)

源代码:

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

/**
 * @description: Breadth first search algorithm
 * @author: Qing Zhang
 * @time: 09
 */
public class BFS {

    /**
    * @Description: Breadth first search algorithm based on queue.
    * @Param: [paraGraph : Graph that need to be searched, paraNode : Node currently being traversed]
    * @return: java.lang.String
    */
    public static String breathFirstSearch(int[][] paraGraph, int paraNode){
        Queue<Integer> tempQueue = new LinkedList<>();
        boolean[] visited = new boolean[paraGraph.length];
        visited[paraNode] = true;
        tempQueue.add(paraNode);
        String resString = ""+paraNode;
        int tempNode;
        while(!tempQueue.isEmpty()){
            tempNode = tempQueue.poll();
            for (int i = 0; i < paraGraph.length; i++) {
                if(paraGraph[tempNode][i]==1 && !visited[i]){
                    tempQueue.add(i);
                    visited[i] = true;
                    resString += i;
                }
            }
        }
        return resString;
    }

    /**
     * @Description: Used to test breadth-first traversal.
     * @Param: []
     * @return: void
     */
    public static void unitTestForBFS() {
        int[][] testGraph = new int[][]{
                {0, 1, 0, 1, 0, 0, 1},
                {1, 0, 0, 0, 1, 0, 0},
                {0, 0, 0, 1, 0, 1, 0},
                {1, 0, 1, 0, 0, 0, 0},
                {0, 1, 0, 0, 0, 0, 0},
                {0, 0, 1, 0, 0, 0, 0},
                {1, 0, 0, 0, 0, 0, 0}
        };

        // Breadth-first traversal starting at each node.
        for (int i = 0; i < testGraph.length; i++) {
            String str = BFS.breathFirstSearch(testGraph, i);
            System.out.println("Use the " + (i + 1) + "-th node as the starting position");
            System.out.println(str);
        }
    }

    public static void main(String[] args) {
        BFS.unitTestForBFS();
    }
}

构造数据:
在这里插入图片描述

运行结果:

C:\Users\Administrator\.jdks\temurin-11.0.12-1\bin\java.exe "-javaagent:E:\IntelliJ IDEA 2020.1\lib\idea_rt.jar=49850:E:\IntelliJ IDEA 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\JavaProject\JustForTest\out\production\JustForTest BFS
Use the 1-th node as the starting position
0136425
Use the 2-th node as the starting position
1043625
Use the 3-th node as the starting position
2350164
Use the 4-th node as the starting position
3021654
Use the 5-th node as the starting position
4103625
Use the 6-th node as the starting position
5230164
Use the 7-th node as the starting position
6013425

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值