Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the shortest path of Java undirected undirected graph

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

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)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report