Java做题要用到的工具

图的搜索算法:BFS和DFS详解

参考文章:https://www.jianshu.com/p/2226dbe98e06

DFS(深度优先遍历)

深度优先搜索是从起始顶点开始,递归访问其所有邻近节点,比如A节点是其第一个邻近节点,而C节点又是A的一个邻近节点,则DFS访问A节点后再访问C节点,如果C节点有未访问的邻近节点的话将继续访问其邻近节点,否则继续访问A的未访问邻近节点,当所有从A节点出去的路径都访问完之后,继续递归访问除A以外未被访问的邻近节点。
所以上面我们的示意图的遍历顺序会是A->C->B->D->E->F。

BFS(广度优先遍历)

其主要思想是从起始点开始,将其邻近的所有顶点都加到一个队列(FIFO)中去,然后标记下这些顶点离起始顶点的距离为1.最后将起始顶点标记为已访问,今后就不会再访问。然后再从队列中取出最先进队的顶点A,也取出其周边邻近节点,加入队列末尾,将这些顶点的距离相对A再加1,最后离开这个顶点A。依次下去,直到队列为空为止。从上面描述的过程我们知道每个顶点被访问的次数最多一次(已访问的节点不会再访问),而对于连通图来说,每个顶点都会被访问。加上每个顶点的邻接链表都会被遍历,因此BFS的时间复杂度是Θ(V+E),其中V是顶点个数,E是边数,也就是所有邻接表中的元素个数。
所以我们上图的遍历顺序将会为:A->C->D->B->E->F

实现代码如下:
public class Graph {                   //A, B, C,D, E, F
    static int[][] graph = new int[][]{{0, 0, 1, 1, 0, 0},
            {0, 0, 1, 0, 0, 0},
            {1, 1, 0, 0, 0, 0},
            {0, 0, 1, 0, 1, 0},
            {0, 0, 0, 1, 0, 1},
            {0, 0, 0, 0, 1, 0}};
    int[] help = new int[graph.length];//用来记录已经遍历过的元素

    //DFS(深度优先遍历)同样适用于有向图 A->C->B->D->E->F 即 0->2->1->3->4->5
    public void dfsTraversing(int node, int[][] graph) {
        help[node]=1;
        System.out.println(node);
        for (int i = 0; i < graph[node].length; ++i) {
            if (help[i]==0&&i != node&&graph[node][i]==1) {
                dfsTraversing(i, graph);
            }
        }
    }

    //BFS(广度优先遍历)同样适用于有向图 A->C->D->B->E->F 即0->2->3->1->4->5
    public void bfsTraversing(int[][] graph) {
        int[] queue=new int[graph.length];
        int cnt=1;
        queue[0]=0;//将A作为起始顶点加入队列
        help[0]=1;
        System.out.println(0);
        for (int i=0;i<cnt;++i){
            for (int j=0;j<graph[queue[i]].length;++j){
                if (queue[i]!=j&&graph[queue[i]][j]==1&&help[j]==0){
                        help[queue[i]]=1;
                        queue[cnt++]=j;
                        System.out.println(j);
                }
            }
        }
    }
}

DecimalFormat - 格式化数据

参考文章:https://www.jianshu.com/p/c1dec1796062

0占位符的使用
占位符比实际数字多
new DecimalFormat("00.00").format(3.14567);//结果:03.15
new DecimalFormat("0.000").format(3.14);//结果:3.140
new DecimalFormat("00.000").format(3.14);//结果:03.140

占位符比实际数字少
new DecimalFormat("00.00").format(13.14567);//结果:13.15
new DecimalFormat("0.000").format(13.14567);//结果:13.146
new DecimalFormat("0.00").format(13.14567);//结果:13.15
#占位符的使用
占位符比实际数字多
new DecimalFormat("##.##").format(3.14567);//结果:3.15
new DecimalFormat("#.###").format(3.14);//结果:3.14
new DecimalFormat("##.###").format(3.14);//结果:3.14

占位符比实际数字少
new DecimalFormat("#.###").format(13.145678);//结果:13.146
new DecimalFormat("##.##").format(13.14567);//结果:13.15
new DecimalFormat("#.##").format(13.14567);//结果:13.15
DecimalFormat的格式化方式(舍入方式)
DecimalFormat format = new DecimalFormat("#.##");
//指定舍入方式为:RoundingMode.DOWN,直接舍去格式化以外的部分
format.setRoundingMode(RoundingMode.DOWN);
String formatDown = format.format(13.14567);//结果:13.14
//指定舍入方式为:RoundingMode.HALF_UP,四舍五入
format.setRoundingMode(RoundingMode.HALF_UP);
String formatHalfUp = format.format(13.14567);//结果:13.15

Dijkstra算法Java实现

import java.util.Scanner;
import java.util.*;

public class Main {

    private static int IMAX = 10000; //不连通状态
public static int[][] adjMat ={
        {0,1,3,6},
        {1,0,IMAX,6},
        {3,IMAX,0,2},
        {6,6,2,0}
};

    public static void main(String[] args) {

        Dijkstra(adjMat,0,3);
    }

    public static void LoopNode(){
        Node head = new Node();
        Node temp = head;
        for (int i = 0;i<10;i++){
            temp.val = i;
            temp.next = new Node();
            temp = temp.next;
        }
        temp.val = 10;
        temp.next = head;

        Node node1,node2;
        node1 = node2 = head;
        while (true){
            node1 = node1.next;
            node2 = node2.next.next;
            if (node1 == node2)
                break;
        }

        System.out.println("is Loop");

    }

    public static void Dijkstra(int[][] martix,int start,int terminal){
        boolean []isVisted = new boolean[martix.length];
        int []d = new int[martix.length];
        for (int i = 0;i<martix.length;i++){
            isVisted[i] = false; //该点是否被计入,可以理解为判断该点是否已经加入集合B
            d[i] = IMAX;//在当前的集合B所能连接的路径中,从起始点到该点的最短路径
        }
        isVisted[start] = true;
        d[start] = 0;
        int unVisitNode = martix.length ;
        int index = start;
        while (unVisitNode > 0 && !isVisted[terminal] ){
            int min = IMAX;
            //选出集合A中的点到集合B的路径最短的点
            for (int i = 0;i<d.length;i++){
                if (min > d[i] && !isVisted[i]){
                    index = i;
                    min = d[i];
                }
            }
            
            
        for (int i = 0;i<martix.length;i++){
                if (d[index] + martix[index][i] < d[i]) //更新当前的最短路径
                    d[i] = d[index] + martix[index][i];
        }

        unVisitNode -- ;
            isVisted[index] = true;
        }

        System.out.println(d[terminal]);
    }
``
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值