Lecture 16

18
Lecture 16 CSE 331 Oct 8, 2012

description

Lecture 16. CSE 331 Oct 8, 2012. Sample mid-term exam. Sign-up for Friday ’ s mini project. Today ’ s agenda. Quick recap of run time analysis for BFS. Quick run time analysis for DFS (and Queue version of BFS). Helping you schedule your activities for the day. O(m+n) BFS Implementation. - PowerPoint PPT Presentation

Transcript of Lecture 16

Page 1: Lecture 16

Lecture 16

CSE 331Oct 8, 2012

Page 2: Lecture 16

Sample mid-term exam

Page 3: Lecture 16

Sign-up for Friday’s mini project

Page 4: Lecture 16

Today’s agenda

Quick recap of run time analysis for BFS

Helping you schedule your activities for the day

Quick run time analysis for DFS (and Queue version of BFS)

Page 5: Lecture 16

O(m+n) BFS Implementation

BFS(s)

CC[s] = T and CC[w] = F for every w≠ s

Set i = 0Set L0= {s}

While Li is not empty

Li+1 = Ø

For every u in Li

For every edge (u,w)

If CC[w] = F then

CC[w] = TAdd w to Li+1

i++

ArrayArray

Linked ListLinked List

Input graph as Adjacency listInput graph as Adjacency list

Version in KT also

computes a BFS tree

Version in KT also

computes a BFS tree

Page 6: Lecture 16

All the layers as one

BFS(s)

CC[s] = T and CC[w] = F for every w≠ s

Set i = 0Set L0= {s}

While Li is not empty

Li+1 = Ø

For every u in Li

For every edge (u,w)

If CC[w] = F then

CC[w] = TAdd w to Li+1

i++

All layers are considered in first-in-first-out order

All layers are considered in first-in-first-out order

Can combine all layers into one queue: all the children of a node are

added to the end of the queue

Can combine all layers into one queue: all the children of a node are

added to the end of the queue

Page 7: Lecture 16

An illustration

11

22 33

44 55

66

77

88

11 22 33 44 55 77 88 66

Page 8: Lecture 16

Queue O(m+n) implementation

BFS(s)

CC[s] = T and CC[w] = F for every w≠ s

Intitialize Q= {s}

While Q is not empty

Delete the front element u in Q

For every edge (u,w)

If CC[w] = F then

CC[w] = TAdd w to the back of Q

O(n)O(n)

O(1)O(1)

O(1)O(1)

Repeated nu times

Repeated nu times

O(nu)O(nu)

Repeated at most once for each

vertex u

Repeated at most once for each

vertex u

Σu O(nu) = O(Σu nu) =

O(m)

Σu O(nu) = O(Σu nu) =

O(m) O(1)O(1)

Page 9: Lecture 16

Questions?

Page 10: Lecture 16

Implementing DFS in O(m+n) time

Same as BFS except stack instead of a queue

Page 11: Lecture 16

A DFS run using an explicit stack

11

22 33

44 55

66

77

88

11

22

44

55

66

33

88

77

33

55

33

77

Page 12: Lecture 16

DFS stack implementation

DFS(s)

CC[s] = T and CC[w] = F for every w≠ s

Intitialize Ŝ = {s}

While Ŝ is not empty

Pop the top element u in Ŝ

For every edge (u,w)

If CC[w] = F then

CC[w] = TPush w to the top of Ŝ

Same O(m+n) run

time analysis as for BFS

Same O(m+n) run

time analysis as for BFS

Page 13: Lecture 16

Questions?

Page 14: Lecture 16

Reading AssignmentSec 3.3, 3.4 and 3.5 of [KT]

Page 15: Lecture 16

Directed graphs

Model asymmetric relationships

Precedence relationships

u needs to be done before w means (u,w) edge

Page 16: Lecture 16

Directed graphsAdjacency

matrix is not symmetric

Adjacency matrix is not symmetric

Each vertex has two lists in Adj. list rep.

Each vertex has two lists in Adj. list rep.

Page 17: Lecture 16

Directed Acyclic Graph (DAG)

No directed cycles

Precedence relationships are

consistent

Precedence relationships are

consistent

Page 18: Lecture 16

Topological Sorting of a DAG

Order the vertices so that all edges go “forward”