CSE 373: More on graphs; DFS and BFS Michael Lee Wednesday, Feb 14, - - PowerPoint PPT Presentation

cse 373 more on graphs dfs and bfs
SMART_READER_LITE
LIVE PREVIEW

CSE 373: More on graphs; DFS and BFS Michael Lee Wednesday, Feb 14, - - PowerPoint PPT Presentation

CSE 373: More on graphs; DFS and BFS Michael Lee Wednesday, Feb 14, 2018 1 Warmup Warmup: Discuss with your neighbor: A simple graph is a graph that has no self-loops and no parallel edges. is the maximum number of edges it can have, in


slide-1
SLIDE 1

CSE 373: More on graphs; DFS and BFS

Michael Lee Wednesday, Feb 14, 2018

1

slide-2
SLIDE 2

Warmup

Warmup:

Discuss with your neighbor: ◮ Remind your neighbor: what is a simple graph? A simple graph is a graph that has no self-loops and no parallel edges. ◮ Suppose we have a simple, directed graph with x nodes. What is the maximum number of edges it can have, in terms of x? Each vertex can connect to x

  • ther vertices, so x x

. ◮ Now, suppose we have a difgerent simple, undirected graph with y edges. What is the maximum number of vertices it can have, in terms of y? Infjnite: just keep adding nodes with no edges attached.

2

slide-3
SLIDE 3

Warmup

Warmup:

Discuss with your neighbor: ◮ Remind your neighbor: what is a simple graph? A simple graph is a graph that has no self-loops and no parallel edges. ◮ Suppose we have a simple, directed graph with x nodes. What is the maximum number of edges it can have, in terms of x? Each vertex can connect to x − 1 other vertices, so x(x − 1). ◮ Now, suppose we have a difgerent simple, undirected graph with y edges. What is the maximum number of vertices it can have, in terms of y? Infjnite: just keep adding nodes with no edges attached.

2

slide-4
SLIDE 4

Warmup:

Some follow-up questions: ◮ Suppose we have a simple, undirected graph with x nodes. What is the maximum number of edges it can have? What if the graph is not simple? If the graph is simple, the max number of edges is exactly half

  • f what it would be if the graph were directed. So, x x

. If the graph is not simple, it’s infjnite: assuming x , we can just keep adding more and more self-loops. Note that if x , there can’t be any edges at all. ◮ Now, suppose we have a difgerent simple, undirected graph with y edges. What is the maximum number of vertices it can have? What if the graph is not simple? Either way, it’s still infjnite, for the same reasons given previously.

3

slide-5
SLIDE 5

Warmup:

Some follow-up questions: ◮ Suppose we have a simple, undirected graph with x nodes. What is the maximum number of edges it can have? What if the graph is not simple? If the graph is simple, the max number of edges is exactly half

  • f what it would be if the graph were directed. So, x(x − 1)

2 . If the graph is not simple, it’s infjnite: assuming x > 0, we can just keep adding more and more self-loops. Note that if x = 0, there can’t be any edges at all. ◮ Now, suppose we have a difgerent simple, undirected graph with y edges. What is the maximum number of vertices it can have? What if the graph is not simple? Either way, it’s still infjnite, for the same reasons given previously.

3

slide-6
SLIDE 6

Summary

What did we learn? ◮ In graphs with no restrictions, number of edges and number of vertices are independent. In simple graphs, if we know V is some fjxed value, we also know E V , for both directed and undirected graphs. Dense graph If E V , we say the graph is dense. To put it another way, dense graphs have “lots of edges” Sparse graph If E V , we sau the graph is sparse. To put it another way, sparse graphs have “few” edges.

4

slide-7
SLIDE 7

Summary

What did we learn? ◮ In graphs with no restrictions, number of edges and number of vertices are independent. ◮ In simple graphs, if we know |V | is some fjxed value, we also know |E| ∈ O

  • |V |2

, for both directed and undirected graphs. Dense graph If E V , we say the graph is dense. To put it another way, dense graphs have “lots of edges” Sparse graph If E V , we sau the graph is sparse. To put it another way, sparse graphs have “few” edges.

4

slide-8
SLIDE 8

Summary

What did we learn? ◮ In graphs with no restrictions, number of edges and number of vertices are independent. ◮ In simple graphs, if we know |V | is some fjxed value, we also know |E| ∈ O

  • |V |2

, for both directed and undirected graphs. Dense graph If |E| ∈ Θ

  • |V |2

, we say the graph is dense. To put it another way, dense graphs have “lots of edges” Sparse graph If E V , we sau the graph is sparse. To put it another way, sparse graphs have “few” edges.

4

slide-9
SLIDE 9

Summary

What did we learn? ◮ In graphs with no restrictions, number of edges and number of vertices are independent. ◮ In simple graphs, if we know |V | is some fjxed value, we also know |E| ∈ O

  • |V |2

, for both directed and undirected graphs. Dense graph If |E| ∈ Θ

  • |V |2

, we say the graph is dense. To put it another way, dense graphs have “lots of edges” Sparse graph If |E| ∈ O (|V |), we sau the graph is sparse. To put it another way, sparse graphs have “few” edges.

4

slide-10
SLIDE 10

How do we represent graphs in code?

So, how do we actually represent graphs in code? Two common approaches, with difgerent tradeofgs: Adjacency matrix Adjacency list

5

slide-11
SLIDE 11

How do we represent graphs in code?

So, how do we actually represent graphs in code? Two common approaches, with difgerent tradeofgs: ◮ Adjacency matrix ◮ Adjacency list

5

slide-12
SLIDE 12

Adjacency matrix

Core idea: ◮ Assign each node a number from 0 to |V | − 1 ◮ Create a |V | × |V | nested array of booleans or ints ◮ If (x, y) ∈ E, then nestedArray[x][y] == true

a b c d

a b c d d c b a

6

slide-13
SLIDE 13

Adjacency matrix

Core idea: ◮ Assign each node a number from 0 to |V | − 1 ◮ Create a |V | × |V | nested array of booleans or ints ◮ If (x, y) ∈ E, then nestedArray[x][y] == true

a b c d

a b c d d c b a

6

slide-14
SLIDE 14

Adjacency list

What is the worst-case runtime to: ◮ Get out-edges: V ◮ Get in-edges: V ◮ Decide if an edge exists: ◮ Insert an edge: ◮ Delete an edge: How much space do we use? V Is this better for sparse or dense graphs? Dense ones Can we handle self-loops and parallel edges? Self-loops yes, parallel edges, not easily

7

slide-15
SLIDE 15

Adjacency list

What is the worst-case runtime to: ◮ Get out-edges: O (|V |) ◮ Get in-edges: O (|V |) ◮ Decide if an edge exists: O (1) ◮ Insert an edge: O (1) ◮ Delete an edge: O (1) How much space do we use? O

  • |V |2

Is this better for sparse or dense graphs? Dense ones Can we handle self-loops and parallel edges? Self-loops yes, parallel edges, not easily

7

slide-16
SLIDE 16

Adjacency list

Core idea: ◮ Assign each node a number from 0 to |V | − 1 ◮ Create an array of size |V | ◮ Each element in the array stores its out edges in a list or set On a higher level: represent as IDictionary<Vertex, Edges>.

a b c d

8

slide-17
SLIDE 17

Adjacency list

Core idea: ◮ Assign each node a number from 0 to |V | − 1 ◮ Create an array of size |V | ◮ Each element in the array stores its out edges in a list or set ◮ On a higher level: represent as IDictionary<Vertex, Edges>.

a b c d

8

slide-18
SLIDE 18

Adjacency list

Core idea: ◮ Assign each node a number from 0 to |V | − 1 ◮ Create an array of size |V | ◮ Each element in the array stores its out edges in a list or set ◮ On a higher level: represent as IDictionary<Vertex, Edges>.

a b c d

8

slide-19
SLIDE 19

Adjacency list

We can store edges using either sets or lists. Answer these questions for both. What is the worst-case runtime to: ◮ Get out-edges: ◮ Get in-edges: ◮ Decide if an edge exists: ◮ Insert an edge: ◮ Delete an edge: How much space do we use? Is this better for sparse or dense graphs? Can we handle self-loops and parallel edges?

9

slide-20
SLIDE 20

Which do we pick?

So which do we pick? Observations: Most graphs are sparse If we implement adjacency lists using sets, we can get comparable worst-case performance So by default, pick adjacency lists.

10

slide-21
SLIDE 21

Which do we pick?

So which do we pick? Observations: ◮ Most graphs are sparse ◮ If we implement adjacency lists using sets, we can get comparable worst-case performance So by default, pick adjacency lists.

10

slide-22
SLIDE 22

Which do we pick?

So which do we pick? Observations: ◮ Most graphs are sparse ◮ If we implement adjacency lists using sets, we can get comparable worst-case performance So by default, pick adjacency lists.

10

slide-23
SLIDE 23

Walks and paths

Walk A walk is a list of vertices v0, v1, v2, . . . , vn where if i is some int where 0 ≤ i < vn, every pair (vi, vi+1) ∈ E is true. More intuitively, a walk is one continous line following the edges. Path A path is a walk that never visits the same vertex twice. Walk

a b c d e

Path

a b c d e

11

slide-24
SLIDE 24

Walks and paths

Walk A walk is a list of vertices v0, v1, v2, . . . , vn where if i is some int where 0 ≤ i < vn, every pair (vi, vi+1) ∈ E is true. More intuitively, a walk is one continous line following the edges. Path A path is a walk that never visits the same vertex twice. Walk

a b c d e

Path

a b c d e

11

slide-25
SLIDE 25

Walks and paths

Walk A walk is a list of vertices v0, v1, v2, . . . , vn where if i is some int where 0 ≤ i < vn, every pair (vi, vi+1) ∈ E is true. More intuitively, a walk is one continous line following the edges. Path A path is a walk that never visits the same vertex twice. Path or walk? Walk

a b c d e

Path or walk? Path

a b c d e

11

slide-26
SLIDE 26

Walks and paths

Walk A walk is a list of vertices v0, v1, v2, . . . , vn where if i is some int where 0 ≤ i < vn, every pair (vi, vi+1) ∈ E is true. More intuitively, a walk is one continous line following the edges. Path A path is a walk that never visits the same vertex twice. Walk

a b c d e

Path

a b c d e

11

slide-27
SLIDE 27

Connected components

Connected graph A graph is connected if every vertex is connected to every other vertex via some path. E.g.: if we pick up the graph and shake it, nothing fmies ofg. Connected

a b c d

Not connected

a b c d e

Connected component A connected component of a graph is any subgraph (part of a graph) where all vertices are connected to each other. Note: A connected graph has only one connected component.

12

slide-28
SLIDE 28

Connected components

Connected graph A graph is connected if every vertex is connected to every other vertex via some path. E.g.: if we pick up the graph and shake it, nothing fmies ofg. Connected or not connected? Connected

a b c d

Connected or not connected? Not connected

a b c d e

Connected component A connected component of a graph is any subgraph (part of a graph) where all vertices are connected to each other. Note: A connected graph has only one connected component.

12

slide-29
SLIDE 29

Connected components

Connected graph A graph is connected if every vertex is connected to every other vertex via some path. E.g.: if we pick up the graph and shake it, nothing fmies ofg. Connected

a b c d

Not connected

a b c d e

Connected component A connected component of a graph is any subgraph (part of a graph) where all vertices are connected to each other. Note: A connected graph has only one connected component.

12

slide-30
SLIDE 30

Trees vs graphs

Is this a graph or tree? a b c d e f g Both! Is this the same thing?

b a e c d f g

Yes! (If ‘a’ is the root...) Tree A tree is a connected and acyclic graph. Rooted tree A rooted tree is a tree where we call one special node the “root”.

13

slide-31
SLIDE 31

Trees vs graphs

Is this a graph or tree? a b c d e f g Both! Is this the same thing?

b a e c d f g

Yes! (If ‘a’ is the root...) Tree A tree is a connected and acyclic graph. Rooted tree A rooted tree is a tree where we call one special node the “root”.

13

slide-32
SLIDE 32

Trees vs graphs

Is this a graph or tree? a b c d e f g Both! Is this the same thing?

b a e c d f g

Yes! (If ‘a’ is the root...) Tree A tree is a connected and acyclic graph. Rooted tree A rooted tree is a tree where we call one special node the “root”.

13

slide-33
SLIDE 33

Trees vs graphs

Is this a graph or tree? a b c d e f g Both! Is this the same thing?

b a e c d f g

Yes! (If ‘a’ is the root...) Tree A tree is a connected and acyclic graph. Rooted tree A rooted tree is a tree where we call one special node the “root”.

13

slide-34
SLIDE 34

Trees vs graphs

Is this a graph or tree? a b c d e f g Both! Is this the same thing?

b a e c d f g

Yes! (If ‘a’ is the root...) Tree A tree is a connected and acyclic graph. Rooted tree A rooted tree is a tree where we call one special node the “root”.

13

slide-35
SLIDE 35

Trees vs graphs

Is this a graph or tree? a b c d e f g Both! Is this the same thing?

b a e c d f g

Yes! (If ‘a’ is the root...) Tree A tree is a connected and acyclic graph. Rooted tree A rooted tree is a tree where we call one special node the “root”.

13

slide-36
SLIDE 36

Detecting if a graph is connected

Question: How can we tell if a graph is connected or not? Idea: Let’s just fjnd out! Pick a node and see if there’s a path to every other node!

14

slide-37
SLIDE 37

Detecting if a graph is connected

Question: How can we tell if a graph is connected or not? Idea: Let’s just fjnd out! Pick a node and see if there’s a path to every other node!

14

slide-38
SLIDE 38

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Pick some node and “mark” it (or save it in a set, etc...)
  • 2. Examine each neighbor and visit each one (note: save the ones

we haven’t visited yet in some data structure, like a queue?)

  • 3. Dequeue some node from the data structure. Go to step 1.
  • 4. Keep going until the data structure is empty.

Pseudocode, version 1:

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

slide-39
SLIDE 39

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Pick some node and “mark” it (or save it in a set, etc...)
  • 2. Examine each neighbor and visit each one (note: save the ones

we haven’t visited yet in some data structure, like a queue?)

  • 3. Dequeue some node from the data structure. Go to step 1.
  • 4. Keep going until the data structure is empty.

Pseudocode, version 1:

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

slide-40
SLIDE 40

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Pick some node and “mark” it (or save it in a set, etc...)
  • 2. Examine each neighbor and visit each one (note: save the ones

we haven’t visited yet in some data structure, like a queue?)

  • 3. Dequeue some node from the data structure. Go to step 1.
  • 4. Keep going until the data structure is empty.

Pseudocode, version 1:

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

slide-41
SLIDE 41

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Pick some node and “mark” it (or save it in a set, etc...)
  • 2. Examine each neighbor and visit each one (note: save the ones

we haven’t visited yet in some data structure, like a queue?)

  • 3. Dequeue some node from the data structure. Go to step 1.
  • 4. Keep going until the data structure is empty.

Pseudocode, version 1:

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

slide-42
SLIDE 42

Breadth-fjrst search (BFS)

Breadth-fjrst traversal, core idea:

  • 1. Pick some node and “mark” it (or save it in a set, etc...)
  • 2. Examine each neighbor and visit each one (note: save the ones

we haven’t visited yet in some data structure, like a queue?)

  • 3. Dequeue some node from the data structure. Go to step 1.
  • 4. Keep going until the data structure is empty.

Pseudocode, version 1:

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

slide-43
SLIDE 43

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: Queue: a, What went wrong?

16

slide-44
SLIDE 44

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: a Queue: What went wrong?

16

slide-45
SLIDE 45

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: a Queue: b, d, What went wrong?

16

slide-46
SLIDE 46

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: b Queue: d, What went wrong?

16

slide-47
SLIDE 47

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: b Queue: d, a, b, e, What went wrong?

16

slide-48
SLIDE 48

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: d Queue: a, b, e, What went wrong?

16

slide-49
SLIDE 49

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: d Queue: a, b, e, f, g, What went wrong?

16

slide-50
SLIDE 50

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: a Queue: b, e, f, g, What went wrong?

16

slide-51
SLIDE 51

Breadth-fjrst search (BFS) example

search(v): queue.enqueue(v) while (queue is not empty): curr = queue.dequeue() for (w : v.neighbors()): queue.enqueue(w)

a b d c e f g h i j

Current node: a Queue: e, f, g, What went wrong?

16

slide-52
SLIDE 52

A broken traversal

Problem: We’re re-visiting nodes we already visited! A fjx: Keep track of nodes we’ve already visited in a set!

17

slide-53
SLIDE 53

A broken traversal

Problem: We’re re-visiting nodes we already visited! A fjx: Keep track of nodes we’ve already visited in a set!

17

slide-54
SLIDE 54

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-55
SLIDE 55

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-56
SLIDE 56

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-57
SLIDE 57

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-58
SLIDE 58

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-59
SLIDE 59

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-60
SLIDE 60

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-61
SLIDE 61

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-62
SLIDE 62

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-63
SLIDE 63

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-64
SLIDE 64

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-65
SLIDE 65

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-66
SLIDE 66

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-67
SLIDE 67

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-68
SLIDE 68

Breadth-fjrst search (BFS) example

search(v): visited = empty set queue.enqueue(v) visited.add(curr) 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,

18

slide-69
SLIDE 69

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

19

slide-70
SLIDE 70

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

19

slide-71
SLIDE 71

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

19

slide-72
SLIDE 72

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

19

slide-73
SLIDE 73

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

19

slide-74
SLIDE 74

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

19

slide-75
SLIDE 75

BFS analysis

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

Questions: ◮ What is the worst-case runtime? (Let |V | be the number of vertices, let |E| be the number of edges) We visit each vertex once, and each edge once, so V E . ◮ What is the worst-case amount of memory used? Whatever the largest “horizon size” is. In the worst case, the horizon will contain V nodes, so V . Note: V E is also called “graph linear”.

20

slide-76
SLIDE 76

BFS analysis

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

Questions: ◮ What is the worst-case runtime? (Let |V | be the number of vertices, let |E| be the number of edges) We visit each vertex once, and each edge once, so O (|V | + |E|). ◮ What is the worst-case amount of memory used? Whatever the largest “horizon size” is. In the worst case, the horizon will contain V nodes, so V . Note: V E is also called “graph linear”.

20

slide-77
SLIDE 77

Other applications of BFS

Describe how you would use or modify BFS to solve the following: ◮ Determine if some graph is also a tree. ◮ Print out all the elements in a tree level by level. ◮ Find the shortest path from one node to another.

21

slide-78
SLIDE 78

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) 22

slide-79
SLIDE 79

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) 22

slide-80
SLIDE 80

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) 22

slide-81
SLIDE 81

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) 22

slide-82
SLIDE 82

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 c e f g h i j

Current node: Stack: Visited:

23