Sign in

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, its neighbors become free, so they are the candidates for the next removal.
  • Keep removing vertices without dependencies until all nodes are processed, or a cycle is discovered.

Example Graph

  • Counting the incoming degree of each vertex incoming degree : how many edges point to this vertex A’s incoming degree = 0…

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 on, specifies versions of a package to use, so it makes your build reproducible, and therefore easier to share with other developers.

Note package.json is used not only for dependencies management, its purpose is to give detail information about the project such as author, license, repository, scripts, …etc.

What is package-lock.json

package-lock.json is…


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 its own database.

If one service crashes for some reason, other services are not impacted and still up and running, so a portion of our application is still working fine.


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 the member you wish to remove.

Then connect to the replica set’s current primary instance.

Use rs.remove() to remove the secondary member :

rs.remove("127.0.0.1:27027")
rs.remove("127.0.0.1:27037")


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 nodes.

The primary node receives all write operations and records all changes to its data sets in its operation log, i.e. oplog.

The secondaries replicate the primary’s oplog and apply the operations to their data sets asynchronously.

By having the secondaries’ data sets reflect the primary’s data set, the replica…

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