Skip to content
Closed
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
21 changes: 20 additions & 1 deletion graphs/depth_first_search_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ def dfs_recursive(self, start_vertex: int, visited: list) -> None:
print(" ", end="")
self.dfs_recursive(i, visited)

def topological_sort(self):
visited = set()
stack = []

for vertex in self.vertex:
if vertex not in visited:
self.topological_sort_util(vertex, visited, stack)

return stack[::-1] # Reverse the stack to get the correct order

def topological_sort_util(self, v, visited, stack):
visited.add(v)

for neighbor in self.vertex.get(v, []):
if neighbor not in visited:
self.topological_sort_util(neighbor, visited, stack)

stack.append(v) # Push the vertex to stack


if __name__ == "__main__":
import doctest
Expand All @@ -123,5 +142,5 @@ def dfs_recursive(self, start_vertex: int, visited: list) -> None:
g.add_edge(3, 3)

g.print_graph()
print("DFS:")
print("Topological Sort:", g.topological_sort())
g.dfs()
6 changes: 4 additions & 2 deletions machine_learning/loss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,10 @@
"""
if len(y_true) != len(y_pred):
raise ValueError("Input arrays must have the same length.")

kl_loss = y_true * np.log(y_true / y_pred)

Check failure on line 661 in machine_learning/loss_functions.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

machine_learning/loss_functions.py:661:1: W293 Blank line contains whitespace
# Mask y_true is 0 to avoid invalid log calculation
mask = y_true != 0
kl_loss = y_true[mask] * np.log(y_true[mask] / y_pred[mask])
return np.sum(kl_loss)


Expand Down
Loading