28
CS221 Week 12 - Monday

Week 12 - Monday. What did we talk about last time? Topological sort and cycle detection in directed graphs Connectivity Strong connectivity

Embed Size (px)

Citation preview

Page 1: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

CS221Week 12 - Monday

Page 2: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Last time

What did we talk about last time? Topological sort and cycle detection in

directed graphs Connectivity Strong connectivity

Page 3: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Questions?

Page 4: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Project 4

Page 5: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Assignment 6

Page 6: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Minimum Spanning Trees and Shortest PathsStudent Lecture

Page 7: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Minimum Spanning Tree

Page 8: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

What if…

An airline has to stop running direct flights between some cities

But, it still wants to be able to reach all the cities that it can now

What’s the set of flights with the lowest total cost that will keep all cities connected?

Essentially, what’s the lowest cost tree that keeps the graph connected?

Page 9: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Minimum spanning tree

This tree is called the minimum spanning tree or MST

It has countless applications in graph problems

How do we find such a thing?

Page 10: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Prim’s Algorithm

1. Start with two sets, S and V: S has the starting node in it V has everything else

2. Find the node u in V that is closest to any node in S

3. Put the edge to u into the MST4. Move u from V to S5. If V is not empty, go back to Step 2

Page 11: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

MST Example

A

C

G

E

B

F

D

9

2

2

1

35

4

33

25

8

Page 12: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Prim's algorithm running time

Naïve implementation with an adjacency matrix O(|V|2)

Adjacency lists with binary heap O(|E| log |V|)

Adjacency lists with Fibonacci heap O(|E| + |V| log |V|)

Page 13: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

MST practice

A

L

I

F

B

E

G

C

J

H

K

D

5

3

11

6

5

1

4

3

8

5

12

9

2

7

195

4

10

24

1

Page 14: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Shortest Paths

Page 15: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Google Maps

How does Google Maps find the shortest route from California to Elizabethtown?

Graph theory, of course! It stores a very large graph where

locations are nodes and streets (well, parts of streets) are edges

Page 16: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Shortest paths

We use a weighted graph Weight can represent time, distance,

cost: anything, really The shortest path (lowest total

weight) is not always obvious5

3 4

16

2

8

6

A

B

D

C

E

Page 17: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

What’s the shortest path?

Take a moment and try to find the shortest path from A to E.

The shortest path has cost 14

5

3 4

16

A

B

D

C

E

2

8

6

A

B

C

D

E

Page 18: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

How can we always find the shortest path?

On a graph of that size, it isn’t hard to find the shortest path

A Google Maps graph has millions and millions of nodes

How can we come up with an algorithm that will always find the shortest path from one node to another?

Page 19: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Dijkstra’s Algorithm

In 1959, Edsger Dijkstra invented an algorithm to find shortest paths

First, a little notation:Notation Meaning

s Starting node

d(v) The best distance from s to v found so far

d(u, v) The direct distance between nodes u and v

S

A set which contains the nodes for which we

know the shortest path from s

V

A set which contains the nodes for which we do

not yet know the shortest path from s

Page 20: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Dijkstra’s Algorithm

1. Start with two sets, S and V: S is empty V has all the nodes in it

2. Set the distance to all nodes in V to ∞3. Set the distance to the starting node s to 04. Find the node u in V that is closest to s5. For every neighbor v of u in V

▪ If d(v) > d(u) + d(u,v)▪ Set d(v) = d(u) + d(u,v)

6. Move u from V to S7. If V is not empty, go back to Step 4

Page 21: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Node d(u)

A 0

B 5

C 8

D ∞

E 16

Node d(u)

A 0

B 5

C 7

D 11

E 16

Node d(u)

A 0

B 5

C 7

D 10

E 16

Node d(u)

A 0

B 5

C 7

D 10

E 14

Node d(u)

A 0

B 5

C 7

D 10

E 14

Node d(u)

A 0

B ∞

C ∞

D ∞

E ∞

Example for Dijkstra

5

3 4

16

A

B

D

C

E

2

8

6

Sets

S V

A B, C, D, E

Sets

S V

A, B C, D, E

Sets

S V

A, B, C D, E

Sets

S V

A, B, C, D E

Sets

S V

A, B, C, D, E

Finding the shortest distance

from A to all other nodes

Sets

S V

A, B, C, D, E

A

Page 22: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Features of Dijkstra

Always gets the next closest node, so we know there isn’t a better way to get there

Finds the shortest path from a starting node to all other nodes

Works even for directed graphs

Page 23: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Dijkstra's running time

The normal running time for Dijkstra's is O(|V|2) At worst, we may have to update each node in V

for each node V that we find the shortest path to A special data structure called a min-priority

queue can implement the process of updating priorities faster Total running time of O(|E| + |V| log |V|) Technically faster for sparse graphs Algorithm wizards Fredman and Tarjan created an

implementation called a Fibonacci Heap▪ Actually slow in practice

Page 24: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Shortest path practice

A

L

I

F

B

E

G

C

J

H

K

D

5

3

11

6

5

1

4

3

8

5

12

9

2

7

195

4

10

24

1

Page 25: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Prim vs. Dijkstra

The algorithms have very similar forms

Here is a graph for which the shortest path tree from A to all other nodes is not an MST

A

B

C

D

4

3

3

6

5

Page 26: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Upcoming

Page 27: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Next time…

Matching on bipartite graphs Stable marriage Euler tours

Page 28: Week 12 - Monday.  What did we talk about last time?  Topological sort and cycle detection in directed graphs  Connectivity  Strong connectivity

Reminders

Start on Project 4 Form teams by Friday

Start on Assignment 6