Skip to content

Introduction to Graphs

A comprehensive overview of graphs — what they are, key terminologies, and the different types you’ll encounter in interviews.


A graph is a non-linear data structure consisting of a set of vertices (nodes) connected by edges (links). Unlike trees (which are a special type of graph), graphs can have cycles, disconnected components, and edges pointing in any direction.

Graph Types

Simple Example:

(A)-----(B)
| / |
| / |
| / |
(C)-----(D)
|
(E)
Vertices: A, B, C, D, E
Edges: A-B, A-C, B-C, B-D, C-D, C-E

TermDefinitionReal-World Example
Vertex (Node)A fundamental unit/point in a graphCities on a map
EdgeA connection between two verticesRoads between cities
DegreeNumber of edges connected to a vertexCity with 3 roads → degree 3
In-DegreeEdges coming INTO a vertex (directed)Twitter followers
Out-DegreeEdges going OUT of a vertex (directed)Twitter following
PathA sequence of vertices connected by edgesRoute from A to E
Simple PathA path with no repeated verticesDirect route, no backtracking
CycleA path that starts and ends at the same vertexA → B → C → A
Self-LoopAn edge from a vertex to itselfA → A
NeighborVertex directly connected by an edgeAdjacent cities
ConnectedEvery vertex reachable from every other vertexOne continent
ComponentA maximal connected subgraphIslands in a sea
WeightA value/cost assigned to an edgeDistance in kilometers

UndirectedDirected (Digraph)
Edges have no direction — A-B means both A→B and B→AEdges have direction (arrows) — A→B does NOT imply B→A
Used for: social networks (friendships), road networksUsed for: web pages (hyperlinks), Twitter follows
flowchart LR
subgraph Undirected
A1((A)) --- B1((B))
A1 --- C1((C))
B1 --- D1((D))
C1 --- D1
end
subgraph Directed
A2((A)) --> B2((B))
A2 --> C2((C))
C2 --> D2((D))
B2 --> D2
end
style Undirected fill:#1e293b,color:#fff
style Directed fill:#1e293b,color:#fff
Undirected: Directed:
A --- B A ---> B
| | | |
C --- D v v
C <--- D
UnweightedWeighted
All edges are equalEach edge has a numeric cost/weight
Used for: hop count, simple connectivityUsed for: shortest path (distance, time, cost)
flowchart LR
subgraph Unweighted
A1((A)) --- B1((B))
A1 --- C1((C))
B1 --- D1((D))
C1 --- D1
end
subgraph Weighted
A2((A)) -- 5 --- B2((B))
A2 -- 10 --- C2((C))
B2 -- 3 --- D2((D))
C2 -- 7 --- D2
end
style Unweighted fill:#1e293b,color:#fff
style Weighted fill:#1e293b,color:#fff
Unweighted: Weighted:
A --- B A --5-- B
| | | |
C --- D 10 3
| |
C --7-- D
CyclicAcyclic (DAG)
Contains at least one cycleNo cycle possible
Used for: detecting deadlocksUsed for: task scheduling, build systems
flowchart LR
subgraph Cyclic
A1((A)) --> B1((B))
B1 --> C1((C))
C1 --> D1((D))
D1 --> A1
end
subgraph DAG[Acyclic (DAG)]
A2((A)) --> B2((B))
A2 --> C2((C))
B2 --> D2((D))
C2 --> D2
D2 --> E2((E))
end
style Cyclic fill:#1e293b,color:#fff
style DAG fill:#1e293b,color:#fff
Cyclic: Acyclic (DAG):
A → B A → B
↑ ↓ ↓ ↓
D ← C C D
↓
E
ConnectedDisconnected
All nodes reachable from every other nodeSome nodes are isolated (multiple components)
flowchart LR
subgraph Connected
A1((A)) --- B1((B))
A1 --- C1((C))
B1 --- D1((D))
C1 --- D1
end
subgraph Disconnected
A2((A)) --- B2((B))
D2((D)) --- E2((E))
D2 --- F2((F))
end
style Connected fill:#1e293b,color:#fff
style Disconnected fill:#1e293b,color:#fff
Connected: Disconnected:
A --- B A --- B D --- E
| | |
C --- D F
TypeDescriptionEdges
TreeConnected, acyclic, undirectedN-1 edges for N nodes
ForestCollection of disjoint treesMultiple trees
Complete Graph (Kₙ)Every pair of vertices is connectedN×(N-1)/2 edges
Bipartite GraphVertices split into 2 groups; edges only between groupsNo odd-length cycles
DAGDirected graph with no cyclesUsed for scheduling, dependency resolution
flowchart LR
subgraph Tree
T1((1)) --- T2((2))
T1 --- T3((3))
T2 --- T4((4))
T2 --- T5((5))
end
subgraph Bipartite
B1((1)) --- B4((4))
B1 --- B5((5))
B2((2)) --- B4
B3((3)) --- B5
end
style Tree fill:#1e293b,color:#fff
style Bipartite fill:#1e293b,color:#fff

Now that you understand what graphs are and their types, move on to learn about Graph Representations — how to store and work with graphs in code.