CSE 373: Graph traversal Michael Lee Friday, Feb 16, 2018 1 Goal: - - PowerPoint PPT Presentation

cse 373 graph traversal
SMART_READER_LITE
LIVE PREVIEW

CSE 373: Graph traversal Michael Lee Friday, Feb 16, 2018 1 Goal: - - PowerPoint PPT Presentation

CSE 373: Graph traversal Michael Lee Friday, Feb 16, 2018 1 Goal: How do we traverse graphs? Todays goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs


slide-1
SLIDE 1

CSE 373: Graph traversal

Michael Lee Friday, Feb 16, 2018

1

slide-2
SLIDE 2

Goal: How do we traverse graphs?

Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... Traverse a graph to fjnd if there’s a connection from one node to another? Determine if we can start from our node and touch every

  • ther node?

Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-fjrst search and depth-fjrst search

2

slide-3
SLIDE 3

Goal: How do we traverse graphs?

Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... ◮ Traverse a graph to fjnd if there’s a connection from one node to another? Determine if we can start from our node and touch every

  • ther node?

Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-fjrst search and depth-fjrst search

2

slide-4
SLIDE 4

Goal: How do we traverse graphs?

Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... ◮ Traverse a graph to fjnd if there’s a connection from one node to another? ◮ Determine if we can start from our node and touch every

  • ther node?

Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-fjrst search and depth-fjrst search

2

slide-5
SLIDE 5

Goal: How do we traverse graphs?

Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... ◮ Traverse a graph to fjnd if there’s a connection from one node to another? ◮ Determine if we can start from our node and touch every

  • ther node?

◮ Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-fjrst search and depth-fjrst search

2

slide-6
SLIDE 6

Goal: How do we traverse graphs?

Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... ◮ Traverse a graph to fjnd if there’s a connection from one node to another? ◮ Determine if we can start from our node and touch every

  • ther node?

◮ Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-fjrst search and depth-fjrst search

2

slide-7
SLIDE 7

Goal: How do we traverse graphs?

Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... ◮ Traverse a graph to fjnd if there’s a connection from one node to another? ◮ Determine if we can start from our node and touch every

  • ther node?

◮ Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-fjrst search and depth-fjrst search

2

slide-8
SLIDE 8

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: Queue: a, Visited: a,

3

slide-9
SLIDE 9

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: a Queue: Visited: a,

3

slide-10
SLIDE 10

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: a Queue: b, d, Visited: a, b, d,

3

slide-11
SLIDE 11

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: b Queue: d, Visited: a, b, d,

3

slide-12
SLIDE 12

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: b Queue: d, c, e, Visited: a, b, d, c, e,

3

slide-13
SLIDE 13

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: d Queue: c, e, Visited: a, b, d, c, e,

3

slide-14
SLIDE 14

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: d Queue: c, e, f, g, Visited: a, b, d, c, e, f, g,

3

slide-15
SLIDE 15

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: c Queue: e, f, g, Visited: a, b, d, c, e, f, g,

3

slide-16
SLIDE 16

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: e Queue: f, g, Visited: a, b, d, c, e, f, g,

3

slide-17
SLIDE 17

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: e Queue: f, g, h, Visited: a, b, d, c, e, f, g, h,

3

slide-18
SLIDE 18

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: f Queue: g, h, Visited: a, b, d, c, e, f, g, h,

3

slide-19
SLIDE 19

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: g Queue: h, Visited: a, b, d, c, e, f, g, h,

3

slide-20
SLIDE 20

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: g Queue: h, i, Visited: a, b, d, c, e, f, g, h, i,

3

slide-21
SLIDE 21

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: h Queue: i, Visited: a, b, d, c, e, f, g, h, i,

3

slide-22
SLIDE 22

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

a b d c e f g h i j

Current node: i Queue: Visited: a, b, d, c, e, f, g, h, i,

3

slide-23
SLIDE 23

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

We visit each node once. For each node, check each edge to see if we should add to queue So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-24
SLIDE 24

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

We visit each node once. For each node, check each edge to see if we should add to queue So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-25
SLIDE 25

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

We visit each node once. For each node, check each edge to see if we should add to queue So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-26
SLIDE 26

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

We visit each node once. For each node, check each edge to see if we should add to queue So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-27
SLIDE 27

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

◮ We visit each node once. For each node, check each edge to see if we should add to queue So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-28
SLIDE 28

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

◮ We visit each node once. ◮ For each node, check each edge to see if we should add to queue So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-29
SLIDE 29

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

◮ We visit each node once. ◮ For each node, check each edge to see if we should add to queue ◮ So we check each edge at most twice

So, V E , which simplifjes to V E .

4

slide-30
SLIDE 30

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Use something (e.g. a queue) to keep track of every vertex to

visit

  • 2. Add and remove nodes from queue until it’s empty
  • 3. Use a set to store nodes we don’t want to recheck/revisit
  • 4. Runtime:

◮ We visit each node once. ◮ For each node, check each edge to see if we should add to queue ◮ So we check each edge at most twice

So, O (|V | + 2|E|), which simplifjes to O (|V | + |E|).

4

slide-31
SLIDE 31

Breadth-fjrst search (BFS)

Pseudocode:

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr) 5

slide-32
SLIDE 32

An interesting property...

Note: We visited the nodes in “rings” – maintained a gradually growing “frontier” of nodes.

a b d c e f g h i j

6

slide-33
SLIDE 33

An interesting property...

Note: We visited the nodes in “rings” – maintained a gradually growing “frontier” of nodes.

a b d c e f g h i j

6

slide-34
SLIDE 34

An interesting property...

Note: We visited the nodes in “rings” – maintained a gradually growing “frontier” of nodes.

a b d c e f g h i j

6

slide-35
SLIDE 35

An interesting property...

Note: We visited the nodes in “rings” – maintained a gradually growing “frontier” of nodes.

a b d c e f g h i j

6

slide-36
SLIDE 36

An interesting property...

Note: We visited the nodes in “rings” – maintained a gradually growing “frontier” of nodes.

a b d c e f g h i j

6

slide-37
SLIDE 37

An interesting property...

Note: We visited the nodes in “rings” – maintained a gradually growing “frontier” of nodes.

a b d c e f g h i j

6

slide-38
SLIDE 38

An interesting property...

What does this look like for trees? The algorithm traverses the width, or “breadth” of the tree

7

slide-39
SLIDE 39

An interesting property...

What does this look like for trees? The algorithm traverses the width, or “breadth” of the tree

7

slide-40
SLIDE 40

An interesting property...

What does this look like for trees? The algorithm traverses the width, or “breadth” of the tree

7

slide-41
SLIDE 41

An interesting property...

What does this look like for trees? The algorithm traverses the width, or “breadth” of the tree

7

slide-42
SLIDE 42

An interesting property...

What does this look like for trees? The algorithm traverses the width, or “breadth” of the tree

7

slide-43
SLIDE 43

Depth-fjrst search (DFS)

Question: Why a queue? Can we use other data structures? Answer: Yes! Any kind of list-like thing that supports appends and removes works! For example, what if we try using a stack? The BFS algorithm:

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

The DFS algorithm:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(v) 8

slide-44
SLIDE 44

Depth-fjrst search (DFS)

Question: Why a queue? Can we use other data structures? Answer: Yes! Any kind of list-like thing that supports appends and removes works! For example, what if we try using a stack? The BFS algorithm:

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

The DFS algorithm:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(v) 8

slide-45
SLIDE 45

Depth-fjrst search (DFS)

Question: Why a queue? Can we use other data structures? Answer: Yes! Any kind of list-like thing that supports appends and removes works! For example, what if we try using a stack? The BFS algorithm:

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

The DFS algorithm:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(v) 8

slide-46
SLIDE 46

Depth-fjrst search (DFS)

Question: Why a queue? Can we use other data structures? Answer: Yes! Any kind of list-like thing that supports appends and removes works! For example, what if we try using a stack? The BFS algorithm:

search(v): visited = empty set queue.enqueue(v) visited.add(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): if (w not in visited): queue.enqueue(w) visited.add(curr)

The DFS algorithm:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(v) 8

slide-47
SLIDE 47

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: Stack: a, Visited: a,

9

slide-48
SLIDE 48

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: a Stack: Visited: a,

9

slide-49
SLIDE 49

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: a Stack: b, d, Visited: a, b, d,

9

slide-50
SLIDE 50

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: d Stack: b, Visited: a, b, d,

9

slide-51
SLIDE 51

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: d Stack: b, e, f, g, Visited: a, b, d, e, f, g,

9

slide-52
SLIDE 52

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: g Stack: b, e, f, Visited: a, b, d, e, f, g,

9

slide-53
SLIDE 53

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: g Stack: b, e, f, h, i, Visited: a, b, d, e, f, g, h, i,

9

slide-54
SLIDE 54

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: i Stack: b, e, f, h, Visited: a, b, d, e, f, g, h, i,

9

slide-55
SLIDE 55

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: h Stack: b, e, f, Visited: a, b, d, e, f, g, h, i,

9

slide-56
SLIDE 56

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: f Stack: b, e, Visited: a, b, d, e, f, g, h, i, e,

9

slide-57
SLIDE 57

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: e Stack: b, e, Visited: a, b, d, e, f, g, h, i, e,

9

slide-58
SLIDE 58

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: e Stack: b, e, c, Visited: a, b, d, e, f, g, h, i, e, c,

9

slide-59
SLIDE 59

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: c Stack: b, Visited: a, b, d, e, f, g, h, i, e, c,

9

slide-60
SLIDE 60

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: b Stack: Visited: a, b, d, e, f, g, h, i, e, c,

9

slide-61
SLIDE 61

Depth-fjrst search (DFS) example

search(v): visited = empty set stack.push(v) while (stack is not empty): curr = stack.pop() visited.add(curr) for (w : v.neighbors()): if (w not in visited): stack.push(w)

a b d e f g h i c j

Current node: Stack: Visited: a, b, d, e, f, g, h, i, e, c,

9

slide-62
SLIDE 62

Depth-fjrst search (DFS)

Depth-fjrst traversal, core idea:

  • 1. Instead of using a queue, use a stack. Otherwise, keep

everything the same.

  • 2. Runtime: also

V E for same reasons as BFS Pseudocode:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(curr) 10

slide-63
SLIDE 63

Depth-fjrst search (DFS)

Depth-fjrst traversal, core idea:

  • 1. Instead of using a queue, use a stack. Otherwise, keep

everything the same.

  • 2. Runtime: also O (|V | + |E|) for same reasons as BFS

Pseudocode:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(curr) 10

slide-64
SLIDE 64

Depth-fjrst search (DFS)

Depth-fjrst traversal, core idea:

  • 1. Instead of using a queue, use a stack. Otherwise, keep

everything the same.

  • 2. Runtime: also O (|V | + |E|) for same reasons as BFS

Pseudocode:

search(v): visited = empty set stack.push(v) visited.add(v) while (stack is not empty): curr = stack.pop() for (w : v.neighbors()): if (w not in visited): stack.push(w) visited.add(curr) 10

slide-65
SLIDE 65

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-66
SLIDE 66

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-67
SLIDE 67

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-68
SLIDE 68

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-69
SLIDE 69

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-70
SLIDE 70

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-71
SLIDE 71

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-72
SLIDE 72

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-73
SLIDE 73

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-74
SLIDE 74

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-75
SLIDE 75

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-76
SLIDE 76

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-77
SLIDE 77

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-78
SLIDE 78

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-79
SLIDE 79

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-80
SLIDE 80

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-81
SLIDE 81

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-82
SLIDE 82

An interesting property...

Note: Rather the growing the node in “rings”, we randomly wandered through the graph until we got stuck, then “backtracked”.

a b d c e f g h i j

11

slide-83
SLIDE 83

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-84
SLIDE 84

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-85
SLIDE 85

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-86
SLIDE 86

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-87
SLIDE 87

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-88
SLIDE 88

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-89
SLIDE 89

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-90
SLIDE 90

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-91
SLIDE 91

An interesting property...

What does this look like for trees? The algorithm traverses to the bottom fjrst: it prioritizes the “depth” of the tree

Note: rest of algorithm omitted

12

slide-92
SLIDE 92

Compare and contrast

Question: When do we use BFS vs DFS? Related question: How much memory does BFS and DFS use in the worst case? BFS: V – what if every node is connected to the start? DFS: V – what if the nodes are arranged like a linked list? So, in the worst case, BFS and DFS both have the same worst-case runtime and memory usage. They only difger in what order they visit the nodes.

13

slide-93
SLIDE 93

Compare and contrast

Question: When do we use BFS vs DFS? Related question: How much memory does BFS and DFS use in the worst case? ◮ BFS: V – what if every node is connected to the start? ◮ DFS: V – what if the nodes are arranged like a linked list? So, in the worst case, BFS and DFS both have the same worst-case runtime and memory usage. They only difger in what order they visit the nodes.

13

slide-94
SLIDE 94

Compare and contrast

Question: When do we use BFS vs DFS? Related question: How much memory does BFS and DFS use in the worst case? ◮ BFS: O (|V |) – what if every node is connected to the start? ◮ DFS: O (|V |) – what if the nodes are arranged like a linked list? So, in the worst case, BFS and DFS both have the same worst-case runtime and memory usage. They only difger in what order they visit the nodes.

13

slide-95
SLIDE 95

Compare and contrast

How much memory does BFS and DFS use in the average case? Related question: how much memory do they use when we want to traverse a tree? BFS: “width” of tree num leaves DFS: height For graphs: Use BFS if graph is “narrow”, or if solution is “near” start Use DFS if graph is “wide” In practice, graphs are often large/very wide, so DFS is often a good default choice. (It’s also possible to implement DFS recursively!)

14

slide-96
SLIDE 96

Compare and contrast

How much memory does BFS and DFS use in the average case? Related question: how much memory do they use when we want to traverse a tree? BFS: “width” of tree num leaves DFS: height For graphs: Use BFS if graph is “narrow”, or if solution is “near” start Use DFS if graph is “wide” In practice, graphs are often large/very wide, so DFS is often a good default choice. (It’s also possible to implement DFS recursively!)

14

slide-97
SLIDE 97

Compare and contrast

How much memory does BFS and DFS use in the average case? Related question: how much memory do they use when we want to traverse a tree? ◮ BFS: “width” of tree num leaves ◮ DFS: height For graphs: Use BFS if graph is “narrow”, or if solution is “near” start Use DFS if graph is “wide” In practice, graphs are often large/very wide, so DFS is often a good default choice. (It’s also possible to implement DFS recursively!)

14

slide-98
SLIDE 98

Compare and contrast

How much memory does BFS and DFS use in the average case? Related question: how much memory do they use when we want to traverse a tree? ◮ BFS: O (“width” of tree) = O (num leaves) ◮ DFS: O (height) For graphs: Use BFS if graph is “narrow”, or if solution is “near” start Use DFS if graph is “wide” In practice, graphs are often large/very wide, so DFS is often a good default choice. (It’s also possible to implement DFS recursively!)

14

slide-99
SLIDE 99

Compare and contrast

How much memory does BFS and DFS use in the average case? Related question: how much memory do they use when we want to traverse a tree? ◮ BFS: O (“width” of tree) = O (num leaves) ◮ DFS: O (height) For graphs: ◮ Use BFS if graph is “narrow”, or if solution is “near” start ◮ Use DFS if graph is “wide” In practice, graphs are often large/very wide, so DFS is often a good default choice. (It’s also possible to implement DFS recursively!)

14

slide-100
SLIDE 100

Compare and contrast

How much memory does BFS and DFS use in the average case? Related question: how much memory do they use when we want to traverse a tree? ◮ BFS: O (“width” of tree) = O (num leaves) ◮ DFS: O (height) For graphs: ◮ Use BFS if graph is “narrow”, or if solution is “near” start ◮ Use DFS if graph is “wide” In practice, graphs are often large/very wide, so DFS is often a good default choice. (It’s also possible to implement DFS recursively!)

14

slide-101
SLIDE 101

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-102
SLIDE 102

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-103
SLIDE 103

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-104
SLIDE 104

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-105
SLIDE 105

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-106
SLIDE 106

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-107
SLIDE 107

Design challenge

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Observation: Since BFS moves out in rings, we will reach the end node via the path of length 3 fjrst. Idea: when we enqueue, store where we came from in some way. (e.g. mark node, use a dictionary...) After BFS is done, backtrack.

15

slide-108
SLIDE 108

Design challenge: pathfjnding

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Now, start from any node, follow arrows, then reverse to get path.

16

slide-109
SLIDE 109

Design challenge: pathfjnding

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Now, start from any node, follow arrows, then reverse to get path.

16

slide-110
SLIDE 110

Design challenge: pathfjnding

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Now, start from any node, follow arrows, then reverse to get path.

16

slide-111
SLIDE 111

Design challenge: pathfjnding

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Now, start from any node, follow arrows, then reverse to get path.

16

slide-112
SLIDE 112

Design challenge: pathfjnding

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Now, start from any node, follow arrows, then reverse to get path.

16

slide-113
SLIDE 113

Design challenge: pathfjnding

Question: How would you modify BFS to fjnd the shortest path between every node?

S E a w b x y z

Now, start from any node, follow arrows, then reverse to get path.

16

slide-114
SLIDE 114

Design challenge: pathfjnding

Question: What if the edges have weights? 100 100 100 2 2 2 2 2

S E a b w x y z

Weighted graph A weighted graph is a kind of graph where each edge has a numerical “weight” associated with it. This number can represent anything, but is often (but not always!) used to indicate the “cost” of traveling down that edge.

17

slide-115
SLIDE 115

Design challenge: pathfjnding

Question: What if the edges have weights? 100 100 100 2 2 2 2 2

S E a b w x y z

Weighted graph A weighted graph is a kind of graph where each edge has a numerical “weight” associated with it. This number can represent anything, but is often (but not always!) used to indicate the “cost” of traveling down that edge.

17

slide-116
SLIDE 116

Design challenge: pathfjnding

Question: What if the edges have weights? 100 100 100 2 2 2 2 2

S E a b w x y z

Weighted graph A weighted graph is a kind of graph where each edge has a numerical “weight” associated with it. This number can represent anything, but is often (but not always!) used to indicate the “cost” of traveling down that edge.

17

slide-117
SLIDE 117

Pathfjnding and DFS

We can use BFS to correctly fjnd the shortest path between two nodes in an unweighted graph... ...but it fails if the graph is weighted! We need a better algorithm. Today: Dijkstra’s algorithm

18

slide-118
SLIDE 118

Pathfjnding and DFS

We can use BFS to correctly fjnd the shortest path between two nodes in an unweighted graph... ...but it fails if the graph is weighted! We need a better algorithm. Today: Dijkstra’s algorithm

18

slide-119
SLIDE 119

Pathfjnding and DFS

We can use BFS to correctly fjnd the shortest path between two nodes in an unweighted graph... ...but it fails if the graph is weighted! We need a better algorithm. Today: Dijkstra’s algorithm

18

slide-120
SLIDE 120

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-121
SLIDE 121

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-122
SLIDE 122

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-123
SLIDE 123

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-124
SLIDE 124

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-125
SLIDE 125

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-126
SLIDE 126

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-127
SLIDE 127

Dijkstra’s algorithm

Core idea:

  • 1. Assign each node an initial cost of ∞
  • 2. Set our starting node’s cost to 0
  • 3. Update all adjacent vertices costs to the minimum known cost
  • 4. Mark the current node as being “done”
  • 5. Pick the next unvisited node with the minimum cost. Go to

step 3. Metaphor: Treat edges as canals and edge weights as distance. Imagine opening a dam at the starting node. How long does it take for the water to reach each vertex? Caveat: Dijkstra’s algorithm only guaranteed to work for graphs with no negative edge weights. Pronunciation: DYKE-struh (“dijk” rhymes with “bike”)

19

slide-128
SLIDE 128

Dijkstra’s algorithm

Suppose we start at vertex “a”: a b f h d c e g 2 1 4 5 10 2 9 11 2 7 1 3 2 3 1

20

slide-129
SLIDE 129

Dijkstra’s algorithm

Some implementation details... ◮ How do we keep track of the node costs?

Could use a dictionary Could manually mark each node

How do we fjnd the node with the smallest cost?

Could maintain a sorted list Could use a heap!

If we’re using a heap, how do we update node costs?

Could add a changeKeyPriority(...) method to heap Alternatively, add the node and the cost to the heap again (and ignore duplicates)

21

slide-130
SLIDE 130

Dijkstra’s algorithm

Some implementation details... ◮ How do we keep track of the node costs?

◮ Could use a dictionary ◮ Could manually mark each node

How do we fjnd the node with the smallest cost?

Could maintain a sorted list Could use a heap!

If we’re using a heap, how do we update node costs?

Could add a changeKeyPriority(...) method to heap Alternatively, add the node and the cost to the heap again (and ignore duplicates)

21

slide-131
SLIDE 131

Dijkstra’s algorithm

Some implementation details... ◮ How do we keep track of the node costs?

◮ Could use a dictionary ◮ Could manually mark each node

◮ How do we fjnd the node with the smallest cost?

Could maintain a sorted list Could use a heap!

If we’re using a heap, how do we update node costs?

Could add a changeKeyPriority(...) method to heap Alternatively, add the node and the cost to the heap again (and ignore duplicates)

21

slide-132
SLIDE 132

Dijkstra’s algorithm

Some implementation details... ◮ How do we keep track of the node costs?

◮ Could use a dictionary ◮ Could manually mark each node

◮ How do we fjnd the node with the smallest cost?

◮ Could maintain a sorted list ◮ Could use a heap!

If we’re using a heap, how do we update node costs?

Could add a changeKeyPriority(...) method to heap Alternatively, add the node and the cost to the heap again (and ignore duplicates)

21

slide-133
SLIDE 133

Dijkstra’s algorithm

Some implementation details... ◮ How do we keep track of the node costs?

◮ Could use a dictionary ◮ Could manually mark each node

◮ How do we fjnd the node with the smallest cost?

◮ Could maintain a sorted list ◮ Could use a heap!

◮ If we’re using a heap, how do we update node costs?

Could add a changeKeyPriority(...) method to heap Alternatively, add the node and the cost to the heap again (and ignore duplicates)

21

slide-134
SLIDE 134

Dijkstra’s algorithm

Some implementation details... ◮ How do we keep track of the node costs?

◮ Could use a dictionary ◮ Could manually mark each node

◮ How do we fjnd the node with the smallest cost?

◮ Could maintain a sorted list ◮ Could use a heap!

◮ If we’re using a heap, how do we update node costs?

◮ Could add a changeKeyPriority(...) method to heap ◮ Alternatively, add the node and the cost to the heap again (and ignore duplicates)

21