Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Add Topological Sorting using DFS#7111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Raghu0703 wants to merge4 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
fromRaghu0703:topological-sort-dfs

Conversation

@Raghu0703
Copy link

Description

This PR implements Topological Sorting using Depth-First Search (DFS) for Directed Acyclic Graphs (DAGs) as requested in issue#6938.

Changes Made

  • ✅ ImplementedTopologicalSortDFS.java with DFS-based topological sorting algorithm
  • ✅ Added cycle detection using recursion stack to validate DAG property
  • ✅ Created comprehensive test suite (TopologicalSortDFSTest.java) with 20+ test cases
  • ✅ Added practical examples (TopologicalSortExample.java) demonstrating real-world usage
  • ✅ Included complete Javadoc documentation for all methods

Algorithm Details

  • Approach: DFS traversal with visited array and recursion stack for cycle detection
  • Time Complexity: O(V + E) where V = number of vertices, E = number of edges
  • Space Complexity: O(V) for recursion stack and auxiliary data structures
  • Key Features:
    • Detects cycles and throwsIllegalArgumentException for non-DAG graphs
    • Handles disconnected components correctly
    • IncludesisDAG() utility method for graph validation

Implementation Highlights

  1. Cycle Detection: Uses recursion stack to accurately detect back edges
  2. Adjacency List: Efficient graph representation usingList<List<Integer>>
  3. Stack-based Ordering: Pushes vertices to stack after DFS completion for correct topological order
  4. Edge Case Handling: Properly handles empty graphs, single vertices, and null inputs

Test Coverage

The test suite includes comprehensive coverage for:

  • ✅ Simple and complex DAGs with multiple dependencies
  • ✅ Linear graphs and disconnected components
  • ✅ Cycle detection (self-loops, simple cycles, complex cycles)
  • ✅ Edge cases (empty graph, single vertex, null input)
  • ✅ Real-world scenarios:
    • Task scheduling with dependencies
    • Course prerequisite ordering
    • Build system dependency resolution

Example Usage

- Implements Kruskal's algorithm using Union-Find- Includes comprehensive unit tests- Time complexity: O(E log E)- Space complexity: O(V + E)FixesTheAlgorithms#7067
- Implements backtracking algorithm to solve 9x9 Sudoku puzzles- Includes isValid() method to check Sudoku constraints- Includes solveSudoku() method with recursive backtracking- Adds comprehensive unit tests covering multiple scenarios- Tests include solvable, unsolvable, empty, and difficult puzzles- Includes example usage in main method with formatted output- Documents time complexity O(9^m) and space complexity O(m)FixesTheAlgorithms#6929
- Implement DFS-based topological sort for DAGs- Add cycle detection for DAG validation- Include comprehensive test cases- Add documentation and examplesFixesTheAlgorithms#6938
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@DenizAltunkapanDenizAltunkapanAwaiting requested review from DenizAltunkapanDenizAltunkapan is a code owner

@yanglbmeyanglbmeAwaiting requested review from yanglbmeyanglbme is a code owner

@alxkmalxkmAwaiting requested review from alxkmalxkm is a code owner

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@Raghu0703

[8]ページ先頭

©2009-2025 Movatter.jp