Download presentation
Presentation is loading. Please wait.
1
Lecture 16 CSE 331 Oct 5, 2011
2
Announcement by Sean
3
Online Office hours Tonight 9:30-10:30pm
4
On Friday, Oct 7 hours-a-thon Atri: 2:00-3:00 (Bell 123)
Jiun-Jie: 4:00-5:00 (Commons 9) Jesse: 5:00-6:00 (Bell 224)
5
The microphone experiment
6
A mid term tip You’ll reap the benefits of spending a bit of extra time on reading the questions
7
Today’s agenda Quick recap of run time analysis for BFS and DFS
Helping you schedule your activities for the day
8
O(m+n) BFS Implementation
Input graph as Adjacency list BFS(s) Array CC[s] = T and CC[w] = F for every w≠ s Set i = 0 Set L0= {s} While Li is not empty Linked List Li+1 = Ø For every u in Li For every edge (u,w) Version in KT also computes a BFS tree If CC[w] = F then CC[w] = T Add w to Li+1 i++
9
All layers are considered in first-in-first-out order
All the layers as one BFS(s) All layers are considered in first-in-first-out order CC[s] = T and CC[w] = F for every w≠ s Set i = 0 Set L0= {s} While Li is not empty Li+1 = Ø For every u in Li Can combine all layers into one queue: all the children of a node are added to the end of the queue For every edge (u,w) If CC[w] = F then CC[w] = T Add w to Li+1 i++
10
Queue O(m+n) implementation
BFS(s) CC[s] = T and CC[w] = F for every w≠ s O(n) Intitialize Q= {s} O(1) Σu O(nu) = O(Σu nu) = O(m) While Q is not empty Repeated at most once for each vertex u Delete the front element u in Q For every edge (u,w) O(nu) Repeated nu times O(1) If CC[w] = F then O(1) CC[w] = T Add w to the back of Q
11
Questions?
12
DFS stack implementation
CC[s] = T and CC[w] = F for every w≠ s Same O(m+n) run time analysis as for BFS Intitialize Ŝ = {s} While Ŝ is not empty Pop the top element u in Ŝ For every edge (u,w) If CC[w] = F then CC[w] = T Push w to the top of Ŝ
13
Questions?
14
Reading Assignment Sec 3.3, 3.4 and 3.5 of [KT]
15
Directed graphs Model asymmetric relationships
Precedence relationships u needs to be done before v means (u,v) edge
16
Directed graphs Adjacency matrix is not symmetric
Each vertex has two lists in Adj. list rep.
17
Directed Acyclic Graph (DAG)
No directed cycles Precedence relationships are consistent
18
Topological Sorting of a DAG
Order the vertices so that all edges go “forward”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.