graphs + shortest paths david kauchak cs302 spring 2013

120
Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Upload: gwen-walker

Post on 19-Jan-2018

221 views

Category:

Documents


0 download

DESCRIPTION

Tree BFS

TRANSCRIPT

Page 1: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Graphs + Shortest Paths

David Kauchakcs302

Spring 2013

Page 2: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Admin

Page 3: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree BFS

Page 4: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time of Tree BFSAdjacency list

How many times does it visit each vertex? How many times is each edge traversed? O(|V|+|E|)

Adjacency matrix For each vertex visited, how much work is done? O(|V|2)

Page 5: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

BFS RecursivelyHard to do!

Page 6: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

BFS for graphsWhat needs to change for graphs?

Need to make sure we don’t visit a node multiple times

B

D E

F

A

C

G

Page 7: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

distance variable keeps track of how far from the starting node and whether we’ve seen the node yet

B

D E

F

A

C

G

Page 8: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

Page 9: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

set all nodes as unseen

B

D E

F

A

C

G

Page 10: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

check if the node has been seen

B

D E

F

A

C

G

Page 11: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

set the node as seen and record distance

B

D E

F

A

C

G

Page 12: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

Page 13: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0

Q: A

Page 14: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0

Q:

Page 15: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

11

Q: D, E, B

Page 16: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

11

Q: E, B

Page 17: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

11

Q: B

Page 18: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

11

Q: B

Page 19: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

11

Q:

Page 20: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

11

Q:

Page 21: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

2

2

11

Q: F, C

Page 22: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

2

2 3

11

Page 23: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

2

2 3

11

Page 24: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

B

D E

F

A

C

G

0 1

2

2 3

11

Page 25: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it visit all nodes reachable from the starting node?Can you prove it?

Assume we “miss” some node ‘u’, i.e. a path exists, but we don’t visit ‘u’

S … U

Page 26: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it visit all nodes reachable from the starting node?Can you prove it?

Find the last node along the path to ‘u’ that was visited

S … U… Z W

why do we know that such a node exists?

Page 27: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it visit all nodes reachable from the starting node?Can you prove it?

We visited ‘z’ but not ‘w’, which is a contradiction, given the pseudocode

S … U… Z W

contradiction

Page 28: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it correctly label each node with the shortest distance from the starting node?

Page 29: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it correctly label each node with the shortest distance from the starting node?

Assume the algorithm labels a node with a longer distance. Call that node ‘u’

S … U

Page 30: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it correctly label each node with the shortest distance from the starting node?

Find the last node in the path with the correct distance

S … U… Z W

correct incorrect

Page 31: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is BFS correct?Does it correctly label each node with the shortest distance from the starting node?

Find the last node in the path with the correct distance

S … U… Z W

contradiction

Page 32: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Runtime of BFSNothing changed over our analysis of TreeBFS

Page 33: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Runtime of BFS

Adjacency list: O(|V| + |E|)Adjacency matrix: O(|V|2)

Page 34: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Depth First Search (DFS)

Page 35: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Depth First Search (DFS)

Page 36: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 37: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 38: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 39: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 40: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Frontier?

Page 41: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 42: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 43: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 44: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Tree DFS

A

B

C

ED

F G

Page 45: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

DFS on graphs

Page 46: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

DFS on graphs

mark all nodes as not visited

Page 47: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

DFS on graphs

until all nodes have been visited repeatedly call DFS-Visit

Page 48: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

DFS on graphs

What happened to the stack?

Page 49: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

What does DFS do?Finds connected components

Each call to DFS-Visit from DFS starts exploring a new set of connected components

Helps us understand the structure/connectedness of a graph

Page 50: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is DFS correct?

Does DFS visit all of the nodes in a graph?

Page 51: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?

Like BFS Visits each node exactly once Processes each edge exactly twice (for an

undirected graph) O(|V|+|E|)

Page 52: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

DAGsCan represent dependency graphs

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 53: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sortA linear ordering of all the vertices such that for all edges (u,v) E, u appears before v in the ordering

An ordering of the nodes that “obeys” the dependencies, i.e. an activity can’t happen until it’s dependent activities have happened

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

underwear

pants

belt

watch

shirt

tie

socks

shoes

jacket

Page 54: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

Page 55: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 56: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 57: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 58: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 59: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 60: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 61: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 62: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 63: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?

Page 64: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?

O(|V|+|E|)

Page 65: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?

O(E) overall

Page 66: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?

How many calls? |V|

Page 67: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?

Overall running time?

O(|V|2+|V| |E|)

Page 68: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Can we do better?

Page 69: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort 2

Page 70: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort 2

Page 71: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort 2

Page 72: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Topological sort 2

Page 73: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Running time?How many times do we process each node?How many times do we process each edge?

O(|V| + |E|)

Page 74: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Connectedness

Given an undirected graph, for every node u V, can we reach all other nodes in the graph?

Run BFS or DFS-Visit (one pass) and mark nodes as we visit them. If we visit all nodes, return true, otherwise false.

Running time: O(|V| + |E|)

Page 75: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Strongly connectedGiven a directed graph, can we reach any node v from any other node u?

Ideas?

Page 76: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Transpose of a graphGiven a graph G, we can calculate the transpose of a graph GR by reversing the direction of all the edges

A

B

C

ED

A

B

C

ED

G GR

Running time to calculate GR? O(|V| + |E|)

Page 77: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Strongly connected

Page 78: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Is it correct?What do we know after the first pass?

Starting at u, we can reach every node

What do we know after the second pass? All nodes can reach u. Why? We can get from u to every node in GR, therefore, if we reverse

the edges (i.e. G), then we have a path from every node to u

Which means that any node can reach any other node. Given any two nodes s and t we can create a path through u

s u t… …

Page 79: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Runtime?

O(|V| + |E|)

O(|V| + |E|)

O(|V| + |E|)

O(|V| + |E|)

O(|V|)

O(|V|)

Page 80: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Detecting cyclesUndirected graph

BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle

Directed graph

A

BD

have to be careful

Page 81: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Detecting cyclesUndirected graph

BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle

Directed graph Call TopologicalSort If the length of the list returned ≠ |V| then a cycle exists

Page 82: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsWhat is the shortest path from a to d?

A

B

C E

D

Page 83: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsBFS

A

B

C E

D

Page 84: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsWhat is the shortest path from a to d?

A

B

C E

D

1

1

3

2

23

4

Page 85: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsWe can still use BFS

A

B

C E

D

1

1

3

2

23

4

Page 86: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsWe can still use BFS

A

B

C E

D

1

1

3

2

23

4

A

B

C E

D

Page 87: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsWe can still use BFS

A

B

C E

D

Page 88: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsWhat is the problem?

A

B

C E

D

Page 89: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest pathsRunning time is dependent on the weights

A

B

C4

1

2

A

B

C200

50

100

Page 90: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest paths

A

B

C200

50

100

A

B

C

Page 91: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest paths

A

B

C

Page 92: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest paths

A

B

C

Page 93: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Shortest paths

A

B

C

Nothing will change as we expand the frontier until we’ve gone out 100 levels

Page 94: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Dijkstra’s algorithm

Page 95: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Dijkstra’s algorithm

Page 96: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Dijkstra’s algorithmprev keeps track of the shortest path

Page 97: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Dijkstra’s algorithm

Page 98: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Dijkstra’s algorithm

Page 99: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

Dijkstra’s algorithm

Page 100: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

Page 101: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

Page 102: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

0

Heap

A 0B C D E

Page 103: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

0

Heap

B C D E

Page 104: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

0

Heap

B C D E

Page 105: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

1

0

Heap

C 1B D E

Page 106: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

1

0

Heap

C 1B D E

Page 107: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

3

1

0

Heap

C 1B 3D E

Page 108: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

1

3

3

21

4

3

1

0

Heap

C 1B 3D E

Page 109: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 110: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 111: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 112: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2

1

0

Heap

B 2D E

Page 113: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2

1

0

Heap

B 2D E

Page 114: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

Page 115: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

Frontier?

Page 116: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

All nodes reachable from starting node within a given distance

Page 117: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

E 3D 5

Page 118: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

D 5

Page 119: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

Page 120: Graphs + Shortest Paths David Kauchak cs302 Spring 2013

A

B

C E

D

1

11

2 5

31

0

Heap

3