85
Graphs David Kauchak cs302 Spring 2012

Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Embed Size (px)

Citation preview

Page 1: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Graphs

David Kauchak

cs302

Spring 2012

Page 2: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

DAGs

Can represent dependency graphs

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 3: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort A 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 4: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

Page 5: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 6: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 7: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 8: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 9: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 10: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 11: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 12: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort

underwear

pants

belt

shirt

tie

jacket

socks

shoes

watch

Page 13: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

Page 14: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

O(|V|+|E|)

Page 15: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

O(E) overall

Page 16: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

How many calls? |V|

Page 17: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

Overall running time?

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

Page 18: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Can we do better?

Page 19: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort 2

Page 20: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort 2

Page 21: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort 2

Page 22: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Topological sort 2

Page 23: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time? How many times do we process each node? How many times do we process each edge? O(|V| + |E|)

Page 24: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

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 25: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Strongly connected

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

Ideas?

Page 26: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Transpose of a graph Given 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 27: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Strongly connected

Page 28: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

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 29: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Runtime?

O(|V| + |E|)

O(|V| + |E|)

O(|V| + |E|)

O(|V| + |E|)

O(|V|)

O(|V|)

Page 30: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Detecting cycles

Undirected 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 31: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Detecting cycles

Undirected 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 32: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

Page 33: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

BFS

A

B

C E

D

Page 34: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

What is the shortest path from a to d?

A

B

C E

D

1

1

3

2

23

4

Page 35: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

23

4

Page 36: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

We can still use BFS

A

B

C E

D

1

1

3

2

23

4

A

B

C E

D

Page 37: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

We can still use BFS

A

B

C E

D

Page 38: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

What is the problem?

A

B

C E

D

Page 39: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths Running time is dependent on the weights

A

B

C4

1

2

A

B

C200

50

100

Page 40: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

A

B

C200

50

100

A

B

C

Page 41: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

A

B

C

Page 42: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

A

B

C

Page 43: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Shortest paths

A

B

C

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

Page 44: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Dijkstra’s algorithm

Page 45: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Dijkstra’s algorithm

Page 46: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Dijkstra’s algorithm

prev keeps track of the shortest path

Page 47: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Dijkstra’s algorithm

Page 48: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Dijkstra’s algorithm

Page 49: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Dijkstra’s algorithm

Page 50: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Single source shortest paths

All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms

Why?

Page 51: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

Page 52: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

Page 53: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

0

Heap

A 0B C D E

Page 54: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

0

Heap

B C D E

Page 55: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

0

Heap

B C D E

Page 56: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

1

0

Heap

C 1B D E

Page 57: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

1

0

Heap

C 1B D E

Page 58: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

3

1

0

Heap

C 1B 3D E

Page 59: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

1

3

3

21

4

3

1

0

Heap

C 1B 3D E

Page 60: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 61: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 62: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

3

1

0

Heap

B 3D E

Page 63: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2

1

0

Heap

B 2D E

Page 64: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2

1

0

Heap

B 2D E

Page 65: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

Page 66: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2

51

0

Heap

B 2E 5D

Frontier?

Page 67: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

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 68: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

E 3D 5

Page 69: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

D 5

Page 70: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

3

A

B

C E

D

1

1

3

21

4

2 5

31

0

Heap

Page 71: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

A

B

C E

D

1

11

2 5

31

0

Heap

3

Page 72: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Is Dijkstra’s algorithm correct?

Invariant:

Page 73: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Is Dijkstra’s algorithm correct?

Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v

proof?

Page 74: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Is Dijkstra’s algorithm correct?

Invariant: For every vertex removed from the heap, dist[v] is the actual shortest distance from s to v

The only time a vertex gets visited is when the distance from s to that vertex is smaller than the distance to any remaining vertex

Therefore, there cannot be any other path that hasn’t been visited already that would result in a shorter path

Page 75: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

Page 76: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

1 call to MakeHeap

Page 77: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

|V| iterations

Page 78: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

|V| calls

Page 79: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

O(|E|) calls

Page 80: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

Depends on the heap implementation

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

O(|E| log |V|)

Page 81: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

Depends on the heap implementation

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

O(|E| log |V|)

Is this an improvement? If |E| < |V|2 / log |V|

Page 82: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

Running time?

Depends on the heap implementation

1 MakeHeap |V| ExtractMin |E| DecreaseKey Total

Array O(|V|) O(|V|2) O(|E|) O(|V|2)

Bin heap O(|V|) O(|V| log |V|) O(|E| log |V|) O((|V|+|E|) log |V|)

Fib heap O(|V|) O(|V| log |V|) O(|E|) O(|V| log |V| + |E|)

O(|E| log |V|)

Page 83: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

What about Dijkstra’s on…?

A

B

C E

D11

-10

5

10

Page 84: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

What about Dijkstra’s on…?

A

B

C E

D11

-10

5

10

Page 85: Graphs David Kauchak cs302 Spring 2012. DAGs Can represent dependency graphs underwear pants belt shirt tie jacket socks shoes watch

What about Dijkstra’s on…?

A

B

C E

D11

5

10

Dijkstra’s algorithm only works for positive edge weights