58
Data Structures and Algorithms Session 20. April 8, 2009 Instructor: Bert Huang http://www.cs.columbia.edu/~bert/courses/3137

Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Data Structures and AlgorithmsSession 20. April 8, 2009

Instructor: Bert Huanghttp://www.cs.columbia.edu/~bert/courses/3137

Page 2: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Announcements

Homework 5 is out:

You can use adjacency lists if you prefer

Page 3: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Review

Extensions of Dijkstra’s Algorithm

Critical Path Analysis

All Pairs Shortest Path (Floyd-Warshall)

Maximum Flow

Floyd-Fulkerson Algorithm

Page 4: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Today’s Plan

Minimum Spanning Tree

Prim’s Algorithm

Kruskal’s Algorithm

Depth first search

Euler Paths

Page 5: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Minimum Spanning TreeProblem definition

Given connected graph G, find the connected, acyclic subgraph T with minimum edge weight

A tree that includes every node is called a spanning tree

The method to find the MST is another example of a greedy algorithm

Page 6: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Motivation for Greed

Consider any spanning tree

Adding another edge to the tree creates exactly one cycle

Removing an edge from that cycle restores the tree structure

Page 7: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Algorithm

Grow the tree like Dijkstra’s Algorithm

Dijkstra’s: grow the set of vertices to which we know the shortest path

Prim’s: grow the set of vertices we have added to the minimum tree

Store shortest edge D[ ] from each node to tree

Page 8: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Algorithm

Start with a single node tree, set distance of adjacent nodes to edge weights, infinite elsewhere

Repeat until all nodes are in tree:

Add the node v with shortest known distance

Update distances of adjacent nodes w: D[w] = min( D[w], weight(v,w))

Page 9: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

Page 10: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

∞ ∞

∞∞

Page 11: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

∞ ∞

∞3

9

Page 12: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

∞ ∞

9

Page 13: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

7 ∞

4

9

Page 14: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

7 ∞9

Page 15: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

7 59

Page 16: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

79

Page 17: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

69

Page 18: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

9

Page 19: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

5

Page 20: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Example

3

5

7

4

6

59

Page 21: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Implementation DetailsStore “previous node” like Dijkstra’s Algorithm; backtrack to construct tree after completion

Of course, use a priority queue to keep track of edge weights. Either

keep track of nodes inside heap & decreaseKey

or just add a new copy of the node when key decreases, and call deleteMin until you see a node not in the tree

Page 22: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Algorithm Justification

At any point, we can consider the set of nodes in the tree T and the set outside the tree Q

Whatever the MST structure of the nodes in Q, at least one edge must connect the MSTs of T and Q

The greedy edge is just as good structurally as any other edge, and has minimum weight

Page 23: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Prim’s Running Time

Each stage requires one deleteMin O(log |V|), and there are exactly |V| stages

We update keys for each edge, updating the key costs O(log |V|) (either an insert or a decreaseKey)

Total time: O(|V| log |V| + |E| log |V|) = O(|E| log |V|)

Page 24: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Kruskal’s AlgorithmSomewhat simpler conceptually, but more challenging to implement

Algorithm: repeatedly add the shortest edge that does not cause a cycle until no such edges exist

Each added edge performs a union on two trees; perform unions until there is only one tree

Need special ADT for unions (Disjoint Set... we’ll cover it later)

Page 25: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Kruskal’s Example

3

5

7

4

6

59

Page 26: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Kruskal’s Example

3

5

7

4

6

592

Page 27: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Kruskal’s Example

3

5

7

4

6

592

Page 28: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

3

Kruskal’s Example

5

7

4

6

592

Page 29: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

3

Kruskal’s Example

5

7

4

6

592

Page 30: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

3

Kruskal’s Example

5

7

4

6

592

Page 31: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

3

Kruskal’s Example

5

7

4

6

592

Page 32: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

3

Kruskal’s Example

5

7

4

6

592

Page 33: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Kruskal’s Justification

At each stage, the greedy edge e connects two nodes v and w

Eventually those two nodes must be connected;

we must add an edge to connect trees including v and w

We can always use e to connect v and w, which must have less weight since it's the greedy choice

Page 34: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Kruskal’s Running Time

First, buildHeap costs O(|E|)

Each edge, need to check if it creates a cycle(costs O(log V))

In the worst case, we have to call |E| deleteMins

Total running time O(|E| log |E|); but |E| !| V |2

O(|E| log |V |2) = O(2|E| log |V |) = O(|E| log |V |)

Page 35: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

MST Wrapup

Connect all nodes in graph using minimum weight tree

Two greedy algorithms:

Prim’s: similar to Dijkstra’s. Easier to code

Kruskal’s: easy on paper

Page 36: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Depth First Search

Level-order <–> Breadth-first SearchPreorder <–> Depth-first Search

Visit vertex v, then recursively visit v’s neighbors

To avoid visiting nodes more than once in a cyclic graph, mark visited nodes,

and only recurse on unmarked nodes

Page 37: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

The Seven Bridges of Königsberg

http://math.dartmouth.edu/~euler/docs/originals/E053.pdf

Königsburg Bridge Problem: can one walk across the seven bridges and never cross the same bridge twice?

Page 38: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

The Seven Bridges of Königsberg

Königsburg Bridge Problem: can one walk across the seven bridges and never cross the same bridge twice?

Euler solved the problem by inventing graph theory

http://math.dartmouth.edu/~euler/docs/originals/E053.pdf

Page 39: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Euler Paths and Circuits

Euler path – a (possibly cyclic) path that crosses each edge exactly once

Euler circuit - an Euler path that starts and ends on the same node

Page 40: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Euler’s Proof

Does an Euler path exist? No

Nodes with an odd degree must either be the start or end of the path

Only one node in the Königsberg graph has odd degree; the path cannot exist

What about an Euler circuit?

Page 41: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Finding an Euler Circuit

Run a partial DFS; search down a path until you need to backtrack (mark edges instead of nodes)

At this point, you will have found a circuit

Find first node along the circuit that has unvisited edges; run a DFS starting with that edge

Splice the new circuit into the main circuit, repeat until all edges are visited

Page 42: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Euler Circuit Example

2 3

1

5 6

4

7

Page 43: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

Page 44: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

Page 45: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

Page 46: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

Page 47: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

Page 48: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

Page 49: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

6

Page 50: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

6

4

Page 51: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3

1

4

3

6

4

5

Page 52: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

Page 53: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

6

Page 54: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

6

7

Page 55: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

7

Euler Circuit Example

2 3

1

5 6

4

1

2

3 1

4

3

6

4

5

2

6

7 5

Page 56: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Euler Circuit Running TimeAll our DFS's will visit each edge once, so at least O(|E|)

Must use a linked list for efficient splicing of path, so searching for a vertex with unused edge can be expensive

but cleverly saving the last scanned edge in each adjacency list can prevent having to check edges more than once, so also O(|E|)

Page 57: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Hamiltonian Cycle

Now that we know how to find Euler circuits efficiently, can we find Hamiltonian Cycles?

Hamiltonian cycle - path that visits each node once, starts and ends on same node

Page 58: Data Structures and Algorithms - Columbia Universitybert/courses/3137/Lecture20.pdf · Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm Depth first search Euler Paths

Reading

Weiss 9.5 (MST - today’s material)

Weiss 9.6 (DFS - today’s material)

Weiss 9.7 (P vs. NP - Monday)