GithubHelp home page GithubHelp logo

sleekpanther / bellman-ford Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 840 KB

Shortest Paths from every vertex to a goal vertex allowing negative-weight edges (when Dijkstra's fails)

Java 100.00%
noah-patullo noah patullo pattullo patulo pattulo bellman-ford shortest-paths algorithm algorithms

bellman-ford's Introduction

Bellman Ford (Shortest Paths with Negative Weights)

Dynamic programming algorithm to find the shortest paths from every vertex to the goal vertex. Edges can have negative weights, but no negative-weight cycles (otherwise you could keep looping over a negative-weight cycle to get an infinitely negative number & shortest path would have no meaning). Dijkstra's fails for negative-wiehgt edges but Bellman-Ford can do it.

Optimal Substructure


The shortest path between any 2 vertices uses at most n-1 edges (n=number of vertices). Since there cannot be any negative-weight cycles, any more than n-1 edges would only add to the total path cost so the algorithm stops when n-1 edges have been considered.
Bellman-Ford considers the shortest paths in increasing order of number of edges used starting from 0 edges (hence infinity for all but the goal node), then shortest paths using 1 edge, up to n-1 edges.

Pseudocode

Input Graphs

Graph 1

Graph 2

Usage

  • int[][][] graph is an adjacency list for a weighted, directed graph
    • graph[0] contains all edges FROM vertex 0
    • Each graph[0][v] is an edge as a 2D array containing [destinationVertex, edgeWeight]
    • So for Graph 1, graph[0] is { {1, 6}, {2, 7} }, meaning there are 2 edges FROM vertex 0:
      1. (0 โ†’ 1) weight=6
      2. (0 โ†’ 2) weight=7
    • Put an empty array {} in a row for a vertex that has no outgoing edges (see graph 2 for example)
  • String[] vertexNames convert array index (e.g. 0, 1, 2, ...) to human-readable names
    The Dynamic Programming deals with vertices as int, but prints the final paths using this conversion array
  • startingVertex & a goalVertex as int. These indexes correspond to vertexNames[] and graph[][][], but S and T don't have to be the 1st & last items in the arrays, they don't even have to be called "S" and "T"

Graph 1 Memoization Table

For Bellman-Ford, the last row is the most important. This final row holds cost of the shortest path from any vertex to the goal vertex using using at most n-1 edges. (A value is infinity if it cannot be reached)
Retracing the actual path is done using the successors[]. Each entry in the memoization table also contains the next vertex in the path (or -1 if no path has been found).
Follow the path by looking 1 row above and at the column indicated by the successor. Stop when the goalVertex is reached

Code Notes

  • In findShortestPaths, i is an edge counter representing how many edges can currently be included in the shortest path
  • printTable() displays "inf" for "infinity" because the underlying code uses Integer.MAX_VALUE and 10-digit numbers mess up the column alignment when printing
  • Integer.MAX_VALUE represents infinity, but that requires some extra code to make sure overflow doesn't occur.
    if(memoTable[i-1][destinationVertex] != Integer.MAX_VALUE)
    This makes sure that the previous row's value is NOT infinity before adding the weight of the new edge, otherwise it would result in a hugely negative number
  • possiblePathCosts holds costs of all possible paths coming FROM the sourceVertex and also the successor where that path went. The minimum of all these paths 1 edge away from sourceVertex is chosen and the successor is also stored

bellman-ford's People

Contributors

sleekpanther avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.