A graph data structure consists of a finite (and possibly mutable) set of vertices or nodes or points, together with a set of unordered pairs of these vertices for an undirected graph or a set of ordered pairs for a directed graph.
Nodes
+ Connections
A
B
E
C
F
D
A
B
C
D
E
F
A
B
E
C
F
D
A
B
C
D
E
F
A
B
E
C
F
D
A
B
C
D
E
F
A
B
E
C
F
D
A
B
C
D
E
F
https://www.flickr.com/photos/kencf0618/6602925613/
Borderlands
Halo
Things in Common
Space
Future
Sci-Fi
Guns
A
B
E
C
F
D
A
B
C
D
E
F
TERMINOLOGY
Edge
Vertex
A
B
E
C
F
D
A
B
C
D
E
F
Maria
Armie
Tim
Nan
Joan
A
B
E
C
F
D
A
B
C
D
E
F
Maria
Justin
Bieber
Tim
Kanye
Joan
A
B
E
C
F
D
A
B
C
D
E
F
WEIGHTED GRAPH
10
17
20
8
15
7
90km
120km
65km
19km
3km
20km
40km
13km
HOW DO WE STORE THIS???
A
F
B
C
E
D
- | A | B | C | D | E | F |
---|---|---|---|---|---|---|
A | 0 | 1 | 0 | 0 | 0 | 1 |
B | 1 | 0 | 1 | 0 | 0 | 0 |
C | 0 | 1 | 0 | 1 | 0 | 0 |
D | 0 | 0 | 1 | 0 | 1 | 0 |
E | 0 | 0 | 0 | 1 | 0 | 1 |
F | 1 | 0 | 0 | 0 | 1 | 0 |
[
[1,5],
[0,2],
[1,3],
[2,4],
[3,5],
[4,0]
]
0
5
1
2
4
3
0
1
2
3
4
5
{
A: ["B", "F"],
B: ["A", "C"],
C: ["B", "D"],
D: ["C", "E"],
E: ["D", "F"],
F: ["E", "A"]
}
A
F
B
C
E
D
OPERATION | ADJACENCY LIST | ADJACENCY MATRIX |
---|---|---|
Add Vertex | O(1) | O(|V^2|) |
Add Edge | O(1) | O(1) |
Remove Vertex | O(|V| + |E|) | O(|V^2|) |
Remove Edge | O(|E|) | O(1) |
Query | O(|V| + |E|) | O(1) |
Storage | O(|V| + |E|) | O(|V^2|) |
|V| - number of vertices
|E| - number of edges
VS.
Most data in the real-world tends to lend itself to sparser and/or larger graphs
Why?
An Adjacency List
An Adjacency List
An Adjacency List
class Graph {
constructor(){
this.adjacencyList = {}
}
}
WE ARE BUILDING AN UNDIRECTED GRAPH
g.addVertex("Tokyo")
{
"Tokyo": []
}
{
"Tokyo": [],
"Dallas": [],
"Aspen": []
}
g.addEdge("Tokyo", "Dallas")
{
"Tokyo": ["Dallas"],
"Dallas": ["Tokyo"],
"Aspen": []
}
g.addEdge("Dallas", "Aspen")
{
"Tokyo": ["Dallas"],
"Dallas": ["Tokyo", "Aspen"],
"Aspen": ["Dallas"]
}
g.removeEdge("Tokyo", "Dallas")
{
"Tokyo": ["Dallas"],
"Dallas": ["Tokyo", "Aspen"],
"Aspen": ["Dallas"]
}
{
"Tokyo": [],
"Dallas": ["Aspen"],
"Aspen": ["Dallas"]
}
g.removeVertex("Hong Kong")
{
"Tokyo": ["Dallas", "Hong Kong"],
"Dallas": ["Tokyo", "Aspen", "Hong Kong", "Los Angeles"],
"Aspen": ["Dallas"],
"Hong Kong": ["Tokyo", "Dallas", "Los Angeles"],
"Los Angeles": ["Hong Kong", "Dallas"]
}
{
"Tokyo": ["Dallas"],
"Dallas": ["Tokyo", "Aspen","Los Angeles"],
"Aspen": ["Dallas"],
"Los Angeles": ["Dallas"]
}
Borderlands
Halo
Things in Common
Space
Future
Sci-Fi
Guns
Maria
Armie
Tim
Nan
Joan
Explore as far as possible down one branch before "backtracking"
A
B
E
C
F
D
A
B
C
D
E
F
(STARTING FROM "A")
A
B
E
C
F
D
A
B
C
D
E
F
(STARTING FROM "A")
A
B
E
C
F
D
A
B
C
D
E
F
{
"A":["B", "C"],
"B":["A", "D"],
"C":["A", "E"],
"D":["B", "E", "F"],
"E":["C", "D", "F"],
"F":["D", "E"]
}
X
X
X
X
X
X
X
X
X
X
X
X
X
X
A
B
E
C
F
D
A
B
C
D
E
F
{
"A":["B", "C"],
"B":["A", "D"],
"C":["A", "E"],
"D":["B", "E", "F"],
"E":["C", "D", "F"],
"F":["D", "E"]
}
{
"A": true,
"B": true,
"D": true,
"E": true,
"C": true,
"F": true
}
{
"A": true,
"B": true,
"D": true,
"E": true,
"C": true,
}
{
"A": true,
"B": true,
"D": true,
"E": true,
}
{
"A": true,
"B": true,
"D": true,
}
{
"A": true,
"B": true,
}
{
"A": true,
}
{
}
g.addVertex("A")
g.addVertex("B")
g.addVertex("C")
g.addVertex("D")
g.addVertex("E")
g.addVertex("F")
g.addEdge("A","B")
g.addEdge("A","C")
g.addEdge("B","D")
g.addEdge("C","E")
g.addEdge("D","E")
g.addEdge("D","F")
g.addEdge("E","F")
A
B
E
C
F
D
A
B
C
D
E
F
(STARTING FROM "A")
DFS(vertex):
if vertex is empty
return (this is base case)
add vertex to results list
mark vertex as visited
for each neighbor in vertex's neighbors:
if neighbor is not visited:
recursively call DFS on neighbor
Recursive
{
"A": true,
"B": true,
"D": true
}
The function should accept a starting node
Create a list to store the end result, to be returned at the very end
Create an object to store visited vertices
Create a helper function which accepts a vertex
The helper function should return early if the vertex is empty
The helper function should place the vertex it accepts into the visited object and push that vertex into the result array.
Loop over all of the values in the adjacencyList for that vertex
If any of those values have not been visited, recursively invoke the helper function with that vertex
Invoke the helper function with the starting vertex
Return the result array
Recursive
DFS-iterative(start):
let S be a stack
S.push(start)
while S is not empty
vertex = S.pop()
if vertex is not labeled as discovered:
visit vertex (add to result list)
label vertex as discovered
for each of vertex's neighbors, N do
S.push(N)
Iterative
The function should accept a starting node
Create a stack to help use keep track of vertices (use a list/array)
Create a list to store the end result, to be returned at the very end
Create an object to store visited vertices
Add the starting vertex to the stack, and mark it visited
While the stack has something in it:
Pop the next vertex from the stack
If that vertex hasn't been visited yet:
Mark it as visited
Add it to the result list
Push all of its neighbors into the stack
Return the result array
Iterative
Visit neighbors at current depth first!
A
B
E
C
F
D
A
B
C
D
E
F
(STARTING FROM "A")
A
B
E
C
F
D
A
B
C
D
E
F
When working with weighted and directed/undirected graphs, we very commonly want to know how to get from one vertex to another! Better yet, how to do it quickly.
What's the fastest way to get from point A to point B?
One of the most famous and widely used algorithms around!
Finds the shortest path between two vertices on a graph
"What's the fastest way to get from point A to point B?"
Edsger Dijkstra was a Dutch programmer, physicist, essayist, and all around smarty-pants
He helped to advance the field of computer science from an "art" to an academic discipline
Many of his discoveries and algorithms are still commonly used to this day.
HE
DID
A
LOT...
What is the shortest way to travel from Rotterdam to Groningen, in general: from given city to given city. It is the algorithm for the shortest path, which I designed in about twenty minutes. One morning I was shopping in Amsterdam with my young fiancée, and tired, we sat down on the café terrace to drink a cup of coffee and I was just thinking about whether I could do this, and I then designed the algorithm for the shortest path. As I said, it was a twenty-minute invention. Eventually that algorithm became, to my great amazement, one of the cornerstones of my fame.
— Edsger Dijkstra, in an interview with Philip L. Frana, Communications of the ACM, 2001[2]
A QUOTE
90km
120km
65km
19km
3km
20km
40km
13km
HOW DOES GPS WORK?
Find the shortest path from Santiago to Antigua
A
B
90km
120km
65km
19km
3km
20km
40km
13km
OUR GRAPH CAN'T DO THIS YET!
A
B
C
D
E
F
2
4
4
1
1
3
3
2
Find the shortest path from A to E
THE APPROACH
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | |
B | |
C | |
D | |
E | |
F |
[]
{
A: null,
B: null,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B | Infinity |
C | Infinity |
D | Infinity |
E | Infinity |
F | Infinity |
[]
{
A: null,
B: null,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B | Infinity |
C | Infinity |
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: null,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B | Infinity |
C | Infinity |
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: null,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B |
|
C | Infinity |
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: null,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B |
|
C | Infinity |
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: A,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B |
|
C | Infinity |
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: A,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B |
|
C |
|
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: A,
C: null,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
A | 0 |
B |
|
C |
|
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: A,
C: A,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...A
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D | Infinity |
E | Infinity |
F | Infinity |
[A]
{
A: null,
B: A,
C: A,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D | Infinity |
E | Infinity |
F | Infinity |
[A,C]
{
A: null,
B: A,
C: A,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D |
|
E | Infinity |
F | Infinity |
[A,C]
{
A: null,
B: A,
C: A,
D: null,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D |
|
E | Infinity |
F | Infinity |
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D |
|
E | Infinity |
F | Infinity |
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D |
|
E | Infinity |
F |
|
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: null
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
C |
|
D |
|
E | Infinity |
F |
|
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: C
}
Previous:
Visited:
Pick The Smallest...C
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
|
|
D |
|
E | Infinity |
F |
|
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: C
}
Previous:
Visited:
Pick The Smallest...B
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
|
|
D |
|
E | Infinity |
F |
|
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: C
}
Previous:
Visited:
Pick The Smallest...B
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
|
|
D |
|
E |
|
F |
|
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: null,
F: C
}
Previous:
Visited:
Pick The Smallest...B
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
|
|
D |
|
E |
|
F |
|
[A,C]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: C
}
Previous:
Visited:
Pick The Smallest...B
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
B |
|
|
|
D |
|
E |
|
F |
|
[A,C,B]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: C
}
Previous:
Visited:
Pick The Smallest...B
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
D |
|
E |
|
F |
|
[A,C,B]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: C
}
Previous:
Visited:
Pick The Smallest...D
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
D |
|
E |
|
F |
|
[A,C,B]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: C
}
Previous:
Visited:
Pick The Smallest...D
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
D |
|
E |
|
F |
|
[A,C,B]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: C
}
Previous:
Visited:
Pick The Smallest...D
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
D |
|
E |
|
F |
|
[A,C,B]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: C
}
Previous:
Visited:
Pick The Smallest...D
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
D |
|
E |
|
F |
|
[A,C,B]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: D
}
Previous:
Visited:
Pick The Smallest...D
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
D |
|
E |
|
F |
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: D
}
Previous:
Visited:
Pick The Smallest...D
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
F |
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: D
}
Previous:
Visited:
Pick The Smallest...F
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
F |
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: D
}
Previous:
Visited:
Pick The Smallest...F
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
F |
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: B,
F: D
}
Previous:
Visited:
Pick The Smallest...F
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
F |
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: F,
F: D
}
Previous:
Visited:
Pick The Smallest...F
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
F |
|
[A,C,B,D,F]
{
A: null,
B: A,
C: A,
D: C,
E: F,
F: D
}
Previous:
Visited:
Pick The Smallest...F
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: F,
F: D
}
Previous:
Visited:
Pick The Smallest...E
WE ARE DONE!!!
A
B
C
D
E
F
2
4
4
1
1
3
3
2
FIND THE SHORTEST PATH
FROM A TO E
Vertex | Shortest Dist From A |
---|---|
0 | |
|
|
|
|
|
|
E |
|
|
[A,C,B,D]
{
A: null,
B: A,
C: A,
D: C,
E: F,
F: D
}
Previous:
Visited:
Pick The Smallest...E
Notice we are sorting which is O(N * log(N))
class PriorityQueue {
constructor(){
this.values = [];
}
enqueue(val, priority) {
this.values.push({val, priority});
this.sort();
};
dequeue() {
return this.values.shift();
};
sort() {
this.values.sort((a, b) => a.priority - b.priority);
};
}
We sure can! Using a min binary heap
Right now, let's focus on Dijkstra's
Dijkstra's algorithm is greedy! That can cause problems!
We can improve this algorithm by adding a heuristics (a best guess)