Graphs: Networks of Data
Explore graphs, the data structure that models complex networks and relationships, from social networks to GPS navigation.
What Is a Graph?
A graph is a non-linear data structure consisting of **nodes** (or vertices) and **edges**. An edge is a connection between two nodes. Unlike trees, graphs can have cycles (a path that starts and ends at the same node) and nodes can be connected in any way, making them perfect for representing networks.
Key Concepts
- Directed vs. Undirected: In a directed graph, edges have a direction (like a one-way street). In an undirected graph, edges are two-way.
- Weighted vs. Unweighted: In a weighted graph, each edge has a "cost" or "weight". This is used in GPS systems to represent distance or travel time.
- Adjacency List: The most common way to represent a graph in code, where each node has a list of its neighbors.