Merge branch 'TheAlgorithms:master' into master

This commit is contained in:
Muhammad Junaid Khalid 2024-10-05 12:50:46 +05:00 committed by GitHub
commit bda983488e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 172 additions and 25 deletions

View File

@ -1,36 +1,61 @@
def topological_sort(graph): def topological_sort(graph: dict[int, list[int]]) -> list[int] | None:
""" """
Kahn's Algorithm is used to find Topological ordering of Directed Acyclic Graph Perform topological sorting of a Directed Acyclic Graph (DAG)
using BFS using Kahn's Algorithm via Breadth-First Search (BFS).
Topological sorting is a linear ordering of vertices in a graph such that for
every directed edge u v, vertex u comes before vertex v in the ordering.
Parameters:
graph: Adjacency list representing the directed graph where keys are
vertices, and values are lists of adjacent vertices.
Returns:
The topologically sorted order of vertices if the graph is a DAG.
Returns None if the graph contains a cycle.
Example:
>>> graph = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []}
>>> topological_sort(graph)
[0, 1, 2, 3, 4, 5]
>>> graph_with_cycle = {0: [1], 1: [2], 2: [0]}
>>> topological_sort(graph_with_cycle)
""" """
indegree = [0] * len(graph) indegree = [0] * len(graph)
queue = [] queue = []
topo = [] topo_order = []
cnt = 0 processed_vertices_count = 0
# Calculate the indegree of each vertex
for values in graph.values(): for values in graph.values():
for i in values: for i in values:
indegree[i] += 1 indegree[i] += 1
# Add all vertices with 0 indegree to the queue
for i in range(len(indegree)): for i in range(len(indegree)):
if indegree[i] == 0: if indegree[i] == 0:
queue.append(i) queue.append(i)
# Perform BFS
while queue: while queue:
vertex = queue.pop(0) vertex = queue.pop(0)
cnt += 1 processed_vertices_count += 1
topo.append(vertex) topo_order.append(vertex)
for x in graph[vertex]:
indegree[x] -= 1
if indegree[x] == 0:
queue.append(x)
if cnt != len(graph): # Traverse neighbors
print("Cycle exists") for neighbor in graph[vertex]:
else: indegree[neighbor] -= 1
print(topo) if indegree[neighbor] == 0:
queue.append(neighbor)
if processed_vertices_count != len(graph):
return None # no topological ordering exists due to cycle
return topo_order # valid topological ordering
# Adjacency List of Graph if __name__ == "__main__":
graph = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []} import doctest
topological_sort(graph)
doctest.testmod()

View File

@ -7,6 +7,8 @@ the Binet's formula function because the Binet formula function uses floats
NOTE 2: the Binet's formula function is much more limited in the size of inputs NOTE 2: the Binet's formula function is much more limited in the size of inputs
that it can handle due to the size limitations of Python floats that it can handle due to the size limitations of Python floats
NOTE 3: the matrix function is the fastest and most memory efficient for large n
See benchmark numbers in __main__ for performance comparisons/ See benchmark numbers in __main__ for performance comparisons/
https://en.wikipedia.org/wiki/Fibonacci_number for more information https://en.wikipedia.org/wiki/Fibonacci_number for more information
@ -17,6 +19,9 @@ from collections.abc import Iterator
from math import sqrt from math import sqrt
from time import time from time import time
import numpy as np
from numpy import ndarray
def time_func(func, *args, **kwargs): def time_func(func, *args, **kwargs):
""" """
@ -230,6 +235,88 @@ def fib_binet(n: int) -> list[int]:
return [round(phi**i / sqrt_5) for i in range(n + 1)] return [round(phi**i / sqrt_5) for i in range(n + 1)]
def matrix_pow_np(m: ndarray, power: int) -> ndarray:
"""
Raises a matrix to the power of 'power' using binary exponentiation.
Args:
m: Matrix as a numpy array.
power: The power to which the matrix is to be raised.
Returns:
The matrix raised to the power.
Raises:
ValueError: If power is negative.
>>> m = np.array([[1, 1], [1, 0]], dtype=int)
>>> matrix_pow_np(m, 0) # Identity matrix when raised to the power of 0
array([[1, 0],
[0, 1]])
>>> matrix_pow_np(m, 1) # Same matrix when raised to the power of 1
array([[1, 1],
[1, 0]])
>>> matrix_pow_np(m, 5)
array([[8, 5],
[5, 3]])
>>> matrix_pow_np(m, -1)
Traceback (most recent call last):
...
ValueError: power is negative
"""
result = np.array([[1, 0], [0, 1]], dtype=int) # Identity Matrix
base = m
if power < 0: # Negative power is not allowed
raise ValueError("power is negative")
while power:
if power % 2 == 1:
result = np.dot(result, base)
base = np.dot(base, base)
power //= 2
return result
def fib_matrix_np(n: int) -> int:
"""
Calculates the n-th Fibonacci number using matrix exponentiation.
https://www.nayuki.io/page/fast-fibonacci-algorithms#:~:text=
Summary:%20The%20two%20fast%20Fibonacci%20algorithms%20are%20matrix
Args:
n: Fibonacci sequence index
Returns:
The n-th Fibonacci number.
Raises:
ValueError: If n is negative.
>>> fib_matrix_np(0)
0
>>> fib_matrix_np(1)
1
>>> fib_matrix_np(5)
5
>>> fib_matrix_np(10)
55
>>> fib_matrix_np(-1)
Traceback (most recent call last):
...
ValueError: n is negative
"""
if n < 0:
raise ValueError("n is negative")
if n == 0:
return 0
m = np.array([[1, 1], [1, 0]], dtype=int)
result = matrix_pow_np(m, n - 1)
return int(result[0, 0])
if __name__ == "__main__": if __name__ == "__main__":
from doctest import testmod from doctest import testmod
@ -242,3 +329,4 @@ if __name__ == "__main__":
time_func(fib_memoization, num) # 0.0100 ms time_func(fib_memoization, num) # 0.0100 ms
time_func(fib_recursive_cached, num) # 0.0153 ms time_func(fib_recursive_cached, num) # 0.0153 ms
time_func(fib_recursive, num) # 257.0910 ms time_func(fib_recursive, num) # 257.0910 ms
time_func(fib_matrix_np, num) # 0.0000 ms

View File

@ -17,11 +17,27 @@ def compute_transform_tables(
delete_cost: int, delete_cost: int,
insert_cost: int, insert_cost: int,
) -> tuple[list[list[int]], list[list[str]]]: ) -> tuple[list[list[int]], list[list[str]]]:
"""
Finds the most cost efficient sequence
for converting one string into another.
>>> costs, operations = compute_transform_tables("cat", "cut", 1, 2, 3, 3)
>>> costs[0][:4]
[0, 3, 6, 9]
>>> costs[2][:4]
[6, 4, 3, 6]
>>> operations[0][:4]
['0', 'Ic', 'Iu', 'It']
>>> operations[3][:4]
['Dt', 'Dt', 'Rtu', 'Ct']
>>> compute_transform_tables("", "", 1, 2, 3, 3)
([[0]], [['0']])
"""
source_seq = list(source_string) source_seq = list(source_string)
destination_seq = list(destination_string) destination_seq = list(destination_string)
len_source_seq = len(source_seq) len_source_seq = len(source_seq)
len_destination_seq = len(destination_seq) len_destination_seq = len(destination_seq)
costs = [ costs = [
[0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1) [0 for _ in range(len_destination_seq + 1)] for _ in range(len_source_seq + 1)
] ]
@ -31,33 +47,51 @@ def compute_transform_tables(
for i in range(1, len_source_seq + 1): for i in range(1, len_source_seq + 1):
costs[i][0] = i * delete_cost costs[i][0] = i * delete_cost
ops[i][0] = f"D{source_seq[i - 1]:c}" ops[i][0] = f"D{source_seq[i - 1]}"
for i in range(1, len_destination_seq + 1): for i in range(1, len_destination_seq + 1):
costs[0][i] = i * insert_cost costs[0][i] = i * insert_cost
ops[0][i] = f"I{destination_seq[i - 1]:c}" ops[0][i] = f"I{destination_seq[i - 1]}"
for i in range(1, len_source_seq + 1): for i in range(1, len_source_seq + 1):
for j in range(1, len_destination_seq + 1): for j in range(1, len_destination_seq + 1):
if source_seq[i - 1] == destination_seq[j - 1]: if source_seq[i - 1] == destination_seq[j - 1]:
costs[i][j] = costs[i - 1][j - 1] + copy_cost costs[i][j] = costs[i - 1][j - 1] + copy_cost
ops[i][j] = f"C{source_seq[i - 1]:c}" ops[i][j] = f"C{source_seq[i - 1]}"
else: else:
costs[i][j] = costs[i - 1][j - 1] + replace_cost costs[i][j] = costs[i - 1][j - 1] + replace_cost
ops[i][j] = f"R{source_seq[i - 1]:c}" + str(destination_seq[j - 1]) ops[i][j] = f"R{source_seq[i - 1]}" + str(destination_seq[j - 1])
if costs[i - 1][j] + delete_cost < costs[i][j]: if costs[i - 1][j] + delete_cost < costs[i][j]:
costs[i][j] = costs[i - 1][j] + delete_cost costs[i][j] = costs[i - 1][j] + delete_cost
ops[i][j] = f"D{source_seq[i - 1]:c}" ops[i][j] = f"D{source_seq[i - 1]}"
if costs[i][j - 1] + insert_cost < costs[i][j]: if costs[i][j - 1] + insert_cost < costs[i][j]:
costs[i][j] = costs[i][j - 1] + insert_cost costs[i][j] = costs[i][j - 1] + insert_cost
ops[i][j] = f"I{destination_seq[j - 1]:c}" ops[i][j] = f"I{destination_seq[j - 1]}"
return costs, ops return costs, ops
def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]: def assemble_transformation(ops: list[list[str]], i: int, j: int) -> list[str]:
"""
Assembles the transformations based on the ops table.
>>> ops = [['0', 'Ic', 'Iu', 'It'],
... ['Dc', 'Cc', 'Iu', 'It'],
... ['Da', 'Da', 'Rau', 'Rat'],
... ['Dt', 'Dt', 'Rtu', 'Ct']]
>>> x = len(ops) - 1
>>> y = len(ops[0]) - 1
>>> assemble_transformation(ops, x, y)
['Cc', 'Rau', 'Ct']
>>> ops1 = [['0']]
>>> x1 = len(ops1) - 1
>>> y1 = len(ops1[0]) - 1
>>> assemble_transformation(ops1, x1, y1)
[]
"""
if i == 0 and j == 0: if i == 0 and j == 0:
return [] return []
elif ops[i][j][0] in {"C", "R"}: elif ops[i][j][0] in {"C", "R"}: