Sei sulla pagina 1di 12

Design and Analysis

of
Algorithms
Assignment

William Stewart
18349788
Q1) a)Use the Master method to solve the following recurrence function:
T (n) = 4T ( √2 n ) + log22 n

T (n) = 4T ( √2 n ) + log 22 n

N ote : k = log 2 n
N ote : n = 2k

k
T (2k ) = 4T (2 2 ) + k 2

S (k) = T (2k )

S (k) = 3S( 2k ) + k 2

k logb a = k log2 3 = k 1.58

k 1.58 < k 2

Regularity Condition :
3( n2 ) ≤ cn2

T heref ore Case 3 : Θ(k 2 ) where k = log 2 n

1) b) . Formally prove or disprove the correctness of each of the following.


a)
(n + a) b = Θ(nb ) f or any real constants a and b, and f or a > 0, b > 0
c1 nb ≥ (n + a) b n > 0, a ≤ n c2 nb ≤ (n + a) b
c1 nb ≥ (n + n) b c2 nb ≤ 2b a b
c1 nb ≥ (2n) b c2 = 2b a b
nb
c1 = 2 b b > 0, n > 0
b
2 b ≥ 0, n > 0
(n + a) b = Θ(nb ) , b > 0, n > 1
b)
2n+1 = O(2n )
2n+1 ≤ c2n
2 2n ≤ c 2n
2≤c

2n+1 = O(2n ) n ≥0

c)
22n = O(2n )
2n c ≥ 22n
c ≥ 2n
22n = O(2n ) n ≥ 0

Q2) a)
Design an algorithm to generate a (p × q) sorted grid G from a given list L of n integers

gridify(L, p, q)
//Init
col = 0
row = 0
D[q][p]

//Add list to arrays


//No specific order, just placing in a grid
For i in L
D[col][row] = i
If col == q
col = 0
row++
col++

//Sort Cols
For i in q
mergeSort(D[i])

//Re-init
col = 0
row = 0
G[p][q]

//Shift best then next best etc numbers into their own arrays to be sorted later
For i in L
G[col][row] = D[row][col]
If col == q
col = 0
row++
col++

//Sort Rows
For i in p
mergeSort(G[i])

Return G

Analysis:
O(n + q n logn + n + p n logn)
O(2n + pq (n logn))
O(n + n (n logn))

I was unable to produce a better algorithm. The two n values came from manipulating the list
into arrays and moving the array to another array.

The p n logn and q n logn came from the uses of Merge Sort in a loop that has p and q
iterations.

I changed pq to equal n as p times q is the same as the number of elements in the grid, so
equals n.

Correctness
The algorithm is correct as the largest value is placed in the bottom right. Each iteration then is
guaranteed to have values less than the row below. As each row is sorted, all values are sorted
by row and column.
Example:
L = <20, 3, 5, 7, 31, 22, 3, 4, 10, 8, 4, 19, 1, 7, 18, 9, 2, 6, 23, 11>
1. 2. 3.
20 3 5 7 1 3 3 4 1 3 3 4

31 22 3 4 2 6 4 7 2 4 6 7

10 8 4 19 10 7 5 9 5 7 9 10

1 7 18 9 20 8 18 11 8 11 18 20

2 6 23 11 31 22 23 19 19 22 23 31

1. Insert values into q arrays (horizontally)


2. Sort values in each array (vertically)
3. Get largest value from each array from step 2 and insert into p arrays, leaving the grid
sorted.

Q2) b)
sortedGridSearch(G, p, q, num)
//Init
row = 0
col = q

//Start at the top right position in the grid


While col >= 0 && row <= p
//Move left
if(num < G[row][col])
col--
//Move down
Else if num > G[row][col]
row++
//Found element
Else
Return true
//Not found
Return false

Analysis:
Worst case scenario, the num will be the value on the bottom left or not in the grid and the
algorithm will have to traverse all the way to the left and all the way down.
Meaning the complexity will be O(p + q)
The algorithm uses 0(1) memory as it only declares two additional variables, a constant.
Correctness
The algorithm will always find the value if it exists, due to the sorted nature of the grid. All values
to the left are smaller and all values below are larger. So it moves towards the value,
automatically removing values that are too large or too small from consideration and eventually
finding it if it exits.

K=7

Starts at 4 in the top right, 7 is larger so it moves down to the next row and returns true as the
value has been found.

1 3 3 4

2 4 6 7

5 7 9 10

8 11 18 20

19 22 23 31

K = 12

Starts at the 4 in the top right, 12 is larger so it goes down, 12 is larger than 7 so it goes down,
12 is larger than 10 so it goes down, 20 is larger than 12 so it goes left, 18 is larger than 12 so it
goes left, 11 is less than 12 so it goes down, 22 is larger than 12 so it goes left, 19 is larger than
12 so it goes down and terminates the loop and returns false as 12 cannot be found in the grid.

1 3 3 4

2 4 6 7

5 7 9 10

8 11 18 20

19 22 23 31
Q3) a)
Write a pseudocode of an efficient algorithm to generate 2DP/C for a given (s, t) pair. From the
pseudocode, analyse the time complexity of the algorithm.

I used the Bhandari disjoint edge algorithm, it used bellman ford and dijkstra's algorithms to find
the two shortest disjoint edge paths.

Bhandaris algorithm was first published in ​Survivable networks: algorithms for diverse routing
(1999) b​y Bhandari, Ramesh.

bhandari(E, V)
//Get best path
bestPath = dijkstra(E, V)

For edge in bestPath


//Invert edge value
E[edge] = E[edge] x -1

//Invert the direction of the edge on the graph


invertDirection[E[edge], V]

//Use bellman ford algorithm as it allows for negative edge values


secondBestPath = bellmanFord(E, V)

//Combines the two shortest paths and remove edges used by both paths
pathUnion(bestPath, secondBestPath)

Analysis
Dijkstra's algorithm runs at O(ElogV )
Bellman Ford algorithm runs at O(|V | |E |)
Bhandari algorithm runs at a combined cost of O(ElogV + |V | |E |)
The cost of the other steps is insignificant

Correctness
The algorithm is correct, as Dijkstra's algorithm will always find the shortest path, so one path is
guaranteed to be correct. Inverting the path means that the same path cannot be used again.
Bellman Ford is also guaranteed to find the shortest path, so the two shortest paths have been
found, any edges used by both paths are removed, so both paths are guaranteed to be using
separate edges.
Examples
C TO E

Find shortest path using dijkstra's algorithm


Invert the edge weights and the direction of those edges

Using the bellman ford algorithm, find the shortest path from the graph
Combine the paths, removing any edges that are used in both paths(all other nodes/edges are
there, I just removed them for illustrative purposes, code just returns two paths)
Return the values that were inverted to their original state/value.

F TO E
Find shortest path using dijkstra's algorithm

Invert the edge weights and the direction of those edges


Using the bellman ford algorithm, find the shortest path from the graph

Combine the paths, removing any edges that are used in both paths(all other nodes/edges are
there, I just removed them for illustrative purposes, code just returns two paths)
Return the values that were inverted to their original state/value.

Potrebbero piacerti anche