Graphs are a data structure consisting of nodes/vertices connected by edges.
Each of these use this example graph:
A---B
| / |
C D
A table of nodes with a list of the adjacent nodes.
Vertex | Adjacent Vertices |
---|---|
A | B C |
B | A C D |
C | A B |
D | B |
Each node is in a row and column and edges correspond to 1/true for the row and column.
A | B | C | D | |
---|---|---|---|---|
A | 1 | 1 | ||
B | 1 | 1 | 1 | |
C | 1 | 1 | ||
D | 1 |
Process the starting vertex, then as far down a path as possible, then backtracking until to find a new unvisited path.
Useful for solving mazes.
Process the starting vertex, then all vertices of distance 1, then of distance 2, etc. without visiting a vertex twice.
Useful to find the shortest path between two nodes.
Edges can only go in one direction.
Useful for links between web pages, airline connections between cities, college course prerequisites.
Each edge has a weight/cost associated with it.
A minimum spanning tree (MST) is a subset of edges that connects all nodes with the minimum total edge weight and no cycles.
Time complexity is O(E log E) and space complexity is O(E + V).
Example:
A -5- B
/ /
1 2
/ /
E -2- D ---2-- H
| / \ /
1 5 11 1
| / \ /
F ---7-- G
Iteration 0:
Prio Q: E-A, E-F, G-H, E-D, D-H, D-B, F-D, A-B, D-G
Groups:
Iteration 1:
Prio Q: E-F, G-H, E-D, D-H, D-B, F-D, A-B, D-G
Groups: 0: E-A
Iteration 2:
Prio Q: G-H, E-D, D-H, D-B, F-D, A-B, D-G
Groups: 0: E-A, E-F
Iteration 3:
Prio Q: E-D, D-H, D-B, F-D, A-B, D-G
Groups: 0: E-A, E-F
1: G-H
Iteration 4:
Prio Q: D-H, D-B, F-D, A-B, D-G
Groups: 0: E-A, E-F, E-D
1: G-H
Iteration 5:
Prio Q: D-B, F-D, A-B, D-G
Groups: 0: E-A, E-F, E-D, D-H, G-H
Iteration 6:
Prio Q: F-D, A-B, D-G
Groups: 0: E-A, E-F, E-D, D-H, G-H, D-B
Iteration 7:
Prio Q: A-B, D-G
Groups: 0: E-A, E-F, E-D, D-H, G-H, D-B
Iteration 8:
Prio Q: D-G
Groups: 0: E-A, E-F, E-D, D-H, G-H, D-B
Iteration 9:
Prio Q:
Groups: 0: E-A, E-F, E-D, D-H, G-H, D-B
Result:
A B
/ /
1 2
/ /
E -2- D ---2-- H
| /
1 1
| /
F G
Time Complexity is O((V + E) log V) and space complexity is O(V + E).
Example:
A -5- B
/ /
1 2
/ /
E -2- D ---2-- H
| / \ /
1 5 11 1
| / \ /
F ---7-- G
Ad eds - The adjacent edges to the result nodes that aren't already in the result.
Iteration 0:
Result: E-A
Ad eds: E-F, E-D, A-B
Iteration 1:
Result: E-A, E-F
Ad eds: F-G, F-D, E-D, A-B
Iteration 2:
Result: E-A, E-F, E-D
Ad eds: F-G, F-D, D-G, D-H, D-B, A-B
Iteration 3:
Result: E-A, E-F, E-D, D-H
Ad eds: F-G, F-D, D-G, G-H, D-B, A-B
Iteration 3:
Result: E-A, E-F, E-D, D-H, G-H
Ad eds: F-G, F-D, D-G, D-B, A-B
Iteration 4:
Result: E-A, E-F, E-D, D-H, G-H, D-B
The shortest path is found by tracing backward from the destination to the start through its prevNodes.
| | A | B | C | D | E |
|---|----|----|----|----|----|
| A | 0 | 90 | 10 | 0 | 50 |
| B | 90 | 0 | 0 | 30 | 0 |
| C | 10 | 0 | 0 | 40 | 20 |
| D | 0 | 30 | 40 | 0 | 10 |
| E | 50 | 0 | 20 | 10 | 0 |
Iteration 0:
| Nodes | A | B | C | D | E |
|-------------------|---|-----|-----|-----|-----|
| distanceFromStart | 0 | inf | inf | inf | inf |
| prevNode | 0 | 0 | 0 | 0 | 0 |
| processed | 0 | 0 | 0 | 0 | 0 |
Iteration 1:
| Nodes | A | B | C | D | E |
|-------------------|---|----|----|-----|----|
| distanceFromStart | 0 | 90 | 10 | inf | 50 |
| prevNode | 0 | A | A | 0 | A |
| processed | 1 | 0 | 0 | 0 | 0 |
Iteration 2:
| Nodes | A | B | C | D | E |
|-------------------|---|----|----|----|----|
| distanceFromStart | 0 | 90 | 10 | 40 | 30 |
| prevNode | 0 | A | A | C | C |
| processed | 1 | 0 | 1 | 0 | 0 |
Iteration 3:
| Nodes | A | B | C | D | E |
|-------------------|---|----|----|----|----|
| distanceFromStart | 0 | 90 | 10 | 40 | 30 |
| prevNode | 0 | A | A | C | C |
| processed | 1 | 0 | 1 | 0 | 1 |
Iteration 4:
| Nodes | A | B | C | D | E |
|-------------------|---|----|----|----|----|
| distanceFromStart | 0 | 70 | 10 | 40 | 30 |
| prevNode | 0 | D | A | C | C |
| processed | 1 | 0 | 1 | 1 | 1 |
Backtrack from B to A.
B-D-C-A with total weight of 70
Can also be used for for unweighted graphs with an edge weight of 1, but shouldn’t be used if edge weights are negative.