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

cs 170 algorithms
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

CS 170: Algorithms S H H H H

. . . . .

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

slide-3
SLIDE 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

slide-4
SLIDE 4

Today

1

Graphs

2

Reachability.

3

Depth First Search

David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 4 / 17

slide-5
SLIDE 5

Scheduling: coloring.

61A 61B 61C 70 170 61A 61B 61C 70 61C 70 170 Exam Slot 1. Exam Slot 2. Exam Slot 3.

David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 5 / 17

slide-6
SLIDE 6

Graph Implementations.

1 2 3 4 5 Matrix Representation.         1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1         V = {0,1,2,3,4,5} E = {(0,1),(0,2),(0,5),(1,3)...} 0 : 1,2,5 1 : 0,2,3,4,5 2 : 0,1,3 3 : 1,2,4 4 : 1,3,5 5 : 0,1,2,4 Adjacency List Matrix

  • Adj. List

Edge (u,v)? O(1) O(d) Neighbors of u O(|V|) O(d) Space O(|V|2) O(|V|+|E|)

David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 6 / 17

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 10

Explore.

C B D A E F G Explore(v): 1. Set visited[v] := true 2. for each edge (v,w) in E 3. if not visited[w]: Explore(w). Chalk. Stack is Thread. A F C D C G F B E B A Explore builds tree. Tree and back edges.

David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 10 / 17

slide-11
SLIDE 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

slide-12
SLIDE 12

Proof was induction.

a z w u 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

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 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

slide-17
SLIDE 17

Connected Components.

C B D A E F G A F C D D C G G F A B 1 E 1 E B

David Wagner (UC Berkeley) CS 170: Fall 2014 September 19, 2014 17 / 17