A Graph is a non-linear network consisting of Vertex nodes connected by Edge lines, representing relationships in social graphs, maps, and networks.
Enqueue source vertex A and mark A as visited.
Queue: [A] | Visited: {A}Dequeue A. Fetch adjacent neighbor nodes B and C. Enqueue unvisited neighbors.
Dequeued: A | Queue: [B, C] | Visited: {A, B, C}Dequeue B, inspect neighbor D. Enqueue D. Next dequeue C.
Traversal Output Order: A -> B -> C -> DCities are vertices and direct flight paths between them are edges. Connections can be one-way (directed) or round-trip (undirected).
~4,483 steps (Linearithmic)
O((V + E) log V) Time
O(V + E) Graph Memory
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Adjacency List Space | O(V + E) | O(V + E) |
| Adjacency Matrix Space | O(V²) | O(V²) |
class Graph {
constructor() { this.adjList = new Map(); }
addVertex(v) { if (!this.adjList.has(v)) this.adjList.set(v, []); }
addEdge(u, v) { this.adjList.get(u).push(v); }
}Models friend connections on LinkedIn/Facebook.
Finds shortest routes between city locations.
Validate your conceptual understanding, operation mechanics, and core concepts of Graph.
Use Adjacency Lists for sparse graphs (E << V²) to save memory. Use Adjacency Matrices for dense graphs where fast edge lookup is needed!
Graph loaded. Add or remove edges to dynamically update the adjacency list.