You are on page 1of 5

* Now talking on #nitkieee

* Topic for #nitkieee is: Hangout #3 NITK IEEE Summer Project


* Topic for #nitkieee set by ashish1294!67157f4e@gateway/web/freenode/ip.103.21.
127.78 at Sat May 24 08:12:45 2014
<ashish1294> we will start at 9:10
* ankit_deepak_ (0e63d948@gateway/web/freenode/ip.14.99.217.72) has joined #nitk
ieee
* shravya (31f93ede@gateway/web/freenode/ip.49.249.62.222) has joined #nitkieee
* ankit_deepak has quit (Ping timeout: 240 seconds)
* abhishek has quit (Client Quit)
* kavya (056c615a@gateway/web/freenode/ip.5.108.97.90) has joined #nitkieee
* gp007 (7aa69f0f@gateway/web/freenode/ip.122.166.159.15) has joined #nitkieee
<kavya> hey
<kavya> is it at 9 or 9:30?
* abhishek_d (dfe31003@gateway/web/freenode/ip.223.227.16.3) has joined #nitkiee
e
* Shivani_ (6da1918c@gateway/web/freenode/ip.109.161.145.140) has joined #nitkie
ee
<ashish1294> just give me 5 mins please
* vadi_ (dfb2dea6@gateway/web/freenode/ip.223.178.222.166) has joined #nitkieee
* ashish (75c36eff@gateway/web/freenode/ip.117.195.110.255) has joined #nitkieee
* medhz (~medhini@122.171.75.177) has joined #nitkieee
* surya176 (3d031d44@gateway/web/freenode/ip.61.3.29.68) has joined #nitkieee
<ashish1294> Yeah so lets's start
* ankit_tripathi (75c043a8@gateway/web/freenode/ip.117.192.67.168) has joined #n
itkieee
* abhi95 (75c0a889@gateway/web/freenode/ip.117.192.168.137) has joined #nitkieee
* varunkumar (~varunkuma@117.192.98.222) has joined #nitkieee
* Aish (75c091ec@gateway/web/freenode/ip.117.192.145.236) has joined #nitkieee
<Aish> hi
<ashish1294> hello
* poorva (7ab36bd4@gateway/web/freenode/ip.122.179.107.212) has joined #nitkieee
* karthik (2a683f81@gateway/web/freenode/ip.42.104.63.129) has joined #nitkieee
<ashish1294> first of all, I hope everyone who submitted their codes are clear a
bout their mistakes
<ashish1294> For, Today's hangout we do not have a guest lecture. :(
<Shivani_> i m not clear about mine
<Shivani_> theres something wrong with my mergesort and invesioncount code
<ashish1294> @shivani_ : I dont remember looking at your code !
<Shivani_> i sent it recently!
<ashish1294> Oh ok. got it! I have now made a filter for u ppl.
<ashish1294> So, all ur mails will go directly to my thrash
<ashish1294> post in group only
<Shivani_> oh ok
* Abraham (75c0ecc3@gateway/web/freenode/ip.117.192.236.195) has joined #nitkiee
e
<ashish1294> @shivani I will look at it and let u know after the chat
<Shivani_> thx:)
<ashish1294> Most of you made common errors !
<ashish1294> So, avoid all those in future
<ashish1294> I hope all of you had great results :D
<ashish1294> Now, Fibinacci Numbers using Dynamic Programming
<ashish1294> *Fibonacci
<ashish1294> So, read this problem here : http://www.codechef.com/COOK40/problem
s/AMIFIB/
<ashish1294> This problem appeared in Cook40 of Codechef. Question set by Vineet
Paliwal
<ashish1294> So anyone with a solution ?
<ashish1294> Do not look at other's solutions! :/
<medhz> optimize DP using matrix multiplication
<ashish1294> matrix ?
<medhz> yeah a 2D matrix
<medhz> {{1,1}{1,0}}
<ashish1294> Oh Okay! That Method, gives a particular fib in O(log n)
<medhz> yeah
<ashish1294> So how will you check wheter a number is fibonacci
<ashish1294> ?
<ashish1294> The best you can do using this method is similar to binary search -
O(logn *logn)
<medhz> okay that gives only the nth fib number...
<ashish1294> for each test case
<ashish1294> plus since the number of digits in 1000, this will have large const
ant
<ashish1294> But the fact that you read about that method is really good :D
<ashish1294> (y)
<medhz> Thanks !
<ashish1294> anyone else
<vadi_> whats the maximum value a big integer can hold?
<ashish1294> Big Integer is arbitrary precision
* Sanjeev_ (0e63da87@gateway/web/freenode/ip.14.99.218.135) has joined #nitkieee
<ashish1294> can hold billions on digit theoretically
<ashish1294> It is evident that Java's BigInteger or python is choice here!
<vadi_> oh ok if we create an array of big ints will it overflow memory limit?
<ashish1294> Handling such large string is really difficult in C++!
* karthik has quit (Ping timeout: 240 seconds)
<gp007> i think the 10 power 1000th fib number is around 5000
<ashish1294> @vadi_ : No the array will not overflow before 10^7 elements or so!
<gp007> there wont be that many fib numbers so you can just store everything in
a 2d array with 1000 columns
<vadi_> nice
<gp007> add the previous 2 rows to get that row
<gp007> run that loop till you generate a 1000 digit number
<ashish1294> @gp007: Right. That's the way to do it in C++!
<ashish1294> or C!
<gp007> yeah
<ashish1294> Or instead of storing each digit in a separate block, you can store
the whole number as a string!
<gp007> yeah better
<ashish1294> It will be faster, since anyway you do not need random access to ea
ch element, you only need whole row at a time!
<gp007> yeah
<ashish1294> Python's int and Java's BigInteger does exactly that!
<gp007> oh nice
<ashish1294> BigInteger stores the number as a character string internally and p
rovides you with wrapper functions!
<ashish1294> Now lets, lets boil down the problem - We have to find 1000 Fib Nu
mbers!
<ashish1294> You people must have done a C prog for fib number in 1st year!
<ashish1294> Try running it, to give first 1000 numbers!
<ashish1294> :/
<ashish1294> Most probably you wont get the answer. Because the recursive / iter
ative code has a complexity of O(1.68^n)
<ashish1294> so for n=1000, complexity is too high for this century at least !
<ashish1294> So here we use a simple dp. We declare an array of 1000 elements sa
y Arr, such that Arr[i] is the ith fib number
<ashish1294> So we can say that Arr[0] = 0, Arr[1] = 1, and Arr[i] = Arr[i-1] +
Arr[i-2] for i>=2
<ashish1294> We can run this in a loop for (i = 2; i <= 1000 ; i++)
<ashish1294> This is exactly what I and most others did in their solution!
<gp007> (y)
<ashish1294> This is the simplest example of DP!
<Sanjeev_> +y r we storing all the numbers?
<ashish1294> @sanjeev: See every time to calculate ith fib no, we need to have (
i-1)th and (i-2)th fib number
<Sanjeev_> ok
<Sanjeev_> but y all other numbers?
<pranav> so after dis v need to search fr the input num?
<ashish1294> So for calculating f(1000) we need, f(999) and f(998) again to calc
ulate f(999) we need f(998) and f(997) and so on.....
<Sanjeev_> y uing recursion?
<Sanjeev_> *using
<ashish1294> Yeah, after this the numbers will obviously be sorted. so use bin s
earch
<ashish1294> Yeah recursion we keep calculating previous numbers. Plus suppose w
e calculated F(1000) for the first case. then we already calculated F(999) and F
(998). So if the 2nd test is 998, we need not calculate again
<ashish1294> We simply return Arr[998]
<ashish1294> SO before any test case we calculate all 1000 fib numbers!
<ashish1294> and then simply bin search!
<ashish1294> So total complexity is O( n + (T*log n)) where, T= no.. of test cas
e!
<ashish1294> I dont think any one of you have read about these 4 progs before !
:(
<pranav> if recursion is not used it will b exponential rite?
<medhz> yeah red first 3
<medhz> read*
<Aish> for lesser test cases, can we not run a while loop for every number?
<ashish1294> @pranav : Even in recursion u can use dp!
<ashish1294> @Aish:we can for small test case only. When n<40 or so
<Sanjeev_> if no recursion, it is linear right?
<pranav> by checking if dat value is aldready computed or not?
<pranav> nd den make a recursive call
<ashish1294> Recursion wont affect complexity !
<ashish1294> :/
<ashish1294> Depends on how you are using recursion !
<Aish> .
<ashish1294> Take a look at this : http://ideone.com/QQsjHB
<ashish1294> A recursive function, but still linear
<ashish1294> I have also given naive recursion
<ashish1294> Can u understand ?
<medhz> yes
<gp007> yesh
<chinmay_dd> Yup.
<Aish> yes
<ankit_deepak_> ya
<pranav> yeah
<Sanjeev_> s
<ashish1294> Great :D
<ashish1294> Now the LCS Problem !
<ashish1294> What is the complexity of naive algo ?
<medhz> n^2?
<ashish> O(MN), M and N are sizes of sequences
<Sanjeev_> 2^n?
<ashish1294> @sanjeev : Right :)
<ashish1294> :D
<ashish1294> a) 2^avg(n,m) b) 2^(n+m) c) 2^max(n, m) d) 2^min(n,m)
<ashish1294> Which one ?
<ashish> c
<vadi_> d
<gp007> d
<medhz> d
<Sanjeev_> d
<chinmay_dd> d
<shravya> d
<Abraham> d
<ankit_deepak_> d
<ankit_tripathi> d
<abhishek_d> d
<ashish1294> Yeah. It's d!
<ashish1294> So complexity of DP algo is NM
<ashish1294> Now, tell me the Space COmplexity
<ashish1294> i.e., Memory Complexity
* karthik (75e6f13a@gateway/web/freenode/ip.117.230.241.58) has joined #nitkieee
<vadi_> O(nm)
<Sanjeev_> NM
<vadi_> theta
<ashish1294> There was a LCS prob on SPOJ whose link I cannot find, but it requi
red not only DP, but DP with Space optimization
<ashish1294> Yup. Theta(nm)
<ashish1294> Can anyone tell me a method to reduce space complexity without comp
romising time complexity
<vadi_> We only need to keep track of ith and i-1th states so we can reduce it t
o 2*min(n,m)
<ashish1294> @all : you get this question right ?
<chinmay_dd> nope.
* kavya has quit (Quit: Page closed)
<ashish1294> Participation in hangout is decreasing exponentially and I do not k
now an algorithm for it :/
<ashish1294> :(
<ashish1294> @chinmay : you understand what a space complexity is right ?
<ashish1294> Similar to time complexity !
<Sanjeev_> yes
<chinmay_dd> yeah.the amount of space occupied with respect to the input?
<ashish1294> Yeah!
<ashish1294> So what is is the space complexity for this DP algo ?
<ashish1294> You declare a N*M array. and that is the only space required!
<ashish1294> So it is Theta(nm)
<chinmay_dd> Ah ok!
<medhz> we can do what vadi said...
<medhz> since we need just i and i-1th term
<ashish1294> now, See the recursive function = Arr[i][j] = Arr[i-1][j-1] + 1, or
Arr[i][j]= max(Arr[i-1][j], Arr[j-1][i])
<ashish1294> Yeah @vadi_ is right!
<ashish1294> We can do that!
<gp007> how to implement ot but
<gp007> it*
<ashish1294> Check out this : http://www.ics.uci.edu/~eppstein/161/960229.html
<ashish1294> Nice explanation !
<gp007> ok got it
<ashish1294> After every iteration of row, we swap the pointers!
<ashish1294> So that will require 2*min(n,m)
<ashish1294> Can we do better than this ?
<ashish1294> I mean better than 2*min(n,m)
<ashish1294> Indeed we can. We can do it using just one array and one variable.
So its Theta(min(n,m))
<gp007> yeah
<vadi_> How?
<ashish1294> See from (i-1)th state you only need one variable
<ashish1294> why keep whole array just keep one variable
<vadi_> no we need all values of the state right
<vadi_> because for ith state we arr[i-1][j] for all j
<ashish1294> Yeah we need all values, but we can keep updating the variable as w
e go
<vadi_> oh yeah!
<vadi_> nice
<gp007> can you explain again?
<ashish1294> Wait I will code it
<ashish1294> Here : http://ideone.com/QQsjHB
<ashish1294> @all: I hope you all get it !
<ashish1294> :)
<ashish1294> A similar question was asked in the End Semester Lab Exam of 4th Se
mester, IT branch !
<ashish1294> This problem is there in cormen
<medhz> so we return B[n] right?
<ashish1294> Yeah !
<medhz> (y)
<ashish1294> So you are actually learning it 2 semester ahead :-)
<gp007> can you explain the else part of your code?
<gp007> ok got it leave
* vadi_ has quit (Ping timeout: 240 seconds)
<ashish1294> in else the normal method is = B[i][j] = max(B[i-1][j], B[i][j-1])
<medhz> yeah
<ashish1294> So, Here the B[i-1][j] is B[j] itself
<ashish1294> Because B[j] is the old state now!
<ashish1294> and B[i][j-1] is B[j-1] because B[j-1] is in updated state
<ashish1294> 3rd Prob is Knapsack
<ashish1294> There is no known special optimization in this problem
<ashish1294> The basic algo is fine !
<ashish1294> Only thing is to deal with certain variations. Sometimes it is diff
icult to identify that the problem is knapsack
<ashish1294> We will come back to knapsack next week when we discuss greedy !
<gp007> ok
* vadi_ (dfb2dea6@gateway/web/freenode/ip.223.178.222.166) has joined #nitkieee
<medhz> okay!
<ashish1294> Last Prob is subset Problem !
<ashish1294> So, tell me what is pseudo linear time ?
<ashish> n*W, where W is a constraint/limit?
<ashish> i.e depends on the constraint
<ashish1294> W is max weight allowed
<ashish> yeah
<ashish> or in this case the sum needed
* varsha (2a683f0a@gateway/web/freenode/ip.42.104.63.10) has joined #nitkieee
<shravya> yes o(n*sum)
<varsha> .
<ashish1294> Ok I have some interesting tricks for subset sum
<ashish1294> but it cannot be discussed without the knowledge of advance concept
s
<ashish1294> So I will again discuss is later.
<varsha> .
<ashish1294> So We are done for today!
<varsha> can u please post the chat....I am sorry...was not able to connect till
now
<pranav> i dint understand optimization of lcs
<ashish1294> LCS !
<ashish1294> @pranav : read the code and link !
<ashish1294> It may be difficult to understand in one go!

You might also like