Problems to algorithms to programs Naveen Garg IIT
Problems to algorithms to programs Naveen Garg, IIT Delhi
Efficiency •
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 B 0 B 1 B 2 B 3 B 4 B 5 B 6 B 7 S 0 0 1 0 1 0 S 1 0 0 1 1 0 0 S 2 0 0 1 S 3 0 0 S 4 0 0 0 S 5 0 0 S 6 0 S 7 . . . B 1020 B 1021 B 1022 B 1023 1 0 1 1 1 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 1 1 S 8 0 0 0 0 1 1 S 9 0 0 0 0 1 1 If (Si, Bj) is 1 then Slave i gets a drop of wine from Bottle j. If B 7 is poisoned then only slaves S 0, S 1, S 2 die.
Hamming Codes •
Hamming codes (illustration) m 1 • m 2 m 3 m 4 c 1 c 2 m 1 c 3 m 2 m 3 m 4 1 0 1 0 1 1 0 0 0 1 1
Hamming codes (illustration) 1 c 1 0 c 2 m 1 0 1 1 c 3 1 m 2 m 3 m 4 1 0 1 1 1 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 1 c 1 C 2 m 1 c 3 m 2 m 3 m 4 X 0 1 1 1 0 1 1 X 0 1 1 1 Violated check bits form the code (c 3, c 2, c 1) 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 Longterm-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 -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 -100, 20 -150, 25 Jan ‘ 13 Apr ‘ 13 (50, +5) (50, 0) • 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 (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 { else { } } CG += t. shares*(t. price - x. price) x. shares -= t. shares = 0 } 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 •
Finding EMI: Illustration • Suppose P = 10, 000, r = 12%, d= 240 and E= 20, 000 • In month 1: interest = 1% of 10, 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 •
Computing Hamming code •
Solving system of linear equations •
- Slides: 19