Skip to content

Commit

Permalink
Added W06 materials
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanSol committed Nov 11, 2024
1 parent 4509f6a commit cd6c6e1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
| 03 | Basic Data sturctures | [Slides][Slides_W03] | [Test][WarmUp_test_W03] | [Contest][ContestID_W03] | 28.10.2024 19:00 UTC+3 |
| 04 | Dynamic programming | [Slides][Slides_W04] | [Test][WarmUp_test_W04] | [Contest][ContestID_W04] | 04.11.2024 19:00 UTC+3 |
| 05 | Knapsack problem | [Slides][Slides_W05] | [Test][WarmUp_test_W05] | [Contest][ContestID_W05] | 11.11.2024 19:00 UTC+3 |
| 06 | KMP & Heap | [Slides][Slides_W06] | [Test][WarmUp_test_W06] | [Contest][ContestID_W06] | 18.11.2024 19:00 UTC+3 |
<!---
| 06 | KMP & Heap | [Slides][Slides_W06] | [Test][WarmUp_test_W06] | [Contest][ContestID_W06] | ??.11.2024 19:00 UTC+3 |
| 07 | DFS & BFS | [Slides][Slides_W07] | [Test][WarmUp_test_W07] | [Contest][ContestID_W07] | ??.12.2024 19:00 UTC+3 |
| 08 | Shortest paths | [Slides][Slides_W08] | [Test][WarmUp_test_W08] | [Contest][ContestID_W08] | ??.12.2024 19:00 UTC+3 |
| 09 | RSQ & RMQ | [Slides][Slides_W09] | [Test][WarmUp_test_W09] | [Contest][ContestID_W09] | ??.12.2024 19:00 UTC+3 |
Expand Down
Binary file added week06_kmp_heap/MSAI.2024.Algo.W06.slides.pdf
Binary file not shown.
51 changes: 51 additions & 0 deletions week06_kmp_heap/heap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import random


def sift_up(data, i):
if i == 0:
return
parent = (i - 1) // 2
if data[parent] > data[i]:
data[parent], data[i] = data[i], data[parent]
sift_up(data, parent)


def sift_down(data, i):
child1 = i * 2 + 1
child2 = i * 2 + 2
if child1 >= len(data):
return
if child2 >= len(data):
child_min = child1
else:
child_min = child1 if data[child1] < data[child2] else child2
if data[child_min] < data[i]:
data[i], data[child_min] = data[child_min], data[i]
sift_down(data, child_min)


def heapify(data):
for i in range(len(data) - 1, -1, -1):
sift_down(data, i)


def heappush(data, x):
data.append(x)
sift_up(data, len(data) - 1)


def heappop(data, i=0):
data[i], data[-1] = data[-1], data[i]
res = data.pop()
sift_up(data, i)
sift_down(data, i)
return res


if __name__ == '__main__':
n = random.randint(0, 100)
h = [random.randint(0, 1000) for i in range(n)]

heapify(h)
while len(h) > 0:
print(heappop(h))
55 changes: 55 additions & 0 deletions week06_kmp_heap/substring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
def find_substrings_naive(s, p):
N = len(s)
K = len(p)
substrings = []
for i in range(N - K + 1):
if all([s[i + j] == p[j] for j in range(K)]):
substrings.append(i)
return substrings


def prefix_function(s):
d = [0] * (len(s) + 1)
for i in range(2, len(d)):
d[i] = d[i - 1]
while s[i - 1] != s[d[i]] and d[i] > 0:
d[i] = d[d[i]]
if s[i - 1] == s[d[i]]:
d[i] += 1
return d


def find_substrings_kmp(s, p):
substrings = []
d = prefix_function(p + '$' + s)
for i in range(len(p) + 1, len(d)):
if d[i] == len(p):
substrings.append(i - 2 * len(p) - 1)
return substrings


def find_substrings_native(s, p):
i = -1
substrings = []
while True:
i = s.find(p, i + 1)
if i >= 0:
substrings.append(i)
else:
return substrings

from time import time
if __name__ == '__main__':
#s = input().strip()
#p = input().strip()
s = 'a' * 100000 + 'b'
p = 'a' * 50000 + 'b'

t1 = time()
print('Python: ', ' '.join(map(str, find_substrings_native(s, p))))
t2 = time()
#print('Naive : ', ' '.join(map(str, find_substrings_naive(s, p))))
t3 = time()
print('KMP : ', ' '.join(map(str, find_substrings_kmp(s, p))))
t4 = time()
print(f'{t2 - t1:.8f}, {t3 - t2:.8f}, {t4 - t3:.8f}')

0 comments on commit cd6c6e1

Please sign in to comment.