Skip to content

Commit a800a85

Browse files
authored
Update README.md
1 parent d3fe2e5 commit a800a85

File tree

1 file changed

+53
-99
lines changed

1 file changed

+53
-99
lines changed

README.md

Lines changed: 53 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Data Structures and Algorithms
1+
![image](https://github.com/ashfarhangi/Data-Structures-and-Algorithms/assets/46503022/dd996054-507f-46a1-bd25-e7c28aed5d80)# Data Structures and Algorithms
22

33
These notes can be used to find top patterns for each category of questions and concepts. Note that reading these notes can be much faster than watching videos. Also, it can be used to refresh your knowledge or understand the important parts of each pattern. This repository provides the key information presented from all these sources:
44

@@ -20,6 +20,12 @@ These notes can be used to find top patterns for each category of questions and
2020
Source: [Data Structures, Algorithms, and Machine Learning Optimization](https://www.oreilly.com/library/view/data-structures-algorithms/9780137644889/)
2121
# 2. Big O Notion
2222

23+
The *rate of increase* of an algorithm is also referred to as the **order** of the algorithm
24+
25+
For example, instead of saying "this relationship has a linear rate of increase", we could instead say, "The *order* of this relationship is linear".
26+
27+
*O Notation*, and you'll see that the "O" in the name refers to the **o**rder of the rate of increase.
28+
2329
![](media/runfi.png)
2430

2531
![](media/runtime.png)
@@ -33,41 +39,25 @@ We keep the *m*. Given that it might be large. ![](media/sol.png)
3339

3440
# 3. Data structures
3541

36-
37-
![](media/types.png)
3842
- Common data structures:
3943
![](media/28a37a2c4d8794faa0b22686c21cbece.png)
4044
- Big O notion
4145
![](media/best.png)
4246

43-
Linked lists are not indexed. Only nodes are linked toghether. ![](media/linked.png)
47+
Linked lists are not indexed. Only nodes are linked together. ![](media/linked.png)
4448

4549
![](media/double.png)
4650

47-
- Stacks are implemented as lists in python. s.append, s.pop
48-
- Queue: beginning and end are available. You can also take a peek of the first one.
49-
- Deques (دک) (Double ended- queue):
51+
- Stacks are implemented as lists in Python. s.append, s.pop
52+
- Queue: beginning and end are available. You can also take a peek at the first one.
53+
- Deques (Double ended- queue):
5054

5155
# 4. Algorithms
5256

53-
The summary of **Data Structures and Algorithms** plus **Interview steps** from Udacity's nano degree "Data Structures and Algorithms".
54-
55-
I’ve included the code for each concept in Python in the code folder. I would suggest to implement short codes for your projects or coding interviews.
57+
The summary of **Data Structures and Algorithms** plus **Interview steps**.
5658

5759
## Solution to any problems
58-
59-
Think in simple terms:
60-
61-
1. What are the inputs
62-
2. What are the outputs
63-
3. Step-by-step ways that can connect the two
64-
65-
**Next:**
66-
67-
Do some examples on paper or on your mind. The trick is to find the correct **relationships** between input and output. Finally, you can test some cases and fine-tune your approach for special cases.
68-
69-
Writing the algorithm can be done by writing the *pseudocode*.
70-
60+
Take a look at a simple question like below:
7161
Question 1: Write an algorithm/program that returns the difference of two dates.
7262

7363
**Step 1: Ask yourself the following questions**
@@ -87,122 +77,86 @@ Tips: Break into simple parts so that we can see our progress. Write simple smal
8777

8878
Now we look into the main topics that are being covered in most interviews/coding challenges. We also look into the patterns that appear in solutions which can be used in new problems. The goal is to understand the fundamentals and try to use them to solve problems.
8979

90-
## Big O Notion
91-
92-
As the input to an algorithm increases, the time required to run the algorithm may also increase.
93-
94-
For example in **Nested Loops**:
95-
96-
\` For i in range(len(n)):
97-
98-
```
99-
For j in range(len(n)):
100-
```
101-
102-
Print(“hello World”) \`
103-
104-
n\^2
105-
106-
Will increases the lines dramatically
107-
108-
The *rate of increase* of an algorithm is also referred to as the **order** of the algorithm
109-
110-
For example, instead of saying "this relationship has a linear rate of increase", we could instead say, "The *order* of this relationship is linear".
111-
112-
*O Notation*, and you'll see that the "O" in the name refers to the **o**rder of the rate of increase.
113-
114-
Length of input to my function
115-
116-
O(2n+2) \> n=10 -\> 22
117-
118-
In n\^2 + 5*n*2+5, the 55 has very little impact on the total efficiency—especially as the input size gets larger and larger. Asking the computer to do 10,005 operations vs. 10,000 operations makes little difference. Thus, it is the n\^2*n*2 that we really care about the most, and the + makes little difference
119-
120-
Interviewer wants us to think about efficiency.
121-
122-
Run time analysis
123-
124-
O(3N) space efficiency (copying code)
125-
126-
Int , float 4 bytes
127-
128-
char 1 byte
12980

13081
## Binary Search
13182
What are time complexity of the following problems?
13283
Finding the smallest of n numbers?
133-
• O(n-1)
134-
Finding the biggest of n numbers?
135-
• O(n-1)
136-
Finding the smallest and the biggest of n numbers?
137-
• O(2n-3)
138-
139-
**Binary search (Middle …. Middle, O (log(n)).:**
14084

141-
![](media/393309480a114e09e85165f26cfdf15d.png)
85+
• Answer: O(n-1) ~ O(n)
86+
87+
Finding the biggest of n numbers?
14288

143-
**Algorithm:** Tricks to solve a problem
89+
• Answer: O(n-1) ~ O(n)
90+
91+
Finding the smallest and the biggest of n numbers?
14492

93+
• Answer: O(2n-3) ~ O(n)
14594
**Q:** How to find number 25 in this a sorted array?
146-
14795
**A:**
14896

14997
**1. Linear search: O(n)**
15098

15199
**2. Binary Search:** O(log(n)+1) ( Why it is called binary search? find a postion of binary)
152100

153-
Middle, Middle, Middle.
101+
![](media/393309480a114e09e85165f26cfdf15d.png)
102+
154103

155104
## Sorting
156105

157106
**Bubble sort:**
158107

159-
Simplest and most inefficient one O(n\^2)
108+
![image](https://github.com/ashfarhangi/Data-Structures-and-Algorithms/assets/46503022/f79577f4-1ee4-4176-8887-c43884c2e741)
109+
![image](https://github.com/ashfarhangi/Data-Structures-and-Algorithms/assets/46503022/bebb63d7-2d01-4653-9b85-8475bf56b8fe)
110+
![image](https://github.com/ashfarhangi/Data-Structures-and-Algorithms/assets/46503022/92b909ff-b87f-4137-9216-cbd0f168a838)
111+
Simple but not efficient
112+
T: $$O(n\^2)$$
113+
S: O(1)
160114

161115
**Merge Sort:**
162116

163-
Using divide and conquer, first, we divide 2 by 2
117+
Divide and Conquer: First, we divide 2 by 2
164118

165119
![](media/d60404983770ccf9700e87bf00057d26.png)
166120

167-
Then we compare the elements
121+
Second, we compare the elements and join the elements
168122

169123
![](media/aedaa435d36c4f0f3cbf29d50034fdd2.png)
170124

171125
Efficiency:
172126

173-
O (n Log (n))
127+
O (n log (n))
174128

175-
Why we are seeing log (n) in our efficiency? Hint: same as binary search problem
129+
**Q:** Why we are seeing log (n) in our efficiency? Hint: same as the binary search problem
176130

177131
**Quick Sort:**
178-
179-
Pick one and move it around
132+
Idea is to use pivots to sort the array.
180133

181134
![](media/efb964d97b2648ca21e04162697e01e1.png)
182135

183-
Why it can be chosen as the most efficient algorithm?
184-
185-
Because on average it will outperform merge sort.
186136

187137
On each step we are performing two moving around.
188138

189139
![](media/2aab5b68c0b0e7ea62486cfe66e08caa.png)
190140

191-
(Pivot = Last element) First move we move
141+
(Pivot = Last element)
192142

193-
1. Select right P
194-
2. PL LS SP (Shift Pivot to left , Swap start and left)
195-
3. Compare Start with pivot. If (Start \> Pivot repeat step 2)
196-
4. Else move to second start
143+
1. Select the rightmost element as the pivot
144+
2. Shift Pivot to left by one element
145+
3. Swap start and left
146+
4. Compare Start with pivot. If (Start \> Pivot repeat step 2)
147+
5. Else move to second start
197148

198149
![](media/b41c1c2d2548c9e6940e3506ec84f34f.png)
199150

200151
![](media/4def5b724f6f15c6b099ce425f8c4e09.png)
201152

202-
I advise to practice this for cementing the algorithm.
203-
204153
![](media/4a3923fde43ff67cc4066b14626677b0.png)
205154

155+
Why it can be chosen as the most efficient algorithm?
156+
157+
Because on average it will outperform merge sort.
158+
159+
206160
**Great no need to move the original pivot anymore (2 is at the right place)**
207161

208162
![](media/c018057d901372ab5b57e93742036550.png)
@@ -213,8 +167,8 @@ New pivot on right. Compare
213167

214168
Moving phase
215169

216-
Everything less than 8 and 10 sweap results in:
217-
Everythin less than 8 are already below 8 so cemented
170+
Everything less than 8 and 10 sweep results in:
171+
Everything less than 8 is already below 8
218172

219173
![](media/c018057d901372ab5b57e93742036550.png)
220174

@@ -224,7 +178,7 @@ O(n2) if it’s already sorted. Why? Wasting time
224178

225179

226180
## Sliding Window
227-
How to tell if its a sliding window problem?
181+
How to tell if it's a sliding window problem?
228182
• Deals with arrays
229183
• Find/calculate among subarrays
230184

@@ -233,11 +187,11 @@ How to tell if its a sliding window problem?
233187

234188
Input: ![[Pasted image 20220910145630.png]]
235189
Output: ![[Pasted image 20220910145753.png]]
236-
Alg1: Bruteforce return the average for every 5 element
190+
Alg1: Bruteforce returns the average for every 5 element
237191
Time: O(N * K)
238-
• Its not optimal because we are calculating the sum of overlapping elements between subarray more than once.
192+
• Its not optimal because we are calculating the sum of overlapping elements between subarrays more than once.
239193
![[Pasted image 20220910150423.png]]
240-
• Why not use a sliding window and substract and add to the sum?
194+
• Why not use a sliding window and subtract and add to the sum?
241195
![[Pasted image 20220910150647.png]]
242196
Alg2: Sliding window
243197

@@ -247,7 +201,7 @@ Alg2: Sliding window
247201

248202
(Programming == tables)
249203

250-
Factorial problem in python:
204+
The factorial problem in Python:
251205

252206
![](media/c75f1ae6443175cde6bff55aaa014eda.png)
253207

@@ -257,9 +211,9 @@ Knapsack problem: Max value for weight limit
257211

258212
![](media/8772bf182b4a29b45fa854704118a58d.png)
259213

260-
Knapsack. Imagine the skyrim world where you can carry a certain weight (50kg) in your bag, how can you gather the most valuable items (3w,500v) with you?
214+
Knapsack. If you have to carry a weight (50kg) in your bag, how can you gather the most valuable items (e.g., 3 as the weight,500 as the value) with you?
261215

262-
How you optimize which items with their weights to carry with you?
216+
How do you optimize which items with their weights to carry with you?
263217

264218
1. Brute force: Check all the solutions and pick the best one: O (2\^n) possible combinations. “exponential time”
265219

0 commit comments

Comments
 (0)