Tuesday 7 June 2016

Algorithms and Data Structures : Sequence

Algorithms and Data Structures : Sequence - Algorithm is a sequence of one or more instructions or statements, and each statement is done sequentially in the order of writing. Which is:

  1. Each instruction is done one by one 
  2. Each instruction executed exactly once (no instruction is repeated) 
  3. Each instruction executed in the same order as it written in the algorithm 
  4. The end of the last instruction is the end of the algorithm. 

For example, suppose we have two cup, say cup A and cup B. Cup A contains coffee and cup B is milk. If we want to switch the content of the two cup, milk on A and coffee on B. How are we going to do that? Consider the following algorithm:

  1. Move the content of cup A to B 
  2. Move the content of cup B to A 

What is going on here? Is that solve the problem?
No, we do not solve the problem. We do not exchange the contents of the two cup but mixing both contents. In the end, cup A contains milky coffee while cup B is empty.

Then, what is the right algorithm? Here we need additional cup, say cup C, to temporary hold the content of cup to avoid mixing the contents. Thus, the algorithm will be:

  1. Move the content of cup A to C 
  2. Move the content of cup B to A 
  3. Move the content of cup C to B 

What about now? First we move coffee from cup A to cup C, which is empty, thus not mixing it. Then move milk from B to A, which is also empty after the first step. And finally, move coffee to cup B. In the end, milk in cup A, coffee in cup B, as expected, and cup C is empty. That solve the problem.

So to switch two cups content, we need an additional cup. Is that all? Consider the following algorithm:

  1. Move the content of cup A to C 
  2. Move the content of cup C to B 
  3. Move the content of cup B to A 

In above algorithm, in the end, cup A content milky coffee, cup B and C is empty. We use additional cup as the second algorithm. Then why the result was the same as the first algorithm? We see that the sequence of the second and the third algorithm was different. On the second algorithm we move content B to A, before we moving content cup C to B. While on the third, we move content B to A after moving content C to B.

So, to solve a problem not only we need right technique, but also the right sequence.


A similar problem on a programming algorithm is when we want to swap the contents of two variables, as this code:

//We want to swap the contents of the following variables
int x = 10;
int y = 33;

So what we need is an auxiliary variable to temporarily store the data of the variable

//Need 1 auxiliary variables
int z = 0;

 If we print the contents of all the variables in the initial stages, it will be found the following results

Initialization
x = 10
y = 33
z = 0

The first step in doing this is to move the value of the variable x to variable z.

//move the value of x to var z
z = x;

The contents from each variable after this process is as follows

The first exchange of x to z
x = 10
y = 33
z = 10

The next step is to move the value of the variable y to variable x

//move the value of var y to var x
x = y;

So that each variable would be

The second exchange y to x
x = 33
y = 33
z = 10

The final step is to move the value of the variable z to the variable y

//move the value of var z to var y
y = z;

so the result of this step is the solution of the initial problem, that is

Last exchange z to y
x = 33
y = 10
z = 10

Here's the entire code, and the output of the above issues

#include <iostream>
using namespace std;
int main()
{
   //We want to swap the contents of the following variables
   int x = 10;
   int y = 33;
   //Need 1 auxiliary variables
   int z = 0;
   cout << "initialization" << endl;
   cout << "x = " << x << endl;
   cout << "y = " << y << endl;
   cout << "z = " << z << endl;
   //move the value of x to var z
   z = x;
   cout << "\nthe first exchange of x to z" << endl;
   cout << "x = " << x << endl;
   cout << "y = " << y << endl;
   cout << "z = " << z << endl;
   //move the value of var y to var x
   x = y;
   cout << "\nthe second exchange of y to x" << endl;
   cout << "x = " << x << endl;
   cout << "y = " << y << endl;
   cout << "z = " << z << endl;
   //move the value of var z to var y
   x = y;
   cout << "\nlast exchange of z to y" << endl;
   cout << "x = " << x << endl;
   cout << "y = " << y << endl;
   cout << "z = " << z << endl;
   return 0;
}

Swapping Variables Value Source Code

Initialization
x = 10
y = 33
z = 0
The first exchange of x to z
x = 10
y = 33
z = 10
The second exchange y to x
x = 33
y = 33
z = 10
Last exchange z to y
x = 33
y = 10
z = 10

Swapping Variables Value Programs When Running.


EmoticonEmoticon