Skip to content

Commit e52c94e

Browse files
committed
algorithms
1 parent 878cb39 commit e52c94e

21 files changed

Lines changed: 374 additions & 0 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# __init__.py
2+
from .bfs import breadth_first_search
3+
from .dfs import depth_first_search
4+
from .dijkstra import dijkstra
5+
6+
__all__ = ['breadth_first_search', 'depth_first_search', 'dijkstra']
7+

‎competitivepython/graphs/bfs.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def breadth_first_search(graph, node):
2+
visited, queue = set(), [node]
3+
visited.add(node)
4+
5+
while queue:
6+
vertex = queue.pop(0)
7+
visited.add(vertex)
8+
9+
for neighbour in graph[vertex]:
10+
if neighbour not in visited:
11+
queue.append(neighbour)
12+
13+
return visited

‎competitivepython/graphs/dfs.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def depth_first_search(graph, node):
2+
visited = set()
3+
visited = dfs_helper(graph, node, visited)
4+
return visited
5+
6+
def dfs_helper(graph, node, visited):
7+
if node not in visited:
8+
visited.add(node)
9+
for neighbour in graph[node]:
10+
dfs_helper(graph, neighbour, visited)
11+
return visited
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import heapq
2+
3+
def dijkstra(graph, start, end):
4+
distances = {vertex: float('inf') for vertex in graph}
5+
distances[start] = 0
6+
path = {vertex: [] for vertex in graph}
7+
queue = [(0, start)]
8+
9+
while queue:
10+
(current_distance, current_vertex) = heapq.heappop(queue)
11+
12+
if current_vertex == end:
13+
break
14+
15+
for neighbor, weight in graph[current_vertex].items():
16+
distance = current_distance + weight
17+
18+
if distance < distances[neighbor]:
19+
distances[neighbor] = distance
20+
heapq.heappush(queue, (distance, neighbor))
21+
path[neighbor] = path[current_vertex] + [neighbor]
22+
23+
return {'distance': distances[end], 'path': path[end]}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
def kmp_search(pat, txt):
2+
M = len(pat)
3+
N = len(txt)
4+
5+
# create lps[] that will hold the longest prefix suffix
6+
# values for pattern
7+
lps = [0] * M
8+
j = 0 # index for pat[]
9+
10+
# Preprocess the pattern (calculate lps[] array)
11+
computeLPSArray(pat, M, lps)
12+
13+
i = 0 # index for txt[]
14+
indices = []
15+
while i < N:
16+
if pat[j] == txt[i]:
17+
i += 1
18+
j += 1
19+
20+
if j == M:
21+
indices.append(i - j)
22+
j = lps[j - 1]
23+
24+
# mismatch after j matches
25+
elif i < N and pat[j] != txt[i]:
26+
# Do not match lps[0..lps[j-1]] characters,
27+
# they will match anyway
28+
if j != 0:
29+
j = lps[j - 1]
30+
else:
31+
i += 1
32+
33+
return indices
34+
35+
def computeLPSArray(pat, M, lps):
36+
len = 0 # length of the previous longest prefix suffix
37+
38+
lps[0] = 0
39+
i = 1
40+
41+
# the loop calculates lps[i] for i = 1 to M-1
42+
while i < M:
43+
if pat[i] == pat[len]:
44+
len += 1
45+
lps[i] = len
46+
i += 1
47+
else:
48+
# This is tricky. Consider the example.
49+
# AAACAAAA and i = 7. The idea is similar
50+
# to search step.
51+
if len != 0:
52+
len = lps[len - 1]
53+
54+
# Also, note that we do not increment i here
55+
else:
56+
lps[i] = 0
57+
i += 1
58+
59+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# __init__.py
2+
from .binarysearch import binary_search
3+
from .linearsearch import linear_search
4+
from .KMP_pattern_search import kmp_search
5+
6+
__all__ = ['binary_search', 'linear_search', 'kmp_search']
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def binary_search(arr, target):
2+
left, right = 0, len(arr) - 1
3+
while left <= right:
4+
mid = left + (right - left) // 2
5+
if arr[mid] == target:
6+
return mid
7+
elif arr[mid] < target:
8+
left = mid + 1
9+
else:
10+
right = mid - 1
11+
return -1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def linear_search(arr, target):
2+
for search in range(len(arr)):
3+
if arr[search] == target:
4+
return search
5+
return -1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# __init__.py
2+
from .bubblesort import bubble_sort
3+
from .insertionsort import insertion_sort
4+
from .shellsort import shell_sort
5+
from .selectionsort import selection_sort
6+
from .bucketsort import bucket_sort
7+
from .mergesort import merge_sort
8+
from .timsort import tim_sort
9+
from .quicksort import quick_sort
10+
from .heapsort import heap_sort
11+
from .radixsort import radix_sort
12+
13+
__all__ = ['bubble_sort', 'insertion_sort', 'shell_sort', 'selection_sort',
14+
'bucket_sort', 'merge_sort', 'tim_sort', 'quick_sort', 'heap_sort', 'radix_sort']
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def bubble_sort(arr):
2+
length = len(arr)
3+
for i in range(length):
4+
for j in range(0, length-i-1):
5+
if arr[j] > arr[j+1]:
6+
arr[j], arr[j+1] = arr[j+1], arr[j]
7+
return arr

0 commit comments

Comments
 (0)