Skip to content

Graph

Graph

“A graph models a set of connections.”[1] It’s a mathematical data structure used to model 1-to-1 relationships between components of the graph.

Graphs are made up of nodes and edges:

There are multiple types of graphs:

Implementation

Graphs are implemented using Hash tables. Each key in the hash table is assigned a list of its direct neighbors.

Here’s a python implementation:

graph = {}
graph["chicago"] = ["detroit", "st. louis", "minneapolis"]
graph["detroit"] = ["chicago", "toronto"]
graph["st. louis"] = ["chicago, "nashville", "tulsa"]
graph["nashville"] = ["st. louis", "tulsa", "charlotte"]
graph["honolulu"] = []