32
. . 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 algorithms covered in class: Kruskal’s and Prim’s. This lab can be done in pairs. If you do it in pairs, submit only one copy of deliverables, but put both names on it. Assignment The worksheets below show five different graphs. For each graph, simulate the work of Prim’s and Kruskal’s algorithms. For Prim’s algorithm, the root node 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

Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

  • Upload
    others

  • View
    16

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

. .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

Page 2: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

• ”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

Page 3: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 4: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 5: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 6: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 7: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 8: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 9: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 10: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 11: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 12: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 13: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 14: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 15: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 16: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 17: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 18: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 19: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 20: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 21: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 22: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 23: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 24: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 25: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 26: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 27: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 28: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 29: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 30: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 31: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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

Page 32: Lab Assignment - users.csc.calpoly.eduusers.csc.calpoly.edu/~dekhtyar/349-Spring2010/labs/lab07.349.pdfLab Assignment Assignment Preparation This is a paper-and-pencil lab! The main

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