Algorithms R OBERT S EDGEWICK | K EVIN W AYNE K RUSKAL ' S A LGORITHM - - PowerPoint PPT Presentation

algorithms
SMART_READER_LITE
LIVE PREVIEW

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE K RUSKAL ' S A LGORITHM - - PowerPoint PPT Presentation

Algorithms R OBERT S EDGEWICK | K EVIN W AYNE K RUSKAL ' S A LGORITHM D EMO Algorithms F O U R T H E D I T I O N R OBERT S EDGEWICK | K EVIN W AYNE http://algs4.cs.princeton.edu Kruskal's algorithm demo Consider edges in ascending order of


slide-1
SLIDE 1

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

http://algs4.cs.princeton.edu

Algorithms

ROBERT SEDGEWICK | KEVIN WAYNE

KRUSKAL'S ALGORITHM DEMO

slide-2
SLIDE 2

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

2

Kruskal's algorithm demo

5 4 7 1 3 2 6 0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93

graph edges sorted by weight

an edge-weighted graph

slide-3
SLIDE 3

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

3

Kruskal's algorithm demo

0-7 0.16 5 4 7 1 3 2 6

does not create a cycle in MST

slide-4
SLIDE 4

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

4

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 5 4 7 1 3 2 6

in MST does not create a cycle

slide-5
SLIDE 5

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

5

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 5 4 7 1 3 2 6

in MST does not create a cycle

slide-6
SLIDE 6

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

6

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5 4 7 1 3 2 6

in MST does not create a cycle

slide-7
SLIDE 7

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

7

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 5 4 7 1 3 2 6

in MST does not create a cycle

slide-8
SLIDE 8

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

8

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 5 4 7 1 3 2 6

creates a cycle not in MST

slide-9
SLIDE 9

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

9

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 5 4 7 1 3 2 6

creates a cycle not in MST

slide-10
SLIDE 10

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

10

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 5 4 7 1 3 2 6

creates a cycle not in MST

slide-11
SLIDE 11

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

11

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 5 4 7 1 3 2 6

in MST does not create a cycle

slide-12
SLIDE 12

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

12

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 5 4 7 1 3 2 6

creates a cycle not in MST

slide-13
SLIDE 13

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

13

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 5 4 7 1 3 2 6

creates a cycle not in MST

slide-14
SLIDE 14

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

14

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 5 4 7 1 3 2 6

creates a cycle not in MST

slide-15
SLIDE 15

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

15

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 5 4 7 1 3 2 6

in MST does not create a cycle

slide-16
SLIDE 16

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

16

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 3-6 0.52 5 4 7 1 3 2 6

creates a cycle not in MST

slide-17
SLIDE 17

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

17

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 5 4 7 1 3 2 6

creates a cycle not in MST

slide-18
SLIDE 18

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

18

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 5 4 7 1 3 2 6

creates a cycle not in MST

slide-19
SLIDE 19

Consider edges in ascending order of weight.

・Add next edge to tree T unless doing so would create a cycle.

19

Kruskal's algorithm demo

0-7 0.16 2-3 0.17 1-7 0.19 0-2 0.26 5-7 0.28 1-3 0.29 1-5 0.32 2-7 0.34 4-5 0.35 1-2 0.36 4-7 0.37 0-4 0.38 6-2 0.40 3-6 0.52 6-0 0.58 6-4 0.93 5 4 7 1 3 2 6 a minimum spanning tree