From e08aadd00eff6b419fd738d7838912740c0223ab Mon Sep 17 00:00:00 2001 From: Ved Prakash Vishwakarma Date: Sun, 20 Oct 2024 13:11:19 +0530 Subject: [PATCH 1/6] Issue 12192 fixed, Traversal direction is considered from top to bottom as stated. --- sorts/topological_sort.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index efce8165fcac..c790cc93d54b 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -21,12 +21,13 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st # add current to visited visited.append(current) neighbors = edges[current] + #as we are traversing in from top to down in tree like graph (direction not given) we consider direction from top to down + #as the current node encounter add it to the topo sort list + sort.append(current) for neighbor in neighbors: # if neighbor not in visited, visit if neighbor not in visited: sort = topological_sort(neighbor, visited, sort) - # if all neighbors visited add current to sort - sort.append(current) # if all vertices haven't been visited select a new one to visit if len(visited) != len(vertices): for vertice in vertices: From 239b8c83e4117b1cb9bffe553a5e1a9e54edf2bd Mon Sep 17 00:00:00 2001 From: Ved Prakash Vishwakarma Date: Sun, 20 Oct 2024 14:16:08 +0530 Subject: [PATCH 2/6] Issue 12192 fixed with all checks passed --- sorts/topological_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index c790cc93d54b..e39585e0bb8e 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -21,8 +21,8 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st # add current to visited visited.append(current) neighbors = edges[current] - #as we are traversing in from top to down in tree like graph (direction not given) we consider direction from top to down - #as the current node encounter add it to the topo sort list + # direction not given so consider from top to bottom + # as the current node encounter add it to the topo sort list sort.append(current) for neighbor in neighbors: # if neighbor not in visited, visit From 7ac458d7462b48fd791f66e908842098f153dd02 Mon Sep 17 00:00:00 2001 From: Ved Prakash Vishwakarma Date: Mon, 21 Oct 2024 01:32:40 +0530 Subject: [PATCH 3/6] comment updated --- sorts/topological_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index e39585e0bb8e..fc638cbfd5a0 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -17,11 +17,11 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]: """Perform topological sort on a directed acyclic graph.""" + # consider edge direction from top to bottom current = start # add current to visited visited.append(current) neighbors = edges[current] - # direction not given so consider from top to bottom # as the current node encounter add it to the topo sort list sort.append(current) for neighbor in neighbors: From d400b6339c92d7df49dd7305399737cb7fdccc27 Mon Sep 17 00:00:00 2001 From: Ved Prakash Vishwakarma Date: Wed, 23 Oct 2024 17:15:59 +0530 Subject: [PATCH 4/6] kl_loss won't return -inf if y_true is 0 Issue Fixed --- machine_learning/loss_functions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/machine_learning/loss_functions.py b/machine_learning/loss_functions.py index 0bd9aa8b5401..fddb4081a778 100644 --- a/machine_learning/loss_functions.py +++ b/machine_learning/loss_functions.py @@ -659,7 +659,10 @@ def kullback_leibler_divergence(y_true: np.ndarray, y_pred: np.ndarray) -> float 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) + kl_loss = 0 + if y_true != 0: + kl_loss = y_true * np.log(y_true / y_pred) + return np.sum(kl_loss) From 7a85256e8ac03bf00534038ed2dbcc298ee9a5b9 Mon Sep 17 00:00:00 2001 From: Ved Prakash Vishwakarma <119104602+vedprakash226@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:13:55 +0530 Subject: [PATCH 5/6] Original code --- sorts/topological_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index fc638cbfd5a0..c67f18a09418 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -15,19 +15,19 @@ vertices: list[str] = ["a", "b", "c", "d", "e"] + def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]: """Perform topological sort on a directed acyclic graph.""" - # consider edge direction from top to bottom current = start # add current to visited visited.append(current) neighbors = edges[current] - # as the current node encounter add it to the topo sort list - sort.append(current) for neighbor in neighbors: # if neighbor not in visited, visit if neighbor not in visited: sort = topological_sort(neighbor, visited, sort) + # if all neighbors visited add current to sort + sort.append(current) # if all vertices haven't been visited select a new one to visit if len(visited) != len(vertices): for vertice in vertices: From 607a993ab23645b674cfd8ffece82c56daa741d9 Mon Sep 17 00:00:00 2001 From: Ved Prakash Vishwakarma <119104602+vedprakash226@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:14:41 +0530 Subject: [PATCH 6/6] Code backup --- sorts/topological_sort.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index c67f18a09418..efce8165fcac 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -15,7 +15,6 @@ vertices: list[str] = ["a", "b", "c", "d", "e"] - def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]: """Perform topological sort on a directed acyclic graph.""" current = start