Lecture PPT - IIIT
Download
Report
Transcript Lecture PPT - IIIT
Problems to algorithms to
programs
Naveen Garg, IIT Delhi
Efficiency
β’ How would you check if π is prime?
β’ Try to divide it by numbers 1,2,3,4, β¦ , π β 1 . If any of these
succeed then π is not a prime.
β’ Can we do something better, which requires less divisions?
β’ Any number more than π/2 would not divide π and so we could stop
at π/2.
β’ Even better?
β’ Claim: If π is not a prime then it has a factor less than π.
β’ Proof: if π is composite it has at least 2 factors. Both factors cannot be
larger than π as their product would exceed π.
β’ Hence we need only π divisions !!
Coding Theory
β’ Suppose I have to send a message, S.
β’ The communication channel might flip some bits of my message so
that the received message, say R, is different from S.
β’ Is there some way to recover the original message or at least to know
that the message has been corrupted.
β’ By adding additional bits to the original message we can achieve error
detection and/or correction.
β’ Efficient codes are those which can provide the necessary guarantees
(can detect/correct if upto x bits are in error) by using very few
additional bits.
King, wine and poison.
β’ A French king was fond of wine. He had a cellar stocked with
the finest wines and would often invite other nobles to taste
them.
β’ He had arranged one such wine tasting party. On the day
before the party he found out that an assassin had added a
deadly poison to one of the 1024 bottles.
β’ Even one drop of wine from the poisoned bottle is enough to
kill a person. But the poisonβs effect happens after 12 hrs.
β’ The king has some slaves who he can βuseβ to identify the
poisoned bottle. He needs to do this before the party.
β’ What is the minimum no. of slaves he needs?
Using binary representations
B0
B1
B2
B3
B4
B5
B6
B7
S0
0
1
0
1
0
1
0
S1
0
0
1
1
0
0
S2
0
0
0
0
1
S3
0
0
0
0
S4
0
0
0
S5
0
0
S6
0
S7
.
.
.
B1020 B1021
B1022
B1023
1
0
1
0
1
1
1
0
0
1
1
1
1
1
1
1
1
1
0
0
0
0
1
1
1
1
0
0
0
0
0
1
1
1
1
0
0
0
0
0
0
1
1
1
1
0
0
0
0
0
0
0
1
1
1
1
0
0
0
0
0
0
0
0
1
1
1
1
S8
0
0
0
0
0
0
0
0
1
1
1
1
S9
0
0
0
0
0
0
0
0
1
1
1
1
If (Si,Bj) is 1 then Slave i gets a drop of wine from Bottle j.
If B7 is poisoned then only slaves S0, S1, S2 die.
Hamming Codes
β’ These are very efficient one bit-error correction codes.
β’ If the message is m bits long then we add roughly log 2 π check (or
parity) bits.
β’ Each check bit, ensures that a certain subset of bits have even parity.
β’ Subsets are cleverly chosen so that one can work backwards: knowing
which check bits are βviolatedβ helps identify the bit in error.
Hamming codes (illustration)
m1
m2
m3
m4
c1
c2
m1
c3
m2
m3
m4
1
0
1
0
1
0
1
0
1
1
0
0
1
1
0
0
0
1
1
1
1
β’ c1 is chosen so that π1 β π1 β π2 β π4 = 0
β’ c2 is chosen so that π2 β π1 β π3 β π4 = 0
β’ c3 is chosen so that π3 β π2 β π3 β π4 = 0
Hamming codes (illustration)
1
c1
0
c2
m1
0
1
1
c3
1
m2
m3
m4
1
0
1
1
1
0
1
1
1
1
0
1
0
Sent Message
0
1
1
0
0
1
1
Hamming codes (illustration)
Sent Message
0
1
1
0
0
1
1
Recd Message
0
1
1
0
1
1
1
c1
C2
m1
c3
m2
m3
m4
X0
1
1
0
1
1
1
0
1
1
0
1
1
1
0
1
1
X0
1
1
1
Violated check bits form the code (c3,c2,c1) 101 which identifies the
position of the error (bit 5)
Computing Capital Gains Tax
β’ Suppose you have bought & sold HDFC stock.
β’ If a stock is held for more than 12 months, the CG is classified as
Long-term-capital-gain (LTCG) and no tax is applicable.
β’ Else it is a short-term-capital-gain (STCG) and 30% tax is applicable.
(200,0)
+100,10
+150,15
Jan β12
Mar β12
(100,+5)
(100,+10)
+200,20
-200,20
-100,20
-150, 25
Jan β13
Apr β13
(50,+5)
(50,0)
(150,+5)
The FIFO rule
+100,10
+150,15
Jan β12
Mar β12
(100,+5)
(100,+10)
+200,20
-200,20
-100,20
-150, 25
Jan β13
Apr β13
(50,+5)
(50,0)
(150,+5)
β’ The law says that the gains have to be computed by the FIFO rule.
β’ The shares that are bought first are also the first to be sold.
β’ In this example, the FIFO rule implies all gains are STCG and the tax
liability is 0.3(100*10+100*5+50*5+150*5)=750
Computing CG using FIFO
β’ The input file contains, for
each transaction, its date,
nature (sell/buy), price and
no of shares.
β’ Each Buy transaction is added
to a Queue.
β’ When we encounter a Sell we
match it to the Buy
transaction at the front of the
queue (x).
β’ CG is updated only if it is a
short term gain.
If t.type = Buy then Q.enqueue(t)
If t.type = Sell then {
while t.shares > 0 do {
If (x.shares > t.shares) then {
CG += t.shares*(t.price - x.price)
x.shares -= t.shares
t.shares = 0
}
else {
CG += x.shares*(t.price-x.price)
t.shares -= x.shares
x = Q.dequeue()
}
}
}
An example from Physics:
Computing current in a resistive circuit.
β’ You are given a network of resistors and a voltage source.
β’ The voltage is applied across two points in this circuit.
β’ The problem is to determine the current in a specific resistor.
β’ We will use Kirchoffβs laws to formulate this as a system of linear
equations.
β’ These linear equations can then be solved using Gaussian elimination.
A resistive circuit
β’ Kirchoffβs law: the total current arriving at a junction equals the
current leaving it.
β’ Ohmβs law: When a voltage V is applied across a resistance R the
current through it equals V/R.
Finding the EMI (Equated Monthly
Instalment) of your loan
β’ You borrow a certain amount of money, π (Prinicipal), from ICICI at an
interest rate π for a duration π months.
β’ ICICI tells you that you have to pay πΈ per month and that there
computation is monthly reducing balance.
β’ How do you know this is correct?
Finding EMI: Illustration
β’ Suppose P = 10,00,000, r = 12%, d= 240 and E= 20,000
β’ In month 1: interest = 1% of 10,00,000 = 10,000. The remaining EMI
amount (20,000-10,000 =) 10,000 goes into reducing the principal.
β’ So principal after month 1 is 9,90,000.
β’ So principal after month 2 is
9,90,000 β (20,000 β 1% of 9,90,000) = 9,79,9,00
Exercises
1. You are given an π-bit message. Assume this is at locations 0 to
π β 1 in an array π. Write a program to compute the Hamming
code corresponding to this message in an array π.
2. You are given an π × π system of linear equations (π equations in
π variables). Write a program to solve this system using Gaussian
elimination (the method discussed in class).
Computing Hamming code
// array π[1. . π] contains the original message and π[1. . π] will contain the sent message
offset =2;
for i = 1 to n do {
if (i + offset == 2^offset) then offset++
S[i + offset] = M[i]
}
k = n + offset
// value of offset is no. of check bits
// the π π‘β bit of number π can be computed as (π πππ 2πβ1 )πππ 2
for i = 1 to offset do // computing the π π‘β check bit at position π 2πβ1
for j = 1 to k do
if
π πππ 2πβ1 πππ 2 == 1 then π 2πβ1 = π 2πβ1 β¨S[j]
Solving system of linear equations
// A[0..n-1,0..n] contains the π × π matrix A and the π × 1 vector π of the system π΄π₯ = π
for j = 0 to n-1 do //zeroing out the entries of the ππ‘β column below the diagonal.
for i = j+1 to n-1 do // zeroing the entry π΄[π, π] and updating row π.
π=β
π΄ π,π
π΄ π,π
//the factor by which we will multiply the ππ‘β row
for k = j to n do
π΄ π, π = π΄ π, π + π β π΄[π, π]
// computing the solution vector π₯ 0. . π β 1
for i=n-1 down to 0 do
t=0
for j = i+1 to n-1 do π‘ += π₯[π] β π΄[π, π]
π₯[π] = (π΄[π, π] β π‘)/π΄[π, π]