图论算法的矩阵实现

本文介绍了使用矩阵来实现图论中的四种基本算法:宽度优先搜索(BFS)、深度优先搜索(DFS)、普里姆算法(PRIM)用于求最小生成树以及克鲁斯卡尔算法(Kruskal)。通过具体的Java代码示例,详细展示了如何利用矩阵表示图,并在图中进行遍历和求解最小生成树。
摘要由CSDN通过智能技术生成

图论算法的矩阵写法
BFS_m

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int start = sc.nextInt();
        int matrix[][] = new int[n][n];
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                matrix[i][j] = sc.nextInt();
            }
        }
        bfs_m(matrix,start);
    }
    public static void bfs_m(int[][] matrix,int start){
        Queue<Integer> queue = new ArrayDeque<>();
        HashSet<Integer> set = new HashSet<>();
        queue.add(start);
        set.add(start);
        while (!queue.isEmpty()){
            int n = queue.poll();
            System.out.println(n);
            for (int i=0;i<matrix[0].length;i++){
                if (matrix[n][i]>0&&!set.contains(i)){
                    queue.add(i);
                    set.add(i);
                }
            }
        }
    }```


---------------------------------------------------------------------------------------------------------------------
DFS

```java
public class DFS_m {
    static HashSet<Integer> set = new HashSet<>();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int start = sc.nextInt();
        int matrix[][] = new int[n][n];
        for (int i=0;i<n;i++){
            for (int j=0;j<n;j++){
                matrix[i][j] = sc.nextInt();
            }
        }
        dfs_m(matrix,start);
    }
    public static void dfs_m(int[][] matrix,int start){
        System.out.println(start);
        set.add(start);
        for (int i=0;i<matrix.length;i++){
            if (matrix[start][i]>0&&!set.contains(i)){
                dfs_m(matrix,i);
            }
        }
    }
}```
-----------------------------------------------------------------
PRIM

```java
public class Prim_m {
    public static void main(String[] args) {
        int m = Integer.MAX_VALUE;
        int[][] matrix = new int[][]{
                {m, 5, 1, 2, m},
                {5, m, 3, m, 4},
                {1, 3, m, 6, 2},
                {2, m, 6, m, 3},
                {m, 4, 2, 3, m}
        };
        prim_m(matrix, 0);
    }

    public static void prim_m(int[][] matrix, int start) {
        int[] lowcost = new int[matrix.length];
        int[] visited = new int[matrix.length];
        int i = 0, j = 0, k = 0, min = Integer.MAX_VALUE;
        int v = start;
        for (i = 0; i < matrix.length; i++) {
            lowcost[i] = matrix[start][i];
            visited[i] = 0;
        }
        visited[v] = 1;
        int sum = 0;
        for (i = 0; i < matrix.length - 1; i++) {//执行n-1次,每次放入一条边
            min = Integer.MAX_VALUE;
            //选出候选边中的最小值
            for (j = 0; j < matrix.length; j++) {
//                System.out.print(lowcost[j]+" ");
                if (visited[j] == 0 && lowcost[j] < min) {
                    min = lowcost[j];//记录最小值
                    k = j;//记录位置
                }
            }
            visited[k] = 1;
            v = k;//v变成k,更新最短边
            sum += min;
            for (j = 0; j < matrix.length; j++) {
                if (visited[j] == 0 && matrix[v][j] < lowcost[j]) {
                    lowcost[j] = matrix[v][j];
                }
            }
        }
        System.out.println(sum);
    }
}

Kruskal

public class Kruskal_t {
    public static void main(String[] args) {
        int[][] matrix = new int[][]{
                {0,1,5},
                {0,2,1},
                {0,3,2},
                {1,2,3},
                {1,4,4},
                {2,3,6},
                {2,4,2},
                {3,4,3}
        };
        kruskal_t(matrix);
    }
    public static int[] v;
    public static int getRoot(int a){
        while (a!=v[a]){
            a = v[a];
        }
        return a;
    }
    public static void kruskal_t(int[][] matirx){
        v = new int[matirx.length];
        for (int i=0;i<matirx.length;i++){
            v[i] = i;
        }
        int i=0,a=0,b=0;
        int sum = 0;
        Arrays.sort(matirx, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[2]-o2[2];
            }
        });
        for (i=0;i<matirx.length;i++){
            a = getRoot(matirx[i][0]);
            b = getRoot(matirx[i][1]);
            if (a!=b){
                v[a] = b;
                sum+=matirx[i][2];
            }
        }
        System.out.println(sum);
    }
}


Floyd

public class Floyd {
    //邻接矩阵的解法
    public static void Floyd(int[][] dis,int[][] path){
        int i,j,k;
        int[][] tmp = new int[dis.length][dis.length];
        for (i=0;i<dis.length;i++){
            for (j=0;j<dis.length;j++){
                tmp[i][j] = Integer.MAX_VALUE;
                if (dis[i][j]!=0){
                    tmp[i][j] = dis[i][j];
                }
                path[i][j] = -1;
            }
        }
        for (k=0;k<dis.length;k++){
            for (i=0;i<dis.length;i++){
                for (j=0;j<dis.length;j++){
                    if (tmp[i][j]>tmp[i][k]+tmp[k][j]){
                        tmp[i][j] = tmp[i][k]+tmp[k][j];
                        path[i][j] = k;
                    }
                }
            }
        }
    }
    public static void printPath(int u,int v,int[][] path){
        if (path[u][v]==-1){
            System.out.println("直接输出");
        }else {
            int mid = path[u][v];
            printPath(u,mid,path);
            printPath(mid,v,path);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值