基于JAVA的广度优先遍历

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

源代码:

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值