0% found this document useful (0 votes)
57 views3 pages

CS203 Assign

This document contains the solutions to 5 problems related to algorithms and data structures. Problem 1 involves arranging functions by complexity order and is solved by comparing the complexities. Problem 2 determines if one function is O, Ω, or θ of another based on upper and lower bounds. Problem 3 solves recurrences using the master theorem and determines upper bounds. Problem 4 applies 2-coloring using backtracking to check if a graph's vertices can be colored with 2 colors without adjacent vertices having the same color, with time complexity O(2v). Problem 5 finds the minimum number of digit changes between two prime numbers using BFS on a graph of prime numbers.

Uploaded by

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

CS203 Assign

This document contains the solutions to 5 problems related to algorithms and data structures. Problem 1 involves arranging functions by complexity order and is solved by comparing the complexities. Problem 2 determines if one function is O, Ω, or θ of another based on upper and lower bounds. Problem 3 solves recurrences using the master theorem and determines upper bounds. Problem 4 applies 2-coloring using backtracking to check if a graph's vertices can be colored with 2 colors without adjacent vertices having the same color, with time complexity O(2v). Problem 5 finds the minimum number of digit changes between two prime numbers using BFS on a graph of prime numbers.

Uploaded by

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

CS203

Name:Prasang Baria ID:202052332


Assignment
1. Arrange the following functions in increasing order of their
complexities:
(a) 1.000001n, 310000000, n√n, n5,3n/2
(b) n, √n, n1.5, n2, nlogn, nloglogn, nlog2n, nlog(n2), 2/n, 2n, 2n/2, 37,
n2logn, n3

(a) 310000000 < n5 <1.000001n < 3n/2 < n√n.


(b) 2/n < 37 < √n < n < nloglogn < nlog(n2) < nlog2n < n1.5 < n2 < n2logn
< n3 < 2n/2 < 2n .
2.In each of the following situations, indicate whether f = O(g) or f = Ω(g)
or both (in which case f = θ(g))
(a) f(n) = n − 100 , g(n) = n – 200
f(n) can be both upper bound and lower bound of g(n) therefore its f =
θ(g)
(b)f(n) = 100n + logn , g(n) = n + (logn)2
Again, f(n) can be both upper bound and lower bound of g(n) therefore its
f = θ(g)
(c) f(n) = n1.5, g(n) = nlog2n
Again f = θ(g) . Since both upper bond and lower bond is possible.
(d)f(n) = log2n, g(n) = log3n
Again f = θ(g).
(e) f(n) = n!, g(n) = 2n

n! will always be greater than or equal to 2n


Therefore, its lower bound or f = Ω(g)

3. Solve the following recurrences and give upper bounds.


(a) 4T (n/2) + n2logn

T(n) = 4T(n/2) + n2logn


Applying master theorem
We get a = 4, b = 2, k =2 and p = 1.
Now, logba = k = 2
And p> -1
Therefore TC = O(n2log2n)

(b) 8T (n/2) + nlogn

T(n) = 8T(n/2) + nlogn


Applying master theorem
We get a = 8, b = 2, k = 1 and p =1
Now logba > k
Therefore TC = O(nlogba) = O(n3)

(c) 2T (√n) + n

T(n) = 2T(√n) + n
Let n = 2m -> n1/2 = 2m/2
 T(2m) = T(2m/2) + 2m
 S(m) = S(m/2) + m
 a = 1 , b = 2 , k =1 and p = 0
Applying master theorem
 We get TC = O(nk log0n)
 TC = O(n).

4.Given a graph G, check whether can we colour the vertices of a graph


G using two colours such that no two adjacent vertices have the same
colour. Which algorithm will you apply for this problem and what
would be the complexity?
We can use m colouring problem set in this we use backtracking and can
set the value of m to 2.
In this we keep on traversing through the nodes of graph and keep
checking , if the adjacent vertices have different color as the problem
required if they do then keep the traversal continue else backtrack and try
for different combination of the colouring.
At the end if there is no right solution then return false else return true as
we have found a solution.
The time complexity of the m colouring problem is O(mv) and here m is
equals to 2 therefore its complexity will be O(2v).

5 Problem
Given two prime numbers, P1 and P2 , your task is to reach from P1 to P2 by
changing only one digit at a time and also the number you get after changing a digit
must also be a prime. For example suppose that P1 is 1033 and P2 is 8179 (both
primes), we can reach from P1 to P2 in following way :-
1033− > 1733− > 3733− > 3739− > 3779− > 8779− > 8179.
Solution:
1. Find all N digit prime numbers and make a graph with these numbers.
2. Consider each prime number as a node of a graph and create an edge from one
node to another if they differ by a single digit.
3. Apply BFS traversal and find out the number of edges between A and B.
4. If no path exists, print -1.
5. Else print the no. of edges, which is the required solution.
6. Time Complexity: O(102N)
Auxiliary Space Complexity: O(10

You might also like