DEV Community

Cover image for Graph and Trees - Basic
Debjoty Mitra
Debjoty Mitra

Posted on


Graph and Trees - Basic


A graph is a non-linear data structure composed of nodes and edges. The nodes are also known as vertices; edges are lines or arcs connecting any two nodes in the graph. There are two types of representation techniques for a graph. Those are:

  • Matrix: Even though we won't be using it frequently, it's still helpful to have some familiarity with it.
  • List:
    • Arrays of Vectors
    • Vectors of Vectors

Vertices and Edges


This is a graph with five Vertices. We can also call them Nodes. If we connect the vertices with lines, then the lines will be called Edges.


Basically, all the edges above are bidirectional. It means that we can go from 1 to 3 and also from 3 to 1. We can make an edge unidirectional by giving it a detection using an arrow.



A tree is a graph that has no cycles in it.


This is not a tree because there is a cycle between 1, 2, and 3. If we remove that edge, then it will become a tree.


If a tree has n nodes, then the number of edges it will have is n-1.

Top comments (1)

suyious profile image
Suyash K.

love the diagrams ❤️

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.