1 / 21

6.006- Introduction to Algorithms

6.006- Introduction to Algorithms. Lecture 12 Prof. Silvio Micali CLRS 22.2-22.3. Graphs. a set of vertices denoted by . Often: a set of edges (pairs of vertices) denoted by Graph Flavors Directed : “edges have a direction” I.e., Undirected: “ ”: possible edges.

ata
Télécharger la présentation

6.006- Introduction to Algorithms

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. 6.006- Introduction to Algorithms Lecture 12 Prof. Silvio Micali CLRS 22.2-22.3

  2. Graphs • a set of vertices denoted by . Often: • a set of edges (pairs of vertices) denoted by Graph Flavors • Directed: “edges have a direction” I.e., • Undirected: “”: possible edges

  3. Examples Undirected • V={a,b,c,d} • E={{a,b}, {a,c}, {b,c}, {b,d}, {c,d}} Directed • V = {a,b,c} • E = {(a,c), (a,b) (b,c), (c,b)} a a b c b c d

  4. Graphs model lots of stuff: • Friendship • Powergrids • Maps • … • Why? • Functions are basic • Relations are more basic! • Graph Power!

  5. Computer Representation Four representations with pros/cons • Adjacency lists (of neighbors of each vertex) • Incidence lists (of edges from each vertex) • Adjacency matrix (of which pairs are adjacent) • Implicit representation (as neighbor function) a c b

  6. Computer Representation Four representations with pros/cons • Adjacency lists (of neighbors of each vertex) • Incidence lists (of edges from each vertex) • Adjacency matrix (of which pairs are adjacent) • Implicit representation (as neighbor function) a c b

  7. Searching Graphs Finding all vertices connected to a given vertex WLOG Class: Undirected Graphs Recitation: Directed Ones • connected to if there is a pathfrom to • such that • Length of (we count edges!) • Distance between and = length of shortest path from to Plan first for “mathematical” correctness Pseudo Code next (implementation ideas) for complexity Real Code homefor getting an output!

  8. Breadth First Search () (Wrong) All vertices initially unmarked, but 1. Until no new vertices are marked, mark all neighbors of currently marked vertices 1. Until all vertices are marked, mark all neighbors of currently marked vertices

  9. Breadth First Search () Example All vertices initially unmarked, but 1. Until no new vertices are marked, mark all neighbors of currently marked vertices Claim: Marks all vertices connected to Proof: On the Board… a s d f z x v c

  10. Breadth First Search () All vertices initially unmarked, but 1. Until no new vertices are marked, mark all neighbors of currently marked vertices Complexity? At least: or Implementation Details! Marked ADJECIENCY LIST 0 1 0 0 1 for such graphs 0 0 Can it get worse? 1

  11. Breadth First Search (Better Code) All vertices initially unmarked, but 1. Until no new vertices are marked, mark all neighbors of currently marked vertices Adjacency List d Frontier h f Marked “marked at last Step 2” 0 “Move & process d” f 1 0 d f w g … Etc. etc. g 1 w w 0 for all graphs 0 h 1 Can you do better?

  12. Augmented Breadth First Search=Shortest Path Alg () Initially, is marked , all other vertices are marked 1. 2. Find all neighbors of at least one vertex marked . If none, STOP. 3. Mark all vertices found in (3) with . 4. Claim:Every vertex is marked with its distance form Proof: … Complexity: …

  13. Example () 1 0 2 a s d f 3 1 z x v c 2 3 2

  14. Example () 1 0 2 a s d 3 f 1 z x v c 2 3 2 If you keep track of the edge through which you got your mark: BFS Tree! z a f s d x v c

  15. BFS Tree Structure • Spanning Tree: subgraphthat • 1. is a tree • 2. • subgraph of iffand • Extra Structure: ? z Nooooooo!!!! a f ? Possible! ? s d Possible Too x v c BFS Tree: Few Data, Very Informative!

  16. Note! Graphs model mazes. But: Searching Graphs Searching Mazes

  17. 1800s Depth First Search () In spockenEnglsh (sort of…) How to visit the Louvre in an hour and come out ALIVE! No strings allowed! (No questions either!) Claim 1: No edge is traversed twice in the same direction Claim 2: Upon Termination each edge has been traversed once in each direction

  18. Hopcroft’s & Tarjan’s DFS • Mark edges rather than their ``entrances” and “exits” • Number vertices (augmentation for future use) • Remember your father node rather than the edge who discovered you 0. Mark all edges “unused”. For all Let and 1. 2. If has no unused edges, go to (4) 3. Choose an unused edge Mark used. If go to (2). Else and go to (1) 4. If HALT 5. and go to (2)

  19. Thm: DFS Visits all vertices connected to Proof: … DFS Tree • Tree edges • Back edges Odds & Ends • Queues vs. Stacks • Strings??!

  20. Good News More Board Explanations! Good Implications More Reasons to come to class! Enjoy it!

  21. Lecture is over! Please walk calmly to the nearest EXIT

More Related