Minimum Spanning Tree-Computer Programming-Assignment Solution, Exercises of Aeronautical Engineering

This is solution to assignment for Aeronautical Engineering and Computer Programming course. It was submitted to Prof. Chitraksh Gavde at Biju Patnaik University of Technology, Rourkela. It includes: Spanning, Tree, Minimum, Prim, Parent, Fringe, Insert, Bubble, Array, Loop, Input, Output

Typology: Exercises

2011/2012

Uploaded on 07/20/2012

savitri_122
savitri_122 🇮🇳

4.6

(14)

184 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Home Work 9
The problems in this problem set cover lectures C7, C8, C9 and C10
1. What is the Minimum Spanning Tree of the graph shown below using both Prim’s and
Kruskal’s algorithm. Show all the steps in the computation of the MST (not just the final
MST).
Prim’s Algorithm
Step 1.
MST
1
1
4 52
10
45
30
Parent
Fringe Set
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Minimum Spanning Tree-Computer Programming-Assignment Solution and more Exercises Aeronautical Engineering in PDF only on Docsity!

Home Work 9

The problems in this problem set cover lectures C7, C8, C9 and C

  1. What is the Minimum Spanning Tree of the graph shown below using both Prim’s and Kruskal’s algorithm. Show all the steps in the computation of the MST (not just the final MST).

Prim’s Algorithm

Step 1.

MST

Parent

Fringe Set

Step 2

MST

Parent

Fringe Set

Step 3

MST

Parent

Fringe Set

Minimum Spanning Tree

MST

Weight of the MST = 10 + 20 + 25 + 20 + 35 = 110

Kruskals Algorithm

Initialization

1

4

5 3

6

2 10

35

50

45

30

25

55

20

20

1

4

5 3

6

2

Step 1

1

4

5 3

6

2

35

50

45

30

25

55

20

20

1

4

5 3

6

2

35

50

45

30

25

55

20

20

10

1

4

5 3

6

2 10

1

4

5 3

6

2

Step 2

1

4

5 3

6

2

35

50

45

30

25

55

20

1

4

5 3

6

2

35

50

45

30

25

55

20 10

20

1

4

5 3

6

2 10

20

1

4

5 3

6

2

  1. Compute the computation complexity of the bubble sort algorithm. Show all the steps in the computation based on the algorithm.

Algorithm Procedure Bubble_Sort(Input_Output_Array)

for I in 1 .. My_Array_Max loop for J in I+1 .. My_Array_Max loop

N N-

if (Input_Output_Array(I) <= Input_Output_Array(J)) then Temp := Input_Output_Array(I); Input_Output_Array(I) := Input_Output_Array(J); Input_Output__Array(J) := Temp; end if; end loop; end loop;

end Bubble_Sort;

O(N(N-1)) = O(N^2 )

  1. What are the best case and worst case computation complexity of:

a. Inserting a node into an unsorted singly linked list

Inserting into an unsorted singly linked list is carried out using the add_to_front operation. Both the best and worst case execution time is O(1).

b. Inserting a node into a sorted singly linked list

In the case of a sorted linked list, the list has to be traversed to find the right position. The list traversal takes O(n) in the worst case.

Best case execution time is O(1) if the element being inserted is the smallest element in the list (list in ascending order)

Worst case execution time is O(n) if the element being inserted is the largest element in the list (list in ascending order)

a. Design an Ada95 Package to: i. Read in N integers from an input file into an array. (N is user defined <=50) ii. Sort the array in ascending order iii. Perform binary search on the array.

The package is designed as follows: Data Structures:

type my_array is array (1 .. 50) of integer;

Subprograms: -- procedure to create an array with <=50 elements -- assumes that input can be found in input_file.txt -- accepts the array -- returns array with elements read from file and the number of elements read procedure Create ( Num_Array : in out My_Array; Size : out Integer );

procedure Merge ( Input_Array : in out My_Array; Lb_1 : in Integer; Ub_1 : in Integer; Lb_2 : in Integer; Ub_2 : in Integer ); -- procedure to sort the array in ascending order -- accepts the array and the lowerbound(lb) and upperbound(ub) -- returns the sorted array procedure Merge_Sort ( Num_Array : in out My_Array; Lb : in Integer; Ub : in Integer );

--function to perform binary search on the array -- accepts the array, lb, ub and the element being searched for -- returns the index of the element if it is found -- returns -1 if the element is not found function Binary_Search ( Num_Array : My_Array; Lb : Integer; Ub : Integer; Looking_For : Integer ) return Integer;

The merge sort procedure will recursively call itself until it has only single element arrays. These are inherently sorted. It will then merge the successively larger elements until the whole sorted array is produced.

Merge

Pre-Conditions: An array of 1 or more elements, Legal values of Lower_Bound_1, Upper_Bound_1, Lower_Bound_2 and Upper_Bound_2,

Post-Condition: A merged array that is sorted

Algorithm

  1. Check if a. Lower_Bound_1 < Upper_Bound_ b. Lower_Bound_2 < Upper_Bound_ c. Lower_Bound_1 < Lower_Bound_ d. Upper_Bound_1 < Upper_Bound_
  2. If any of the above conditions are violated, a. Display Error b. Stop Executing the program
  3. Set a. Index_1 := Lower_Bound_1; b. Index_2 := Lower_Bound_2; c. Index := Lower_Bound_1; d. Temp_Array := Input_Array
  4. While (Index_1 <= Upper_Bound_1) and (Index_2 < =Upper_Bound_2) a. If (Input_Array(Index_1) < Input_Array (Index_2) i. Temp_Array(Index) := Input_Array(Index_1) ii. Index_1 := Index_1 + 1; iii. Index := Index + 1; b. Else i. Temp_Array(Index) := Input_Array(Index_2) ii. Index_2 := Index_2 + 1; iii. Index := Index + 1;
  5. While (Index_1 <= Upper_Bound_1) a. Temp_Array(Index) := Input_Array(Index_1) b. Index_1 := Index_1 + 1; c. Index := Index + 1;
  6. While (Index_2 <= Upper_Bound_2) a. Temp_Array(Index) := Input_Array(Index_2) b. Index_2 := Index_2 + 1;


c. Index := Index + 1;

  1. Input_Array := Temp_Array

Binary Search

Pre-Conditions: Array to be searched Item that you are searching for

Post-condition: Index location of the item you are searching for Return –1 if the number is not found. Algorithm

  1. Set Return_Index to –1;
  2. Set Current_Index to (Upper_Bound - Lower_Bound + 1) /2.
  3. Loop i. if the lower_bound > upper_bound Exit; ii. if ( Input_Array(Current_Index) = Number_to_Search) then Return_Index = Current_Index) Exit;

iii. if ( Input_Array(Current_Index) > Number_to_Search) then Lower_Bound = Current_Index + else Upper_Bound = Current_Index – 1

  1. Return Return_Index

b. Write a program to test your package that will

  • Prompt the user for a number to search for.
  • If the number is found using the binary search algorithm
    • Display the location (index)
    • Display the number
  • If the number is not found using the binary search algorithm
    • Display “Number not in array to the user”

-- program to test Home_Work_9 Package -- Programmer: Jayakanth Srinivasan -- Date Last Modified: April 06,

with Ada.Text_Io; with Ada.Integer_Text_IO; with Home_Work_9; use Home_Work_9;

procedure Test_Hw_9 is