Fireship · @fireship
Graph Search Algorithms in 100 Seconds - And Beyond with JS
Uploaded April 25, 2020 · Length 10:30
Summary
Learn how to implement graph search algorithms in JavaScript, including depth-first search and breadth-first search, to find routes between nodes in a graph.
- graph search
- javascript
- depth-first search
- breadth-first search
- algorithm implementation
Transcript preview
The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.
- 0:00
a graph is a nonlinear data structure
- 0:02
that contains nodes and edges a node or
- 0:04
vertex is just a single unique value
- 0:06
while an edge represents a connection or
- 0:08
relationship between two of these nodes
- 0:10
think of something like Instagram every
- 0:12
user is a node every time you follow a
- 0:14
user you create a new edge connecting
- 0:16
two nodes together this is known as a
- 0:18
directed graph because the relationship
- 0:20
flows one way the follower follows the
- 0:22
following and not vice-versa
- 0:24
now consider facebook your edges in the
- 0:26
graph are friendships this relationship
- 0:27
goes both ways which is known as an
- 0:29
undirected graph graphs can also be
- 0:31
weighted which means the node has some
- 0:33
additional data about the relationship
- 0:35
like the distance between two airports a
- 0:37
node might also point to itself like an
- 0:39
airplane that takes off and comes back
- 0:41
to land at the same airport this is
- 0:42
known as a cycle graphs are used in many
- 0:45
real-life software products and you'll
- 0:46
often be asked to represent a graphic
- 0:48
code in a technical interview one way to
- 0:50
represent a graph is with a 2d array or
- 0:52
adjacency matrix create one row and one
- 0:54
column for every node when two nodes
- 0:56
have an edge or connection add a one at
- 0:59
the point they intersect this makes it
- 1:00
fast and easy to look up a specific age
- 1:02
or to add a new edge but as a 2d array
- 1:05
it takes quadratic space complexity and
- 1:07
quadratic time to insert a new node into
- 1:09
the graph an alternative approach is an
- 1:11
adjacency list in this representation we
- 1:13
start with a collection of nodes then
- 1:15
each item has its own array of its
- 1:17
neighbors this makes it faster to
- 1:18
iterate over a nodes edges and is more
- 1:21
efficient with memory especially when
- 1:22
you have many nodes and few edges in
- 1:24
your interview you'll likely need to
- 1:26
traverse your graph one option is a
- 1:28
depth-first search start with any random
- 1:30
node go to its first child then its
- 1:32
first child and do that for as long as
- 1:33
you can when there are no more children
- 1:35
backtrack to the last node and continue
- 1:37
the process this algorithm is usually
- 1:39
implemented with a recursive function
- 1:41
another option is a breadth-first search
- 1:43
from your starting node add all of the
- 1:45
direct children to a queue once they've
- 1:47
been visited move on to the
- 1:48
grandchildren and continue following
- 1:49
this pattern and layers this has been
- 1:51
graphs in 100 seconds but hang out for a
- 1:54
few minutes because now we're going to
- 1:55
implement these algorithms in JavaScript
- 1:57
if you're new here like and subscribe
- 1:58
hit the like button open your IDE and


