# 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
backoff window size = W0
else
backoff window size = current backoff window size ÷ 2
else
backoff window size = current backoff window size-W0
else
if current backoff window size<Wm then
backoff window size = current backoff window size × 2
else frame lost due to collision or interference
if backoff window size = Wn then
backoff window size = Wn
else
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 Tthe hreshold 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
```

Implementation of the Slow-Start Mechanism :-

 `#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` `Wi, ``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` `(Wi <= Wn) { ` `                ``if` `(Wi == Wo) { ` `                    ``backoff_win_size = Wo; ` `                    ``cout << ``"Backoff Window Size is:-->"`  `                            ``<< backoff_win_size << endl; ` `                ``} ` `                ``else` `{ ` `                    ``backoff_win_size = Wi / 2; ` `                    ``cout << ``"Backoff Window Size is:-->"`  `                            ``<< backoff_win_size << endl; ` `                ``} ` `            ``} ` `            ``else` `{ ` `                ``backoff_win_size = Wi - Wo; ` `                ``cout << ``"Backoff Window Size is:-->"`  `                        ``<< backoff_win_size << endl; ` `            ``} ` `        ``} ` `        ``else` `{ ` `            ``if` `(Wi < Wn) { ` `                ``backoff_win_size = Wi * 2; ` `                ``cout << ``"Backoff Window Size is:-->"`  `                        ``<< backoff_win_size << endl; ` `            ``} ` `            ``else` `{ ` `                ``cout << ``"Frame lost due to collision"` `                        ``<<``" or interference"``; ` `            ``} ` `            ``if` `(backoff_win_size == Wn) { ` `                ``backoff_win_size = Wn; ` `                ``cout << ``"Backoff Window Size is:-->"`  `                        ``<< backoff_win_size << endl ` `                            ``<< endl; ` `            ``} ` `            ``else` `{ ` `                ``backoff_win_size = Wi + Wo; ` `                ``cout << ``"Backoff Window Size is:-->"`  `                        ``<< backoff_win_size << endl; ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `Wm, Wo, Wn, Wi; ` `    ``int` `array_ACK; ` ` `  `    ``Wo = 32; ``// Initial backoff window size ` `    ``Wn = 1024; ``// Maximum backoff window size ` ` `  `    ``// Wi 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 >> Wi; ` `    ``signal``(array_ACK); ` `    ``Slow_Start_Backoff(Wm, Wo, Wn, Wi, array_ACK); ` ` `  `    ``return` `0; ` `} `

How the data sent in the form of packets through the network :-

Any type of data gets converted into the binary formate and this binary format contains 0’s and 1’s bytes
which get divided in 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 which is going to be transmitted.

Implementation of the Random Matrix Generator :-

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

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : devashishchoudhary

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.