Member-only story
Data Structures: Understanding Graphs
What is a graph? Graphs are used to represents relationships and hierarchies and are composed of nodes and edges.
What is a node? A node represents the objects in the graph. If Medium is the graph, users and posts are the objects.
What is an edge? An edge represents the connections between the objects. Nodes are connected by edges. If I post a blog on medium, there is an edge between me and my post. There is also an edge between me and another user I follow.
Graphs can be directed or undirected.
Directed A graph where an edge between nodes goes in one direction. On Instagram, John can follow Jane without Jane following John.
Undirected A graph where an edge between nodes goes in both directions. On Facebook, if John is friends with Jane, Jane must also be friends with John.
There are three ways to represent a graph. Most commonly an adjacency list or edge list, less commonly a grid.
Given this simple flight map, where cities represent the nodes, and routes the edges, here are examples of adjacency and edge list representations.