java-图算法

1. 最短路径算法

迪杰斯特拉算法(Dijkstra's Algorithm)

描述

迪杰斯特拉算法是一种用于在加权图中找到单个源点到所有其他顶点的最短路径的算法。它适用于有向图和无向图,且边的权重必须为非负数。

Java案例

java 复制代码
import java.util.Arrays;

public class DijkstraAlgorithm {
    public static void dijkstra(int[][] graph, int src) {
        int V = graph.length;
        int[] dist = new int[V];
        Arrays.fill(dist, Integer.MAX_VALUE);
        boolean[] sptSet = new boolean[V];

        dist[src] = 0;

        for (int count = 0; count < V - 1; count++) {
            int u = minDistance(dist, sptSet, V);
            sptSet[u] = true;

            for (int v = 0; v < V; v++)
                if (!sptSet[v] && graph[u][v] != 0 && dist[u] != Integer.MAX_VALUE && dist[u] + graph[u][v] < dist[v])
                    dist[v] = dist[u] + graph[u][v];
        }

        printSolution(dist);
    }

    private static int minDistance(int[] dist, boolean[] sptSet, int V) {
        int min = Integer.MAX_VALUE, minIndex = -1;

        for (int v = 0; v < V; v++)
            if (!sptSet[v] && dist[v] <= min)
                min = dist[v], minIndex = v;

        return minIndex;
    }

    private static void printSolution(int[] dist) {
        System.out.println("Vertex \t Distance from Source");
        for (int i = 0; i < dist.length; i++)
            System.out.println(i + " \t\t " + dist[i]);
    }

    public static void main(String[] args) {
        int[][] graph = {
            {0, 4, 0, 0, 0, 0, 0, 8, 0},
            {4, 0, 8, 0, 0, 0, 0, 11, 0},
            {0, 8, 0, 7, 0, 4, 0, 0, 2},
            {0, 0, 7, 0, 9, 14, 0, 0, 0},
            {0, 0, 0, 9, 0, 10, 0, 0, 0},
            {0, 0, 4, 14, 10, 0, 2, 0, 0},
            {0, 0, 0, 0, 0, 2, 0, 1, 6},
            {8, 11, 0, 0, 0, 0, 1, 0, 7},
            {0, 0, 2, 0, 0, 0, 6, 7, 0}
        };
        dijkstra(graph, 0);
    }
}
弗洛伊德算法(Floyd-Warshall Algorithm)

描述

弗洛伊德算法用于在加权图中找到所有顶点对之间的最短路径。它可以处理正权边和负权边,但不能处理负权环。

Java案例

java 复制代码
public class FloydWarshallAlgorithm {
    public static void floydWarshallAlgorithm(int[][] graph) {
        int V = graph.length;
        int[][] dist = new int[V][V];
        boolean[][] isShortestPath = new boolean[V][V];

        for (int i = 0; i < V; i++)
            for (int j = 0; j < V; j++)
                dist[i][j] = graph[i][j];

        for (int k = 0; k < V; k++) {
            for (int i = 0; i < V; i++) {
                for (int j = 0; j < V; j++) {
                    if (dist[i][k] != Integer.MAX_VALUE && dist[k][j] != Integer.MAX_VALUE
                            && dist[i][j] > dist[i][k] + dist[k][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j];
                        isShortestPath[i][j] = false;
                    } else if (dist[i][k] != Integer.MAX_VALUE && dist[k][j] != Integer.MAX_VALUE
                            && dist[i][j] == dist[i][k] + dist[k][j]) {
                        isShortestPath[i][j] = true;
                    }
                }
            }
        }

        printSolution(dist);
    }

    private static void printSolution(int[][] dist) {
        System.out.println("The following matrix shows the shortest distances between every pair of vertices:");
        for (int i = 0; i < dist.length; i++) {
            for (int j = 0; j < dist[i].length; j++) {
                if (dist[i][j] == Integer.MAX_VALUE)
                    System.out.print("\u221e ");
                else
                    System.out.print(dist[i][j] + "   ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] graph = {
            {0, 5, 0, 0, 0, 0, 0, 8, 0},
            {5, 0, 10, 0, 0, 0, 0, 3, 0},
            {0, 10, 0, 15, 0, 0, 0, 0, 2},
            {0, 0, 15, 0, 6, 0, 0, 0, 0},
            {0, 0, 0, 6, 0, 9, 0, 0, 0},
            {0, 0, 0, 0, 9, 0, 7, 0, 0},
            {0, 0, 0, 0, 0, 7, 0, 4, 0},
            {8, 3, 0, 0, 0, 0, 4, 0, 2},
            {0, 0, 2, 0, 0, 0, 0, 2, 0}
        };
        floydWarshallAlgorithm(graph);
    }
}

2. 最小生成树算法

普里姆算法(Prim's Algorithm)

描述

普里姆算法是一种贪心算法,用于在加权连通图中找到最小生成树。它从一个任意顶点开始,逐渐增加树中的顶点,直到包含所有顶点。

Java案例

java 复制代码
import java.util.Arrays;

public class PrimsAlgorithm {
    public static void primsAlgorithm(int[][] graph) {
        int V = graph.length;
        int[] parent = new int[V];
        int[] minHeap = new int[V];
        Arrays.fill(minHeap, Integer.MAX_VALUE);
        boolean[] added = new boolean[V];

        minHeap[0] = 0;
        parent[0] = -1;

        for (int i = 0; i < V - 1; i++) {
            int u = minHeap[0];
            added[u] = true;

            for (int v = 0; v < V; v++) {
                if (graph[u][v] != 0 && !added[v] && graph[u][v] < minHeap[v]) {
                    minHeap[v] = graph[u][v];
                    parent[v] = u;
                }
            }

            for (int v = 0; v < V; v++) {
                if (!added[v] && minHeap[v] < minHeap[0]) {
                    minHeap[0] = minHeap[v];
                }
            }
        }

        printMST(parent);
    }

    private static void printMST(int[] parent) {
        System.out.println("Edge \t Weight");
        for (int i = 1; i < parent.length; i++)
            System.out.println(parent[i] + " - " + i + "\t" + 1);
    }

    public static void main(String[] args) {
        int[][] graph = {
            {0, 2, 0, 6, 0},
            {2, 0, 3, 8, 5},
            {0, 3, 0, 0, 7},
            {6, 8, 0, 0, 9},
            {0, 5, 7, 9, 0}
        };
        primsAlgorithm(graph);
    }
}
克鲁斯卡尔算法(Kruskal's Algorithm)

描述

克鲁斯卡尔算法也是一种贪心算法,用于在加权连通图中找到最小生成树。它按边的权重递增顺序考虑边,如果加入这条

相关推荐
2401_8628808216 小时前
数据结构 --- 栈
c语言·数据结构·算法
萧瑟余晖16 小时前
Java深入解析篇三十六之分布式系统详解
java·开发语言·分布式
致Great16 小时前
Qwen3.8-27B 接入 DSH 全流程实测:写代码、跑长任务,都没问题
算法
KaKa_大王16 小时前
关于秒杀项目的一些理解
java·学习
ZJU_统一阿萨姆17 小时前
【推理优化进阶】调度器的数学内核:排队论、SLO 与在线决策
开发语言·人工智能·语言模型·系统架构·vllm
tomla17 小时前
使用Java VisualVM观察过期对象引用现象
java
Tisfy17 小时前
LeetCode 3471.找出最大的几近缺失整数:三种情况判断
算法·leetcode·题解·分类讨论
M1A117 小时前
Spring Boot YAML 配置读取完全指南:从基础到微服务
java
古法安卓17 小时前
Android-SELinux 策略调试实战:从 AVC 日志到策略修复
android·java·android studio
todoitbo17 小时前
飞算JavaAI的多租户权限隔离实测
java·springboot·ai编程·java开发·飞算javaai·java代码生成