Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions graphs/kahns_algorithm_topo.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from collections import deque


def topological_sort(graph: dict[int, list[int]]) -> list[int] | None:
"""
Perform topological sorting of a Directed Acyclic Graph (DAG)
Expand All @@ -21,10 +24,17 @@ def topological_sort(graph: dict[int, list[int]]) -> list[int] | None:

>>> graph_with_cycle = {0: [1], 1: [2], 2: [0]}
>>> topological_sort(graph_with_cycle)

>>> sparse_graph = {10: [20], 20: []}
>>> topological_sort(sparse_graph)
[10, 20]

>>> sparse_cycle = {10: [20], 20: [10]}
>>> topological_sort(sparse_cycle)
"""

indegree = [0] * len(graph)
queue = []
indegree = dict.fromkeys(graph, 0)
queue: deque[int] = deque()
topo_order = []
processed_vertices_count = 0

Expand All @@ -34,13 +44,13 @@ def topological_sort(graph: dict[int, list[int]]) -> list[int] | None:
indegree[i] += 1

# Add all vertices with 0 indegree to the queue
for i in range(len(indegree)):
if indegree[i] == 0:
queue.append(i)
for vertex, count in indegree.items():
if count == 0:
queue.append(vertex)

# Perform BFS
while queue:
vertex = queue.pop(0)
vertex = queue.popleft()
processed_vertices_count += 1
topo_order.append(vertex)

Expand Down
8 changes: 4 additions & 4 deletions maths/matrix_exponentiation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Matrix Exponentiation"""

import timeit

"""
Matrix Exponentiation is a technique to solve linear recurrences in logarithmic time.
You read more about it here:
https://zobayer.blogspot.com/2010/11/matrix-exponentiation.html
https://www.hackerearth.com/practice/notes/matrix-exponentiation-1/
"""

from __future__ import annotations

import timeit


class Matrix:
def __init__(self, arg: list[list] | int) -> None:
Expand Down
30 changes: 21 additions & 9 deletions searches/ternary_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def ite_ternary_search(array: list[int], target: int) -> int:
-1
>>> ite_ternary_search([.1, .4 , -.1], .1)
0
>>> test_list_large = list(range(100))
>>> ite_ternary_search(test_list_large, 65)
65
>>> ite_ternary_search(test_list_large, 105)
-1
"""

left = 0
Expand All @@ -90,22 +95,22 @@ def ite_ternary_search(array: list[int], target: int) -> int:
if right - left < precision:
return lin_search(left, right, array, target)

one_third = (left + right) // 3 + 1
two_third = 2 * (left + right) // 3 + 1
one_third = left + (right - left) // 3
two_third = right - (right - left) // 3

if array[one_third] == target:
return one_third
elif array[two_third] == target:
return two_third

elif target < array[one_third]:
right = one_third - 1
right = one_third
elif array[two_third] < target:
left = two_third + 1

else:
left = one_third + 1
right = two_third - 1
right = two_third
return -1


Expand Down Expand Up @@ -133,24 +138,31 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
-1
>>> rec_ternary_search(0, 3, [.1, .4 , -.1], .1)
0
>>> test_list_large = list(range(100))
>>> rec_ternary_search(0, len(test_list_large), test_list_large, 65)
65
>>> rec_ternary_search(20, 80, test_list_large, 65)
65
>>> rec_ternary_search(20, 80, test_list_large, 15)
-1
"""
if left < right:
if right - left < precision:
return lin_search(left, right, array, target)
one_third = (left + right) // 3 + 1
two_third = 2 * (left + right) // 3 + 1
one_third = left + (right - left) // 3
two_third = right - (right - left) // 3

if array[one_third] == target:
return one_third
elif array[two_third] == target:
return two_third

elif target < array[one_third]:
return rec_ternary_search(left, one_third - 1, array, target)
return rec_ternary_search(left, one_third, array, target)
elif array[two_third] < target:
return rec_ternary_search(two_third + 1, right, array, target)
else:
return rec_ternary_search(one_third + 1, two_third - 1, array, target)
return rec_ternary_search(one_third + 1, two_third, array, target)
else:
return -1

Expand All @@ -165,7 +177,7 @@ def rec_ternary_search(left: int, right: int, array: list[int], target: int) ->
assert collection == sorted(collection), f"List must be ordered.\n{collection}."
target = int(input("Enter the number to be found in the list:\n").strip())
result1 = ite_ternary_search(collection, target)
result2 = rec_ternary_search(0, len(collection) - 1, collection, target)
result2 = rec_ternary_search(0, len(collection), collection, target)
if result2 != -1:
print(f"Iterative search: {target} found at positions: {result1}")
print(f"Recursive search: {target} found at positions: {result2}")
Expand Down