1 / 24

Lecture 14

Lecture 14. CSE 331 Sep 29, 2017. Mini Project Pitch due WED. Form your group on Autolab BEFORE submitting your pitch. Do not forget to add URL to your references. HW 4 is now posted. Note : Bonus points for the fastest submissions. See WARNING though. Today ’ s agenda.

rock
Télécharger la présentation

Lecture 14

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14 CSE 331 Sep 29, 2017

  2. Mini Project Pitch due WED Form your group on Autolab BEFORE submitting your pitch Do not forget to add URL to your references

  3. HW 4 is now posted Note: Bonus points for the fastest submissions. See WARNING though.

  4. Today’s agenda Run-time analysis of BFS (DFS)

  5. Stacks and Queues Last in First out First in First out

  6. Graph representations 1 0 1 0 1 0 Better for sparse graphs and traversals 0 1 0 Adjacency matrix Adjacency List O(1) O(n) [ O(nv) ] (u,v) in E? All neighbors of u? O(n) O(nu) O(n2) Space? O(m+n)

  7. Questions?

  8. 2 # edges = sum of # neighbors 2m = Σ u in V nu Rest of the graph Give 2 pennies to each edge Total # of pennies = 2m nv=3 v nu=4 u Each edges gives one penny to its end points # of pennies u receives = nu

  9. Breadth First Search (BFS) Build layers of vertices connected to s L0= {s} Assume L0,..,Ljhave been constructed Lj+1set of vertices not chosen yet but are connected to Lj Stop when new layer is empty Use CC[v] array Use linked lists

  10. Rest of Today’s agenda Quick run time analysis for BFS Quick run time analysis for DFS (and Queue version of BFS) Helping you schedule your activities for the day

  11. 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++

  12. 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++

  13. An illustration 1 2 3 4 5 7 8 6 1 7 2 3 8 4 5 6

  14. 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) While Q is not empty Repeated at most once for each vertex u Σu O(nu) = O(Σu nu) = O(m) Delete the front element u in Q For every edge (u,w) Repeated nu times O(1) If CC[w] = F then O(nu) CC[w] = T O(1) Add w to the back of Q

  15. Questions?

  16. Implementing DFS in O(m+n) time Same as BFS except stack instead of a queue

  17. A DFS run using an explicit stack 7 8 1 7 6 7 3 2 3 5 8 4 4 5 5 3 6 2 3 1

  18. DFS stack implementation DFS(s) 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 Ŝ

  19. Questions?

  20. Reading Assignment Sec 3.3, 3.4 and 3.5 of [KT]

  21. Directed graphs Model asymmetric relationships Precedence relationships u needs to be done before w means (u,w) edge

  22. Directed graphs Adjacency matrix is not symmetric Each vertex has two lists in Adj. list rep.

  23. Directed Acyclic Graph (DAG) No directed cycles Precedence relationships are consistent

  24. Topological Sorting of a DAG Order the vertices so that all edges go “forward”

More Related