Data Structure: Graph

Summary

Data Structure NameGraph
TypeLinear Data Structure
Tagline
Use cases
TypesDirected Graph
Undirected Graph

Definition

Graphs are data structures with connected nodes/vertexes which are connected by arcs/edges. We are using the adjacency list approach to represent a Graph here.

Graph of cities
Graph of cities

NOTE

In this article, we are discussing the implementation of Graph in JavaScript and TypeScript.

Check the link below to check the details of Graph-

Graph Overview
Graph Overview

Types

Graphs can be of the following types-

Directed Graph
Undirected Graph

Use Cases

When we have a long data list, and we don’t need random access to an element, then we should use a linked list.

Implementation

We are storing the graph information in an adjacency list. Using JavaScript Map to store the elements and each element has an array. It will look like the below when represented in a JS object-

Graph elements saved in an Adjacency List
Graph elements saved in an Adjacency List

Here is how we can implement a graph data structure in JavaScript and TypeScript-

Step #1: Graph Class

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #2: Add Vertex

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #3: Remove Vertex

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #4: Add Edge

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #5: Remove Edge

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #6: DFS

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #7: DFS with Recursion

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Step #8: BFS

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Full Source Code

Here is the full source code of Graph implementation-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Demo

Use the following code to check the implementation-

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Output:

Here is the output of the above demo code-

---------- Graph - Add Vertex example -----------

{
  Paris: [],
  Tokyo: [],
  Rome: [],
  Berlin: [],
  Moscow: [],
  London: [],
  'Some Remote Place': []
}

---------- Graph - Add Edge example -----------

{
  Paris: [ 'Tokyo', 'Rome', 'London' ],
  Tokyo: [ 'Paris', 'London' ],
  Rome: [ 'Paris', 'London' ],
  Berlin: [ 'Moscow', 'London' ],
  Moscow: [ 'Berlin', 'London', 'Some Remote Place' ],
  London: [ 'Paris', 'Tokyo', 'Rome', 'Berlin', 'Moscow' ],
  'Some Remote Place': [ 'Moscow' ]
}

---------- Graph - DFS example -----------

DFS starting from London:  [
  'London',
  'Moscow',
  'Some Remote Place',
  'Berlin',
  'Rome',
  'Tokyo',
  'Paris'
]
DFS starting from Moscow:  [
  'Moscow',
  'Some Remote Place',
  'London',
  'Rome',
  'Tokyo',
  'Paris',
  'Berlin'
]
DFS starting from Tokyo:  [
  'Tokyo',
  'London',
  'Moscow',
  'Some Remote Place',
  'Berlin',
  'Rome',
  'Paris'
]

---------- Graph - DFS (using Recursion) example -----------

DFS starting from London:  [
  'London',
  'Paris',
  'Tokyo',
  'Rome',
  'Berlin',
  'Moscow',
  'Some Remote Place'
]
DFS starting from Moscow:  [
  'Moscow',
  'Berlin',
  'London',
  'Paris',
  'Tokyo',
  'Rome',
  'Some Remote Place'
]
DFS starting from Tokyo:  [
  'Tokyo',
  'Paris',
  'Rome',
  'London',
  'Berlin',
  'Moscow',
  'Some Remote Place'
]

---------- Graph - BFS example -----------

BFS starting from London:  [
  'London',
  'Paris',
  'Tokyo',
  'Rome',
  'Berlin',
  'Moscow',
  'Some Remote Place'
]
BFS starting from Moscow:  [
  'Moscow',
  'Berlin',
  'London',
  'Some Remote Place',
  'Paris',
  'Tokyo',
  'Rome'
]
BFS starting from Tokyo:  [
  'Tokyo',
  'Paris',
  'London',
  'Rome',
  'Berlin',
  'Moscow',
  'Some Remote Place'
]

---------- Graph - Remove Edge example -----------

{
  Paris: [ 'Rome', 'London' ],
  Tokyo: [],
  Rome: [ 'Paris', 'London' ],
  Berlin: [ 'London' ],
  Moscow: [ 'London', 'Some Remote Place' ],
  London: [ 'Paris', 'Rome', 'Berlin', 'Moscow' ],
  'Some Remote Place': [ 'Moscow' ]
}

---------- Graph - Remove Vertex example -----------

{
  Paris: [ 'Rome', 'London' ],
  Tokyo: [],
  Rome: [ 'Paris', 'London' ],
  Moscow: [ 'London', 'Some Remote Place' ],
  London: [ 'Paris', 'Rome', 'Moscow' ],
  'Some Remote Place': [ 'Moscow' ]
}

Testing

Here is the test cases for the Graph implementation-

Tests are implemented with ‘vitest‘.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Source Code

Use the following links to get the source code used in this article-

Related Data Structures

CommandDetails
Binary Heap Binary Heap

Other Code Implementations

Use the following links to check the BFS implementation in other programming languages-

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.