In another article (Graph Topological Sort — JavaScript implementation) I implement topological sort using DFS, here is the implementation of Kahn’s Algorithm, inspired by this tutorial video:

Kahn’s Algorithm

  • Repeatedly remove vertices without any dependencies from the graph and add them to the topological ordering array
  • When one vertex is removed…


I find this YouTube tutorial well explains topological sort, here I am going to implement the sample mentioned in the video by JavaScript.

Before we start the implementation, just to highlight again

Topological sorting only works for DAG (directed acyclic graph) : a directed graph with no directed cycles

Create sample graph

  • A must come before B and C
  • Both B and C must come before D
  • B and C are interchangeable since there is no edge between them
  • D must come before E

Helper function

This helper function will assign topological number to a given vertex.

Driver function

Finally, a driver function to trigger topological sorting.

Now each vertex is assigned with a number to form the topological sequence.


Photo by Caspar Camille Rubin on Unsplash

What is package.json

package.json is a file holds various metadata relevant to the project. It is usually located at the root directory of a Node.js project.

This file will give information to npm for it to identify the project as well as handle the project’s dependencies.

It lists the packages your project depends…


A typical monolithic server contains all of the codes implemented for all features for our application inside one codebase , and this server interact with a single database.

A microservice contains the codes needed for just one feature of our application.

Each service is self-contained and standalone, it interacts with…


Now you have a MongoDB replica set and decide to downgrade it to a standalone node.

If you don’t have a replica set yet, please refer to my previous post Create a MongoDB replica set to create one to go through this article.

Remove secondary member nodes

Firstable, Shut down the mongod instance for…


A replica set in MongoDB is a group of mongod instances that maintain the same data set. It contains several data bearing nodes and optionally one arbiter node.

Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary…

Adela Chao

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store