Skip to content

Conversation

@nachiket2005
Copy link

Overview

The push_relabel function computes the maximum flow from a given source to a sink vertex in a directed graph.
Unlike the Ford–Fulkerson or Edmonds–Karp methods that rely on augmenting paths, the Push–Relabel algorithm maintains a preflow and adjusts vertex heights to push excess flow efficiently through the network.

This implementation:

  • Initializes a preflow from the source
  • Repeatedly pushes excess flow to neighboring vertices
  • Relabels vertices when no more pushes are possible
  • Uses a discharge loop to efficiently manage active vertices

It is designed for clarity and educational use, closely matching the behavior of class-based implementations in other languages while being idiomatic to R.


Features

  • Implements Push–Relabel (Preflow–Push) method for maximum flow
  • Supports directed graphs represented as square capacity matrices
  • Includes Push, Relabel, and Discharge operations
  • Uses 1-based indexing for compatibility with R conventions
  • Tracks vertex heights, excess flows, and residual capacities
  • Returns detailed results including:
    • max_flow — total maximum flow value
    • flow — final flow matrix
    • excess — excess flow per vertex
    • height — vertex heights after termination
  • Includes ready-to-run example usage

Complexity

  • Time Complexity: O(V³) in the generic implementation
    • (Performance can be improved with heuristics like gap relabeling or global relabeling, not included here.)
  • Space Complexity: O(V²) for flow and capacity matrices

Demonstration

Run the following example in an R session to test the implementation:

source('R/graph_algorithms/push_relabel.r')

graph <- matrix(c(
  0, 16, 13, 0, 0, 0,
  0, 0, 10, 12, 0, 0,
  0, 4, 0, 0, 14, 0,
  0, 0, 9, 0, 0, 20,
  0, 0, 0, 7, 0, 4,
  0, 0, 0, 0, 0, 0
), nrow = 6, byrow = TRUE)

source <- 1L
sink <- 6L

res <- push_relabel(graph, source, sink)
cat("Maximum Flow:", res$max_flow, "\n")

Copilot AI review requested due to automatic review settings October 19, 2025 12:57
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 adds multiple new algorithm implementations to TheAlgorithms/R repository, introducing both graph algorithms and mathematical functions. While the PR title mentions only the Push-Relabel algorithm, it actually contains several new implementations.

  • Adds four new graph algorithms: Push-Relabel maximum flow, Edmonds-Karp maximum flow, Hamiltonian path finder, and Boruvka's MST
  • Implements Fast Fourier Transform in the mathematics section
  • Provides comprehensive documentation for all new algorithms

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
graph_algorithms/push_relabel.r Implements Push-Relabel algorithm for maximum flow computation
graph_algorithms/edmonds_karp.r Implements Edmonds-Karp algorithm for maximum flow computation
graph_algorithms/hamiltonian_path.r Implements backtracking algorithm to find Hamiltonian paths
graph_algorithms/boruvka_mst.r Implements Boruvka's algorithm for minimum spanning tree
mathematics/fast_fourier_transform.r Implements recursive Cooley-Tukey FFT algorithm
documentation/push_relabel.md Documentation for Push-Relabel algorithm
documentation/hamiltonian_path.md Documentation for Hamiltonian path algorithm
documentation/fast_fourier_transform.md Documentation for FFT algorithm
documentation/boruvka_mst.md Documentation for Boruvka's MST algorithm


## Description

The implementation builds a Minimum Spanning Tree for an undirected weighted graph using Boruvka's method. The graph is represented by a list with `V` (number of vertices) and `edges` (a data.frame with columns `u`, `v`, `w`). Vertex indices are 1-based to match other algorithms in the repository.
Copy link
Member

Choose a reason for hiding this comment

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

Please remove separate docs

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