# Slow Start Backoff Algorithm for Ad-Hoc

• Last Updated : 23 Sep, 2022

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:-

## CPP

 `#include ``#include ``#include ``#include ``#include ``#include ` `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;` `    ``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

 `// 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

 `# 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#

 `// 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``;` `    ``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.`

## Javascript

 `// 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:-

## CPP

 `// C++ program to construct the``// Random Matrix Generator.``#include ``#include ``#include ``#include ``#include ``using` `namespace` `std;` `void` `showlist(list l1)``{``    ``list::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 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]);``        ` `        ``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;``}`

## Javascript

 `// 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]);` `    ``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

My Personal Notes arrow_drop_up