Skip to content

Commit 50727f5

Browse files
committed
Decide structure of README
1 parent fd8dc0a commit 50727f5

File tree

6 files changed

+78
-58
lines changed

6 files changed

+78
-58
lines changed

src/algorithms/patternFinding/README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ in text editors when searching for a pattern, in computational biology sequence
66
in NLP problems, and even for looking for file patterns for effective file management.
77
It is hence crucial that we develop an efficient algorithm.
88

9+
![KMP](../../../assets/kmp.png)
10+
Image Source: GeeksforGeeks
11+
12+
## Analysis
13+
**Time complexity**:
14+
915
Naively, we can look for patterns in a given sequence in O(nk) where n is the length of the sequence and k
1016
is the length of the pattern. We do this by iterating every character of the sequence, and look at the
1117
immediate k-1 characters that come after it. This is not a big issue if k is known to be small, but there's
@@ -15,9 +21,9 @@ KMP does this in O(n+k) by making use of previously identified sub-patterns. It
1521
by first processing the pattern input in O(k) time, allowing identification of patterns in
1622
O(n) traversal of the sequence. More details found in the src code.
1723

18-
![KMP](../../../assets/kmp.png)
19-
Image Source: GeeksforGeeks
20-
24+
**Space complexity**: O(k) auxiliary space to store suffix that matches with prefix of the pattern string
2125

22-
If you have trouble understanding the implementation,
23-
here is a good [video](https://www.youtube.com/watch?v=EL4ZbRF587g).
26+
## Notes
27+
A detailed illustration of how the algorithm works is shown in the code.
28+
But if you have trouble understanding the implementation,
29+
here is a good [video](https://www.youtube.com/watch?v=EL4ZbRF587g) as well.

src/algorithms/sorting/countingSort/CountingSort.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,34 @@
11
package src.algorithms.sorting.countingSort;
22

33
/**
4-
* Stable implementation of Counting Sort.
5-
*
6-
* <p></p>
4+
* <p></p> Stable implementation of Counting Sort.
75
*
8-
* Brief Description: <br>
6+
* <p></p> Brief Description: <br>
97
* Counting sort is a non-comparison based sorting algorithm and isn't bounded by the O(nlogn) lower-bound
108
* of most sorting algorithms. <br>
119
* It first obtains the frequency map of all elements (ie counting the occurrence of every element), then
1210
* computes the prefix sum for the map. This prefix map tells us which position an element should be inserted. <br>
1311
* Ultimately, each group of elements will be placed together, and the groups in succession, in the sorted output.
1412
*
15-
* <p></p>
16-
*
17-
* Assumption for use: <br>
13+
* <p></p> Assumption for use: <br>
1814
* To perform counting sort, the elements must first have total ordering and their rank must be known.
1915
*
20-
* <p></p>
21-
*
22-
* Implementation Invariant: <br>
16+
* <p></p> Implementation Invariant: <br>
2317
* At the end of the ith iteration, the ith element from the back will be placed in its rightful position.
2418
*
2519
* <p></p>
26-
*
2720
* COMMON MISCONCEPTION: Counting sort does not require total ordering of elements since it is non-comparison based.
2821
* This is incorrect. It requires total ordering of elements to determine their relative positions in the sorted output.
2922
* In fact, in conventional implementation, the total ordering property is reflected by virtue of the structure
3023
* of the frequency map.
3124
*
32-
* <p></p>
33-
*
34-
* Complexity Analysis: <br>
25+
* <p></p> Complexity Analysis: <br>
3526
* Time: O(k+n)=O(max(k,n)) where k is the value of the largest element and n is the number of elements. <br>
3627
* Space: O(k+n)=O(max(k,n)) <br>
3728
* Counting sort is most efficient if the range of input values do not exceed the number of input values. <br>
3829
* Counting sort is NOT AN IN-PLACE algorithm. For one, it requires additional space to store freq map. <br>
3930
*
40-
* <p></p>
41-
*
42-
* Note: Implementation deals with integers but the idea is the same and can be generalised to other objects,
31+
* <p></p> Note: Implementation deals with integers but the idea is the same and can be generalised to other objects,
4332
* as long as what was discussed above remains true.
4433
*/
4534
public class CountingSort {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Counting Sort
2+
3+
Counting sort is a non-comparison based sorting algorithm and isn't bounded by the O(nlogn) lower-bound
4+
of most sorting algorithms.
5+
It first obtains the frequency map of all elements (ie counting the occurrence of every element), then
6+
computes the prefix sum for the map. This prefix map tells us which position an element should be inserted.
7+
Ultimately, each group of elements will be placed together, and the groups in succession, in the sorted output.
8+
9+
## Complexity Analysis
10+
Time: O(k+n)=O(max(k,n)) where k is the value of the largest element and n is the number of elements. <br>
11+
Space: O(k+n)=O(max(k,n)) <br>
12+
Counting sort is most efficient if the range of input values do not exceed the number of input values. <br>
13+
Counting sort is NOT AN IN-PLACE algorithm. For one, it requires additional space to store freq map. <br>
14+
15+
## Notes
16+
COMMON MISCONCEPTION: Counting sort does not require total ordering of elements since it is non-comparison based.
17+
This is incorrect. It requires total ordering of elements to determine their relative positions in the sorted output.
18+
In fact, in conventional implementation, the total ordering property is reflected by virtue of the structure
19+
of the frequency map.

src/dataStructures/heap/README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Heap
22

3-
## Binary Heap
3+
### Binary Heap
44
A binary heap is often used to introduce the concept of heaps. It is a tree-based data structure that satisfies the
55
following properties:
66
1. Complete binary tree - every level, except possibly the last, is completely filled
@@ -11,7 +11,7 @@ making it suitable as an underlying implementation of the ADT, priority queue.
1111

1212
![max heap](../../../assets/max_heap.png)
1313

14-
## Array-based Heap
14+
### Array-based Heap
1515
The complete binary tree property actually allows the heap to be implemented as a contiguous array (since no gaps!).
1616
The parent-child relationships are derived based on the indices of the elements.
1717

@@ -22,7 +22,7 @@ That said, in practice, the array-based implementation of a heap often provides
2222
former, in cache efficiency and memory locality. This is due to its contiguous memory layout. As such,
2323
the implementation shown here is a 0-indexed array-based heap.
2424

25-
## Relevance of increaseKey and decreaseKey operations
25+
### Relevance of increaseKey and decreaseKey operations
2626
The decision not to include explicit "decrease key" and "increase key" operations in the standard implementations of
2727
heaps in Python and Java is primarily due to design choices and considerations of the typical intended use cases.
2828
Further, this operation, without augmentation, would take O(n) due to having to search for the object to begin with
@@ -34,6 +34,10 @@ This is worth a mention. In cases like Dijkstra where the concern is having to m
3434
by constantly updating rather than insertion of all edges, it is not really a big issue. After all, the log factor
3535
in the order of growth will turn log(E) = log(V^2) in the worst case of a complete graph, to 2log(V) = O(log(V)).
3636

37+
## Complexity Analysis
38+
Time: O(log(n)) in general for most native operations,
39+
except heapify (building a heap from a sequence of elements) that takes o(n) <br>
40+
Space: O(n) where n is the number of elements (whatever the structure, it must store at least n nodes) <br>
3741

3842
## Notes
3943
1. Heaps are often presented as max-heaps (eg. in textbooks), hence the implementation follows a max-heap structure

src/dataStructures/linkedList/LinkedList.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
package src.dataStructures.linkedList;
22

33
/**
4-
* There are many variations when it comes to implementing linked lists. Here's mine.
5-
* A linked list that tracks both head and tail may improve the complexity of some of the methods below
6-
* (e.g being able to branch off and create a new Linked List in O(1) rather than iterating to find size of branched off list)
7-
*
8-
* Constructors:
9-
* LinkedList() -- Initialise a link list with a null head; size = 0
10-
* LinkedList(Node<T> head) -- given a node, make this node the head of a new linked list; iterate to find the size
4+
* <p></p>
5+
* There are many variations when it comes to implementing linked lists. Here's ours. <br>
6+
* A linked list that tracks both head and tail may improve the complexity of some methods below <br>
7+
* (e.g being able to branch off and create a new Linked List in O(1)
8+
* rather than iterating to find size of branched off list)
9+
* <p></p>
10+
* Constructors: <br>
11+
* LinkedList() -- Initialise a link list with a null head; size = 0 <br>
12+
* LinkedList(Node<T> head) -- given a node, make node the head of a new linked list;
13+
* size is found by iterating to the end from this specified head <br>
1114
* LinkedList(Node<T>head, int size) -- given a head node and size of linked list specified;
1215
* made private to avoid client from constructing a linked list with invalid size
13-
*
14-
* Callable methods are:
15-
* size() -- Gets the size of the linked list
16-
* insertFront(T object) -- inserts the object at the front of the linked list
17-
* insertEnd(T object) -- inserts the object at the end of the linked list
18-
* insert(T object, int idx) -- inserts the object at the specified index of the linked list
19-
* remove(int idx) -- remove the node at the specified index
20-
* delete(T object) -- delete the 1st encounter of the specified object from the linked list
21-
* pop() -- remove the last node from the linked list
22-
* poll() -- remove the first node from the linked list
23-
* search(T object) -- search for the 1st encounter of the node that holds the specified object
24-
* get(int idx) -- get the node at the specified index
25-
* reverse() -- reverse the linked list (head of linked list now starts from the back)
26-
* sort() -- sorts the linked list by their natural order
27-
*
16+
* <p></p>
17+
* Callable methods are: <br>
18+
* size() -- Gets the size of the linked list <br>
19+
* insertFront(T object) -- inserts the object at the front of the linked list <br>
20+
* insertEnd(T object) -- inserts the object at the end of the linked list <br>
21+
* insert(T object, int idx) -- inserts the object at the specified index of the linked list <br>
22+
* remove(int idx) -- remove the node at the specified index <br>
23+
* delete(T object) -- delete the 1st encounter of the specified object from the linked list <br>
24+
* pop() -- remove the last node from the linked list <br>
25+
* poll() -- remove the first node from the linked list <br>
26+
* search(T object) -- search for the 1st encounter of the node that holds the specified object <br>
27+
* get(int idx) -- get the node at the specified index <br>
28+
* reverse() -- reverse the linked list (head of linked list now starts from the back) <br>
29+
* sort() -- sorts the linked list by their natural order <br>
30+
*
2831
* @param <T> generic type for objects to be stored in the linked list
2932
*/
3033
public class LinkedList<T extends Comparable<T>> {
@@ -241,9 +244,9 @@ public void reverse() {
241244
/**
242245
* Sorts the linked list by the natural order of the elements.
243246
* Generally, merge sort is the most efficient sorting algorithm for linked lists.
244-
* Addressing a random node in the linked list incurrs O(n) time complexity.
247+
* Accessing a random node in the linked list incurs O(n) time complexity.
245248
* This makes sort algorithms like quicksort and heapsort inefficient since they rely
246-
* on the O(1) lookup time of an array.
249+
* on the O(1) lookup time, like in an array.
247250
*
248251
* A good video to visualise this algorithm can be found
249252
* <a = https://www.youtube.com/watch?v=JSceec-wEyw, href = "url">here</a>.

src/dataStructures/linkedList/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ linked lists are stored across memory and are connected to each other via pointe
1818
*Source: BeginnersBook*
1919

2020
## Analysis
21-
Some common operations involved in a linked list includes looking up elements in a linked list and inserting elements into a linked list.
21+
Time Complexity: Depends on operations, O(n) in general for most operations.
2222

23-
Searching a linked list requires O(n) time complexity whereas inserting into a linked list from a specified index requires O(n) time complexity.
23+
Most operations require iterating the linked list. For instance,
24+
searching for an element in a linked list requires iterating from the head to the tail, incurring O(n)
25+
time complexity in the worst and average case. The best case would be O(1), for instance, when the head is the desired
26+
element.
2427

25-
## Notes / Further Details / Conclusion
28+
Space Complexity: O(n) where n is the size of the linked list.
29+
30+
## Notes
2631

2732
### Memory Requirements & Flexibility
2833
As a contiguous block of memory must be allocated for an array, its size is fixed.
@@ -38,12 +43,6 @@ we end up needing to store more elements at run time.
3843
However, linked list gives us the option of adding new nodes at run time based on our requirements,
3944
allowing us to allocate memory to store items dynamically, giving us more flexibility.
4045

41-
### Conclusion
42-
You should aim to use linked list in scenarios where you cannot predict how many elements you need to store
43-
or if you require constant time insertions to the list.
44-
45-
However, arrays would be preferred if you already know the amount of elements you need to store ahead of time.
46-
It would also be preferred if you are conducting a lot of look up operations.
4746

4847
## Linked List Variants
4948
The lookup time within a linked list is its biggest issue.

0 commit comments

Comments
 (0)