Open In App

Slow Start Backoff Algorithm for Ad-Hoc

If the receiver proclaims a large window-size, larger than what the network en-route can manage, then there will always be packet losses. So there will be re-transmissions as well. However, the sender can’t send all the packets for which ACK (Acknowledgement) has not been received. Because of this way, even more congestion in the network will be caused. Moreover, the sender cannot be sure about the packets that have been lost due to transmission. It might be that this is the only packet that has been lost. It will also be not know how many packets have been actually received and buffered by the receiver. In that case, the sender will have superfluously sent various packets.
So the re-transmission of the packets also follows slow – start backoff mechanism. However, we do precisely need to keep an upper bound on the size of the packets as it gets increases in slow start, to prevent it from the increasing in unbounded and causing congestion. In slow – start backoff mechanism 
the threshold window size is half the value of the size of the congestion window.
Prerequisite: Back-off Algorithm for CSMA/CD 
Algorithm of Slow-Start Mechanism:- 
 

repeat
if ACK frame received then successful transmission
if current backoff window size <= Wm then
if current backoff window size = W0 then
current backoff window size = W0
else
current backoff window size = current backoff window size ÷ 2
else
current backoff window size = current backoff window size-W0
else
if current backoff window size < Wm then
current backoff window size = current backoff window size × 2
else frame lost due to collision or interference
if current backoff window size = Wn then
current backoff window size = Wn
else
current backoff window size = current backoff window size +W0
until no more frame to transmit
end

Examples: 
 



This represents the successful transmission because Threshold window is lesser the current window. 
Input : 
Enter the Threshold value:–>512 
Enter the Current backoff window size:–>64 
Output : 
Backoff Window Size is:–>128 
Backoff Window Size is:–>96 
Successful transmission 
Backoff Window Size is:–>32 
Successful transmission 
Backoff Window Size is:–>32 
Backoff Window Size is:–>128 
Backoff Window Size is:–>96 
Successful transmission 
Backoff Window Size is:–>32
This represents the unsuccessful transmission because the threshold window is greater the current window. 
Input : 
Enter the Threshold value:–>512 
Enter the Current backoff window size:–>1024 
Output : 
Frame lost due to collision or interferenceBackoff Window Size is:–>1056 
Successful transmission 
Backoff Window Size is:–>512 
Successful transmission 
Backoff Window Size is:–>512 
Frame lost due to collision or interferenceBackoff Window Size is:–>1056 
Successful transmission 
Backoff Window Size is:–>512 
 

Notations:- 



W0 -->  Initial backoff window size
Wm --> Threshold
Wn --> Maximum backoff window size
ACK --> Acknowledgement
Curr_BT --> Current backoff window size

Implementation of the Slow-Start Mechanism:- 




#include <cstdlib>
#include <iostream>
#include <math.h>
#include <random>
#include <string>
#include <time.h>
 
using namespace std;
 
void signal(int array_ACK[])
{
    srand(time_t(0));
    for (int i = 0; i < 5; i++) {
        array_ACK[i] = rand() % 2;
    }
}
 
void Slow_Start_Backoff(int Wm, int Wo, int Wn,
                        int Curr_BT, int array_ACK[])
{
    // Taking ACK Binary values in
    // array by user one by one
 
    // backoff_win_size defines
    // backoff window size
 
    int backoff_win_size = 0;
 
    // Printing of backoff window size takes place
 
    for (int j = 0; j < 5; j++) {
        if (array_ACK[j] == 1) {
            cout << "Successful transmission" << endl;
            if (Curr_BT <= Wm) {
                if (Curr_BT == Wo) {
                    Curr_BT = Wo;
                    cout << "Backoff Window Size is:-->"
                            << Curr_BT << endl;
                }
                else {
                    Curr_B= Curr_BT / 2;
                    cout << "Backoff Window Size is:-->"
                            << Curr_BT << endl;
                }
            }
            else {
                Curr_BT = Curr_BT - Wo;
                cout << "Backoff Window Size is:-->"
                        << Curr_BT << endl;
            }
        }
        else {
            if (Curr_BT < Wm) {
                Curr_BT = Curr_BT * 2;
                cout << "Backoff Window Size is:-->"
                        << Curr_BT << endl;
            }
            else {
                cout << "Frame lost due to collision"
                        <<" or interference";
            }
            if (Curr_BT == Wn) {
                Curr_BT = Wn;
                cout << "Backoff Window Size is:-->"
                        << Curr_BT << endl
                            << endl;
            }
            else {
                Curr_BT = Curr_BT + Wo;
                cout << "Backoff Window Size is:-->"
                        << Curr_BT << endl;
            }
        }
    }
}
 
// Driver Code
int main()
{
    int Wm, Wo, Wn, Curr_BT;
    int array_ACK[5];
 
    Wo = 32; // Initial backoff window size
    Wn = 1024; // Maximum backoff window size
 
    // Curr_BT defines the current backoff window size
 
    cout << "Enter the Threshold value:-->";
    cin >> Wm; // Threshold backoff window size
    cout << "Enter the Current backoff window size:-->";
    cin >> Curr_BT;
    signal(array_ACK);
    Slow_Start_Backoff(Wm, Wo, Wn, Curr_BT, array_ACK);
 
    return 0;
}




// Java program to implement the approach
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
 
class GFG {
  static void signal(int[] array_ACK)
  {
    for (var i = 0; i < 5; i++) {
      array_ACK[i] = ThreadLocalRandom.current().nextInt(1, 101) % 2;
    }
  }
 
  static void Slow_Start_Backoff(int Wm, int Wo, int Wn,
                                 int Curr_BT,
                                 int[] array_ACK)
  {
 
    // Taking ACK Binary values in
    // array by user one by one
 
    // backoff_win_size defines
    // backoff window size
 
    // Printing of backoff window size takes place
    for (var j = 0; j < 5; j++) {
      if (array_ACK[j] == 1) {
        System.out.println(
          "Successful transmission");
        if (Curr_BT <= Wm) {
          if (Curr_BT == Wo) {
            Curr_BT = Wo;
            System.out.println(
              "Backoff Window Size is:-->"
              + Curr_BT);
          }
          else {
            Curr_BT = (int)(Curr_BT / 2);
            System.out.println(
              "Backoff Window Size is:-->"
              + Curr_BT);
          }
        }
        else {
          Curr_BT = Curr_BT - Wo;
          System.out.println(
            "Backoff Window Size is:-->"
            + Curr_BT);
        }
      }
      else {
        if (Curr_BT < Wm) {
          Curr_BT = Curr_BT * 2;
          System.out.println(
            "Backoff Window Size is:-->"
            + Curr_BT);
        }
        else {
          System.out.println(
            "Frame lost due to collision"
            + " or interference");
        }
        if (Curr_BT == Wn) {
          Curr_BT = Wn;
          System.out.println(
            "Backoff Window Size is:-->"
            + Curr_BT + "\n");
        }
        else {
          Curr_BT = Curr_BT + Wo;
          System.out.println(
            "Backoff Window Size is:-->"
            + Curr_BT);
        }
      }
    }
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int Wm, Wo, Wn, Curr_BT;
    int[] array_ACK = new int[5];
 
    Wo = 32; // Initial backoff window size
    Wn = 1024; // Maximum backoff window size
 
    // Curr_BT defines the current backoff window size
 
    Wm = 512; // Threshold backoff window size
    Curr_BT = 1024;
    signal(array_ACK);
    Slow_Start_Backoff(Wm, Wo, Wn, Curr_BT, array_ACK);
  }
}
 
// This code is contributed by phasing17.




# Python3 program to implement the approach
import random
 
def signal(array_ACK):
    for i in range(5):
        array_ACK[i] = (random.randint(1, 101) % 2);
     
def Slow_Start_Backoff( Wm, Wo, Wn, Curr_BT, array_ACK):
 
    # Taking ACK Binary values in
    # array by user one by one
 
    # backoff_win_size defines
    # backoff window size
 
    backoff_win_size = 0;
 
    # Printing of backoff window size takes place
    for j in range(5):
        if (array_ACK[j] == 1):
            print("Successful transmission");
            if (Curr_BT <= Wm) :
                if (Curr_BT == Wo):
                    Curr_BT = Wo;
                    print("Backoff Window Size is:-->", Curr_BT);
                 
                else :
                    Curr_B= Math.floor(Curr_BT / 2);
                    print( "Backoff Window Size is:-->", Curr_BT);
                 
             
            else :
                Curr_BT = Curr_BT - Wo;
                print("Backoff Window Size is:-->", Curr_BT);
             
         
        else :
            if (Curr_BT < Wm):
                Curr_BT = Curr_BT * 2;
                print("Backoff Window Size is:-->", Curr_BT);
             
            else :
                print("Frame lost due to collision" + " or interference");
             
            if (Curr_BT == Wn) :
                Curr_BT = Wn;
                print("Backoff Window Size is:-->", Curr_BT, "\n")
             
            else:
                Curr_BT = Curr_BT + Wo;
                print("Backoff Window Size is:-->", Curr_BT);
     
# Driver Code
array_ACK = [0 for _ in range(5)]
 
Wo = 32; # Initial backoff window size
Wn = 1024; # Maximum backoff window size
 
# Curr_BT defines the current backoff window size
 
Wm = 512; # Threshold backoff window size
Curr_BT = 1024;
signal(array_ACK);
Slow_Start_Backoff(Wm, Wo, Wn, Curr_BT, array_ACK);
 
# This code is contributed by phasing17.




// C# program to implement the approach
 
using System;
using System.Collections.Generic;
 
class GFG {
  static void signal(int[] array_ACK)
  {
    Random rand = new Random();
    for (var i = 0; i < 5; i++) {
      array_ACK[i] = (rand.Next(1, 100)) % 2;
    }
  }
 
  static void Slow_Start_Backoff(int Wm, int Wo, int Wn,
                                 int Curr_BT,
                                 int[] array_ACK)
  {
    // Taking ACK Binary values in
    // array by user one by one
 
    // backoff_win_size defines
    // backoff window size
 
    // Printing of backoff window size takes place
 
    for (var j = 0; j < 5; j++) {
      if (array_ACK[j] == 1) {
        Console.WriteLine(
          "Successful transmission");
        if (Curr_BT <= Wm) {
          if (Curr_BT == Wo) {
            Curr_BT = Wo;
            Console.WriteLine(
              "Backoff Window Size is:-->"
              + Curr_BT);
          }
          else {
            Curr_BT = (int)(Curr_BT / 2);
            Console.WriteLine(
              "Backoff Window Size is:-->"
              + Curr_BT);
          }
        }
        else {
          Curr_BT = Curr_BT - Wo;
          Console.WriteLine(
            "Backoff Window Size is:-->"
            + Curr_BT);
        }
      }
      else {
        if (Curr_BT < Wm) {
          Curr_BT = Curr_BT * 2;
          Console.WriteLine(
            "Backoff Window Size is:-->"
            + Curr_BT);
        }
        else {
          Console.WriteLine(
            "Frame lost due to collision"
            + " or interference");
        }
        if (Curr_BT == Wn) {
          Curr_BT = Wn;
          Console.WriteLine(
            "Backoff Window Size is:-->"
            + Curr_BT + "\n");
        }
        else {
          Curr_BT = Curr_BT + Wo;
          Console.WriteLine(
            "Backoff Window Size is:-->"
            + Curr_BT);
        }
      }
    }
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int Wm, Wo, Wn, Curr_BT;
    int[] array_ACK = new int[5];
 
    Wo = 32; // Initial backoff window size
    Wn = 1024; // Maximum backoff window size
 
    // Curr_BT defines the current backoff window size
 
    Wm = 512; // Threshold backoff window size
    Curr_BT = 1024;
    signal(array_ACK);
    Slow_Start_Backoff(Wm, Wo, Wn, Curr_BT, array_ACK);
  }
}
 
// This code is contributed by phasing17.




// JS program to implement the approach
 
function signal(array_ACK)
{
    for (var i = 0; i < 5; i++) {
        array_ACK[i] = ( Math.floor(Math.random() * 100)) % 2;
    }
}
 
function Slow_Start_Backoff( Wm, Wo, Wn,  Curr_BT, array_ACK)
{
    // Taking ACK Binary values in
    // array by user one by one
 
    // backoff_win_size defines
    // backoff window size
 
    let backoff_win_size = 0;
 
    // Printing of backoff window size takes place
 
    for (var j = 0; j < 5; j++) {
        if (array_ACK[j] == 1) {
            console.log("Successful transmission");
            if (Curr_BT <= Wm) {
                if (Curr_BT == Wo) {
                    Curr_BT = Wo;
                    console.log("Backoff Window Size is:-->" + Curr_BT);
                }
                else {
                    Curr_B= Math.floor(Curr_BT / 2);
                    console.log( "Backoff Window Size is:-->"
                            + Curr_BT);
                }
            }
            else {
                Curr_BT = Curr_BT - Wo;
                console.log("Backoff Window Size is:-->"
                        + Curr_BT);
            }
        }
        else {
            if (Curr_BT < Wm) {
                Curr_BT = Curr_BT * 2;
                console.log("Backoff Window Size is:-->"
                        + Curr_BT);
            }
            else {
                console.log("Frame lost due to collision"
                        + " or interference");
            }
            if (Curr_BT == Wn) {
                Curr_BT = Wn;
                console.log("Backoff Window Size is:-->"
                        + Curr_BT + "\n")
            }
            else {
                Curr_BT = Curr_BT + Wo;
                console.log("Backoff Window Size is:-->"
                        + Curr_BT);
            }
        }
    }
}
 
// Driver Code
let Wm, Wo, Wn, Curr_BT;
let array_ACK = new Array(5);
 
Wo = 32; // Initial backoff window size
Wn = 1024; // Maximum backoff window size
 
// Curr_BT defines the current backoff window size
 
     Wm = 512; // Threshold backoff window size
      Curr_BT = 1024;
    signal(array_ACK);
    Slow_Start_Backoff(Wm, Wo, Wn, Curr_BT, array_ACK);
 
// This code is contributed by phasing17.

How the data sent in the form of packets through the network:-
Any type of data gets converted into the binary format and this binary format contains 0’s and 1’s bytes 
which gets divided into the small binary bit sequences called packets.
Now, we are implementing code in which the random matrix of 0’s and 1’s generates and suppose the binary file bit sequence arranged in the matrix row-wise in fixed-length (Taking a Length 10). Then each row of the matrix represents a single data packet that is going to be transmitted.
Implementation of the Random Matrix Generator:-




// C++ program to construct the
// Random Matrix Generator.
#include <iostream>
#include <iterator>
#include <list>
#include <random>
#include <string>
using namespace std;
 
void showlist(list<string> l1)
{
    list<string>::iterator it;
    cout << "list of frame packets are "
            <<"show as below :-> " << endl;
    cout << endl;
     
    for (it = l1.begin(); it != l1.end(); ++it)
        cout << *it << endl;
    cout << '\n';
}
 
// Driver Code
int main()
{
    int x, y, k = 1;
    string s, s1, s2;
     
    list<string> l1;
     
    srand(time_t(0));
     
    cout << "Rows: " << endl;
    cin >> x;
     
    cout << "Columns: " << endl;
    cin >> y;
     
    int randomNums[x][y];
    std::string temp[x];
     
    int random;
     
    for (int i = 0; i < x; i++) {
     
        // This loop is for the row
        for (int p = 0; p < y; p++) {
             
            // Here you would randomize each
            // element for the array.
            random = rand() % 2;
            randomNums[i][p] = random;
        }
    }
 
    for (int i = 0; i < x; i++) {
         
        // This loop is for the row
        for (int p = 0; p < y; p++) {
             
            // Here you would randomize each
            // element for the array.
            cout << randomNums[i][p] << "\t";
        }
         
        cout << endl;
    }
    cout << endl;
     
    // concatenation of the bits in the matrix row
    // to form a single data packet
    for (int i = 0; i < x; i++) {
         
        temp[i] = to_string(randomNums[i][0]);
         
        for (int j = 0; j < y; ++j) {
             
            s1 = temp[i];
            s2 = to_string(randomNums[i][j]);
 
            s = s1 + s2;
 
            temp[i] = s;
            k++;
        }
         
        temp[i].erase(temp[i].begin() + 1);
        l1.push_back(temp[i]);
    }
 
    showlist(l1);
    return 0;
}




// Java program to construct the
// Random Matrix Generator.
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
 
class GFG {
  static void showlist(ArrayList<String> l1)
  {
    System.out.println("list of frame packets are "
                       + "show as below :-> ");
 
    for (var it : l1)
      System.out.println(it);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int x = 10, y = 10, k = 1;
    String s, s1, s2;
 
    ArrayList<String> l1 = new ArrayList<String>();
 
    System.out.println("Rows:" + x);
 
    System.out.println("Columns:" + y);
 
    int[][] randomNums = new int[x][y];
    String[] temp = new String[x];
 
    int random;
 
    for (int i = 0; i < x; i++) {
 
      // This loop is for the row
      for (int p = 0; p < y; p++) {
 
        // Here you would randomize each
        // element for the array.
        random = Math.abs(
          ThreadLocalRandom.current().nextInt()
          % 2);
        randomNums[i][p] = random;
      }
    }
 
    for (int i = 0; i < x; i++) {
 
      // This loop is for the row
      for (int p = 0; p < y; p++) {
 
        // Here you would randomize each
        // element for the array.
        System.out.print(randomNums[i][p] + "\t");
      }
 
      System.out.print("\n");
    }
    System.out.print("\n");
 
    // concatenation of the bits in the matrix row
    // to form a single data packet
    for (int i = 0; i < x; i++) {
 
      temp[i] = String.valueOf(randomNums[i][0]);
 
      for (int j = 0; j < y; ++j) {
 
        s1 = temp[i];
        s2 = String.valueOf(randomNums[i][j]);
 
        s = s1 + s2;
 
        temp[i] = s;
        k++;
      }
 
      temp[i] = temp[i].substring(1);
      l1.add(temp[i]);
    }
 
    showlist(l1);
  }
}
 
// This code is contributed by phasing17.




# Python3 program to construct the
# Random Matrix Generator.
import random as rn
 
def showlist(l1):
 
    print("list of frame packets are show as below :-> \n");
    print(*l1, sep = '\n')
 
# Driver Code
x = 10
y = 10
k = 1;
s = ""
s1 = ""
s2 = "";
 
l1 = [];
 
print("Rows: 10");
 
print("Columns: 10");
 
randomNums = [[None for _ in range(y)] for _ in range(x) ]
temp = ["" for _ in range(x)]
 
for i in range(x):
 
    # This loop is for the row
    for p in range(y):
 
        # Here you would randomize each
        # element for the array.
        random = (rn.randint(1, 100000)) % 2
        randomNums[i][p] = random;
     
for i in range(x):
     
    # This loop is for the row
    for p in range(y):
         
        # Here you would randomize each
        # element for the array.
        print(randomNums[i][p], end = "\t")
    print()
 
print()
 
# concatenation of the bits in the matrix row
# to form a single data packet
for i in range(x):
    temp[i] = str(randomNums[i][0]);
     
    for j in range(y):
 
        s1 = temp[i];
        s2 =  str(randomNums[i][j]);
 
        s = s1 + s2;
 
        temp[i] = s;
        k += 1;
     
    temp[i] = temp[i][1::];
    l1.append(temp[i]);
 
showlist(l1);
 
# This code is contributed by phasing17




// C# program to construct the
// Random Matrix Generator.
using System;
using System.Collections.Generic;
 
class GFG
{
  static void showlist(List<string> l1)
  {
    Console.WriteLine("list of frame packets are "
                      + "show as below :-> ");
 
    foreach (var it in l1)
      Console.WriteLine(it);
 
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int x = 10, y = 10, k = 1;
    string s, s1, s2;
 
    List<string> l1 = new List<string>();
 
 
    Random rand = new Random();
 
    Console.WriteLine("Rows:" + x);
 
    Console.WriteLine("Columns:" + y);
 
    int[,] randomNums = new int[x, y];
    string[] temp = new string[x];
 
    int random;
 
    for (int i = 0; i < x; i++) {
 
      // This loop is for the row
      for (int p = 0; p < y; p++) {
 
        // Here you would randomize each
        // element for the array.
        random = rand.Next(1, 101) % 2;
        randomNums[i, p] = random;
      }
    }
 
    for (int i = 0; i < x; i++) {
 
      // This loop is for the row
      for (int p = 0; p < y; p++) {
 
        // Here you would randomize each
        // element for the array.
        Console.Write(randomNums[i, p] + "\t");
      }
 
      Console.Write("\n");
    }
    Console.Write("\n");
 
    // concatenation of the bits in the matrix row
    // to form a single data packet
    for (int i = 0; i < x; i++) {
 
      temp[i] = Convert.ToString(randomNums[i, 0]);
 
      for (int j = 0; j < y; ++j) {
 
        s1 = temp[i];
        s2 = Convert.ToString(randomNums[i, j]);
 
        s = s1 + s2;
 
        temp[i] = s;
        k++;
      }
 
      temp[i] = temp[i].Substring(1);
      l1.Add(temp[i]);
    }
 
    showlist(l1);
  }
 
}
 
// This code is contributed by phasing17.




// C++ program to construct the
// Random Matrix Generator.
 
function showlist(l1)
{
    console.log("list of frame packets are "
                + "show as below :-> \n");
    for (var l of l1)
        console.log(l);
}
 
// Driver Code
let x = 10, y = 10, k = 1;
let s = "", s1 = "", s2 = "";
 
let l1 = [];
 
console.log("Rows: 10");
 
console.log("Columns: 10");
 
let randomNums = new Array(x);
for (var i = 0; i < 10; i++)
    randomNums[i] = new Array(y);
 
let temp = new Array(x).fill("");
 
let random;
 
for (var i = 0; i < x; i++) {
 
    // This loop is for the row
    for (var p = 0; p < y; p++) {
 
        // Here you would randomize each
        // element for the array.
        random = Math.floor(Math.random() * 10000) % 2;
        randomNums[i][p] = random;
    }
}
 
for (var i = 0; i < x; i++) {
 
    // This loop is for the row
    for (var p = 0; p < y; p++) {
 
        // Here you would randomize each
        // element for the array.
        process.stdout.write(randomNums[i][p] + "\t");
    }
 
    process.stdout.write("\n");
}
process.stdout.write("\n");
 
// concatenation of the bits in the matrix row
// to form a single data packet
for (var i = 0; i < x; i++) {
 
    temp[i] = "" + (randomNums[i][0]);
 
    for (var j = 0; j < y; ++j) {
 
        s1 = temp[i];
        s2 = "" + (randomNums[i][j]);
 
        s = s1 + s2;
 
        temp[i] = s;
        k++;
    }
 
    temp[i] = temp[i].substring(1);
    l1.push(temp[i]);
}
 
showlist(l1);
 
 
// This code is contributed by phasing17

Example: 
 

Input: 
Rows: 
10 
Columns: 
10 
Output: 
0 1 1 0 1 1 0 1 0 1 
0 0 0 0 0 0 1 1 0 0 
0 1 1 0 1 1 0 1 1 1 
0 1 0 0 0 0 1 0 0 0 
0 0 1 1 0 0 1 0 0 0 
0 0 0 0 1 0 1 1 1 0 
0 0 1 1 0 0 1 1 0 1 
1 1 0 0 1 1 0 0 0 1 
1 1 0 0 1 0 1 1 1 1 
0 1 0 1 1 0 0 1 1 1 
list of frame packets are show as below :-> 
0110110101 
0000001100 
0110110111 
0100001000 
0011001000 
0000101110 
0011001101 
1100110001 
1100101111 
0101100111 
 

Reference: Slow Start Backoff Algorithm for Ad-Hoc Wireless Networks


Article Tags :