Skip to content

Commit

Permalink
dsa
Browse files Browse the repository at this point in the history
- LRU
- data structure (discrete memory location)
- linked list
  • Loading branch information
xy-241 committed Jun 26, 2024
1 parent 6a5d23e commit 3418b7f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions content/Data Structure/Data Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Author Profile:
tags:
- dsa
Creation Date: 2023-10-07T16:38:00
Last Date: 2023-12-13T18:13:47+08:00
Last Date: 2024-06-26T19:00:53+08:00
References:
---
## Abstract
Expand All @@ -21,7 +21,7 @@ References:
### Continuous Memory
- Elements are stored one next to another in the[[Virtual Memory]]

### Discrete Memory
### Discrete Memory Location
- Elements are scattered around the [[Virtual Memory]]
- The exact location depends on [[OS]]'s **Memory Management**

Expand Down
11 changes: 5 additions & 6 deletions content/Data Structure/Linked List.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,23 @@ tags:
- dsa
- java
Creation Date: 2023-08-05T14:45:43+08:00
Last Date: 2024-06-25T23:47:43+08:00
Last Date: 2024-06-26T19:04:46+08:00
References:
---
## Abstract
---
![[linked_list.png|700]]
- A [[Data Structure#Linear]] collection of elements of the same [[Datatype]] that stored in [[Data Structure#Discrete Memory]]. Each node contains a [[Pointer]] to the next node
- A [[Data Structure#Linear]] collection of elements of the same [[Datatype]] that stored in [[Data Structure#Discrete Memory Location]]. Each node contains a [[Pointer]] that **references** the **next node** in the **sequence**


>[!success] Adjustable size with minimal impact
> If we want to add in a new node, we just need to modify the pointer of the previous node & the pointer of the next node - in **constant time**. Since the connection between the 2 nodes are via **Pointer**, we can have nodes all around the [[Main Memory]]. This means we can add more nodes as long as there is free memory size that can fit a single node.
>[!success] Efficiently adjustable size
> If we want to add in a new node, we only need to modify the pointer of the previous node and the next node, which takes **constant time**. Since nodes are connected via pointers, they can be located anywhere in [[Main Memory]]. This allows us to add more nodes as long as there is **free memory space sufficient for a single node**.
>
> ![[linked_list_memory_operation.gif]]


>[!attention] Cache Miss!!!
> Since the connection between 2 nodes are via Pointer, the nodes are scattered around the Main Memory. This means we can't make use of [[CPU Cache#Cache Locality]] and this results in [[CPU Cache#Cache Miss]].
> Since the connection between 2 nodes are via Pointer, the nodes are scattered around the Main Memory. This means we can't make use of [[CPU Cache#Cache Locality]] and this results in a very high rate [[CPU Cache#Cache Miss]].
>
> ![[linked_list_cache_miss.gif]]
Expand Down
15 changes: 9 additions & 6 deletions content/cp/data_structure/LRU.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Author:
Author Profile:
- https://linkedin.com/in/xinyang-yu
Creation Date: 2023-02-02T13:56:00
Last Date: 2024-06-25T23:57:21+08:00
Last Date: 2024-06-26T18:48:38+08:00
tags:
- cp
draft:
Expand All @@ -30,9 +30,13 @@ draft:
[Past Solutions](https://www.notion.so/xy241-dsa/LRU-Cache-7cef86ef560f4e768a24b9043256ae96?pvs=4)
## Idea
---
- The idea here is to use a [[Hash Map]] to keep a mapping between the key and the value. And we use a [[Linked List#Double Linked List]] to keep track the least recently used key-value pair and most recently used key-value pair in constant time
- The value in the hash map is mapped to a node inside the double linked list, so we are able to locate the least recently used key-value pair in linear time
- We also make use of [[Linked List#Virtual Node]] to make the linked list operations easy to manage.
- A [[Linked List#Double Linked List]] allows us to perform **LRU cache manipulation** in $O(1)$ time, regardless of where the cache is located. However, it takes $O(n)$ time to **find the cache given a key**. This can be solved with a [[Hash Map]] that stores a **mapping** between the **key** and the **doubly linked list node** that **holds the cache value** for that particular key

>[!tip]
> We also make use of [[Linked List#Virtual Node]] to make the linked list operations easy to manage.




## Space & Time Analysis
Expand Down Expand Up @@ -99,13 +103,12 @@ class LRUCache {
if (capacity <= 0) {
Node currLast = tail.prev;
removeNode(currLast);
map.remove(currLast.key);
map.remove(currLast.key); // remember to remove from the hash map too!
capacity++;
}

// New key added
Node newNode = new Node(key, value);

map.put(key, newNode);
addToHead(newNode);
capacity--;
Expand Down

0 comments on commit 3418b7f

Please sign in to comment.