Graph traversal Topological sort Breadth-first search Cinda Heeren - - PowerPoint PPT Presentation

graph traversal
SMART_READER_LITE
LIVE PREVIEW

Graph traversal Topological sort Breadth-first search Cinda Heeren - - PowerPoint PPT Presentation

Graph traversal Topological sort Breadth-first search Cinda Heeren / Andy Roth / Will Evans / March 27, 2020 1 Geoffrey Tien Announcements All CPSC 221 deadlines postponed to 23:59, Apr.09 TA evaluations and instructor/course


slide-1
SLIDE 1

Graph traversal

Topological sort Breadth-first search

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 1

slide-2
SLIDE 2

Announcements

  • All CPSC 221 deadlines postponed to 23:59, Apr.09
  • TA evaluations and instructor/course evaluations will be done
  • nline

– Check your e-mail for links/instructions soon

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 2

slide-3
SLIDE 3

Total order

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 3

2 4 5 1 6 7 3 B A means A must go before B

slide-4
SLIDE 4

Partial order

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 4

Getting dressed socks undies shirt pants belt coat boots pocket knife watch

slide-5
SLIDE 5

Topological sort

  • Given a graph, 𝐻 = 𝑊, 𝐹 , output all vertices in V such that

no vertex is output before any other vertex with an edge to it

  • Classic application: course prerequisites

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 5

CPSC 110 CPSC 121 MATH 100 MATH 101 CPSC 210 CPSC 221 STAT 241 MATH 200 MATH 221 CPSC 213 CPSC 310 CPSC 320 CPSC 313

slide-6
SLIDE 6

Topological sort

  • Label each vertex's in-degree (# of inbound edges)
  • Initialize a queue to contain all vertices with in-degree zero
  • While there are vertices remaining in the queue

– Pick a vertex 𝑤 from the queue and output it – Reduce the in-degree of all vertices adjacent to 𝑤 – Put any of these with updated zero in-degree on the queue – Remove 𝑤 from the queue

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 6

slide-7
SLIDE 7

Topological sort

Vertex In-degree 1 2 1 3 1 4 2 5 2 6

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 7

2 4 5 1 6 3

Queue: 1 6

slide-8
SLIDE 8

Topological sort

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 8

Vertex In-degree 1 2 1 3 1 4 2 5 2 6

2 4 5 1 6 3

Queue: 1 6 1 2

1 6

slide-9
SLIDE 9

Topological sort

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 9

Vertex In-degree 1 2 3 1 4 1 5 2 6 Queue: 2

2 4 5 1 6 3 1 6 2

3

slide-10
SLIDE 10

Topological sort

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 10

Vertex In-degree 1 2 3 4 1 5 2 6 Queue: 3

2 4 5 1 6 3 1 6 2 3

1 4

slide-11
SLIDE 11

Topological sort

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 11

Vertex In-degree 1 2 3 4 5 1 6 Queue:

2 4 5 1 6 3 1 6 2 3

4

4

5

5

slide-12
SLIDE 12

How are algorithms affected?

  • Label each vertex's in-degree (# of inbound edges)
  • Initialize a queue to contain all vertices with in-degree zero
  • While there are vertices remaining in the queue

– Pick a vertex 𝑤 from the queue and output it – Reduce the in-degree of all vertices adjacent to 𝑤 – Put any of these with updated zero in-degree on the queue – Remove 𝑤 from the queue

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 12

Adjacency list vs. adjacency matrix

2 4 5 1 6 3

𝑃 𝑛 + 𝑜 vs 𝑃 𝑜2 𝑃 𝑜 𝑜 times... 𝑃 𝑒𝑓𝑕 𝑤 vs 𝑃 𝑜 For the adjacency list, how many vertices/edges have been processed in total?

slide-13
SLIDE 13

Breadth-first search

  • Visits all vertices within 𝑒 "hops" away from starting vertex,

before visiting vertices within 𝑒 + 1 hops, where 𝑒 begins at 0

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 13

start 1 hop 2 hops 3 hops 4 hops This looks very similar to a level-order traversal in a tree! How to control it? Use a queue!

slide-14
SLIDE 14

Breadth-first search

  • Need to be able to identify the neighbours of a given node, and

to check if a vertex has been added to the queue already

– Adjacency matrix / list, and a Boolean array of size 𝑊

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 14

3 1 2 9 8 6 7 4 5 1, 3 1 0, 2, 4, 6, 7 2 1, 3, 8, 9 3 0, 2 4 1, 5, 6, 7 5 4 6 1, 4, 7 7 1, 4, 6 8 2 9 2

Identified: 1 2 3 4 5 6 7 8 9

slide-15
SLIDE 15

Breadth-first search

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 15

3 1 2 9 8 6 7 4 5 1, 3 1 0, 2, 4, 6, 7 2 1, 3, 8, 9 3 0, 2 4 1, 5, 6, 7 5 4 6 1, 4, 7 7 1, 4, 6 8 2 9 2 T T T T T T T T T T

Identified: 1 2 3 4 5 6 7 8 9 Queue: Visited: 1 3 2 4 6 7 8 9 5 1 3 2 4 6 7 8 9 5 1 hop 2 hops 3 hops

slide-16
SLIDE 16

Breadth-first search

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 16

Algorithm

Algorithm BFS 𝐻, 𝑤 { mark 𝑤 as identified enqueue(𝑤) while (queue is not empty) { 𝑣 = dequeue() for all vertices 𝑥 adjacent to 𝑣, do { if (𝑥 has not been identified or visited) { mark 𝑥 as identified enqueue(𝑥) } }

  • utput 𝑣 to visited collection

} } A for loop nested inside a while loop. Running time?

slide-17
SLIDE 17

Readings for this lesson

  • Carrano & Henry

– Chapter 20.3 (Graph traversals)

  • Next class

– Carrano & Henry, Chapter 20.4.2 (Spanning trees) – Carrano & Henry, Chapter 20.4.3 (Minimum spanning trees)

March 27, 2020 Cinda Heeren / Andy Roth / Will Evans / Geoffrey Tien 17