COMP9123 Nehal A4
COMP9123 Nehal A4
COMP9123
Assignment 4
Table of Contents
Problem 1...................................................................................................................................2
Problem 2.................................................................................................................................11
Problem 3.................................................................................................................................12
Problem 4.................................................................................................................................15
1
Problem 1
Since, its undirected graph so we can move any where without any problem of direction.
Initially, for all other vertex lets assume all are unknown and hence distance is infinite.
As of now, we will visit the vertex with smallest distance and then the rest. Si we will go to A
from B
Now, from A we can move to E which is the only lowest non visited
2
FROM B
B 0
A 3 B
E 15 A
On calculating, the cost of visiting F from B to A to E to F will be higher than visiting from B
to F directly. Also, visiting B to E via A is costlier than B to F to E.
3
Now from G the next unvisited with minimum distance is H
4
C 12 D
But overall distance to C from D is much higher than directly from B to C. Else all remains
same.
C) Prim’s Algorithm. The solution has been made using pen and paper and images of the
working has been done.
5
6
The final output has been shown above as final graph.
7
D) The MST cost for the Prim’s algorithm is 3+4+1+2+5+6+7 = 28
E) Kruskal’s Algorithm
E to F cost 1
E to G cost 2
A to B cost 3
B to F cost 4
G to H cost 5
G to C cost 6
H to D cost 7
8
9
10
Problem 2
A) The set of items which can be taken have been calculated using the benefit ratio weight.
We have max W of 21.
Considered:
11
Problem 3
A) Note: All the algorithm has been defined using Python Coding Language.
lower = []
higher = []
higher.append(0)
lower.append(0)
lower.append(i)
higher.append(i)
higher.append(array_size-1)
lower.append(array_size-1)
else:
12
# Driver Code
if __name__ == '__main__':
array_size = input()
array = input() []
LocalMinima(array_size, array)
In this, we have passed the array and its size to the function defined and then compared with
different conditions inside the for loop and find the local minimum value and printed the
value.
B) Better Algorithm
return middle_index
array_size = len(array)
LocalMinimum(array, array_size)
13
In this we have passed the array to the function and conditions has been given and split has
been done to make the recursion call
Length of array = n
Hence, n/2k = 1
Or, n = 2k
Log2 n = log2 2k
Hence, k = log2(n)
14
Problem 4
For i in range(m)
If A1 empty:
continue
Else if:
Check condition of hate in A1[i] among all other member if element of hate together
Store x in A2
continue
Else:
Store x in A1
continue
print(A1)
print(A2)
There are two loop condition, first for m cases, which is O(m) and another loop for n size
array, which has time complexity of O(n). Hence, total algorithm has O(m+n) complexity.
C) Correctness of Algorithm.
15
Hence, after solving, A1 will get [1,3,5,7,11] and A2 will get [4,8,9,2,6]
16