CS With NehalCS With Nehal

Graph Data Structure Simulator

Create vertices and edges and watch the adjacency list and adjacency matrix update live, for directed or weighted graphs.

Edit the graph

Click one vertex, then another, to join them with an edge.

Graph

42735ABCDE
5
Vertices
5
Edges
10
Max edges

Adjacency list

Each vertex lists its neighbours. Space-efficient for sparse graphs.

A→B:4D:7degree 2
B→A:4C:2E:5degree 3
C→B:2degree 1
D→A:7E:3degree 2
E→B:5D:3degree 2

Adjacency matrix

Row → column. Cells hold the weight; 0 means none. Undirected graphs give a symmetric matrix.

ABCDE
A04070
B40205
C02000
D70003
E05030

Matrix uses V² = 25 cells. The list stores 10 entries.

About This Simulation

This graph data structure simulator lets you draw a graph by adding vertices and edges, then shows how it is stored as an adjacency list and as an adjacency matrix. Switch between directed and undirected, weighted and unweighted graphs and watch both representations update.

Features

  • Add, move and delete vertices and edges on a canvas
  • Directed or undirected, weighted or unweighted edges
  • Adjacency list updated live, with degrees
  • Adjacency matrix updated live
  • Edge and vertex counts with the maximum possible edges

Topics covered

  • Graphs
  • Vertices and Edges
  • Directed and Undirected Graphs
  • Weighted Graphs
  • Adjacency List
  • Adjacency Matrix

Explanation

A graph is a set of vertices (nodes) connected by edges. Edges can be directed (one-way) or undirected (two-way), and can carry weights such as distances or costs. Graphs model networks such as roads, social connections, web links and computer networks.

How it works

  1. Adjacency matrix: a V × V grid where cell [i][j] holds 1 (or the weight) if there is an edge from vertex i to vertex j, and 0 otherwise.
  2. In an undirected graph the matrix is symmetric, because an edge A–B is also B–A.
  3. Adjacency list: each vertex keeps a list of its neighbours (and the edge weights).
  4. A matrix uses V² space but checks whether an edge exists in O(1); a list uses space proportional to V + E and is better for sparse graphs.
  5. The degree of a vertex is its number of edges; in a directed graph there is an in-degree and an out-degree.

Why it matters

Graphs are on the A Level syllabus and are central to CSE220 and CSE221. Graph algorithms such as BFS, DFS, Dijkstra, Prim and Kruskal all start from one of these two representations.

Frequently Asked Questions

When should I use an adjacency matrix instead of a list?

Use a matrix for dense graphs or when you need to check quickly whether two vertices are connected. Use a list for sparse graphs, where it saves a lot of memory.

What is a weighted graph?

A graph where each edge has a value, such as a distance, time or cost.

How many edges can a graph have?

An undirected simple graph with V vertices has at most V(V − 1)/2 edges; a directed graph has at most V(V − 1).

Related Simulations