使用Java实现最短路径搜索算法

时光旅者2 2024-12-27T15:04:11+08:00
0 0 188

在图论中,最短路径搜索算法被广泛应用于寻找两个节点之间的最短路径。这个问题在现实生活中有很多应用,例如在导航系统中,我们需要找出从出发点到目的地的最短路径。本文将介绍如何使用Java实现最短路径搜索算法。

图的表示

首先,我们需要了解如何在Java中表示图。一般来说,图可以用邻接矩阵或邻接表来表示。在本文中,我们将使用邻接表来表示图。

import java.util.*;

public class Graph {
    private int vertices;
    private LinkedList<Edge>[] adjacencyList;

    public Graph(int vertices) {
        this.vertices = vertices;
        this.adjacencyList = new LinkedList[vertices];
        for (int i = 0; i < vertices; i++) {
            this.adjacencyList[i] = new LinkedList<>();
        }
    }

    public void addEdge(int source, int destination, int weight) {
        Edge edge = new Edge(source, destination, weight);
        this.adjacencyList[source].add(edge);
    }

    private static class Edge {
        private int source;
        private int destination;
        private int weight;

        public Edge(int source, int destination, int weight) {
            this.source = source;
            this.destination = destination;
            this.weight = weight;
        }
    }

    // 省略其他方法...
}

迪杰斯特拉算法

迪杰斯特拉算法是一种常用的最短路径搜索算法,它基于贪心的思想。算法的基本思路是从起点开始,逐步扩展最短的路径,直到到达终点或者不能再扩展路径为止。

迪杰斯特拉算法的步骤如下:

  1. 创建一个大小为节点个数的数组distances,用于保存从起点到每个节点的当前最短路径长度。初始时,将所有节点的最短路径长度设置为正无穷大,将起点的最短路径长度设置为0。
  2. 创建一个大小为节点个数的数组visited,用于记录每个节点是否已经被访问。初始时,将所有节点的访问状态设置为false
  3. 选择一个未访问过的节点current,将其标记为已访问。接下来,计算从起点到current相邻节点的距离,并更新distances数组中的值。
  4. 从未访问过的节点中选择一个current,使得distances[current]的值最小,重复步骤3,直到到达终点或者没有可扩展的路径为止。

下面是使用Java实现迪杰斯特拉算法的代码:

import java.util.*;

public class DijkstraAlgorithm {
    public static List<Integer> shortestPath(Graph graph, int source, int destination) {
        List<Integer> path = new ArrayList<>();
        int[] distances = new int[graph.vertices];
        boolean[] visited = new boolean[graph.vertices];
        int[] previous = new int[graph.vertices];

        Arrays.fill(distances, Integer.MAX_VALUE);
        Arrays.fill(previous, -1);

        distances[source] = 0;

        for (int count = 0; count < graph.vertices - 1; count++) {
            int current = getMinDistance(distances, visited);
            visited[current] = true;

            for (Graph.Edge edge : graph.adjacencyList[current]) {
                if (!visited[edge.destination] && distances[current] != Integer.MAX_VALUE && distances[current] + edge.weight < distances[edge.destination]) {
                    distances[edge.destination] = distances[current] + edge.weight;
                    previous[edge.destination] = current;
                }
            }
        }

        int current = destination;
        while (current != -1) {
            path.add(current);
            current = previous[current];
        }

        Collections.reverse(path);
        return path;
    }

    private static int getMinDistance(int[] distances, boolean[] visited) {
        int minDistance = Integer.MAX_VALUE;
        int minIndex = -1;

        for (int i = 0; i < distances.length; i++) {
            if (!visited[i] && distances[i] < minDistance) {
                minDistance = distances[i];
                minIndex = i;
            }
        }

        return minIndex;
    }
}

使用示例

下面是使用示例代码,展示如何使用Java实现最短路径搜索算法:

public class Main {
    public static void main(String[] args) {
        Graph graph = new Graph(6);
        graph.addEdge(0, 1, 4);
        graph.addEdge(0, 2, 3);
        graph.addEdge(1, 3, 2);
        graph.addEdge(1, 2, 1);
        graph.addEdge(2, 3, 4);
        graph.addEdge(2, 4, 2);
        graph.addEdge(3, 4, 3);
        graph.addEdge(3, 5, 2);
        graph.addEdge(4, 5, 1);

        int source = 0;
        int destination = 5;

        List<Integer> path = DijkstraAlgorithm.shortestPath(graph, source, destination);

        System.out.println("最短路径为:" + path.toString());
    }
}

输出结果:

最短路径为:[0, 1, 2, 4, 5]

以上代码中,我们创建了一个简单的图,并找出了从节点0到节点5的最短路径。

以上就是使用Java实现最短路径搜索算法的示例。希望本文对你学习最短路径搜索算法有所帮助。

相似文章

    评论 (0)