mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-13 18:19:47 +00:00
Merge branch 'TheAlgorithms:master' into longArthSub
This commit is contained in:
commit
3f534f8a87
@ -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
|
||||
using BFS
|
||||
Perform topological sorting of a Directed Acyclic Graph (DAG)
|
||||
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)
|
||||
queue = []
|
||||
topo = []
|
||||
cnt = 0
|
||||
topo_order = []
|
||||
processed_vertices_count = 0
|
||||
|
||||
# Calculate the indegree of each vertex
|
||||
for values in graph.values():
|
||||
for i in values:
|
||||
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)
|
||||
|
||||
# Perform BFS
|
||||
while queue:
|
||||
vertex = queue.pop(0)
|
||||
cnt += 1
|
||||
topo.append(vertex)
|
||||
for x in graph[vertex]:
|
||||
indegree[x] -= 1
|
||||
if indegree[x] == 0:
|
||||
queue.append(x)
|
||||
processed_vertices_count += 1
|
||||
topo_order.append(vertex)
|
||||
|
||||
if cnt != len(graph):
|
||||
print("Cycle exists")
|
||||
else:
|
||||
print(topo)
|
||||
# Traverse neighbors
|
||||
for neighbor in graph[vertex]:
|
||||
indegree[neighbor] -= 1
|
||||
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
|
||||
graph = {0: [1, 2], 1: [3], 2: [3], 3: [4, 5], 4: [], 5: []}
|
||||
topological_sort(graph)
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
@ -17,11 +17,27 @@ def compute_transform_tables(
|
||||
delete_cost: int,
|
||||
insert_cost: int,
|
||||
) -> 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)
|
||||
destination_seq = list(destination_string)
|
||||
len_source_seq = len(source_seq)
|
||||
len_destination_seq = len(destination_seq)
|
||||
|
||||
costs = [
|
||||
[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):
|
||||
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):
|
||||
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 j in range(1, len_destination_seq + 1):
|
||||
if source_seq[i - 1] == destination_seq[j - 1]:
|
||||
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:
|
||||
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]:
|
||||
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]:
|
||||
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
|
||||
|
||||
|
||||
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:
|
||||
return []
|
||||
elif ops[i][j][0] in {"C", "R"}:
|
||||
|
Loading…
x
Reference in New Issue
Block a user