+ All Categories
Home > Documents > 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr....

1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr....

Date post: 11-Jan-2016
Category:
Upload: jane-flynn
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
79
1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms JFK BOS MIA ORD LAX DFW SFO BWI PVD 867 2704 187 1258 849 144 740 1391 184 946 1090 1121 2342 1846 621 802 1464 1235 337 Asst. Prof. Dr. Bunyarit Uyyanonvara Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing Lab. School of Information, Computer and Communication Technology (ICT) Sirindhorn International Institute of Technology (SIIT) Thammasat University http://www.siit.tu.ac.th/bunyarit [email protected] 02 5013505 X 2005
Transcript
Page 1: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

1

Minimum Spanning Tree Problem

Topic 10 ITS033 – Programming & Algorithms JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

Page 2: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

2

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.vcharkarn.com/vlesson/7

Midterm

Page 3: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

3

Outline

Minimum Spanning Trees Definitions A crucial fact

The Prim-Jarnik Algorithm

Kruskal's Algorithm

Page 4: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

4

Minimum Spanning Tree Problem

Topic 10.1 ITS033 – Programming & Algorithms JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

Page 5: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

5

Imagine: 1. You wish to connect all the computers in an office building using the least amount of cable

Concrete exampleConcrete example

B

A

H

E

G

F

D

C

5 0 2 5

4 62 5

2 3

5 5

3 2

9 8

3 5

6 7

B

A

H

E

G

F

D

C

2 5

4 6

2 52 3

5 5

3 2

3 5

M in im u m am o u n t o f cab le = 2 4 1

M in im u m s p an n in g t reeN et w o rk o f H u b s

Page 6: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

6

Imagine:

2. If you want to build a railway network to all provinces in Thailand with minimum cost, i.e., minimum total length of the rail way.

Concrete exampleConcrete example

Page 7: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

7

Page 8: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

8

Imagine:

3. Or a network of super-highway to connect all the cities of Bangkok with minimum cost

Page 9: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

9

Imagine:

4. We want to connect electricity to all of the houses in the whole village with minimum total of cable length

Concrete exampleConcrete example

Page 10: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

10

Each vertex in a graph G represents a computer, a province or a house

Each edge represents the amount of cable, length of a railway needed to connect all vertices

That‘s a weighted graph problem !! A problem of finding A Minimum Spanning Tree

Minimum Spanning Tree Minimum Spanning Tree ProblemProblem

Page 11: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

11

• Given a connected, undirected graph, a spanning tree of that graph is a sub-graph which is a tree and connects all the vertices together. A single graph can have many different spanning trees.

• We can assign a weight to a spanning tree by computing the sum of the weights of the edges in that spanning tree.

• A minimum spanning tree or minimum weight spanning tree is then a spanning tree with weight less than or equal to the weight of every other spanning tree.

Minimum Spanning Tree Minimum Spanning Tree (MST)(MST)

ORD

PIT

ATL

STL

DEN

DFW

DCA

101

9

8

6

3

25

7

4

Page 12: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

12

We are interested in:

Finding a tree T that contains all the vertices of a graph G (spanning tree)and has the least total weight over all.

such trees is minimum-spanning tree (MST)

Tuv

uvwTw),(

)),(()(

Page 13: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

13

Minimum Spanning Tree

Page 14: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

14

Possible multiplicityThere may be several minimum spanning trees of the same weight; in particular, if all weights are the same, every spanning tree is minimum.

PropertiesProperties

Page 15: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

15

UniquenessIf each edge has a distinct weight then there will only be one, unique minimum spanning tree.

Minimum-cost subgraphIf the weights are non-negative, then a minimum spanning tree is in fact the minimum-cost subgraph connecting all vertices, since subgraphs containing cycles necessarily have more total weight.

Cycle propertyFor any cycle C in the graph, if the weight of an edge e of C is larger than the weights of other edges of C, then this edge cannot belong to a MST.

PropertiesProperties

Page 16: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

16

Cycle Property

Cycle Property: Let T be a minimum

spanning tree of a weighted graph G

Let e be an edge of G that is not in T and C let be the cycle formed by e with T

For every edge f of C, weight(f) weight(e)

Proof: By contradiction If weight(f) weight(e) we

can get a spanning tree of smaller weight by replacing e with f

84

2 36

7

7

9

8e

C

f

84

2 36

7

7

9

8

C

e

f

Replacing f with e yieldsa better spanning tree

Page 17: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

17

U V

Partition Property

Partition Property: Consider a partition of the vertices

of G into subsets U and V Let e be an edge of minimum weight

across the partition There is a minimum spanning tree

of G containing edge e

Proof: Let T be an MST of G If T does not contain e, consider the

cycle C formed by e with T and let f be an edge of C across the partition

By the cycle property,weight(f) weight(e)

Thus, weight(f) weight(e) We obtain another MST by

replacing f with e

74

2 85

7

3

9

8 e

f

74

2 85

7

3

9

8 e

f

Replacing f with e yieldsanother MST

U V

Page 18: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

18

Topic 10.2 ITS033 – Programming & Algorithms JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

Prim’s Algorithm

Page 19: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

19

Try to use exhaustive search ? If we were to try an exhaustive-search approach to

constructing a minimum spanning tree, we would face two serious obstacles.

1) the number of spanning trees grows exponentially with the graph size (at least for dense graphs).

2) generating all spanning trees for a given graph is not easy;

Page 20: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique20

Greedy Approach The greedy approach suggests constructing a solution

through a sequence of steps, each expanding a partially constructed solution obtained so far, until a complete solution to the problem is reached. On each step—and this is the central point of this technique—the choice made must be

feasible, i.e., it has to satisfy the problem’s constraints. locally optimal, i.e., it has to be the best local choice

among all feasible choices available on that step. irrevocable, i.e., once made, it cannot be changed on

subsequent steps of the algorithm.

Page 21: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

21

Prim’s Algorithm Prim’s algorithm constructs a minimum spanning

tree through a sequence of expanding subtrees.

The initial subtree in such a sequence consists of a single vertex selected arbitrarily from the set V of the graph’s vertices.

On each iteration, we expand the current tree in the greedy manner by simply attaching to it the nearest vertex not in that tree.

The algorithm stops after all the graph’s vertices have been included in the tree being constructed.

Page 22: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

22

Prim’s Algorithm

Page 23: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

23

Prim’s Algorithm The nature of Prim’s algorithm makes it necessary to

provide each vertex not in the current tree with the information about the shortest edge connecting the vertex to a tree vertex.

We can provide such information by attaching two labels to a vertex:

(1) The name of the nearest tree vertex and the length (the weight) of the corresponding edge.

(2) Vertices that are not adjacent to any of the tree vertices canbe given the ∞ label indicating their “infinite” distance to the tree vertices and a null label for the name of the nearest tree vertex.

Page 24: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

24

Prim’s Algorithm We pick an arbitrary vertex s and we grow the MST as a

cloud of vertices, starting from s

We store with each vertex v a label d(v) = the smallest weight of an edge connecting v to a vertex in the cloud

At each step: We add to the cloud the vertex u outside the cloud with the

smallest distance label

We update the labels of the vertices adjacent to u

Page 25: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

25

Prim‘s AlgorithmPrim‘s Algorithm

1. All vertices are marked as not visited

2. Any vertex v you like is chosen as starting vertex and is marked as visited (define a cluster C)

3. The smallest- weighted edge e = (v,u), which connects one vertex v inside the cluster C with another vertex u outside of C, is chosen and is added to the MST.

4. The process is repeated until a spanning tree is formed

Page 26: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

26

Example

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

8

Step 1Step 1

Page 27: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

27

Example

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

5

7

Step 2Step 2

Page 28: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

28

Example

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

5

7

Step 3Step 3

Page 29: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

29

Example

BD

C

A

F

E

74

28

5

7

3

9

8

07

2

5 4

7

Step 4Step 4

Page 30: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

30

Example (contd.)

BD

C

A

F

E

74

28

5

7

3

9

8

03

2

5 4

7

Step 5Step 5

Page 31: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

31

Example (contd.)

BD

C

A

F

E

74

28

5

7

3

9

8

03

2

5 4

7

Step 6Step 6

Page 32: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

32

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 33: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

33

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 34: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

34

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 35: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

35

C

FE

A B

D

3

4

2

1 2

3

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 36: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

36

C

FE

A B

D

3

2

1 2

3

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 37: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

37

C

FE

A B

D

3

2

1 2

2

3

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 38: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

38

C

FE

A B

D

3

2

1 2

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 39: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

39

C

FE

A B

D

3

2

1 2

2

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 40: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

40

C

FE

A B

D

3

2

1 2

2

minimum- spanning tree

Prim‘s AlgorithmPrim‘s AlgorithmPrim‘s AlgorithmPrim‘s Algorithm

Page 41: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique41

Prim’s Algorithm Analysis Efficiency of Prim’s algorithm depends on the data

structures chosen for the graph and for the priority queue of the set V-VT whose vertex priorities are the distances to the nearest tree vertices.

For example, if a graph is represented by its weight matrix and the priority queue is implemented as an unordered array, the algorithm’s running time will be in θ(|V |2).

Page 42: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique42

Prim’s Algorithm A min-heap is a complete binary tree in which every element

is less than or equal to its children.

Deletion of the smallest element from and insertion of a new element into a min-heap of size n are O(log n) operations, and so is the operation of changing an element’s priority

If a graph is represented by its adjacency linked lists and the priority queue is implemented as a min-heap, the running time of the algorithm is in O(|E| log |V |).

This is because the algorithm performs |V| - 1 deletions of the smallest element and makes |E| verifications and, possibly, changes of an element’s priority in a min-heap of size not greater than |V |. Each of these operations, as noted earlier, is a O(log |V |) operation. Hence, the running time of this implementation of Prim’s algorithm is in

(|V| - 1+ |E|)O(log |V |) = O(|E| log |V |)

Page 43: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

43

Prim’ Minimum Spanning Tree – Exercise

Page 44: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

44

Kruskal’s AlgorithmTopic 10.3 ITS033 – Programming & Algorithms JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Asst. Prof. Dr. Bunyarit UyyanonvaraAsst. Prof. Dr. Bunyarit UyyanonvaraIT Program, Image and Vision Computing Lab.

School of Information, Computer and Communication Technology (ICT)

Sirindhorn International Institute of Technology (SIIT)

Thammasat Universityhttp://www.siit.tu.ac.th/bunyarit

[email protected] 5013505 X 2005

Page 45: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

45

•Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph.

•If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component).

•Kruskal's algorithm is also an example of a greedy algorithm.

Minimum Spanning Tree Minimum Spanning Tree ProblemProblem

Page 46: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

46

It works as follows:

1.Each vertex is in its own cluster

2. Take the edge e with the smallest weight - if e connects two vertices in different clusters, then e is added to the MST and the two clusters, which are connected by e, are merged into a single cluster - if e connects two vertices, which are already in the same cluster, ignore it

3. Continue until n-1 edges were selected

Minimum Spanning Tree Minimum Spanning Tree ProblemProblem

Page 47: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique47

9.2 Kruskal’s Algorithm

Page 48: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique48

Kruskal’s Algorithm Analysis On each of its iterations, Kruskal’s algorithm has

to check whether the addition of the next edge to the edges already selected would create a cycle.

A new cycle is created if and only if the new edge connects two vertices already connected by a path, i.e., if and only if the two vertices belong to the same connected component

Each connected component of a subgraph generated by Kruskal’s algorithm is a tree because it has no cycles.

Page 49: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique49

Kruskal’s Algorithm Analysis

New edge connecting two vertices may (a) or may not (b) create a cycle

Page 50: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

50

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 51: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

51

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 52: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

52

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 53: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

53

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 54: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

54

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 55: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

55

C

FE

A B

D

5

64

3

4

2

1 2

3

2

cycle!!

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 56: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

56

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 57: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

57

C

FE

A B

D

5

64

3

4

2

1 2

3

2

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 58: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

58

C

FE

A B

D

3

2

1 2

2

minimum- spanning tree

Kruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s AlgorithmKruskal‘s Algorithm

Page 59: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

59

Kruskal Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 60: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

60

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Example

Page 61: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

61

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 62: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

62

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 63: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

63

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 64: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

64

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 65: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

65

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 66: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

66

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 67: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

67

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 68: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

68

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 69: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

69

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 70: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

70

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 71: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

71

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 72: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

72

Example

JFK

BOS

MIA

ORD

LAXDFW

SFO BWI

PVD

8672704

187

1258

849

144740

1391

184

946

1090

1121

2342

1846 621

802

1464

1235

337

Page 73: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

Greedy Technique73

Kruskal’s Algorithm Analysis The check whether two vertices belong to the same

tree is crucial in determining running time of Kruskal’s algorithm

There is efficient algorithm that perform this check => union-find algorithm

With an efficient union- find algorithm, the running time of Kruskal’s algorithm will be dominated by the time needed for sorting the edge weights of a given graph.

Hence, with an efficient sorting algorithm, the time efficiency of Kruskal’s algorithm will be in O(|E| log |E|).

Page 74: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

74

Data Structure for Kruskal Algortihm The algorithm maintains a forest of trees An edge is accepted it if connects distinct

trees We need a data structure that maintains a

partition, i.e., a collection of disjoint sets, with the operations:

Page 75: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

75

Kruskal’s Minimum Spanning Tree

Page 76: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

76

Page 77: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

77

Page 78: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

78

ITS033Topic 01Topic 01 -- Problems & Algorithmic Problem SolvingProblems & Algorithmic Problem SolvingTopic 02Topic 02 – Algorithm Representation & Efficiency Analysis – Algorithm Representation & Efficiency AnalysisTopic 03Topic 03 - State Space of a problem - State Space of a problemTopic 04Topic 04 - Brute Force Algorithm - Brute Force AlgorithmTopic 05Topic 05 - Divide and Conquer - Divide and ConquerTopic 06Topic 06 -- Decrease and ConquerDecrease and ConquerTopic 07Topic 07 - Dynamics Programming - Dynamics ProgrammingTopic 08 - Transform and ConquerTopic 09Topic 09 - Graph Algorithms - Graph AlgorithmsTopic 10Topic 10 - Minimum Spanning Tree - Minimum Spanning TreeTopic 11Topic 11 - Shortest Path Problem - Shortest Path ProblemTopic 12Topic 12 - Coping with the Limitations of Algorithms Power - Coping with the Limitations of Algorithms Power

http://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.siit.tu.ac.th/bunyarit/its033.phphttp://www.vcharkarn.com/vlesson/7

Midterm

Page 79: 1 Minimum Spanning Tree Problem Topic 10 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.

79

End of Chapter 10

Thank you!


Recommended