36. Introduction to Graphs
A Graph is a non-linear data structure used to model relationships between objects. It consists of a set of Vertices (V) (or nodes) and a set of Edges (E) (or connections).
Graph Types
- Undirected Graph: Edges have no direction. If A is connected to B, B is also connected to A (e.g., friendships on Facebook).
- Directed Graph (Digraph): Edges have a direction. A connection from A to B does not imply a connection from B to A (e.g., Twitter followers).
- Weighted Graph: Edges have numerical values (weights) assigned to them, representing cost, distance, or time (e.g., road network distances).
- Cyclic vs. Acyclic: A cycle is a path that starts and ends at the same vertex. A graph without cycles is acyclic (like a tree).
Graphs are used to model everything from transportation networks to website links and complex dependencies.