cs 170 algorithms
play

CS 170: Algorithms Prof David Wagner. Slides edited from a version - PowerPoint PPT Presentation

CS 170: Algorithms Prof David Wagner. Slides edited from a version created by Prof. Satish Rao. For UC-Berkeley CS170 Fall 2014 students use only. Do not re-post or distribute David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 1


  1. CS 170: Algorithms Prof David Wagner. Slides edited from a version created by Prof. Satish Rao. For UC-Berkeley CS170 Fall 2014 students use only. Do not re-post or distribute David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 1 / 17

  2. CS 170: Algorithms . . H . H . H . H S David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 2 / 17

  3. Puzzle ◆ ◆ ♥ ♥ Is it possible to reach this position? ♥ ♥ ◆ ◆ Design an algorithm to determine whether it is reachable. Without looping forever. Take 30 seconds to think about it quietly on your own. Now work with someone next to you to solve this. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 3 / 17

  4. Today Graphs 1 Reachability. 2 Depth First Search 3 David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 4 / 17

  5. Scheduling: coloring. 61B 61B 61C 61C 61C 61A 61A 170 170 70 70 70 Exam Slot 1. Exam Slot 2. Exam Slot 3. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 5 / 17

  6. Graph Implementations. Matrix Representation. 5  0 1 1 0 0 1  1 0 1 1 1 1     1 1 0 1 0 0   0   0 1 1 0 1 0     0 1 0 1 0 1   1 1 1 1 0 1 0 4 2 V = { 0 , 1 , 2 , 3 , 4 , 5 } 3 E = { ( 0 , 1 ) , ( 0 , 2 ) , ( 0 , 5 ) , ( 1 , 3 ) ... } Adjacency List 0 : 1 , 2 , 5 Matrix Adj. List 1 : 0 , 2 , 3 , 4 , 5 Edge ( u , v ) ? O ( 1 ) O ( d ) 2 : 0 , 1 , 3 Neighbors of u O ( | V | ) O ( d ) 3 : 1 , 2 , 4 O ( | V | 2 ) Space O ( | V | + | E | ) 4 : 1 , 3 , 5 5 : 0 , 1 , 2 , 4 David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 6 / 17

  7. Exploring a maze. Theseus: ...gotta kill the minatour ..in the maze Ariadne: he’s cute..fortunately ..she’s smart. Gives Theseus Ball of Thread and Chalk! Explore a room: Mark room with chalk. For each exit. Look through exit. If marked, next exit. Otherwise go in room unwind thread. Explore that room. Wind thread to go back to “previous” room. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 7 / 17

  8. Where is the minatour? X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 8 / 17

  9. Searching Find a minatour! Find out which nodes are reachable from A . David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 9 / 17

  10. Explore. Explore(v): G G 1. Set visited[v] := true F F F 2. for each edge (v,w) in E 3. if not visited[w]: Explore(w). C C C B B B A A A D D E E Chalk. Stack is Thread. Explore builds tree. Tree and back edges. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 10 / 17

  11. Correctness. Explore(v): 1. Set visited[v] := true . 2. For each edge (v,w) in E 3. if not visited[w]: Explore(w) Property: All and only nodes reachable from A are reached by explore. Only: when u visited. stack contains nodes in a path from a to u . All: if a node u is reachable. there is a path to it. Assume: u not found. a z w u z is explored. w is not! Explore ( z ) would explore( w )! Contradiction. David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 11 / 17

  12. Proof was induction. u a z w Property: Every node with a path of length k or less is reached. Induction by Contradiction. Find smallest k (path length) where property doesn’t hold. It does hold for k − 1 So also for k Must hold for every k . Done!!! or . David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 12 / 17

  13. Running Time. Explore(v): 1. Set visited[v] := true . 2. For each edge (v,w) in E 3. if not visited[w]: Explore(w). How to analyse? Let n = | V | , and m = | E | . T ( n , m ) ≤ ( d ) T ( n − 1 , m )+ O ( d ) Exponential ?!?!?! Don’t use recurrence! David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 13 / 17

  14. Running Time. Explore(v): 1. Set visited[v] := true . 2. For each edge (v,w) in E 3. if not visited[w]: Explore(w). How to analyse? Let n = | V | , and m = | E | . “Charge work to something.” Put $1 on each node, and $2 on each edge, to pay for computation. For node x : Explore once! Process each incident edge. Each edge processed twice. O ( n ) - call explore on n nodes. O ( m ) - process each edge twice. Total: O ( n + m ) . David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 14 / 17

  15. Depth first search. Process whole graph. DFS(G) 1: For each node u , 2: visited[ u ] = false . 3: For each node u , 4: if not visited[ u ] explore( u ) Running time: O ( | V | + | E | ) . Intuitively: tree for each “connected component”. Several trees or Forest! Output connected components? David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 15 / 17

  16. DFS and connected components. Change explore a bit: explore(v): 1. Set visited[v] := true . 2. previsit(v) 3. For each edge (v,w) in E 4. if not visited[w]: explore(w). 5. postvisit(v) Previsit(v): 1. Set cc[v] := ccnum. DFS(G): 0. Set cc := 0. 1. for each v in V: 2. if not visited[v]: 3. explore(v) 4. ccnum = ccnum+1 Each node will be labelled with connected component number. Runtime: O ( | V | + | E | ) . David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 16 / 17

  17. Connected Components. 0 0 G G G F F F 0 C C C 1 0 0 B B B A A A D D D 1 E E E David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 17 / 17

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend