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
GraphAdding 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
- Choose a unique identifier for the new node.
- Create an entry for it in the graph's structure (e.g. a new empty list in an adjacency list).
- 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}
- Create a new empty adjacency list for 'D'
- Graph now recognizes D as a vertex with no connections
Output: Vertices: {A, B, C, D}; D has no edges yet
Press ▶ Run to watch it step by step.
def add_node(graph, v):
if v not in graph:
graph[v] = [] # no neighbours yet
return graph
# example: add nodeBehind 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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.