Back

Graph Representation Visualization

Interactive Graph Builder

Build a graph by adding nodes and edges, and see its representation as an Adjacency List and Adjacency Matrix.

Add nodes and edges to build your graph.

Adjacency List

{}

Adjacency Matrix

[]

Graph Representations Explained

Graphs can be represented in several ways, each with trade-offs in terms of space complexity and efficiency for different operations.

Conceptual Data Structures

// Adjacency List (using Map for non-numeric IDs)
const adjList = new Map();
adjList.set('A', [{ node: 'B', weight: 5 }, { node: 'C', weight: 2 }]);
adjList.set('B', [{ node: 'A', weight: 5 }]);
adjList.set('C', [{ node: 'A', weight: 2 }]);
// To check neighbors of 'A': adjList.get('A')

// Adjacency Matrix (assuming nodes 0, 1, 2 correspond to A, B, C)
// INF represents no direct edge
const INF = Infinity;
const adjMatrix = [
  // A  B    C
  [ 0,  5,   2 ], // A
  [ 5,  0, INF ], // B
  [ 2, INF,  0 ]  // C
];
// To check edge A -> C: adjMatrix[0][2] (returns 2)
// To check edge B -> C: adjMatrix[1][2] (returns Infinity)