Skip to content
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

Added Kann's algorithm #1676

Closed
wants to merge 10 commits into from
Closed

Conversation

RaviSadam
Copy link

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

@codecov-commenter
Copy link

codecov-commenter commented Jul 8, 2024

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

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.

Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/KannsAlgorithm.js Outdated Show resolved Hide resolved
Graphs/test/KannsAlgorithm.test.js Outdated Show resolved Hide resolved
Graphs/test/KannsAlgorithm.test.js Outdated Show resolved Hide resolved
Graphs/test/KannsAlgorithm.test.js Outdated Show resolved Hide resolved
@RaviSadam RaviSadam 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

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

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

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

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

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

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

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.

@RaviSadam
Copy link
Author

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

@RaviSadam RaviSadam closed this Jul 11, 2024
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