From 170f5bc13e5f3fecff47c19fc659985e0b3a56a5 Mon Sep 17 00:00:00 2001 From: princegupta22 Date: Sun, 5 Oct 2025 17:27:11 +0530 Subject: [PATCH] Add python datastructure algo --- graphs/floyd_warshall.py | 31 ++++++++++++++++++++++++++ graphs/topological_sort.py | 35 ++++++++++++++++++++++++++++++ strings/binary_search_recursive.py | 17 +++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 graphs/floyd_warshall.py create mode 100644 graphs/topological_sort.py create mode 100644 strings/binary_search_recursive.py diff --git a/graphs/floyd_warshall.py b/graphs/floyd_warshall.py new file mode 100644 index 000000000000..6faf4f740c0f --- /dev/null +++ b/graphs/floyd_warshall.py @@ -0,0 +1,31 @@ +# Algorithm to find shortest paths between all pairs of vertices in a weighted graph. + +def floyd_warshall(graph): + """ + graph: adjacency matrix (2D list) + returns: distance matrix (shortest path between all pairs) + """ + n = len(graph) + dist = [row[:] for row in graph] + + for k in range(n): + for i in range(n): + for j in range(n): + if dist[i][k] + dist[k][j] < dist[i][j]: + dist[i][j] = dist[i][k] + dist[k][j] + return dist + + +if __name__ == "__main__": + INF = 99999 + graph = [ + [0, 5, INF, 10], + [INF, 0, 3, INF], + [INF, INF, 0, 1], + [INF, INF, INF, 0] + ] + + result = floyd_warshall(graph) + print("All-Pairs Shortest Paths:") + for row in result: + print(row) diff --git a/graphs/topological_sort.py b/graphs/topological_sort.py new file mode 100644 index 000000000000..1aa8e18f3539 --- /dev/null +++ b/graphs/topological_sort.py @@ -0,0 +1,35 @@ +# Perform topological sort on a directed acyclic graph (DAG) + +from collections import defaultdict + +class Graph: + def __init__(self): + self.graph = defaultdict(list) + + def add_edge(self, u, v): + self.graph[u].append(v) + + def topological_sort_util(self, v, visited, stack): + visited[v] = True + for i in self.graph[v]: + if not visited[i]: + self.topological_sort_util(i, visited, stack) + stack.append(v) + + def topological_sort(self): + visited = {key: False for key in self.graph} + stack = [] + for vertex in self.graph: + if not visited[vertex]: + self.topological_sort_util(vertex, visited, stack) + print(stack[::-1]) + +if __name__ == "__main__": + g = Graph() + g.add_edge(5, 2) + g.add_edge(5, 0) + g.add_edge(4, 0) + g.add_edge(4, 1) + g.add_edge(2, 3) + g.add_edge(3, 1) + g.topological_sort() diff --git a/strings/binary_search_recursive.py b/strings/binary_search_recursive.py new file mode 100644 index 000000000000..bea915d512fe --- /dev/null +++ b/strings/binary_search_recursive.py @@ -0,0 +1,17 @@ +def binary_search(arr, target, low, high): + if low > high: + return -1 + mid = (low + high) // 2 + if arr[mid] == target: + return mid + elif arr[mid] > target: + return binary_search(arr, target, low, mid - 1) + else: + return binary_search(arr, target, mid + 1, high) + + +if __name__ == "__main__": + arr = [2, 4, 6, 8, 10, 12] + target = 10 + result = binary_search(arr, target, 0, len(arr) - 1) + print(f"Element found at index {result}" if result != -1 else "Not found")