Some Applications: Moving Around Washington

12
Some Applications: Moving Around Washington What’s the fastest way from Seattle to Spokane? Answer: Use Dijkstra!

description

Some Applications: Moving Around Washington. What’s the fastest way from Seattle to Spokane?. Answer:. Use Dijkstra!. Some Applications: Communication in Washington. What’s the cheapest inter-city network?. New problem for today. 4. 7. 9. 2. 1. 5. Spanning Tree. - PowerPoint PPT Presentation

Transcript of Some Applications: Moving Around Washington

Page 1: Some Applications: Moving Around Washington

Some Applications:Moving Around Washington

What’s the fastest way from Seattle to Spokane?

Answer: Use Dijkstra!

Page 2: Some Applications: Moving Around Washington

Some Applications:Communication in Washington

What’s the cheapest inter-city network?

New problem for today

Page 3: Some Applications: Moving Around Washington

Spanning tree: a subset of the edges from a connected graph that……touches all vertices in the graph (spans the graph)

…forms a tree (is connected and contains no cycles)

Minimum spanning tree: the spanning tree with the least total edge cost.

Spanning Tree

4 7

1 5

9

2

Which is the minimum? (the middle)

Page 4: Some Applications: Moving Around Washington

Two Different Algorithms

Prim’s AlgorithmAlmost identical to Dijkstra’s

Kruskals’s AlgorithmCompletely different!

One node, grow greedily Forest of MSTs,Union them together.

I wonder how to union…

Page 5: Some Applications: Moving Around Washington

Prim’s Algorithm for Minimum Spanning Trees

A node-oriented greedy algorithm (builds an MST by greedily adding nodes)

Select a node to be the “root” and mark it as known

While there are unknown nodes left in the graphSelect the unknown node n with the smallest cost from some known

node m

Mark n as known

Add (m, n) to our MST

Update cost of all nodes adjacent to n

Runtime:

Same as Dijkstra: O(ElogV)

Proof:

Same as Dijkstra

Cost == edge weight only.Use priority queue

Page 6: Some Applications: Moving Around Washington

Prim’s Algorithm In Action

A

C

B

D

F H

G

E

2 2 3

21

4

10

8

194

2

7

Start with A

Page 7: Some Applications: Moving Around Washington

Kruskal’s Algorithm for Minimum Spanning Trees

An edge-oriented greedy algorithm (builds an MST by greedily adding edges)

Initialize all vertices to unconnected

While there are still unmarked edgesPick the lowest cost edge e = (u, v) and mark it

If u and v are not already connected, add e to the minimum spanning tree and connect u and v

Sound familiar? Maze use random edge order.

Otherwise the same!

Page 8: Some Applications: Moving Around Washington

Kruskal’s Algorithm In Action

A

C

B

D

F H

G

E

2 2 3

21

4

10

8

194

2

7

Page 9: Some Applications: Moving Around Washington

Play at Home with PrimA

C

B

D

FH

G

E

1

76

5

11

4

12

13

23

9

10

4

1. Starting at node A, find the MST using Prim’s method.(continue on next slide)

Page 10: Some Applications: Moving Around Washington

Play at Home with KruskalA

C

B

D

FH

G

E

1

76

5

11

4

12

13

23

9

10

4

2. Now find the MST using Kruskal’s method.3. Under what conditions will these methods give the same result?4. What data structures should be used for Kruskal’s? Running time?

Page 11: Some Applications: Moving Around Washington

Proof of Correctness

We already showed this finds a spanning tree:That was part of our definition of a good maze.

Proof by contradiction that Kruskal’s finds the minimum:Assume another spanning tree has lower cost than Kruskal’s

Pick an edge e1 = (u, v) in that tree that’s not in Kruskal’s

Kruskal’s tree connects u’s and v’s sets with another edge e2

But, e2 must have at most the same cost as e1!

So, swap e2 for e1 (at worst keeping the cost the same)

Repeat until the tree is identical to Kruskal’s: contradiction!

QED: Kruskal’s algorithm finds a minimum spanning tree.

(contradicts that other tree is better)

Page 12: Some Applications: Moving Around Washington

Kruskal’s Implementation

Initialize all vertices to unconnected

While there are still unmarked edgesPick the lowest cost edge e = (u, v) and mark it

If u and v are not already connected, add e to the minimum spanning tree and connect u and v

E * deleteMin

2E * find + + V * union

Priority queue + disjoint sets

What data structures?

Running time?

O( ElogE + E~O(1)) = O(ElogE) = O(ElogV)b/c log E < logV2 = 2logV