Graphs
Introduction #
Graphs are simply a collection of vertices connected by edges. They’re very similar to trees, but are much more versatile and don’t require hierarchical relationships like trees do.
For most purposes, we will be working with simple graphs that follow two rules:
- There are no loops (a connection of a node to itself).
- There are no parallel edges (two edges that connect the same two vertices).
Graph Properties #
Graphs can be described by some properties that they could have. Here are the important ones:
A graph can be directed if edges are arrows and have a direction, or undirected if you can cross edges in any direction.
A graph is cyclic if the edges form a loop, or acyclic if there are no loops (like in a tree).
Graphs can have edge labels if edges are numbered (great for distances). They can also have vertex weights if vertices are numbered (great for priorities or costs).
Graphs are connected if all of the vertices are connected with edges, such that you can freely move from one vertex to any other vertex.
Graph Queries #
Here are some cool things you can do with graphs:
- Is there a path between two vertices? (s-t path)
- What is the shortest route between two vertices? (shortest s-t path)
- Are there cycles? (cycle detection)
- Can you visit each vertex/edge exactly once? (Euler tour / Hamilton tour)
- Is a graph connected? (connectivity problem)
- Is a vertex that disconnects the graph when removed? (single point of failure / biconnectivity)
- Are two graphs isomorphic?
- Can a graph be drawn with no crossing edges? (planarity)
More on Graphs #
Depth First Search (DFS)
Depth First Traversal
Before we move on to searching, let's talk about traversing. Traversal is the act of visiting nodes in...
Breadth First Search (BFS), like Depth First Search (DFS), is a method of traversing a graph. BFS simply traverses in... Content Note
> Before continuing, make sure you're comfortable with Graphs, Stacks and Queues, and Shortest Paths.
One sentence overview
Visit vertices in... Content Note
> In order to understand A\*, you'll need to be comfortable Dijkstra's Algorithm first!
A\* Algorithm
The A\* Search Algorithm is... Content Note
> Before reading, review Minimum Spanning Trees, as that is the foundation of Prim's algorithm!
Conceptual Overview
Prim's algorithm is an... Content Note
> Before reading, review Minimum Spanning Trees and Union Find (Disjoint Sets) as they both make Kruskal's algorithm possible!
Conceptual...Depth First Search (DFS)
Breadth First Search (DFS)
Dijkstra's Algorithm
A* Search
Prim's Algorithm
Kruskal's Algorithm