Docsity

Circular Queue and Minimum Spanning Tree Problem Set, Exercises of Aeronautical Engineering

Toggle sidebar
bg1
Name: ________________________________
Problem 1 - Queue (5 points)
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
1. Insert entry E
2. Insert entry F
3. Remove one entry
4. Remove one entry
5. Insert entry G
6. Insert entry H
7. Remove one entry
8. Insert entry I
9. Insert entry J
Show the contents of the circular queue after (Final state) performing all of the
operations. Where are the head and tail pointers located?
D
C
B
A
Head
pointer
Tail
pointer
Start state
I
H
G
F
E
D
J
Head
pointer
Tail
pointer
Final state
1
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Circular Queue and Minimum Spanning Tree Problem Set and more Exercises Aeronautical Engineering in PDF only on Docsity!

Problem 1 - Queue (5 points)

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

  1. Insert entry E
  2. Insert entry F
  3. Remove one entry
  4. Remove one entry
  5. Insert entry G
  6. Insert entry H
  7. Remove one entry
  8. Insert entry I
  9. Insert entry J

Show the contents of the circular queue after (Final state) performing all of the operations. Where are the head and tail pointers located?

D

C

B

Head A

pointer

Tail pointer

Start state

I

H

G

F

E

D

J

Head pointer

Tail pointer

Final state

Problem 2 – MST (15 points)

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

Problem 3 – Big-O (10 points)

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)

Algorithm

Create two node pointers Temp, New_Node.

Allocate memory for New_Node using new.

Initialize the fields of New_Node

Element is set to input element

All pointers are set to null

If Root = null

Set root to New_Node

Else

Set Temp to Root.

Set Flag to False

While Flag is False

If Temp.Element is smaller than Element

If Temp has no right child

Temp.right_child := New_Node

New_Node.Parent := Temp

Set Flag to True

Else

Move Temp to Temp.right_child

Else

If Temp has no left child

Temp.left_child := New_Node

New_Node.Parent := Temp

Set Flag to True

Else

Move Temp to Temp.left_child

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;

Problem 5 – Ada Exception Handling (10 points)

  1. with Ada.Text_Io, Ada.Integer_Text_Io, Ada.Unchecked_Deallocation;
  2. procedure Demo_Robust_Programming is
  3. subtype My_Integer is Integer;
  4. type My_Integer_Ptr is access all My_Integer;
  5. My_Num : My_Integer;
  6. My_Num_Ptr : My_Integer_Ptr;
  7. procedure Free is
  8. new Ada.Unchecked_Deallocation(My_Integer, My_Integer_Ptr);
  9. begin
  10. My_Num_Ptr := new My_Integer;
  11. Free(My_Num_Ptr);
  12. Ada.Text_Io.Put("Please enter an integer: ");
  13. Ada.Integer_Text_Io.Get(My_Num);
  14. My_Num_Ptr.All := My_Num;
  15. Ada.Text_Io.Put(Integer'Image(My_Num_Ptr.All));
  16. exception
  17. when Constraint_Error =>
  18. Ada.Text_Io.Put_Line("Unsafe Pointer Handling");
  19. when Ada.Text_Io.Data_Error =>
  20. Ada.Text_Io.Put_Line("Trying to enter a non integer value");
  21. end Demo_Robust_Programming;

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.

Problem 6 – Asymptotic Complexity – Divide and Conquer (15 points)

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 )

  1. When it comes to a stack, which of the following statements is false? a. The process of deleting an object is called Pop b. All insertions and deletions of elements take place at the same end of the data structure c. Stacks are FIFO structures

C

  1. I have put my name in the upper right corner on all pages of the quiz a. Yes b. No c. I will do it by 10am today

A/B/C