Lab Assignment -...

Preview:

Citation preview

. .Spring 2010 CSC/CPE 349: Algorithms Alexander Dekhtyar. .

Lab 7: Minimum Spanning Trees.

Due date: Thursday, May 27, in-class.

Lab Assignment

Assignment Preparation

This is a paper-and-pencil lab!

The main goal of this lab is to prepare you for the second midterm exam,by allowing you to practice the manual execution of the two MST algorithmscovered in class: Kruskal’s and Prim’s.

This lab can be done in pairs. If you do it in pairs, submit only one copyof deliverables, but put both names on it.

Assignment

The worksheets below show five different graphs. For each graph, simulatethe work of Prim’s and Kruskal’s algorithms. For Prim’s algorithm, the rootnode is specified for you.

For Prim’s algorithm, on each step show:

• Nodes removed from the priority queue.

• Edges included in the MST.

• Edges that currently determine ”parent” relations in the priority queue.

• Best distances and parent relation for each node in priority queue.

For Kruskal’s algorithm, on each step show:

• Edges included in the MST.

1

• ”Parent” of each node in the disjoint set structure. (These can bedetermined in a number of ways. When two nodes are united, the”parent” for each can be picked in two different ways. Pick a way andbe consistent throughout your run).

More work. You can also simulate the runs of the Depth-First Searchand Breadth-First search on the supplied graphs. For each run, pick thestart node, indicate the state of the the queue/stack on each step, and theorder in which the nodes are visited.

Additionally, by turning the graphs from undirected into directed acyclic(by adding an arrow to each edge and making sure, there are no cylces), youcan simulate the run of the topological sort algorithm.

Extra worksheets are provided at the back.

Deliverables

Submit the hardcopy of the worksheets attached to this lab. Put yourname(s) on them.

Good Luck!

2

Graph 1. Prim’s Algorithm. Root node = a.

a

b

c d

e

f g

h i

abcde

f

hg

i

3 54

8 2

794

163

5

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

3

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

4

Graph 1. Kruskal’s Algorithm.

a

b

c d

e

f g

h i

abcde

f

hg

i

3 54

8 2

794

163

5

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

5

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

6

Graph 2. Prim’s Algorithm. Root node = a.

a c d

gfb

e h i

abcde

f

hg

i

1

47

4 3

58

7 4

3

5

2

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

7

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

8

Graph 2. Kruskal’s Algorithm.

a c d

gfb

e h i

abcde

f

hg

i

1

47

4 3

58

7 4

3

5

2

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

9

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

10

Graph 3. Prim’s Algorithm. Root node = c.

a c d

gfb

h

i

e

abcde

f

hg

i

3 8

93

7 38

2 1

6

4

5 2

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

11

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

12

Graph 3. Kruskal’s Algorithm.

a c d

gfb

h

i

e

abcde

f

hg

i

3 8

93

7 38

2 1

6

4

5 2

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

13

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

14

Graph 4. Prim’s Algorithm. Root node = h.

a c d

gfb

e h i

abcde

f

hg

i8

9

5

4

74

2 2

4

1 3

35

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

15

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

16

Graph 4. Kruskal’s Algorithm.

a c d

gfb

e h i

abcde

f

hg

i8

9

5

4

74

2 2

4

1 3

35

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

17

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

18

Graph 5. Prim’s Algorithm. Root node = g.

a c d

gfb i

e

h abcde

f

hg

i

3 4

5

6

7

5

265

4

2

38

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

19

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

20

Graph 5. Kruskal’s Algorithm.

a c d

gfb i

e

h abcde

f

hg

i

3 4

5

6

7

5

265

4

2

38

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

21

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

22

Extra Worksheets. Graph 1.

a

b

c d

e

f g

h i

abcde

f

hg

i

3 54

8 2

794

163

5

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

23

Extra Worksheets. Graph 1.

a

b

c d

e

f g

h i

abcde

f

hg

i

3 54

8 2

794

163

5

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

a

b

c d

e

f g

h i

abcde

f

hg

i

24

Extra Worksheets. Graph 2

a c d

gfb

e h i

abcde

f

hg

i

1

47

4 3

58

7 4

3

5

2

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

25

Extra Worksheets. Graph 2

a c d

gfb

e h i

abcde

f

hg

i

1

47

4 3

58

7 4

3

5

2

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

a c d

gfb

e h i

abcde

f

hg

i

26

Extra Worksheets. Graph 3.

a c d

gfb

h

i

e

abcde

f

hg

i

3 8

93

7 38

2 1

6

4

5 2

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

27

Extra Worksheets. Graph 3.

a c d

gfb

h

i

e

abcde

f

hg

i

3 8

93

7 38

2 1

6

4

5 2

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

28

Extra Worksheets. Graph 4.

a c d

gfb

e h i

abcde

f

hg

i8

9

5

4

74

2 2

4

1 3

35

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

29

Extra Worksheets. Graph 4.

a c d

gfb

e h i

abcde

f

hg

i8

9

5

4

74

2 2

4

1 3

35

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

30

Extra Worksheets. Graph 5.

a c d

gfb i

e

h abcde

f

hg

i

3 4

5

6

7

5

265

4

2

38

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

31

Extra Worksheets. Graph 5.

a c d

gfb i

e

h abcde

f

hg

i

3 4

5

6

7

5

265

4

2

38

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

a c d

gfb

h

i

e

abcde

f

hg

i

32

Recommended