Skip to content

Add Topological Sort implementation in Java#216

Open
maitriupadhyay03-cell wants to merge 2 commits into
tulika-99:mainfrom
maitriupadhyay03-cell:add-topological-sort-java
Open

Add Topological Sort implementation in Java#216
maitriupadhyay03-cell wants to merge 2 commits into
tulika-99:mainfrom
maitriupadhyay03-cell:add-topological-sort-java

Conversation

@maitriupadhyay03-cell

Copy link
Copy Markdown

What does this PR do?

Adds a Topological Sort implementation in Java using the DFS (Depth-First Search) approach. This addresses issue #184.

Algorithm Overview

Topological Sort is a linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for every directed edge u → v, vertex u appears before v in the ordering.

Approach used: DFS + Stack

  1. Run DFS from each unvisited vertex
  2. After finishing all neighbors, push the current vertex onto a stack
  3. Pop from the stack to get the topological order

Complexity

  • Time: O(V + E)
  • Space: O(V + E)

Files Added

  • Graphs/Topological Sort/Topological-Sort-Java.java

Example

Graph edges: 5->2, 5->0, 4->0, 4->1, 2->3, 3->1
Output: Topological Sort Order: 5 -> 4 -> 2 -> 3 -> 1 -> 0

Checklist

  • Code compiles and runs correctly
  • Follows the repo naming convention (Algorithm-Name-Language.java)
  • Inline comments added for clarity
  • No sorting algorithms included
  • Algorithm not previously present in Java

Closes #184

Added Topological Sort using DFS approach in Java. Closes tulika-99#184

- Uses adjacency list representation
- DFS-based approach with stack to determine topological order
- Time: O(V+E), Space: O(V+E)
- Includes example with 6 nodes and test output
Removed unnecessary comments explaining topological sorting.
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.

Add any algorithms of you choice in any language

1 participant