19
Problems to algorithms to programs Naveen Garg, IIT Delhi

Problems to algorithms to programs Naveen Garg, IIT Delhi

Embed Size (px)

Citation preview

Problems to algorithms to programs

Naveen Garg, IIT Delhi

Efficiency• How would you check if is prime?• Try to divide it by numbers . If any of these succeed then is not a

prime. • Can we do something better, which requires less divisions?• Any number more than would not divide and so we could stop at .• 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 representationsB0 B1 B2 B3 B4 B5 B6 B7 . . . B1020 B1021 B1022 B1023

S0 0 1 0 1 0 1 0 1 0 1 0 1

S1 0 0 1 1 0 0 1 1 0 0 1 1

S2 0 0 0 0 1 1 1 1 1 1 1 1

S3 0 0 0 0 0 0 0 0 1 1 1 1

S4 0 0 0 0 0 0 0 0 1 1 1 1

S5 0 0 0 0 0 0 0 0 1 1 1 1

S6 0 0 0 0 0 0 0 0 1 1 1 1

S7 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 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)

• c1 is chosen so that • c2 is chosen so that • c3 is chosen so that

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

Hamming codes (illustration)

1 0 1 1

c1 c2 m1 c3 m2 m3 m4

0

1

1

1

11

1

11

10

0

0

1

0

0 1 1 0 0 1 1

Sent Message

Hamming codes (illustration)

c1 C2 m1 c3 m2 m3 m4

0 1 1 0 1 1 1

0 1 1 0 1 1 1

0 1 1 0 1 1 1

0 1 1 0 0 1 1Sent Message

0 1 1 0 1 1 1Recd Message

X

X

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.

Apr ‘13Jan ‘13Jan ‘12

+100,10 -200,20+150,15 -100,20+200,20 -150, 25

Mar ‘12

(100,+10)

(100,+5)(50,+5)

(50,0)(150,+5)

(200,0)

The FIFO rule

• 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

Apr ‘13Jan ‘13Jan ‘12

+100,10 -200,20+150,15 -100,20+200,20 -150, 25

Mar ‘12

(100,+10)

(100,+5)(50,+5)

(50,0)(150,+5)

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.sharest.shares = 0}

else {CG += x.shares*(t.price-x.price)t.shares -= x.sharesx = 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 to 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 contains the original message and 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

for i = 1 to offset do // computing the check bit at position

for j = 1 to k do

if then

Solving system of linear equations// A[0..n-1,0..n] contains the matrix A and the 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 rowfor k = j to n do

// computing the solution vector for i=n-1 down to 0 do

t = 0for j = i+1 to n-1 do