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

Added Kann's algorithm #1676

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost wants to merge 10 commits into TheAlgorithms:master from RaviSadam:master
Closed

Added Kann's algorithm #1676

ghost wants to merge 10 commits into TheAlgorithms:master from RaviSadam:master

Conversation

Copy link

@ghost ghost commented Jul 8, 2024

Summary

  • Kann's algorithm, a well known graph algorithm for finding the topological sorting order of graph

File Added

  • KannsAlgoritm.js: Kann's Algorithm implementation added in Graphs folder
  • KannsAlgorithm.test.js : Test file for Kann's Algorithm added in Graphs/test folder

@ghost ghost requested review from raklaptudirm and appgurueu as code owners July 8, 2024 04:38
Copy link

codecov-commenter commented Jul 8, 2024
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.74%. Comparing base (9010481) to head (191993f).

Additional details and impacted files
@@ Coverage Diff @@
## master #1676 +/- ##
==========================================
+ Coverage 84.65% 84.74% +0.08% 
==========================================
 Files 378 380 +2 
 Lines 19744 19850 +106 
 Branches 2951 2974 +23 
==========================================
+ Hits 16715 16821 +106 
 Misses 3029 3029 

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@appgurueu appgurueu left a comment
edited
Loading

Choose a reason for hiding this comment

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

It's "Kahn's algorithm", not "Kann's algorithm".

The implementation looks correct, there is a runtime concern though.

@ghost ghost requested a review from appgurueu July 11, 2024 13:41
* Kahn's Algorithm is used for finding the topological sorting order of directed acyclic graph
*
* @param graph - Graph (adjacency list)
* @param n - Size of graph
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

Redundant. Just use graph.length.

*
*/

export function KahnsAlgorithm(graph, n) {
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

I'd prefer calling this topoSort or similar.

inDegree[neighbour] += 1
}
}
const queue = new Queue()
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

Why use a queue when you can get away with just using JS data structures, specifically, a stack?

Also, why call this "queue" rather than giving it a more descriptive name like "roots"?

const node = queue.dequeue()
result.push(node)
for (const neighbour of graph[node]) {
inDegree[neighbour] -= 1
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

Suggested change
inDegree[neighbour]-=1
inDegree[neighbour]--

result.push(node)
for (const neighbour of graph[node]) {
inDegree[neighbour] -= 1
if (inDegree[neighbour] == 0) {
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

Suggested change
if (inDegree[neighbour] == 0) {
if (inDegree[neighbour] === 0) {

@@ -0,0 +1,13 @@
import { kannsAlgorithm } from '../KannsAlgorithm'
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

You forgot to delete this file.

@@ -0,0 +1,13 @@
import { KahnsAlgorithm } from '../KahnsAlgorithm'

test('Graph without cycle', () => {
Copy link
Collaborator

@appgurueu appgurueu Jul 11, 2024

Choose a reason for hiding this comment

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

As said, these two test blocks should be in a describe("Kahn's algorithm", () => {...}) block.

Copy link
Author

ghost commented Jul 11, 2024

Hai,
I am closing these pull requests as they involve substantial file modifications, resulting in an overly large number of commits.

@ghost ghost closed this Jul 11, 2024
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@raklaptudirm raklaptudirm Awaiting requested review from raklaptudirm raklaptudirm is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /