Homework #2 - Review Problems for Final Exam
Due Date: Tuesday, December 7, 1999
1) For the array of values shown below, show what happens to the array with the Heapsort algorithm discussed in class and given in the Kruse text and notes. I.e., show the data changes in the array at each step/change as the algorithm executes to completion. First, the algorithm will turn the array into a maxheap, and then the algorithm will finish by executing the sort.
| 30 | 10 | 50 | 80 | 40 | 20 | 60 | 5 | 90 | 70 |
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
Solution:
First you must used build_heap (Kruse, p. 368) to build a max heap, note: build_heap calls insert_heap on p. 366-7. Here is the trace of build_heap -
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 30 | 10 | 50 | 80 | 40 | 20 | 60 | 5 | 90 | 70 |
| 70 | 40 | ||||||||
| 90 | 80 | ||||||||
| 60 | 50 | ||||||||
| 90 | 80 | 10 | |||||||
| 90 | 80 | 30 | |||||||
| 90 | 80 | 60 | 30 | 70 | 20 | 50 | 5 | 10 | 40 |
The last row above is the result of the build_heap algorithm. You can verify it is a heap by drawing the "tree structure", if you like. Next, you must perform the heap_sort algorithm on p. 365 of Kruse -
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 90 | 80 | 60 | 30 | 70 | 20 | 50 | 5 | 10 | 40 |
| 80 | 70 | 40 | 90 | ||||||
| 70 | 40 | 10 | 80 | ||||||
| 60 | 50 | 5 | 70 | ||||||
| 50 | 20 | 5 | 60 | ||||||
| 40 | 30 | 5 | 50 | ||||||
| 30 | 10 | 40 | |||||||
| 20 | 5 | 30 | |||||||
| 10 | 5 | 20 | |||||||
| 5 | 10 |
| 5 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 |
2) Show all the memory components (e.g. found array, distance array, min) and
their values as you trace through the algorithm for Dijkstra's Shortest Path on
the following directed graph. Be sure to also show what this graph would
look like in both adjacency table and "mixed" adjacency list form.
Solution:
Here is the adjacency matrix for the weighted digraph -
| From/To | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 0 | infinity | 3 | 6 | 2 | infinity | infinity | infinity | infinity |
| 1 | infinity | infinity | 2 | infinity | 6 | 4 | infinity | infinity |
| 2 | infinity | infinity | infinity | infinity | 2 | infinity | infinity | infinity |
| 3 | infinity | infinity | 1 | infinity | 5 | infinity | infinity | infinity |
| 4 | infinity | infinity | infinity | infinity | infinity | 4 | 8 | infinity |
| 5 | infinity | infinity | infinity | infinity | infinity | infinity | 1 | 4 |
| 6 | infinity | infinity | infinity | infinity | infinity | infinity | infinity | 2 |
| 7 | infinity | infinity | infinity | infinity | infinity | infinity | infinity | infinity |
Here is the "mixed" adjacency list implementation: (forgive the "poor graphics", the left hand side are the array indexes of a pointer array)
0 --> 1,3 --> 2,6-->3,2
1 --> 2,2--> 4,6 --> 5,4
2 --> 4,2
3 --> 2,1 --> 4,5
4 --> 5,4 --> 6,8
5 --> 6,1 --> 7,4
6 --> 7,2
7
Here is the trace of the Dijkstra Algorithm, according to Kruse's code on page 587:
This is the found array
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| F | F | F | F | F | F | F | F |
| T | |||||||
| T | |||||||
| T | |||||||
| T | |||||||
| T | |||||||
| T | |||||||
| T | |||||||
| T |
This is the distance array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| infinity | infinity | infinity | infinity | infinity | infinity | infinity | infinity |
| 0 | 3 | 6 | 2 | ||||
| 0 | 3 | 3 | 2 | 7 | |||
| 0 | 3 | 3 | 2 | 7 | 7 | ||
| 0 | 3 | 3 | 2 | 5 | 7 | ||
| 0 | 3 | 3 | 2 | 5 | 7 | 13 | |
| 0 | 3 | 3 | 2 | 5 | 7 | 8 | 11 |
| 0 | 3 | 3 | 2 | 5 | 7 | 8 | 10 |
| 0 | 3 | 3 | 2 | 5 | 7 | 8 | 10 |
Values for min : (in order) 0, 2, 3, 3, 5, 7, 7
3) For the undirected graph shown below, trace through Prim's Minimum Spanning Tree algorithm and show all calculations that are made in memory as the algorithm executes to completion. Be sure to draw the adjacency matrix for this undirected graph.
Solution: First the adjacency matrix for this undirected graph:
| From/To | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | infinity | 3 | 1 | 4 | 2 | infinity | 5 | 1 | infinity |
| 1 | 3 | infinity | 7 | infinity | infinity | infinity | infinity | 1 | infinity |
| 2 | 1 | 7 | infinity | 2 | infinity | infinity | infinity | infinity | infinity |
| 3 | 4 | infinity | 2 | infinity | infinity | infinity | infinity | infinity | infinity |
| 4 | 2 | infinity | infinity | infinity | infinity | 4 | 5 | infinity | infinity |
| 5 | infinity | infinity | infinity | infinity | 4 | infinity | 5 | infinity | infinity |
| 6 | 5 | infinity | infinity | infinity | 5 | 5 | infinity | 3 | 4 |
| 7 | 1 | 1 | infinity | infinity | infinity | infinity | 3 | infinity | 2 |
| 8 | infinity | infinity | infinity | infinity | infinity | infinity | 4 | 2 | infinity |
source = 0
values of min (in order): infinity, 1, infinity, 1, infinity, 1, infinity, 2, infinity, 2, infinity, 2, infinity, 3, infinity, 4
values of v (in order): 2, 7,1,3,4,8,6,5
values of the component array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| F | F | F | F | F | F | F | F | F |
| T | ||||||||
| T | ||||||||
| T | ||||||||
| T | ||||||||
| T | ||||||||
| T | ||||||||
| T | ||||||||
| T | ||||||||
| T |
values of the distance array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | 3 | 1 | 4 | 2 | infinity | 5 | 1 | infinity |
| 2 | ||||||||
| 1 | 3 | 2 | ||||||
| 4 |
values of the neighbor array:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| 2 | ||||||||
| 7 | 7 | 7 | ||||||
| 4 |
Here is the network (tree) representation of the minimum spanning tree that would be built with the above algorithm- (symmetric edges are left out, for brevity) :
| From/To | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 0 | |||||||||
| 1 | 1 | ||||||||
| 2 | 1 | ||||||||
| 3 | 2 | ||||||||
| 4 | 2 | ||||||||
| 5 | 4 | ||||||||
| 6 | 3 | ||||||||
| 7 | 1 | ||||||||
| 8 | 2 |
4) Do problem 1.2 a) b) c) d) from your handout on FSMs and FSRs.
Solution:
a) The Next function table
| Next | 0 | 1 |
| q0 | q2 | q1 |
| q1 | q2 | q1 |
| q2 | q2 | q1 |
The Output function table
| Next | 0 | 1 |
| q0 | 1 | 0 |
| q1 | 1 | 0 |
| q2 | 0 | 0 |
b) i) 1100 -->0010
ii)1010 -->0101
iii)11111100000 -->00000010000
c) This machine changes all runs of 1's to 0's and then in any run of O's, it changes the first 0 to a 1 and all remaining 0's in the run remain 0's. It appears to be a "falling edge detector".
d) q0 is the start state, it changes a 1 to 0 and a 0 to 1 (this 0 would then be the first 0 in a run),
q1 - in order for the machine to be in this state, the last input must have been a 1,
q2 - in order for the machine to be in this state, the last input must have been a 0