Data Structures · Visualization

Graph: Add Node

Graph

Adding a node introduces a new vertex to the graph with no connections yet. Time: O(1). Space: O(1) per node.

Big-O: BFS/DFS visit each vertex and edge once: O(V + E).

Graph: Add Node

Graph

Adding a node introduces a new vertex to the graph with no connections yet. In an adjacency-list representation (the most common approach), this simply means creating a new, empty list to hold that node's future neighbors.

How Add Node Works

  1. Choose a unique identifier for the new node.
  2. Create an entry for it in the graph's structure (e.g. a new empty list in an adjacency list).
  3. The node now exists in the graph but has no edges yet.

Add Node Worked Example

Input: Add node 'D' to a graph currently containing {A, B, C}

  1. Create a new empty adjacency list for 'D'
  2. Graph now recognizes D as a vertex with no connections

Output: Vertices: {A, B, C, D}; D has no edges yet

Graph: Introduction
A set of nodes connected by edges — edges can be one-way or two-way, and optionally carry a weight (cost/distance).
A
B
C
D
E
Unvisited Visited / discovered Current Start
Graph: Introduction
Press ▶ Run to watch it step by step.
Python · Graphs in Code
def add_node(graph, v):
    if v not in graph:
        graph[v] = []       # no neighbours yet
    return graph
# example: add node
Input: Add node 'D' to a graph currently containing {A, B, C}

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Add Node Time & Space Complexity

TimeO(1)
SpaceO(1) per node

When to Use Add Node

  • Registering a new user in a social network graph
  • Adding a new city to a map/routing graph
  • Adding a new task to a dependency graph before linking its dependencies

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.