Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to realize the shortest path of Java undirected undirected graph

Shulou Source: shulou.com Published: 2022-05-31 17:57:32 09月16日 Update

This article mainly explains "how to realize the shortest path of Java undirected unauthorized graph". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the shortest path of Java undirected unauthorized graph".

Dijkstra (Dijstra weights are all positive)

Is a typical single-source shortest path algorithm, which is used to calculate the shortest path from one node to all other nodes. The main feature is that it expands outward layer by layer around the starting point until it reaches the end point.

The input to the algorithm is:

Weighted (directed) graph

Start point (source)

The weight of all edges is non-negative

The output of the algorithm is:

The shortest path from the starting point (source) to other points

Floyd (Freud can have negative weights)

It is an algorithm to solve the shortest path between any two points, which can correctly deal with the shortest path problem of directed graph or negative weight. at the same time, it is also used to calculate the transitive closure of directed graph. The time complexity of Floyd-Warshall algorithm is O (N3) and the space complexity is O (N2).

Bellman-Ford (Berman single source shortest path can have negative weights, which is wider than Dijkstra)

First, assume that the distance from the source point to all points is infinite, then from any vertex u, traverse all other vertices vi, calculate the sum of the distance from the source point to the other vertices vi and the distance from vi to u, if the distance is smaller than the original distance, update, traverse all the vertices, and then get the shortest distance from the source point to all vertices.

4. The shortest path algorithm of undirected unauthorized Graph

Public class Dijkstra {static int max = Integer.MAX_VALUE; static int dist [] = new int [6]; static int prev [] = new int [6] Static int a [] [] = {{0pr Maxonome10 Magnum Magnum 30100}, {max,0,5,max,max,max}, {max,max,0,50,max,max}, {max,max,max,0,max,10}, {max,max,max,20,0,60}, {max,max,max,max,max,0}} Void dijkstra (int v.int a [] [], int dist [], int prev []) {int n = dist.length-1; boolean s [] = new boolean [int 1]; for (int I = 1; I)

Tags: Algorithm vertex undirected directed graph path length source point learning complexity between content complexity node starting point problem storage typical at the same time that is ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Xiaomi NVidia MariaDB Shulou Technology