







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Consider the circular queue of size 10, as shown in the Figure below (Start state). The circular queue contains letters A through D.
Assume the following 9 operations take place in sequence
Show the contents of the circular queue after (Final state) performing all of the operations. Where are the head and tail pointers located?
pointer
Tail pointer
Head pointer
Tail pointer
Using the given graph below, what is the ‘minimum-weight spanning tree’? Which algorithm are you using? Show clearly, step by step, on the next page, how the algorithm is used. Finally, draw the resulting Minimum-weight spanning tree here below.
Given Graph:
Name of algorithm you are using: Kruskal’s algorithm
Minimum-weight spanning tree:
a
h
f
b
e
g
c
2 d
5
(^3 )
6
2
4
4
b a
3 2 c
h^4 e
2 d f g
b a
3 2 c 4 h^4
2 (^5) d f g
e
6 b a
3 2 c
4 e
2 f
h
4
(^5) d g
Part a) Show the computation of T(n) and the Big-O complexity for the code shown below. (6 points) Statement Work
function Compare (Value1, Value2 : Float ) return Float is Result : Float; c Minimum : Float := 2.0; c begin –- Compare
if Value1 < Value2 then c
Result := Value1; c
else c
Result := Value2; c
end if ; c
return Minimum; c
end Compare;
T(n) = c1 + c2 + c3 + 2c4 + c5 + c6 + c7 + c8 = c
O(n) = O(1)
Part b) What is the result passed out by the program when the input values are 10.8 and 10.2? (4 Points)
The output is 2.0 because of the “return Minimum; ” statement.
Part b. Write an algorithm to perform ordered insertion into the ordered tree shown in Figure 3. (18 points)
Part c. Implement your algorithm as an Ada95 procedure, which accepts the root node, and the element to be inserted, and performs the ordered insertion operation. (10 points) Program Code procedure Insert (Root : in out Node_ptr; Element : in Element_Type) is
Temp, New_Node : Node_ptr; Inserted : Boolean;
begin
New_Node:= new Node; New_Node.Element := Element; New_Node.Left_Child := null; New_Node.Right_Child := null; New_Node.Parent := null;
if Root = null then Root := New_Node; else Inserted := False; Temp := Root; loop exit when Inserted = True;
if Temp.Element < Element then if Temp.Right_Child /= null then Temp:= Temp.Right_Child; else Temp.Right_Child:= New_Node; New_Node.Parent := Temp; Inserted := True; end if; else if Temp.Left_Child/= null then Temp := Temp.Left_Child; else Temp.Left_Child:= New_Node; New_Node.Parent := Temp; Inserted := True; end if; end if; end loop; end if; end Insert;
Part a) What is the program behavior when the user enters a floating point number? Justify your answer. (4 points)
The program will generate a constraint error on Line 24 because
the user is trying to access already deallocated memory
Part b) What is the program behavior when the user enters a valid integer and Line 24 is commented out? Justify your answer. (6 points)
When line 24 is commented out, the constraint error is raised in
line 26 for exactly the same reason as before: the memory has been
deallocated.
What is the Big-O complexity of the algorithm shown below? Detail the steps in computing T(n) and O(n).
3Sort(A, left, right)
if (left < right) c
first_split := (left + right) / 3 c
second_split := (first_split + right)/2 c
3Sort(A, left, first_split) T(n/3)
3Sort(A, first_split+1, second_split) T(n/3)
3Sort(A, second_split+1, right) T(n/3)
Merge(A, left, first_split, second_split, right) O(n)
Therefore T(n) = 3T(n/3) + 2c2+c1 + O(n) = 3T(n/3) + O(n) + C
Therefore correlating to the simplified master theorem:
cnk^ = O(n) → k = 1
aT(n/b) = 3T(n/3)
Therefore:
T(n) = O( nk^ logb n )= O( n^1 log 3 n ) = O( n log 3 n )
C
A/B/C