## Thursday, December 31, 2009

### Happy New Year....

# Happy new year, wishing you all a very very happy 2010...

# Let 2009 go away with all your sorrows and mistakes...

### Dijkstra's Algorithm in C++

### Dijkstra's Algorithm

Dijkstra's algorithm is a single source shortest path (sssp) algorithm. Like BFS, this famous graph searching algorithm is widely used in programming and problem solving, generally used to determine shortest tour in a weighted graph. This algorithm is almost similar to standard BFS, but instead of using a Queue data structure, it uses a heap like data structure or a priority queue to maintain the weight order of nodes. Here is the algorithm and pseudo-code. You can also look into __Introduction to Algorithms (by C.L.R.S)__.

Here is a short implementation of this algorithm in C++, I assumed that, all the edge-weights are positive, and the max possible distance is less than 2^{20} which is set as INF. The nodes are marked from 1 to N inclusive where N is the number of nodes.

#### Input format:

N E // number of nodes and edges E lines containing an edge (u, v, w) on each line // edge(u, v, w) means weight of edge u-v is w. Nodes u, v are within range 1 to N S // starting node

#### C++ code:

#include <cstdio> #include <queue> #include <vector> using namespace std; #define MAX 100001 #define INF (1<<20) #define pii pair< int, int > #define pb(x) push_back(x) struct comp { bool operator() (const pii &a, const pii &b) { return a.second > b.second; } }; priority_queue< pii, vector< pii >, comp > Q; vector< pii > G[MAX]; int D[MAX]; bool F[MAX]; int main() { int i, u, v, w, sz, nodes, edges, starting; // create graph scanf("%d %d", &nodes, &edges); for(i=0; i<edges; i++) { scanf("%d %d %d", &u, &v, &w); G[u].pb(pii(v, w)); G[v].pb(pii(u, w)); // for undirected } scanf("%d", &starting); // initialize distance vector for(i=1; i<=nodes; i++) D[i] = INF; D[starting] = 0; Q.push(pii(starting, 0)); // dijkstra while(!Q.empty()) { u = Q.top().first; Q.pop(); if(F[u]) continue; sz = G[u].size(); for(i=0; i<sz; i++) { v = G[u][i].first; w = G[u][i].second; if(!F[v] && D[u]+w < D[v]) { D[v] = D[u] + w; Q.push(pii(v, D[v])); } } F[u] = 1; // done with u } // result for(i=1; i<=nodes; i++) printf("Node %d, min weight = %d\n", i, D[i]); return 0; }

This is a straight forward implementation, according to the problem solving purpose, changes are to be made here and there.

Here is another implementation that does not use a custom comparison structure and the code is a bit more re-usable too. Note, that, nodes are numbered from 1 to n, pair for priority queue elements are assumed to be (weight, node) where pair for graph edges are assumed to be (node, weight). Also I have added comments to make the code more readable. Visit this codepad url for uncommented version of the code.

#include <cstring> #include <cstdio> #include <vector> #include <queue> using namespace std; typedef pair< int, int > pii; /* Set MAX according to the number of nodes in the graph. Remember, nodes are numbered from 1 to N. Set INF according to what is the maximum possible shortest path length going to be in the graph. This value should match with the default values for d[] array. */ const int MAX = 1024; const int INF = 0x3f3f3f3f; /* pair object for graph is assumed to be (node, weight). d[] array holds the shortest path from the source. It contains INF if not reachable from the source. */ vector< pii > G[MAX]; int d[MAX]; /* The dijkstra routine. You can send a target node too along with the start node. */ void dijkstra(int start) { int u, v, i, c, w; /* Instead of a custom comparator struct or class, we can use the default comparator class greater<T> defined in quque.h */ priority_queue< pii, vector< pii >, greater< pii > > Q; /* Reset the distance array and set INF as initial value. The source node will have weight 0. We push (0, start) in the priority queue as well that denotes start node has 0 weight. */ memset(d, 0x3f, sizeof d); Q.push(pii(0, start)); d[start] = 0; /* As long as queue is not empty, check each adjacent node of u */ while(!Q.empty()) { u = Q.top().second; // node c = Q.top().first; // node cost so far Q.pop(); // remove the top item. /* We have discarded the visit array as we do not need it. If d[u] has already a better value than the currently popped node from queue, discard the operation on this node. */ if(d[u] < c) continue; /* In case you have a target node, check if u == target node. If yes you can early return d[u] at this point. */ /* Traverse the adjacent nodes of u. Remember, for the graph,, the pair is assumed to be (node, weight). Can be done as you like of course. */ for(i = 0; i < G[u].size(); i++) { v = G[u][i].first; // node w = G[u][i].second; // edge weight /* Relax only if it improves the already computed shortest path weight. */ if(d[v] > d[u] + w) { d[v] = d[u] + w; Q.push(pii(d[v], v)); } } } } int main() { int n, e, i, u, v, w, start; /* Read a graph with n nodes and e edges. */ while(scanf("%d %d", &n, &e) == 2) { /* Reset the graph */ for(i = 1; i <= n; i++) G[i].clear(); /* Read all the edges. u to v with cost w */ for(i = 0; i < e; i++) { scanf("%d %d %d", &u, &v, &w); G[u].push_back(pii(v, w)); G[v].push_back(pii(u, w)); // only if bi-directional } /* For a start node call dijkstra. */ scanf("%d", &start); dijkstra(start); /* Output the shortest paths from the start node. */ printf("Shortest path from node %d:\n", start); for(i = 1; i <= n; i++) { if(i == start) continue; if(d[i] >= INF) printf("\t to node %d: unreachable\n", i); else printf("\t to node %d: %d\n", i, d[i]); } } return 0; }

Dijkstra is a single source shortest path problem. So if you want to see the shortest path from every node, you have to call dijkstra once for each source or starting node.

## Tuesday, December 22, 2009

### Linked List in C

### Linked List in C (covers insert, delete, sort, search, print)

Here it goes:

#include <stdio.h>

#include <stdlib.h>

typedef struct linked_list {

int val;

struct linked_list *next;

} LL;

LL *head, *tail;

void tail_insert(int v)

{

if(head==NULL)

{

head = (LL *)malloc(sizeof(LL));

head->val = v;

head->next = NULL;

tail = head;

}

else

{

LL *temp = (LL *)malloc(sizeof(LL));

temp->val = v;

temp->next = NULL;

tail->next = temp;

tail = tail->next;

}

}

void head_insert(int v)

{

if(head==NULL)

{

head = (LL *)malloc(sizeof(LL));

head->val = v;

head->next = NULL;

tail = head;

}

else

{

LL *temp = (LL *)malloc(sizeof(LL));

temp->val = v;

temp->next = head;

head = temp;

}

}

void insert_before(int v, int n) // insert v before every n

{

LL *curr, *temp;

if(head!=NULL)

{

if(head->val==n)

{

temp = (LL *)malloc(sizeof(LL));

temp->val = v;

temp->next = head;

head = temp;

curr = head->next;

}

else curr = head;

while(curr->next!=NULL)

{

if(curr->next->val==n)

{

temp = (LL *)malloc(sizeof(LL));

temp->val = v;

temp->next = curr->next;

curr->next = temp;

curr = curr->next;

}

curr = curr->next;

}

}

}

void insert_after(int v, int n) // insert v after every n

{

LL *curr, *temp;

if(head!=NULL)

{

curr = head;

while(curr!=NULL)

{

if(curr->val==n)

{

temp = (LL *)malloc(sizeof(LL));

temp->val = v;

temp->next = curr->next;

if(curr->next==NULL) tail = temp;

curr->next = temp;

curr = curr->next;

}

curr = curr->next;

}

}

}

void sort_linked_list()

{

LL *i, *j, *k;

int temp;

k = tail;

for(i=head; i!=tail; i=i->next)

{

for(j=head; ;j=j->next)

{

if(j->val > j->next->val)

{

temp = j->val;

j->val = j->next->val;

j->next->val = temp;

}

if(j->next==k)

{

k = j;

break;

}

}

}

}

void delete_all(int v) // deletes every v

{

LL *temp, *curr = head;

while(head!=NULL && head->val==v)

{

temp = head;

head = head->next;

free(temp);

curr = head;

}

if(curr==NULL) return;

while(curr->next!=NULL)

{

if(curr->next->val==v)

{

temp = curr->next;

curr->next = curr->next->next;

free(temp);

if(curr->next==NULL) tail = curr;

}

else curr = curr->next;

}

}

int search_linked_list(int v)

{

LL *curr = head;

int n = 0;

while(curr!=NULL)

{

if(curr->val==v) return n;

n++;

curr = curr->next;

}

return -1;

}

void print_linked_list()

{

LL *curr = head;

while(curr!=NULL)

{

printf("%d ", curr->val);

curr = curr->next;

}

printf("\n");

}

int main()

{

int i, n, v;

// insert at the end

for(i=1; i<=5; i++)

tail_insert(5);

print_linked_list();

// insert at the beginning

for(i=1; i<=5; i++)

head_insert(i);

print_linked_list();

for(i=1; i<5; i++)

{

scanf("%d %d", &n, &v);

// insert v before every n

insert_before(v, n);

print_linked_list();

}

for(i=1; i<5; i++)

{

scanf("%d %d", &n, &v);

// insert v after every n

insert_after(v, n);

print_linked_list();

}

// sort the list with bubble sort

sort_linked_list();

print_linked_list();

for(i=0; i<5; i++)

{

scanf("%d", &v);

// delete all v in the list

delete_all(v);

print_linked_list();

}

for(i=0; i<5; i++)

{

scanf("%d", &v);

// see if there is v in the list

n = search_linked_list(v);

if(n==-1) printf("not found\n");

else printf("found at %d\n", n);

}

return 0;

}

Everything is commented out, if more explanation needed, leave a comment...

### Bitwise operations in C: Part 3

### Bitwise Operation: Some applications

Back to Part 2

Binary number system is closely related with the powers of 2, and these special numbers always have some amazing bit-wise applications. Along with this, some general aspects will be briefly shown here.

**Is a number a power of 2?**

How can we check this? of course write a loop and check by repeated division of 2. But with a simple bit-wise operation, this can be done fairly easy.

We, know, the binary representation of p = 2^{n} is a bit string which has only 1 on the n^{th} position (0 based indexing, right most bit is LSB). And p-1 is a binary number which has 1 on 0 to n-1 th position and all the rest more significant bits are 0. So, by AND-ing p and (p-1) will result 0 in this case:

p = ....01000 &rArr 8

p-1 = ....00111 &rArr 7

-----------------------

AND = ....00000 &rArr 0

No other number will show this result, like AND-ing 6 and 5 will never be 0.

**Swap two integers without using any third variable:**

Well, as no 3^{rd} variable is allowed, we must find another way to preserve the values, how about we some how combine two values on one variable and the other will then be used as the temporary one...

Let A = 5 and B = 6

A = A ^ B = 3 /* 101 XOR 110 = 011 */

B = A ^ B = 5 /* 011 XOR 110 = 101 */

A = A ^ B = 6 /* 011 XOR 101 = 110 */

So, A = 6 and B = 5

Cool!!!

**Divisibility by power of 2**

Use of % operation is very slow, also the * and / operations. But in case of the second operand is a power of 2, we can take the advantage of bit-wise operations.

Here are some equivalent operations:

Here, P is in the form 2^{X} and N is any integer (typically unsigned)

N % P = N & (P-1)

N / P = N >> X

N * P = N << X

A lot faster and smarter...

About the % operation : the above is possible only when P is a power of 2

**Masking operation**

What is a mask? Its a way that is used to reshape something. In bit-wise operation, a masking operation means to reshape the bit pattern of some variable with the help of a bit-mask using some sort of bit-wise operation. Some examples following here (we won't talk about actual values here, rather we'll look through the binary representation and using only 16 bits for our ease of understanding):**Grab a portion of bit string from an integer variable.**

Suppose A has some value like A = ... 0100 1101 1010 1001

We need the number that is formed by the bit-string of A from 3rd to 9th position.

[Lets assume, we have positions 0 to 15, and 0th position is the LSB]

Obviously the result is B = ... 01 1010 1; [we simply cut the 3rd to 9th position of A by hand]. But how to do this in programming.

Lets assume we have a mask X which contains necessary bit pattern that will help us to cut the desired portion. So, lets have a look how this has to be done:

A = 0100 1101 1010 1001

X = 0000 0011 1111 1000

See, we have X which have 1s in 3rd to 9th position and all the other thing is 0. We know, AND-ing any bit b with 1 leaves b unchanged.

So, X = X & A

Now, we have,

X = 0000 0001 1010 1000

So, now we've cut the desired portion, but this is exactly not what we wanted. We have 3 extra 0s in the tail, So just right-shift 3 times:

X = X >> 3 = 0000 0000 0011 0101; // hurrah we've got it.

Well, I got everything fine, but how to get such a weird X?

int x = 0, i, p=9-3+1; // p is the number of 1s we need

for(i=0; i<p, i++)

x = (x << 1) | 1;

x = x << 3; // as we need to align its lsb with the 3rd bit of A

Execution:

X = 0000 0000 0000 0000 (initially X=0)

X = 0000 0000 0000 0001 (begin loop i=0)

X = 0000 0000 0000 0011 (i=1)

X = 0000 0000 0000 0111 (i=2)

X = 0000 0000 0000 1111 (i=3)

X = 0000 0000 0001 1111 (i=4)

X = 0000 0000 0011 1111 (i=5)

X = 0000 0000 0111 1111 (i=6 loop ends)

X = 0000 0011 1111 1000 (X=X<<3)

So, following the same approach, we may invert some portion of a bit-string (using XOR), make all bits 1 (using OR), make all bits in the portion 0 (using AND) etc etc very easily...

So, these are some tasks for the learner,

You have an integer A with some value, print the integers which have:

1. same bit pattern as A except it has all bits 0 within 4th to 23rd position.

2. same bit pattern as A except it has all bits 1 within 9th and 30th position.

3. same bit pattern as A except it has all bits inverted within positions 2nd and 20th.

4. totally inverted bit pattern.

So, these are some tasks for the learner,

You have an integer A with some value, print the integers which have:

1. same bit pattern as A except it has all bits 0 within 4th to 23rd position.

2. same bit pattern as A except it has all bits 1 within 9th and 30th position.

3. same bit pattern as A except it has all bits inverted within positions 2nd and 20th.

4. totally inverted bit pattern.

**Subset pattern:**

Binary numbers can be used to represent subset ordering and all possible combination of taking n items.

For example, a problem might ask you to determine the n'th value of a series when sorted, where each term is some power of 5 or sum of some powers of 5.

It is clear that, each bit in a binary representation correspondence to a specific power of two in increasing order from right to left. And if we write down the consecutive binary values, we get some sorted integers. Like:

3 2 1 0 ⇒ power of 2

0 0 0 0 = 0 // took no one

0 0 0 1 = 1 // took power 0

0 0 1 0 = 2 // took power 1

0 0 1 1 = 3 // took power 1 and 0

0 1 0 0 = 4 // took power 2

0 1 0 1 = 5 // took power 2 and 0

0 1 1 0 = 6 // took power 2 and 1

0 1 1 1 = 7 // took power 2, 1 and 0

...........

...........

...........

So, what we do here is, take a power of 2 or take the sum of some powers of 2 to get a sorted sequence. Well, if this work for 2.. this will also work for 5 in the same way... Instead of taking the power of 2, we'll take the power of 5.

So the n'th term will be the sum of those powers of 5 on which position n's binary representation has 1. So the 10th term is 5

^{3}+ 5

^{1}; As, 10

_{10}= 1010

_{2}, it has 1 in 3rd and 1st position.

**Worse than worthless**

A bitwise GCD algorithm (wikipedia), translated into C from its assembly routine.

**Sieve of Eratosthenes (SOE)**

This is the idea of compressing the space for flag variables. For example, when we generate prime table using SOE, we normally use 1 int / bool for 1 flag, so if we need to store 10^{8} flags, we barely need 100MB of memory which is surely not available... and using such amount of memory will slow down the process. So instead of using 1 int for 1 flag, why don't we use 1 int for 32 flags on each of its 32 bits? This will reduce the memory by 1/32 which is less than 4MB :D

#define MAX 100000000

#define LMT 10000

unsigned flag[MAX>>6];

#define ifc(n) (flag[n>>6]&(1<<((n>>1)&31)))

#define isc(n) (flag[n>>6]|=(1<<((n>>1)&31)))

void sieve() {

unsigned i, j, k;

for(i=3; i<LMT; i+=2)

if(!ifc(i))

for(j=i*i, k=i<<1; j<MAX; j+=k)

isc(j);

}

ifc(x) checks if x is a composite (if correspondent bit is 1)

isc(x) sets x as a composite (by making correspondent bit 1)

Other than this famous bit-wise sieve, we could use the technique in many places, to reduce memory for flagging.

Keep going on...

### Bitwise operations in C: Part 2

### Bitwise Operation: LEFT SHIFT and RIGHT SHIFT

Back to Part 1

__<< (SHIFT LEFT) operator__

This operator also depends highly on the length of the bit-string. Actually this operator only "shift" or moves all the bits to the left. This operator is mostly used if we want to multiply a number by 2, or, some powers of 2.

Example :

int a = 4978;

printf("%d\n", a<<1);

The output will be 9956. Do you see the relation between 4978 and 9956?

YES, 4978 * 2 = 9956. For more detailed explanation, we will see it in binary:

0001001101110010 ⇒ a = 4978(16 bit)

---------------- << 1 (SHIFT LEFT the bits by one bit)

0010011011100100 ⇒ 9956

Just move left all the bits by one. The left most bit is lost! and at the rightmost, a zero is added. Second example: count 4978 << 8 (count 4978 shifted left by 8 bits)

Answer:

0001001101110010 ⇒ 4978 (16 bit representation)

---------------- << 8 (SHIFT LEFT the bits by 8 bit)

0111001000000000 ⇒ 29184

So, using 16 bit compiler, 4978 << 8 is 29184, which is incorrect! Some of the left most bits are lost...It will not, however, if we use 32 bits data type.

00000000000000000001001101110010 ⇒ 4978(32 bit)

-------------------------------- << 8 (SHIFT LEFT the bits by 8 bit)

00000000000100110111001000000000 ⇒ 1274368

I've told you before that, this operator depends the length of bit-string. Don't be surprised if you got the output of 4978 << 8 is 1274368. (this is actually correct!) As we see, no bits are lost after shifting the bits left by 8 bits if we use a 32 bit data type.

Note that you don't have to have an int to store the value! In C, you can just print like this :

printf("%d", 4978 << 8);

The output will be 29184 for 16 bit compiler as Turbo C (16 bits; some bits will be lost)

The output will be 1274368 for 32 bit compiler as GNU C (32 bits; all bits are reserved since it has bigger capacity)

Now you know why shifting left 1 bit is the same as multiply the number by 2 right?

And shifting left 8 bits is exactly the same as multiplying the number by 2

^{8}.

4978 << 8 = 1274368 (in 32 bits compiler)

4978 * 2

^{8}= 4978 * 256 = 1274368. (exactly the same)

BTW, we need to be careful when using signed data type, as the left most bit is the sign bit. So, while left shifting, if you push a 1 at that position, the number will be negative.

__>> (SHIFT RIGHT) operator__

Not much different with << (SHIFT LEFT) operator. It just shifts all the bits to the right instead of shifting to the left. This operator is mostly used if we want to divide a number by 2, or, some powers of 2.

Example :

count 4978 >> 8

int a = 4978;

printf("%d\n", a>>8);

4978 >> 8 = 19 (in 16 bits compiler or 32 bits compiler, the >> has no difference, because >> will discard the right most bit, and increased capacity on the left side doesn't help here anyhow)

Detailed explanation :

0001001101110010 ⇒ 4978(16 bit)

---------------- >> 8 (SHIFT RIGHT the bits by 8 bit)

0000000000010011 ⇒ 19

and

00000000000000000001001101110010 ⇒ 4978(32 bit)

-------------------------------- >> 8 (SHIFT RIGHT the bits by 8 bit)

00000000000000000000000000010011 ⇒ 19

If you notice:

4978 >> 8 = 19 (in 32 bits compiler)

4978 / 2

^{8}= 4978 / 256 = 19. (They both the same, but using bit-wise operator >> (SHIFT RIGHT) is a LOT faster!)

The RIGHT SHIFT operator is machine dependent, it may work differently on different machines for

**signed**data types. >> empties the left most bit and that is filled either by a 0 or by a 1, depending on the machine.

Filled by a 1 (RIGHT SHIFT ARITHMETIC)

Filled by a 0 (RIGHT SHIFT LOGICAL)

Example:

signed char x = -75 /* 1011 0101 */

signed char y = x >> 2

/* result of logical right shift */

y = 45 /* 0010 1101 */

/* result of arithmetic right shift */

y = -19 /* 1110 1101 */

So this behavior of RIGHT SHIFT leads to a non-portability of a program.

### A few more words

The two shift operators are generally used with unsigned data type to avoid ambiguity.

Result of Shifting Right or Left by a value which is larger than the size of the variable is undefined.

Result of shifting Right or Left by a negative value is also undefined.

Order precedence of the basic operators is:

NOT ( ~ ) highest

AND ( & )

XOR ( ^ )

OR ( | ) lowest

Some basic operations:

Let X is a single bit, then we can write the following:

X & 1 = X; X & 0 = 0

X | 1 = 1; X | 0 = X

X ^ 1 = ~X; X ^ 0 = X

Bit-wise operations are quite fast and easy to use, sometimes they reduce the running time of your program heavily, so use bit-wise operations when-ever you can. But if you look on the software developing aspect, they are not much reliable because, they aren't applicable with any type of data, especially floating points, and signed types. Also, not many people understand them.

But, still, who cares? I want to give some scary looks to my code... lol :D

Hopefully, on the next post, we'll see some simple ideas which will implement some bit-wise operations...

Continue to Part 3

## Monday, December 21, 2009

### Bitwise operations in C: Part 1

### Bitwise Operation: AND, OR, XOR, NOT

In computer, all the numbers and all the other data are stored using 2 based number system or in binary format. So, what we use to say a '5' in decimal (do we use decimal only because we have 10 figures? who knows...), a computer will represent it as '101', in fact, everything is represented as some sequence of 0s and 1s. We call this sequence a bit-string.

Bit-wise operations means working with the individual bits other than using the larger or default data types, like integers, floating points, characters, or some other complex types. C/C++ provides a programmer an efficient way to manipulate individual bits of a data with the help of commonly known logical operators like AND(&), OR(|), NOT(~), XOR(^), LEFT SHIFT(<<) and RIGHT SHIFT(>>) operators.

To be fluent with bit-wise operations, one needs to have a clear concepts about how data is stored in computer and what is a binary number system.

From a programming contest aspect, we'll explore bit-wise operations for only integer types (int in C/C++) and as this is almost 2010, we'll consider int as a 32 bit data type. But for the ease of writing, sometimes only the lowest 16 bits will be shown. The above operators works on bit-by-bit, i.e. during bit-wise operation, we have to align right the bits! (just like addition, multiplication,...) The shorter bits always have leading 0s at the front. And another important thing... we do not need to convert the integers to the binary form ourselves, when we use the operators, they will be automatically evaluated and the following examples shows the underlying activities... :P

### & (AND) operator

This table will clearly explains the bit-wise operator & (AND):

0 & 0 = 0

0 & 1 = 0

1 & 0 = 0

1 & 1 = 1

Example:

What is the value of 24052 & 4978 ?

To solve this, we have to consider the operations in base 2.

So, we imagine both numbers in base 2 and align right the bits, as shown below using 16 bits. Now, AND the numbers : (shorter bits can have leading zeros at the front):

101110111110100 ⇒ 24052

001001101110010 ⇒ 4978

--------------- &

001000101110000 ⇒ 4464

Each bit-column is AND-ed using the AND table above.

### | (OR) operator

This table summarizes the OR operations :

0 | 0 = 0

0 | 1 = 1

1 | 0 = 1

1 | 1 = 1

Now, using the same example above, let's do it using | (OR) operator :

101110111110100 ⇒ 24052

001001101110010 ⇒ 4978

--------------- |

101111111110110 ⇒ 24566

Each bit-column is OR-ed using the OR table above.

### ^ (XOR) operator

This table summarize the XOR operations :

0 ^ 0 = 0

0 ^ 1 = 1

1 ^ 0 = 1

1 ^ 1 = 0

Now, using the same example above, let's do using ^ (XOR) operator :

101110111110100 ⇒ 24052

001001101110010 ⇒ 4978

--------------- ^

100111010000110 ⇒ 20102

Easy, isn't it?

### ~ (NOT) operator

This operator is different from the above operators. This operator is called

**unary**operator, since it only needs one operand. The operators &, |, ^ are

**binary**operators, since it takes 2 operands/number to be operated.

This ~ (NOT) operator, inverts all the bits in a variable. That is, changes all zeros into ones, and changes all ones into zeros.

**Remember!**that the result of this ~ (NOT) operation highly depends on the

**length**of the bit-string.

Example:

int a = 10;

printf("%d\n", ~a);

Surprisingly, the output is -11. But actually this is normal as most of the cases computer represents negative numbers in the 2's complement form. Look at the operation shown below using 16 bits:

0000000000001010 ⇒ a(16 bits)

---------------- ~

1111111111110101 ⇒ -11

This is correct! Because computer stores -11 as 1111111111110101 in binary! (in the 2's complement form). Even if we use 32 bits representation, it is still the 2's complement form of -11;

-11(10) = 11111111111111111111111111110101(2)

Anyway, if we do the same operation for unsigned int:

unsigned int a = 10;

printf("%u\n", ~a);

Hah ha, don't be surprised if you get the output of the unsigned value of -11 is 4294967285.

If you use 32 bits, you actually will get -11 as 11111111111111111111111111110101 in signed binary representation:

00000000000000000000000000001010 ⇒ 10 (32 bits)

-------------------------------- ~

11111111111111111111111111110101 ⇒ -11 (for signed) and -> 4294967285 (for unsigned)

In the unsigned data type, all the bits are considered to be the part of the magnitude, there's no bit position reserved for sign bits.

Continue to Part 2

## Saturday, December 12, 2009

### Power Series Evaluation

### Power series evaluation in O(lg(n)):[n is the number of terms]

**n**.

**Well well... I may be asked, "dude, why do you want to do this in O(lg(n)) when there's already we have a O(1) solution for your problem?". Answer is simple, "I'm trying it from a programming perspective and the O(1) can get sometimes impossible when n keeps going up i.e. like 10**

^{1000}and you are asked to return the answer w.r.t. some %m [modulus m] (meaning the final result will not reach m). This method will be suitable for this case only. Because, calculating {(x^{n-1}-1)/(x-1)}%m is not a very easy task and sometimes, it is not directly possible.This series can be evaluated by binary splitting and recursively solving those building up the answer.

Let n = 6

We have, 1 + x + x

^{2}+ x

^{3}+ x

^{4}+ x

^{5}

⇒ (1 + x + x

^{2}) + x

^{3}(1 + x + x

^{2})

⇒ (1 + x + x

^{2})(1 + x

^{3})

So, when n is even, f(x, n) = (1 + x

^{n/2})f(x, n/2)

Now, what if n is odd, Let n = 7

We have, 1 + x + x

^{2}+ x

^{3}+ x

^{4}+ x

^{5}+ x

^{6}

⇒ (1 + x + x

^{2}) + x

^{3}(1 + x + x

^{2}) + x

^{6}

⇒ (1 + x + x

^{2})(1 + x

^{3}) + x

^{6}

So, when n is odd, f(x, n) = (1 + x

^{n/2})f(x, n/2) + x

^{n-1}

So, by combining the above 2 equations, we get the final solution:

f(x, n) = (1 + x

^{n/2})f(x, n/2) + (n%2)x

^{n-1}

By some clever observation, the need for -1 and %2 operation on n can be eliminated. So, the only thing we need to do is the /2 operation on n, which is easy even when n is a big integer.

Here is a python module that shows only the recursive part, no trick applied.

import math

def evaluate(x, n):

if(n==0):

return 0

if(n==1):

return 1

ret = 0

if(n%2==1):

ret = ret + int(math.pow(x, n-1))

n = n/2

return ret + evaluate(x, n)*(1 + int(math.pow(x, n)))

If this is to be done with C/C++, some biginteger division algorithm should be implemented.

Easy, eh? Check different types of series here

## Friday, December 11, 2009

### Gauss–Jordan Elimination

### Gauss–Jordan Elimination in C++

This is my first attempt to solve linear systems with unique solutions in O(N^{3}).

Preview:

Algorithm:

1. Read augmented matrix for the system.

2. Perform Gaussian Elimination to get the matrix in row echelon form.

3. Transform the matrix to reduced row echelon form.

4. Write results.

[click for a clear view]

My C++ solution (change MAX to desired size, it is the maximum limit of how many equations it can handle, change the macro DEBUG to "if(1)" if you want to see the intermediate steps):

#include <iostream>

#include <cstdlib>

#include <cassert>

#include <algorithm>

using namespace std;

#define DEBUG if(0)

#define MAX 5

struct MAT {

int R[MAX+1];

} M[MAX];

int N;

int gcd(int a, int b) {

while(b) b ^= a ^= b ^= a %= b;

return a;

}

bool comp(MAT A, MAT B) {

for(int k=0; k<N; k++) {

if(A.R[k] > B.R[k]) return true;

if(A.R[k] < B.R[k]) return false;

}

return true;

}

void print(void) {

int i, j;

cout << "..." << endl;

for(i=0; i<N; i++) {

for(j=0; j<N; j++) cout << M[i].R[j] << '\t';

cout << M[i].R[j] << endl;

}

cout << "..." << endl;

}

void modify(MAT &A, MAT B, int a, int b) {

for(int r=0; r<=N; r++) A.R[r] = a*B.R[r] - b*A.R[r];

}

void eliminate(void) {

int i, g, k;

for(i=0; i<N-1; i++) {

if(!M[i].R[i]) {

sort(&M[i], M+N, comp);

assert(M[i].R[i]);

}

for(k=i+1; k<N; k++) {

g = gcd(abs(M[i].R[i]), abs(M[k].R[i]));

modify(M[k], M[i], M[k].R[i]/g, M[i].R[i]/g);

}

DEBUG print();

}

}

void reduce(void) {

int i, g, k;

for(i=N-1; i; i--) {

for(k=i-1; k>=0; k--) {

g = gcd(abs(M[i].R[i]), abs(M[k].R[i]));

modify(M[k], M[i], M[k].R[i]/g, M[i].R[i]/g);

}

DEBUG print();

}

}

void printsol(void) {

double h, l;

cout << "Solve for " << N << " variables:" << endl;

for(int i=0; i<N; i++) {

assert(M[i].R[i]);

h = M[i].R[i];

l = M[i].R[N];

cout << 'x' << i+1 << " = " << l/h << endl;

}

cout << "..." << endl;

}

int main(void) {

int i, j, g;

while(cin >> N) {

if(!N) break;

memset(M, 0, sizeof(M));

for(i=0; i<N; i++) {

for(j=0; j<N; j++) cin >> M[i].R[j];

cin >> M[i].R[j];

for(j=g=0; j<=N; j++) g = gcd(abs(M[i].R[j]), g);

for(j=0; j<=N; j++) M[i].R[j] /= g;

}

eliminate();

reduce();

printsol();

}

return 0;

}

However, I'm trying to do it in a more efficient way. I'll post that when I'm done.

## Thursday, December 10, 2009

### Attacking Recursions

### Some general approach for solving recursive problems:

**If anyone want to read an awesome super cool tutorial on recursion in BANGLA... read from Fahim Vai's page**

**#1:** Forget about what you need to do, just think about any input, for which you know what your function should output, as you know this step, you can build up a solution for your problem. Suppose you have a function which solves a task, and you know, this task is somehow related to another similar task. So you just keep calling that function again and again thinking that, the function I'm calling will solve the problem anyhow, and the function will also say, I'll solve this problem, if you give me the result for another sub-problem first!!! Well, then you'll say, "So, why don't you use your twin-brother to solve that part?" and so on... The following example will show you how to start writing a recursive function.

Example: Think about computing n! recursively. I still don't know what and how my function will do this. And I also don't know, like what could be 5! or 7!... But nothing to worry about, I know that 0! = 1! = 1. And I also know that, n! = n(n-1)!. So I will think like that, "I will get n! from a function F, if some one gives me (n-1)!, then I'll multiply it with n and produce results". And, thus, F is the function for computing n!, so why don't I use again to get (n-1)! ? And when F tries to find out what could be the value of (n-1)!, it also stumbles at the same point, it wonders what would be the value of (n-2)!... then we tell him to use F again to get this... and this thing keeps going as long as we don't know what F actually should return for any k. In case of k = 1, as we know now F can return 1 for k = 1, and it don't need to call another F to solve anything first...

int factorial(int n) {

// I know this, so I don't want my function to go any further...

if(n==0) return 1;

// don't bother what to do, just reuse the function...

else return n*factorial(n-1);

}

**#2:** What ever you can do with loops, can be done with recursions as well. A simple technique for converting recursions and loops is shown below:

__Forward:__

for loop:

for(int i = 0; i < n; i++) {

// do whatever needed

}

Equivalent recursion:

void FOR(int i, int n) {

if(i==n) return; // terminates

// do whatever needed

FOR(i+1, n); // go to next step

}

__Backward:__

for loop:

for(int i = n-1; i >= 0; i -= 1) {

// do whatever needed

}

Equivalent recursion:

void ROF(int i, int n) {

if(i==n) return; // terminates

ROF(i+1, n); // keep going to the last

// do whatever needed when returning from prev steps

}

Well, you may wonder how this is backward loop? But just think of its execution cycle, just after entering the function, it is calling itself again incrementing the value of *i*, and the execution routine that you have written under the function call, was paused there. From the new function it enters, it works the same way, call itself again before executing anything...Thus when you have reached the limiting condition, or the base condition, then the function stops recursion and starts returning, and the whole process can be shown as below...let n=5, and we want to print 5 4 3 2 1...code for this might be:

void function(int i, int n) {

if(i<=n) {

function(i+1, n);

printf("%d ", i);

}

}

Explanation might look like this:

01|call function_{1}with i=1

02| call function_{2}with i=2

03| call function_{3}with i=3

04| call function_{4}with i=4

05| call function_{5}with i=5

06| call function_{6}with i=6

07| i breaks condition, no more calls

08| return to function_{5}

09| print 5

10| return to function_{4}

11| print 4

12| return to function_{3}

13| print 3

14| return to function_{2}

15| print 2

16| return to function_{1}

17| print 1

18|return to main, done!

Left side number shows the execution steps, so from the above program, we get, 5 4 3 2 1. So indeed it ran on reverse direction...

**#3:** Use the advantage of call stack. When you call functions recursively, it stays in the memory as the following picture demonstrates.

int f(int n) {

if(n==0) return 1;

return n*f(n-1);

}

You know about stack, in a stack, you cannot remove any item unless it is the topmost. So you can consider the calling of a recursive function as a stack, where, you can't remove the memory used by f(n=3) before removing f(n=2) and so so... So, you can easily see that, the functions will hold all their variables and values until it is released. This actually serves the purpose of using an array.

**#4:** Be careful while using recursions. From a programming contest aspects, recursions are always best to avoid. As you've seen above, most recursions can be done using loops somehow. Recursions have a great deal of drawbacks and it most of the time extends the execution time of your program. Though recursions are very very easy to understand and they are like the first idea in many problems that pops out in mind first... they still bear the risks of exceeding memory and time limits.

Generally, in loops, all the variables are loaded at the same time which causes it a very low memory consumption and faster access to the instructions. But whenever we use recursions, each function is allotted a space at the moment it is called which requires much more time and all the internal piece of codes stored again which keeps the memory consumption rising up. As your compiler allows you a specific amount of memory (generally 32 MB) to use, you may overrun the stack limit by excessive recursive calls which causes a Stack Overflow error (a Runtime Error).

So, please think a while before writing recursions whether your program is capable of running under the imposed time and memory constraints. Generally recursions in O(lg n) are safe to use, also we may go to a O(n) recursion if n is pretty small.

**#5:** If the recursion tree has some overlapping branches, most of the times, what we do, is to store already computed values, so, when we meet any function which was called before, we may stop branching again and use previously computed values, which is a common technique knows as Dynamic Programming (DP), we will talk about that later as that is pretty advanced.

These techniques will be used in almost all problems when writing recursive solution. Just don't forget the definition of recursion:

Definition of recursion = See the Definition of recursion

Now try this

### Drawing Regular n-gon

### How to draw a regular pentagon with just ruler and compass?

### How about a regular hexagon?

__Underlying theorem:__Primes of the form 2

^{2n}+ 1 are known as Fermat primes.

A regular n-gon is constructible using straightedge (ruler) and compass if and only if

**n = 2**where

^{i}· m**m**is a product of any number of distinct Fermat Prime and

**i**is any natural number, including zero.

Only five Fermat primes are known: 3, 5, 17, 257, and 65,537.

Related study: Compass and straightedge constructions.

## Tuesday, December 8, 2009

### Practice Recursions

### Recursions are fun

You may like to try out some simple problems to practice recursions. Try to solve all of them without using any global variables. And try on your own before looking at the solutions. Also please notify any error to me ( zobayer1[at]gmail[dot]com ).

Before looking at the problems, you may like to read this post about how to attack recursive problems.

__Problem 1:__

You will be given an array of integers, write a recursive solution to print it in reverse order.

Input:

5

69 87 45 21 47Output:

47 21 45 87 69

see answer

__Problem 2:__

Write a recursive function to print an array in the following order.

[0] [n-1]

[1] [n-2]

.........

.........

[(n-1)/2] [n/2]

Input:

5

1 5 7 8 9Output:

1 9

5 8

7 7

see answer

__Problem 3:__

Write a recursive program to remove all odd integers from an array.

**You must not use any extra array or print anything in the function.**Just read input, call the recursive function, then print the array in main().

Input:

6

1 54 88 6 55 7Output:

54 88 6

see answer

__Problem 4:__

Write a recursive solution to print the polynomial series for any input n:

1 + x + x

^{2}+ ................. + x

^{n-1}

Input:

5Output:

1 + x + x^2 + x^3 + x^4

see answer

__Problem 5:__

Write a recursive solution to evaluate the previous polynomial for any given x and n.

Like, when x=2 and n=5, we have 1 + x + x

^{2}+ ................. + x

^{n-1}= 31

Input:

2 5Output:

31

see answer

__Problem 6:__

Write a recursive program to compute n!

Input:

5Output:

120

see answer

__Problem 7:__

Write a recursive program to compute n

^{th}fibonacci number. 1

^{st}and 2

^{nd}fibonacci numbers are 1, 1.

Input:

6Output:

8

see answer

__Problem 8:__

Write a recursive program to determine whether a given integer is prime or not.

Input:

49

999983

1Output:

not prime

prime

not prime

see answer

__Problem 9:__

Write a recursive function that finds the gcd of two non-negative integers.

Input:

25 8895Output:

5

see answer

__Problem 10:__

Write a recursive solution to compute lcm of two integers. You must not use the formula lcm(a,b) = (a x b) / gcd(a,b); find lcm from scratch...

Input:

23 488Output:

11224

see answer

__Problem 11:__

Suppose you are given an array of integers in an arbitrary order. Write a recursive solution to find the maximum element from the array.

Input:

5

7 4 9 6 2Output:

9

see answer

__Problem 12:__

Write a recursive solution to find the

**second maximum**number from a given set of integers.

Input:

5

5 8 7 9 3Output:

8

see answer

__Problem 13:__

Implement linear search recursively, i.e. given an array of integers, find a specific value from it.

Input format: first n, the number of elements. Then n integers. Then, q, number of query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.

Input:

5

2 9 4 7 6

2

5 9Output:

not found

1

see answer

__Problem 14:__

Implement binary search recursively, i.e. given an array of

**sorted**integers, find a query integer from it.

Input format: first n, the number of elements. Then n integers. Then, q, number of query, then q integers. Output format: for each of the q integers, print its index (within 0 to n-1) in the array or print 'not found', whichever is appropriate.

Input:

5

1 2 3 4 5

2

3 -5Output:

2

not found

see answer

__Problem 15:__

Write a recursive solution to get the reverse of a given integer.

**Function must return an int**

Input:

123405Output:

504321

see answer

__Problem 16:__

Read a string from keyboard and print it in reversed order.

**You must not use any array to store the characters**. Write a recursive solutions to solve this problem.

Input:

hellooOutput:

oolleh

see answer

__Problem 17:__

Write a recursive program that determines whether a given sentence is palindromic or not just considering the alpha-numeric characters ('a'-'z'), ('A'-'Z'), ('0'-'9').

Input:

madam, i'm adam

hulalaOutput:

palindromic

not palindromic

see answer

__Problem 18:__

Implement strcat(), stracpy(), strcmp() and strlen() recursively.

Input:test on your ownOutput:test on your own

see answer

__Problem 19:__

If you already solved the problem for finding the n

^{th}fibonacci number, then you must have a clear vision on how the program flow works. So now, in this problem, print the values of your fibonacci function in pre-order, in-order and post-order traversal. For example, when n = 5, your program calls 3 and 4 from it, from the call of 3, your program calls 1 and 2 again....... here is the picture:

Input:

5Output:

Inorder: 1 3 2 5 2 4 1 3 2

Preorder: 5 3 1 2 4 2 3 1 2

Postorder: 1 2 3 2 1 2 3 4 5

see answer

__Problem 20:__

All of you have seen the tower of Hanoi. You have 3 pillars 'a', 'b' and 'c', and you need transfer all disks from one pillar to another. Conditions are, only one disk at a time is movable, and you can never place a larger disk over a smaller one. Write a recursive solution to print the moves that simulates the task, a -> b means move the topmost of tower a to tower b.

Input:

3Output:

a -> c

a -> b

c -> b

a -> c

b -> a

b -> c

a -> c

see answer