# Least Recently Used Page Replacement Algorithm Python Program

Page replacement algorithms are required in OS to employ paging techniques to manage memory to determine which page has to be removed when a new page is added. Every time a new page is referenced but not already in memory, a page fault happens, and the OS replaces the referenced page with the latest referenced page. Various page replacement algorithms offer various recommendations for selecting the appropriate replacement page. Every algorithm strives to decrease the number of page faults.

The Least Recently Used (LRU) algorithm is a Greedy algorithm. In this algorithm, the page to be replaced is least recently used. The idea is based on the locality of reference. The least recently used page is considered to be of less importance.

Let us take an example. We have a string determining the sequence of page reference 3 0 1 2 1 3 5 1 2 5 0 1 2. We have the capacity of page slots equal to 3.

• At first, all page slots are empty, so pages 3 0 1 are accommodated in the empty slots -> 3 Page faults
• 2 is not in the memory of the least used page will be replaced, i.e., 3 -> 1 Page fault.
• when 1 came, it is already present in the memory so that no page will be replaced, but 1 is now the most recently used page hence the sequence of page referencing becomes 0 3 1 instead of 0 1 3 ->0 Page fault
• 3 is already in the memory, so no page fault occurs. Page referencing sequence is 0 1 3 -> 0 Page fault.
• 5 is not present in the memory, so it will replace 0 as it is the least used page -> 1 Page Fault
• We can continue this process and calculate the total number of page faults for the given page referencing sequence.

## Approach-1

Let C be the capacity of the total number of pages the memory can hold. Let S be the set containing the pages currently present in the memory.

1. We will start by traversing the given sequence of page referencing.
• We will check if the memory has fewer pages than its capacity to hold
• We will insert the current page into the set S. This will continue until the memory capacity set is filled.
• Together with these steps, we will maintain the record of the recently used pages. We will execute this step by creating a map of the indices of each page.
• Also, we have to count the number of page faults. So, every time we update the set S we will increment the number of page faults.
• Else,
If the set S has reached capacity, we will check if the current page is already present in the set. If this is True, we will do nothing.
If this is not True,
• We will find the page from the pages in the set which is least recently used. We will find this using the index map. Once we find the page, we will replace that page with the current page.
• Hence, we need to update the set S by adding the current page and removing the LRU page.
• Since we have updated the set S, we need to increment the page faults count.
2. Also, we have to update the index of the current page in the indices map.
3. At last, we will return the total number of page faults.
4. Below is the Python program to implement the above algorithm.

Code

Output:

```s = {3}
s = {0, 3}
s = {0, 1, 3}
s = {0, 1, 2}
s = {0, 1, 2}
s = {3, 1, 2}
s = {1, 3, 5}
s = {1, 3, 5}
s = {1, 2, 5}
s = {1, 2, 5}
s = {0, 2, 5}
s = {0, 1, 5}
s = {0, 2, 1}
The total number of page faults is: 10
```

Time Complexity: The average time complexity of the set and the map operations is O(1). In the worst-case scenario, the complexity will go to O(n). But, since O(n) is the dominant term, this program's time complexity is O(n).

Space Complexity: The space complexity will equal the given capacity of holding pages.

## Approach-2: (Without using HashMap)

We will not use a set in this approach. Below is the algorithmic approach:

• This program performs the page replacement technique by employing the deque data structure.
• The method keeps a certain quantity of pages in the memory based on the given capacity and replaces them when new pages are referenced.
• The code maintains track of how many page faults have occurred throughout the execution by simulating page queries using an array.
• The pages currently in the memory are kept up to date using the deque data structure.
• The code outputs the total amount of page faults that happened throughout the simulation of the page requests.

Below is the code to implement this algorithm in Python:

Code

Output:

```s = 
s = [3, 0]
s = [3, 0, 1]
s = [0, 1, 2]
s = [0, 2, 1]
s = [2, 1, 3]
s = [1, 3, 5]
s = [3, 5, 1]
s = [5, 1, 2]
s = [1, 2, 5]
s = [2, 5, 0]
s = [5, 0, 1]
s = [0, 1, 2]
The total number of page faults is: 10
```

Time Complexity: The time complexity of this LRU algorithm is O(n). The reason behind this time complexity is that we have used a linear for loop to traverse the given sequence of page referencing. The page faults are executed in constant time.

Space Complexity: The space complexity of this LRU algorithm is O(n). Here n is the length of the given sequence of page referencing.

In this program, we can also determine the total number of page hits that have taken place throughout the simulation. We need to create another variable that stores the count for the page hits.

The number of times the current page was already in the memory is called page hits.

### Feedback   