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
31 changes: 31 additions & 0 deletions graphs/floyd_warshall.py
Original file line number Diff line number Diff line change
@@ -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]

Check failure on line 15 in graphs/floyd_warshall.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PLR1730)

graphs/floyd_warshall.py:14:17: PLR1730 Replace `if` statement with `min` call
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)
35 changes: 35 additions & 0 deletions graphs/topological_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Perform topological sort on a directed acyclic graph (DAG)

from collections import defaultdict

Check failure on line 3 in graphs/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

graphs/topological_sort.py:3:1: I001 Import block is un-sorted or un-formatted

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}

Check failure on line 20 in graphs/topological_sort.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (C420)

graphs/topological_sort.py:20:19: C420 Unnecessary dict comprehension for iterable; use `dict.fromkeys` instead
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()
17 changes: 17 additions & 0 deletions strings/binary_search_recursive.py
Original file line number Diff line number Diff line change
@@ -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")
Loading