- Home
- /
- Tutorials
- /
- DSA Tutorial
- /
- Graphs and How to Represent Them
Graphs
Graphs and How to Represent Them
Graphs sound advanced and are not. A graph is a tree that is allowed to have loops and more than one route between two points - and that one difference is why every graph algorithm needs a visited set.
The vocabulary you need
- Node (or vertex) - a thing. A person, a page, a city.
- Edge - a connection between two nodes.
- Directed - edges go one way. Twitter follows.
- Undirected - edges go both ways. Facebook friends.
- Weighted - edges carry a cost. Distance, price, time.
- Cycle - a path that returns to where it started.
Adjacency list - the one you will use
A Map from each node to its neighbours. Compact, and fast to walk.
Building an adjacency list
javascript
