0% found this document useful (0 votes)
52 views17 pages

COMP9123 Nehal A4

Australia College
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views17 pages

COMP9123 Nehal A4

Australia College
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Student ID: 510390888

COMP9123

Assignment 4
Table of Contents
Problem 1...................................................................................................................................2

Problem 2.................................................................................................................................11

Problem 3.................................................................................................................................12

Problem 4.................................................................................................................................15

1
Problem 1

A) Dijkstra’s algorithm assuming start point is B.

Since, its undirected graph so we can move any where without any problem of direction.

Considering Distance to B from B vertex is 0

Initially, for all other vertex lets assume all are unknown and hence distance is infinite.

The table below will show the entries one by one.

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0

As of now, we will visit the vertex with smallest distance and then the rest. Si we will go to A
from B

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B

Now, from A we can move to E which is the only lowest non visited

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX

2
FROM B
B 0
A 3 B
E 15 A

Now from E, we will go to F which is lowest unvisited.

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
E 5 F
F 4 B

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.

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
F 4 B
E 1 F

From F, we can go to E and then go to G

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
F 4 B
E 1 F
G 2 E

3
Now from G the next unvisited with minimum distance is H

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
F 4 B
E 1 F
G 2 E
H 5 G

From H we can go to D which is unvisited with low cost

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
F 4 B
E 1 F
G 2 E
H 5 G
D 7 H
Lastly, from D to C we have last unvisited node.

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
F 4 B
E 1 F
G 2 E
H 5 G
D 7 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.

VERTEX SHORTEST DISTANCE PREVIOUS VERTEX


FROM B
B 0
A 3 B
F 4 B
E 1 F
G 2 E
H 5 G
D 7 H
C 9 B

The final output has been shown below

B) The shortest path cost from B to G is 7, which is B to F to E to G

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

The order is:

 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:

ITEM NUMBER WEIGHT BENEFITS WEIGHT LEFT


Item 2 4 8 17
Item 7 5 8 12
Item 4 12 11.077 0

Total Benefit received = 27.077

B) List of items based on greed.

ITEM NUMBER WEIGHT BENEFITS Benefit/ Weight


Item 1 17 6 0.353
Item 2 4 8 2
Item 3 7 5 0.714
Item 4 13 12 0.923
Item 5 25 6 0.24
Item 6 19 3 0.158
Item 7 5 8 1.6
Item 8 13 2 0.154
Item 9 26 7 0.269

11
Problem 3

A) Note: All the algorithm has been defined using Python Coding Language.

def LocalMinima(array_size, array):

lower = []

higher = []

if(array[0] > array[1]):

higher.append(0)

elif(array[0] < array[1]):

lower.append(0)

for i in range(1, array_size-2):

if(array[i-1] > array[i] < array[i + 1]):

lower.append(i)

elif(array[i-1] < array[i] > array[i + 1]):

higher.append(i)

if(array[-1] > array[-2]):

higher.append(array_size-1)

elif(array[-1] < array[-2]):

lower.append(array_size-1)

if(len(lower) > 0):

print("Points of Local minima are : ", lower)

else:

print("There are no points of Local minima.")

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

def LocalMinimum_Rcurrsion(array, lower_index, upper_index, array_size):

middle_index = lower_index + int (upper_index - lower_index) / 2)

if (middle_index is 0 or array[middle_index - 1] > array[middle_index] and


middle_index is array_size - 1 or array[middle_index] < array[middle_index + 1]):

return middle_index

elif (middle_index > 0 and array[middle_index - 1] < array[middle_index]):

return LocalMinimum_Rcurrsion(array, lower_index, middle_index - 1,


array_size)

return LocalMinimum_Rcurrsion(array, middle_index + 1, upper_index, array_size)

def LocalMinimum(array, array_size):

return LocalMinimum_Rcurrsion(array, 0, array_size - 1, array_size)

array = input values []

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

C) Analysing the complexity of the Algorithm B.

Since the used algorithm has recursion,

Length of array = n

Length of array after 1st iteration = n/2

After total k iterations Length of array = n/2k

Suppose, after k iterations the length of array = 1

Hence, n/2k = 1

Or, n = 2k

Applying log both sides,

Log2 n = log2 2k

Hence, k = log2(n)

Hence, Time Complexity of algorithm = O(Log2(n))

14
Problem 4

A) Input size of people = n, Input condition list = m

Declared 2 Arrays = A1[], A2[]

For i in range(m)

Split list i and j

For x in range(0, length(A1))

If A1 empty:

Store x in A1, no checking required

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)

B) Time complexity of Algorithm

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.

Checking based on 10 = n, given values [1,2,3,4,5,6,7,8,9,11] m = 5 conditions, 1 hates 4, 5


hates 8, 8 hates 3, 9 hates 7, 7 hates 2, 6 hates 11 and 6 hates 5

15
Hence, after solving, A1 will get [1,3,5,7,11] and A2 will get [4,8,9,2,6]

16

You might also like