Skip to content

feat: Add topological sort and disjoint set algorithms#185

Closed
Siddhram wants to merge 1 commit intoTheAlgorithms:masterfrom
Siddhram:add-new-algorithm
Closed

feat: Add topological sort and disjoint set algorithms#185
Siddhram wants to merge 1 commit intoTheAlgorithms:masterfrom
Siddhram:add-new-algorithm

Conversation

@Siddhram
Copy link
Contributor

@Siddhram Siddhram commented Oct 9, 2025

Implemented Graph Algorithms in R

Topological Sort (Kahn's Algorithm)

Finds a linear ordering of vertices in a directed acyclic graph (DAG).
Detects cycles and resolves dependencies efficiently.
Time Complexity: O(V + E)
Space Complexity: O(V) for adjacency list and queue.
Includes examples for task scheduling and dependency resolution.

Disjoint Set (Union-Find)

Efficiently performs set operations using path compression and union by rank.
Supports find_set and union_sets operations for connected components and network connectivity.
Time Complexity: Near O(1) amortized per operation
Space Complexity: O(N) for parent and rank arrays.
Includes examples demonstrating graph connectivity and merging operations.

Both implementations include:

  • Clear documentation and real-world applications
  • Multiple test cases and edge case handling
  • Optimized performance and readable code structure

@Siddhram Siddhram requested review from acylam and siriak as code owners October 9, 2025 16:42
@siriak siriak requested a review from Copilot October 11, 2025 09:28
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 two fundamental graph algorithms in R: topological sort using Kahn's algorithm and a disjoint set (Union-Find) data structure. These algorithms provide efficient solutions for dependency resolution and connectivity problems with optimized time complexities.

  • Topological sort implementation for finding linear ordering of vertices in directed acyclic graphs
  • Disjoint set data structure with path compression and union by rank optimizations
  • Comprehensive test cases and examples demonstrating real-world applications

Reviewed Changes

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

File Description
graph_algorithms/topological_sort.r Implements Kahn's algorithm for topological sorting with cycle detection and test cases
graph_algorithms/disjoint_set.r Implements Union-Find data structure with path compression and union by rank optimizations

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +37 to +41
if (ds$parent[v + 1] != v) {
# Path compression: Make all nodes on path point to root
ds$parent[v + 1] <- find_set(ds, ds$parent[v + 1])
}
ds$parent[v + 1]
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The find_set function modifies ds$parent but doesn't return the updated data structure. In R, modifications to list elements inside functions don't persist unless the modified object is returned and reassigned.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

Please check

Comment on lines +94 to +95
find_set(ds2, 3) # This should compress the path
cat("Parents after find:", paste(ds2$parent, collapse = " "), "\n")
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The path compression demonstration won't work as expected because find_set doesn't modify the original ds2 object. The parent array will remain unchanged after the find_set call.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

Please check

@Siddhram Siddhram closed this by deleting the head repository Oct 13, 2025
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.

3 participants