Skip to content

Conversation

@Siddhram
Copy link
Contributor

This PR introduces a fully documented implementation of Johnson’s algorithm for computing shortest paths between all pairs of vertices in a sparse weighted directed graph in R.

Overview

The johnson_shortest_paths function computes shortest paths efficiently in graphs with positive and negative edge weights (but no negative cycles). It uses a combination of Bellman-Ford to reweight edges and Dijkstra’s algorithm for shortest path computation from each vertex. This ensures optimal handling of sparse graphs and negative edges without introducing negative cycles.

Features

  • Computes shortest paths between all pairs of vertices
  • Supports graphs with negative edge weights (no negative cycles allowed)
  • Detects negative cycles and reports them clearly
  • Handles disconnected components
  • Returns a distance matrix for all vertex pairs
  • Uses adjacency list representation consistent with other graph modules
  • Includes demonstration with a sample graph, including negative edges

Complexity

  • Time Complexity: O(V · E + V · (E log V)) with a binary heap priority queue
  • Space Complexity: O(V + E)

Demonstration

Run the included demo code to see Johnson’s algorithm in action on a 5-vertex graph with negative edges but no negative cycles. The output is a distance matrix representing shortest paths between all vertices.

Summary

This implementation expands the R graph algorithms module by adding a core algorithm for efficient all-pairs shortest paths in sparse graphs, suitable for network analysis, routing, and optimization.

@siriak siriak requested a review from Copilot October 18, 2025 20:51
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements Johnson's algorithm for computing all-pairs shortest paths in sparse directed graphs with potentially negative edge weights (but no negative cycles). The implementation combines Bellman-Ford for edge reweighting with Dijkstra's algorithm for efficient shortest path computation.

Key Changes:

  • Adds a complete implementation of Johnson's algorithm with helper functions for priority queue operations, Bellman-Ford potentials, and Dijkstra's algorithm
  • Includes negative cycle detection and clear error messaging
  • Provides a working demonstration with a 5-vertex graph containing negative edges

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 60af194 into TheAlgorithms:master Oct 18, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants