Graph

A non-linear data structure consisting of a finite set of vertices (or nodes) and a set of edges connecting these vertices.

Traversal Visualizer

Goes as deep as possible before backtracking.

A
B
C
D
E
Order:

Types / Variants

Undirected Graph

Directed Graph

Weighted Graph

Unweighted Graph

Cyclic Graph

Acyclic Graph

Undirected Graph

Edges have no direction, representing a two-way relationship. If A is connected to B, B is also connected to A.

ABCD

Complexity Analysis

OperationTime ComplexityNote
Add VertexO(1)For adjacency list
Add EdgeO(1)For adjacency list
Remove VertexO(V + E)For adjacency list
Remove EdgeO(E)For adjacency list
Search (DFS/BFS)O(V + E)

V = number of vertices, E = number of edges

When to Use Graphs

  • Modeling networks: social networks, computer networks, road maps.
  • Finding shortest paths (e.g., GPS navigation).
  • Dependency resolution (e.g., course prerequisites, build systems).
  • Web page crawling and search engine indexing.