Skip to content

Conversation

@nachiket2005
Copy link

Overview

The edmonds_karp function finds the maximum flow in a directed network by repeatedly searching for augmenting paths using Breadth-First Search (BFS).
At each iteration, it augments the flow along the shortest available path in terms of edge count, ensuring predictable and polynomial runtime.

This implementation follows the repository style conventions:

  • Uses 1-based vertex indexing
  • Provides a clear header and example usage
  • Returns both the maximum flow value and residual capacity matrix

Features

  • Implements the Edmonds-Karp variant of the Ford-Fulkerson algorithm
  • Uses BFS to find shortest augmenting paths
  • Works with directed graphs represented as numeric capacity matrices
  • Returns both:
    • max_flow — total maximum flow value
    • residual — resulting residual capacity matrix
  • Includes a ready-to-run example for verification

Complexity

  • Time Complexity: O(V × E²)
  • Space Complexity: O(V²) (due to capacity and residual matrices)

Demonstration

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

source('R/graph_algorithms/edmonds_karp.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 <- edmonds_karp(graph, source, sink)
cat("Maximum Flow:", res$max_flow, "\n")

Copilot AI review requested due to automatic review settings October 19, 2025 12:41
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

Adds a new Edmonds–Karp maximum-flow algorithm in R and, additionally, introduces FFT (Cooley–Tukey), Hamiltonian Path (backtracking), and Borůvka’s MST implementations, with accompanying docs and examples.

  • Implements edmonds_karp with BFS-based augmenting paths and returns max_flow and residual.
  • Adds new algorithms (FFT, Hamiltonian Path, Borůvka) with example code and docs.
  • Documentation includes usage and complexity, but some paths reference the wrong directory prefix and examples run unconditionally in code.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
graph_algorithms/edmonds_karp.r Adds Edmonds–Karp max-flow with BFS and an example; core of the PR.
mathematics/fast_fourier_transform.r Adds recursive Cooley–Tukey FFT with zero-padding and an interactive example.
graph_algorithms/hamiltonian_path.r Adds Hamiltonian Path (backtracking) with example; includes function definitions and execution.
graph_algorithms/boruvka_mst.r Adds Borůvka’s MST implementation with union-find and printed output.
documentation/fast_fourier_transform.md Documents FFT usage/complexity; some path prefixes inconsistent.
documentation/hamiltonian_path.md Documents Hamiltonian Path; includes path prefix inconsistencies.
documentation/boruvka_mst.md Documents Borůvka MST; includes a wrong path prefix.
Comments suppressed due to low confidence (15)

graph_algorithms/hamiltonian_path.r:1

  • The recursion returns only TRUE/FALSE, so the filled path from deeper frames is not propagated back to hamiltonianPath; the printed path will remain largely unchanged (with -1s). Return the path from hamiltonianUtil (e.g., list(found, path)) and capture it in hamiltonianPath, or use superassignment/closure to store the final path.
# Hamiltonian Path (Backtracking)

graph_algorithms/edmonds_karp.r:1

  • [nitpick] The example code auto-runs and shadows base functions source() and sink(); this can break subsequent calls in a user session. Gate the example with if (interactive()) and rename these variables (e.g., s <- 1L, t <- 6L) to avoid masking base functions.
# Edmonds-Karp Maximum Flow (Ford-Fulkerson with BFS)

graph_algorithms/boruvka_mst.r:1

  • [nitpick] Prefer returning a value without printing from within the algorithm; let callers decide how to handle messages. Consider returning a status field instead of cat(), or guarding output with a verbose flag.
# Boruvka's Minimum Spanning Tree (MST) — improved R translation

graph_algorithms/hamiltonian_path.r:1

  • Function names use camelCase (isSafe, hamiltonianUtil), while repository conventions prefer snake_case or dot-separated names. Please rename to is_safe and hamiltonian_util to align with project style.
# Hamiltonian Path (Backtracking)

graph_algorithms/hamiltonian_path.r:1

  • Rename hamiltonianPath to hamiltonian_path to follow repository naming conventions for functions.
# Hamiltonian Path (Backtracking)

graph_algorithms/hamiltonian_path.r:1

  • [nitpick] Example code executes unconditionally on source, producing side effects during import. Wrap this block with if (interactive()) { ... } or move it to a separate example/test file.
# Hamiltonian Path (Backtracking)

graph_algorithms/hamiltonian_path.r:1

  • Adjust path reference to match the actual repository structure; it should be graph_algorithms (without the leading R/).
# Hamiltonian Path (Backtracking)

mathematics/fast_fourier_transform.r:1

  • The path prefix 'R/' does not match the file location; update to source('mathematics/fast_fourier_transform.r') or provide a project-root-relative path consistent with other docs.
# Fast Fourier Transform (Cooley-Tukey recursive implementation)

documentation/hamiltonian_path.md:1

  • Update the path reference to graph_algorithms/hamiltonian_path.r (remove the leading R/), consistent with the repository structure.
# Hamiltonian Path (Backtracking)

documentation/hamiltonian_path.md:1

  • Correct the sourcing path to match the actual location, e.g., Rscript -e "source('graph_algorithms/hamiltonian_path.r')".
# Hamiltonian Path (Backtracking)

documentation/fast_fourier_transform.md:1

  • Fix the path to mathematics/fast_fourier_transform.r (remove the leading R/).
# Fast Fourier Transform (FFT)

documentation/fast_fourier_transform.md:1

  • Update the command to source('mathematics/fast_fourier_transform.r') to reflect the actual path.
# Fast Fourier Transform (FFT)

documentation/boruvka_mst.md:1

  • Correct the path to graph_algorithms/boruvka_mst.r (remove 'R/').
# Boruvka's Minimum Spanning Tree (MST)

graph_algorithms/edmonds_karp.r:1

  • Please update DIRECTORY.md to list the new algorithms (edmonds_karp, fast_fourier_transform, hamiltonian_path, boruvka_mst) under their respective sections, per repository contribution guidelines.
# Edmonds-Karp Maximum Flow (Ford-Fulkerson with BFS)

graph_algorithms/edmonds_karp.r:1

  • Add basic tests to validate the new algorithm (e.g., assert max flow = 23 on the provided 6-node example, and verify residual capacity properties). The repository guidelines recommend including tests or a test file demonstrating correctness.
# Edmonds-Karp Maximum Flow (Ford-Fulkerson with BFS)

@@ -0,0 +1,26 @@
# Boruvka's Minimum Spanning Tree (MST)

This document describes the Boruvka MST implementation located at `R/graph_algorithms/boruvka_mst.r`.
Copy link
Member

Choose a reason for hiding this comment

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

We don't add documentation separately from the code, please remove those files

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