πŸ“š Chapter 1

πŸ”— Graph Basics: Introduction to Graph Structures

Learn the definition, terminology, representations, and properties of graph data structures

πŸ•ΈοΈ Graph Visualization (Click a node!)

Selected
Neighbor
Normal
πŸ“Š Current Graph Properties
Vertices
6
Edges
9
Type
Undirected
Weighted
Yes
Connected
Yes
Density
0.60

πŸ“ Graph Terminology Dictionary (Click to visualize)

Vertex (Node)
A fundamental unit of a graph, representing an entity or data point
Edge (Link)
A connection between two vertices; may have weight or direction
Adjacent
Two vertices connected directly by an edge are adjacent
Degree
Number of edges connected to a vertex (in-degree / out-degree for directed)
Path
A sequence of vertices connected by edges with no repeated vertices
Cycle
A path that starts and ends at the same vertex, forming a loop
Connected
A graph where a path exists between every pair of vertices
Weight
A numerical value assigned to an edge representing cost or distance
Directed
Edges have a specific direction from source to destination
Component
A maximal connected subgraph where every vertex is reachable from every other

πŸ“– Concept Guide

πŸ”— What is a Graph?

A Graph is a non-linear data structure consisting of vertices (nodes) and edges (links) connecting pairs of vertices.

πŸ’‘ Formal Definition:
G = (V, E) where:
β€’ V = set of vertices {v₁, vβ‚‚, ..., vβ‚™}
β€’ E = set of edges {(u,v) | u,v ∈ V}
β€’ Unlike trees, graphs can have cycles and multiple paths

Graph vs Tree

FeatureTreeGraph
CyclesNot allowedAllowed
Root nodeHas one rootNo root concept
EdgesExactly N-1Any number
DirectionParent β†’ ChildOptional
ConnectionAlways connectedCan be disconnected
πŸ“ Analogy

Think of a city map! Cities are vertices, roads are edges. Roads can be one-way (directed) or two-way (undirected), and have distances (weights).

πŸ“ Mathematical Properties

PropertyFormula
Max edges (undirected)V(V-1)/2
Max edges (directed)V(V-1)
Sum of all degrees2 Γ— |E| (undirected)
Density2|E| / V(V-1)
Tree condition|E| = |V| - 1 + connected
πŸ“Š Handshaking Lemma:
The sum of all vertex degrees = 2 Γ— number of edges.
Example: 9 edges β†’ sum of degrees = 18

⏱️ Complexity by Representation

OperationAdj. MatrixAdj. List
Check edge existsO(1)O(deg)
Get all neighborsO(V)O(deg)
Add edgeO(1)O(1)
Remove edgeO(1)O(deg)
SpaceO(VΒ²)O(V+E)

πŸ“š Types of Graphs

TypeDescription
UndirectedEdges have no direction; (u,v) = (v,u)
Directed (Digraph)Edges have direction; (u→v) ≠ (v→u)
WeightedEdges carry numerical values (cost/distance)
UnweightedAll edges are treated equally
ConnectedPath exists between every pair of vertices
DisconnectedSome vertices are unreachable from others
CyclicContains at least one cycle
Acyclic (DAG)No cycles; Directed Acyclic Graph
Complete (Kn)Every vertex connects to every other vertex
BipartiteVertices split into two disjoint sets; edges only between sets
Sparse|E| much less than |V|Β² β†’ use Adjacency List
Dense|E| close to |V|Β² β†’ use Adjacency Matrix
πŸ’‘ DAG (Directed Acyclic Graph):
Used in task scheduling, dependency resolution, build systems (Makefile, npm), and version control (Git commit history).

🌍 Real-World Applications

  • Social Networks β€” Friend connections (Facebook, LinkedIn, Instagram)
  • GPS / Maps β€” Shortest path navigation (Google Maps, Waze)
  • Internet β€” Web page links (PageRank), network routing
  • Recommendations β€” Netflix, Amazon, Spotify suggestions
  • Package Managers β€” Dependency resolution (npm, pip, Maven)
  • Compilers β€” Control flow graphs, syntax trees, call graphs
  • Biology β€” Protein interactions, neural networks, gene regulation
  • Transportation β€” Flight routes, railway networks, delivery optimization
  • AI / ML β€” Knowledge graphs, graph neural networks (GNN)
  • Blockchain β€” Transaction graphs, DAG-based ledgers
Data Structure Hierarchy:
β€’ Array/List β†’ 1D sequential data
β€’ Tree β†’ Hierarchical (1 parent per node)
β€’ Graph β†’ Complex relationships (any-to-any connections)

🎯 Selected Node Info

πŸ“ Click a node
Click a node in the graph to display its detailed information.
πŸ”€ Graph Mode
πŸ”¦ Highlight by Term
πŸ’Ύ Graph Representations
// Adjacency Matrix (Weighted) // A B C D E F A [ 0 4 2 0 0 0 ] B [ 4 0 1 5 0 0 ] C [ 2 1 0 8 10 0 ] D [ 0 5 8 0 2 6 ] E [ 0 0 10 2 0 3 ] F [ 0 0 0 6 3 0 ] // 0 means no edge exists // Space: O(VΒ²) = O(36)
// Adjacency List (Weighted) A β†’ [B:4] β†’ [C:2] B β†’ [A:4] β†’ [C:1] β†’ [D:5] C β†’ [A:2] β†’ [B:1] β†’ [D:8] β†’ [E:10] D β†’ [B:5] β†’ [C:8] β†’ [E:2] β†’ [F:6] E β†’ [C:10] β†’ [D:2] β†’ [F:3] F β†’ [D:6] β†’ [E:3] // Space: O(V + 2E) = O(6+18)
// Edge List (src, dst, weight) edges = [ (A, B, 4), (A, C, 2), (B, C, 1), (B, D, 5), (C, D, 8), (C, E, 10), (D, E, 2), (D, F, 6), (E, F, 3) ] // Space: O(E) = O(9) // Good for Kruskal's MST
πŸ“ Key Formulas
Max edges (undirected)
V(V-1)/2
Sum of degrees
2|E|
Density
2E / V(V-1)
Tree condition
E = V-1
Complete graph edges
n(n-1)/2
🌍 Real-World Examples
🌐Social Network β€” Friends & followers
πŸ—ΊοΈGoogle Maps β€” Shortest path routes
✈️Flight Routes β€” Airport connections
πŸ“¦npm / pip β€” Package dependencies