Algorithm note _070:bellmanford algorithm Brief introduction (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

2.1 Specific Code

  1 problem description

What is Bellmanford algorithm?

Floyd algorithm function: given a weighted connected graph, select a vertex, called the starting point, to find the shortest distance from the starting point to all other vertices, the salient feature is that it can The shortest path of a single source with a negative weighted graph is obtained .

Floyd algorithm idea:

  • First, initialize all of the points. Each point holds a value that represents the distance from the origin to the point, sets the value of the origin to 0, and the value of the other points to infinity (which means that it is not reachable).
  • Second, loop, loop subscript from 1 to n - 1 ( n
  • third, traverse all edges (edge(u,v)) to determine if there is a condition: if D( v ) > D (U) + w (u,v) , the return false , which indicates that there is a negative loop from the source point.
2 Solutions 2.1 specific code

The time complexity of Bellman-ford algorithm for finding single source shortest path is O (v*e). (V is a collection of vertices for a given graph, andE is a set of edges for a given graph)

The coding thought of this paper is mainly referenced from the last reference 1 in the blog, want to learn more, you can refer to the end of the article reference.

first look at the connected graphs used in the code ( PS: The graph is undirected connected, so there are two edges between each of the two vertices):

now extract vertices the shortest distance from a to all other vertices

The specific code is as follows:

 PackageCom.liuzhen.chapter9;ImportJava.util.Scanner; Public classBellmanford { Public  Long[] result;//the shortest distance between the No. 0 vertex and the other vertices//an inner class that represents a weighted edge of a graph    classEdge { Public intA//the beginning of the side         Public intb//the end of the edge         Public intValue//weighted value of the edgeEdge (intAintBintvalue) {             This. A =A;  This. B =b;  This. Value =value; }    }    //returns the shortest distance from a No. 0 vertex to all other vertices     Public  BooleanGetshortestpaths (intN, edge[] A) {Result=New Long[n];  for(inti = 1;i < n;i++) Result[i]= Integer.max_value;//the distance from the initialization of the No. 0 vertex to the other vertices is infinity, which is represented by an integer maximum value.         for(inti = 1;i < n;i++) {             for(intj = 0;j < a.length;j++) {                if(result[a[j].b] > RESULT[A[J].A] +a[j].value) result[a[j].b]= Result[a[j].a] +A[j].value; }        }        BooleanJudge =true;  for(inti = 1;i < n;i++) {//determine if a negative ring exists in a given graph            if(result[a[i].b] > RESULT[A[I].A] +a[i].value) {Judge=false;  Break; }        }        returnjudge; }         Public Static voidMain (string[] args) {Bellmanford test=NewBellmanford (); Scanner in=NewScanner (system.in); System.out.println ("Please enter the total number of vertices of a graph n and the total number of sides P:"); intn =In.nextint (); intp =In.nextint (); Edge[] A=NewEdge[p]; System.out.println ("Please enter data for specific edges:");  for(inti = 0;i < p;i++) {            intA =In.nextint (); intb =In.nextint (); intValue =In.nextint (); A[i]= Test.NewEdge (A, B, value); }        if(Test.getshortestpaths (n, A)) { for(inti = 0;i < test.result.length;i++) System.out.print (Test.result[i]+" "); } ElseSystem.out.println ("There is a negative ring in the given graph, there is no shortest distance"); }    }

Operation Result:

Please enter the total number of vertices of a graph n and the total number of sides P:6 Please enter data for specific edges:

Resources:

1.bellman Ford algorithm

Algorithm note _070:bellmanford algorithm Brief introduction (Java)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.