Fireship · @fireship

Graph Search Algorithms in 100 Seconds - And Beyond with JS

Uploaded April 25, 2020 · Length 10:30

Graph Search Algorithms in 100 Seconds - And Beyond with JS

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.

  1. 0:00

    a graph is a nonlinear data structure

  2. 0:02

    that contains nodes and edges a node or

  3. 0:04

    vertex is just a single unique value

  4. 0:06

    while an edge represents a connection or

  5. 0:08

    relationship between two of these nodes

  6. 0:10

    think of something like Instagram every

  7. 0:12

    user is a node every time you follow a

  8. 0:14

    user you create a new edge connecting

  9. 0:16

    two nodes together this is known as a

  10. 0:18

    directed graph because the relationship

  11. 0:20

    flows one way the follower follows the

  12. 0:22

    following and not vice-versa

  13. 0:24

    now consider facebook your edges in the

  14. 0:26

    graph are friendships this relationship

  15. 0:27

    goes both ways which is known as an

  16. 0:29

    undirected graph graphs can also be

  17. 0:31

    weighted which means the node has some

  18. 0:33

    additional data about the relationship

  19. 0:35

    like the distance between two airports a

  20. 0:37

    node might also point to itself like an

  21. 0:39

    airplane that takes off and comes back

  22. 0:41

    to land at the same airport this is

  23. 0:42

    known as a cycle graphs are used in many

  24. 0:45

    real-life software products and you'll

  25. 0:46

    often be asked to represent a graphic

  26. 0:48

    code in a technical interview one way to

  27. 0:50

    represent a graph is with a 2d array or

  28. 0:52

    adjacency matrix create one row and one

  29. 0:54

    column for every node when two nodes

  30. 0:56

    have an edge or connection add a one at

  31. 0:59

    the point they intersect this makes it

  32. 1:00

    fast and easy to look up a specific age

  33. 1:02

    or to add a new edge but as a 2d array

  34. 1:05

    it takes quadratic space complexity and

  35. 1:07

    quadratic time to insert a new node into

  36. 1:09

    the graph an alternative approach is an

  37. 1:11

    adjacency list in this representation we

  38. 1:13

    start with a collection of nodes then

  39. 1:15

    each item has its own array of its

  40. 1:17

    neighbors this makes it faster to

  41. 1:18

    iterate over a nodes edges and is more

  42. 1:21

    efficient with memory especially when

  43. 1:22

    you have many nodes and few edges in

  44. 1:24

    your interview you'll likely need to

  45. 1:26

    traverse your graph one option is a

  46. 1:28

    depth-first search start with any random

  47. 1:30

    node go to its first child then its

  48. 1:32

    first child and do that for as long as

  49. 1:33

    you can when there are no more children

  50. 1:35

    backtrack to the last node and continue

  51. 1:37

    the process this algorithm is usually

  52. 1:39

    implemented with a recursive function

  53. 1:41

    another option is a breadth-first search

  54. 1:43

    from your starting node add all of the

  55. 1:45

    direct children to a queue once they've

  56. 1:47

    been visited move on to the

  57. 1:48

    grandchildren and continue following

  58. 1:49

    this pattern and layers this has been

  59. 1:51

    graphs in 100 seconds but hang out for a

  60. 1:54

    few minutes because now we're going to

  61. 1:55

    implement these algorithms in JavaScript

  62. 1:57

    if you're new here like and subscribe

  63. 1:58

    hit the like button open your IDE and