├── 01-Introduction ├── .gitkeep ├── index.py └── index3.py ├── 02-Time Complexity ├── .gitkeep ├── index.py └── index3.py ├── 03-Sorting ├── .gitkeep ├── index.py └── index3.py ├── 04-Data Structures ├── .gitkeep ├── index.py └── index3.py ├── 05-Complete Search ├── .gitkeep ├── index.py └── index3.py ├── 06-Greedy Algorithms └── .gitkeep ├── 07-Dynamic Programming ├── .gitkeep ├── coins.py └── dp_applications ├── 08-Amortized Analysis └── .gitkeep ├── 09-Range Queries └── .gitkeep ├── 10-Bit Manipulation └── .gitkeep ├── 11-Basics Of Graphs └── .gitkeep ├── 12-Graph Traversal ├── .gitkeep └── graph_traversal.py ├── 13-Shortest Paths └── .gitkeep ├── 14-Tree Algorithms └── .gitkeep ├── 15-Spanning Trees └── .gitkeep ├── 16-Directed Graphs └── .gitkeep ├── 17-Strong Connectivity └── .gitkeep ├── 18-Tree Queries └── .gitkeep ├── 19-Paths And Circuits └── .gitkeep ├── 20-Flows And Cuts └── .gitkeep ├── 21-Number Theory └── .gitkeep ├── 22-Combinatorics └── .gitkeep ├── 23-Matrices └── .gitkeep ├── 24-Probability └── .gitkeep ├── 25-Game Theory └── .gitkeep ├── 26-String Algorithms └── .gitkeep ├── 27-Square Root Algorithms └── .gitkeep ├── 28-Segment Trees Revisited └── .gitkeep ├── 29-Geometry └── .gitkeep ├── 30-Sweep Line Algorithms └── .gitkeep ├── Competitive-Programmer-Handbook.pdf ├── LICENSE └── README.md /01-Introduction/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/01-Introduction/.gitkeep -------------------------------------------------------------------------------- /01-Introduction/index.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | # Input -> 10 20 monkey 4 | a, b, c = raw_input().split(' ') 5 | print(a, b, c) 6 | 7 | # Input with map -> 10 20 8 | a, b = map(int, raw_input().split(' ')) 9 | print(a, b) 10 | 11 | # Input with map -> 10, 20 12 | a, b = map(int, raw_input().split(',')) 13 | print(a, b) 14 | 15 | # Python has dynamic data type so integers, float are same as variables 16 | 17 | # Mathematics 18 | ## Sum Formulas -> 1 + 2 + 3 ... n = n(n+1)/2 19 | ## Sum Formulas -> 1^k + 2^k + 3^k ... n^k = n(n+1)(2n+1)/6 20 | ## AP -> a + .. + b = n(a+b)/2 21 | ## GP -> a + ak + ak^2 + .. + b = (bk -a) / (k - 1) 22 | ap = [3, 7, 11, 15] 23 | print(4*(3+15)/2) 24 | 25 | gp = [3, 6, 12, 24] # k is 2 here 26 | print((24*2 - 3)/(2-1)) 27 | 28 | # Set 29 | x = set([2, 4, 7]) 30 | a = set([1, 2, 5]) 31 | b = set([2, 4]) 32 | print(x) 33 | print(a.intersection(b)) 34 | print(a.union(b)) 35 | print(a.difference(b)) 36 | 37 | # Functions 38 | print(3/2) 39 | print(min([1, 2, 3])) 40 | print(max([1, 2, 3])) 41 | print(math.factorial(10)) 42 | 43 | # Not memoized 44 | def fibo(n): 45 | if n == 0: 46 | return 0 47 | if n == 1: 48 | return 1 49 | return fibo(n-1) + fibo(n-2) 50 | 51 | print(fibo(10)) 52 | 53 | # Logarithms 54 | ## logk(a, b) = logk(a) + logk(b) 55 | ## logk(x^n) = n * logk(x) 56 | ## logk(a/b) = logk(a) - logk(b) 57 | ## logu(x) = logk(x) / logk(u) 58 | print(math.log(32, 2)) 59 | -------------------------------------------------------------------------------- /01-Introduction/index3.py: -------------------------------------------------------------------------------- 1 | # Python 3 version of index.py 2 | 3 | import math 4 | 5 | # Input -> 10 20 monkey 6 | a, b, c = input().split(' ') 7 | print(a, b, c) 8 | 9 | # Input with map -> 10 20 10 | a, b = map(int, input().split(' ')) 11 | print(a, b) 12 | 13 | # Input with map -> 10, 20 14 | a, b = map(int, input().split(',')) 15 | print(a, b) 16 | 17 | # Python has dynamic data type so integers, float are same as variables 18 | 19 | # Mathematics 20 | # Sum Formulas -> 1 + 2 + 3 ... n = n(n+1)/2 21 | # Sum Formulas -> 1^k + 2^k + 3^k ... n^k = n(n+1)(2n+1)/6 22 | # AP -> a + .. + b = n(a+b)/2 23 | # GP -> a + ak + ak^2 + .. + b = (bk -a) / (k - 1) 24 | ap = [3, 7, 11, 15] 25 | print(4*(3+15)/2) 26 | 27 | gp = [3, 6, 12, 24] # k is 2 here 28 | print((24*2 - 3)/(2-1)) 29 | 30 | # Set 31 | x = set([2, 4, 7]) 32 | a = set([1, 2, 5]) 33 | b = set([2, 4]) 34 | print(x) 35 | print(a.intersection(b)) 36 | print(a.union(b)) 37 | print(a.difference(b)) 38 | 39 | # Functions 40 | print(3/2) 41 | print(min([1, 2, 3])) 42 | print(max([1, 2, 3])) 43 | print(math.factorial(10)) 44 | 45 | # Not memoized 46 | def fibo(n): 47 | if n == 0: 48 | return 0 49 | if n == 1: 50 | return 1 51 | return fibo(n-1) + fibo(n-2) 52 | 53 | print(fibo(10)) 54 | 55 | # Logarithms 56 | # logk(a, b) = logk(a) + logk(b) 57 | # logk(x^n) = n * logk(x) 58 | # logk(a/b) = logk(a) - logk(b) 59 | # logu(x) = logk(x) / logk(u) 60 | print(math.log(32, 2)) 61 | -------------------------------------------------------------------------------- /02-Time Complexity/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/02-Time Complexity/.gitkeep -------------------------------------------------------------------------------- /02-Time Complexity/index.py: -------------------------------------------------------------------------------- 1 | # Loops 2 | # O(n) 3 | for i in range(100): # better to use xrange 4 | print i 5 | 6 | # O(n^2) 7 | for i in xrange(100): 8 | for j in xrange(100): 9 | print(i, j) 10 | 11 | # Maximum Subarray Sum 12 | arr = [-1, 2, 4, -3, 5, 2, -5, 2] 13 | 14 | # O(n^3) solution 15 | best = 0 16 | for i in arr: 17 | for j in arr: 18 | sum = 0 19 | for k in arr: 20 | sum += k 21 | best = max(best, sum) 22 | print best 23 | 24 | # O(n^2) solution 25 | best = 0 26 | for i in arr: 27 | sum = 0 28 | for j in arr: 29 | sum += j 30 | best = max(best, sum) 31 | print best 32 | 33 | # O(n) solution 34 | best, sum = 0, 0 35 | for i in arr: 36 | sum = max(i, sum + i) 37 | best = max(best, sum) 38 | print best -------------------------------------------------------------------------------- /02-Time Complexity/index3.py: -------------------------------------------------------------------------------- 1 | # Loops 2 | # O(n) 3 | for i in range(100): # better to use xrange 4 | print(i) 5 | 6 | # O(n^2) 7 | for i in range(100): 8 | for j in range(100): 9 | print(i, j) 10 | 11 | # Maximum Subarray Sum 12 | arr = [-1, 2, 4, -3, 5, 2, -5, 2] 13 | 14 | # O(n^3) solution 15 | best = 0 16 | for i in arr: 17 | for j in arr: 18 | sum = 0 19 | for k in arr: 20 | sum += k 21 | best = max(best, sum) 22 | print(best) 23 | 24 | # O(n^2) solution 25 | best = 0 26 | for i in arr: 27 | sum = 0 28 | for j in arr: 29 | sum += j 30 | best = max(best, sum) 31 | print(best) 32 | 33 | # O(n) solution 34 | best, sum = 0, 0 35 | for i in arr: 36 | sum = max(i, sum + i) 37 | best = max(best, sum) 38 | print(best) -------------------------------------------------------------------------------- /03-Sorting/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/03-Sorting/.gitkeep -------------------------------------------------------------------------------- /03-Sorting/index.py: -------------------------------------------------------------------------------- 1 | # Bubble Sort O(n^2) 2 | arr = [1, 3, 8, 2, 9, 2, 5, 6] 3 | for i,j in enumerate(arr): 4 | for a,b in enumerate(arr[:-1]): 5 | if arr[a] > arr[a+1]: 6 | arr[a], arr[a+1] = arr[a+1], arr[a] 7 | print arr 8 | 9 | # Merge Sort 10 | arr = [1, 3, 8, 2, 9, 2, 5, 6] 11 | def merge_sort(arr): 12 | if len(arr) <= 1: 13 | return arr 14 | middle = len(arr) // 2 15 | left = arr[:middle] 16 | right = arr[middle:] 17 | sleft = merge_sort(left) 18 | sright = merge_sort(right) 19 | return merge(sleft, sright) 20 | 21 | def merge(left, right): 22 | result = [] 23 | while (left and right): 24 | if left[0] < right[0]: 25 | result.append(left[0]) 26 | left.pop(0) 27 | else: 28 | result.append(right[0]) 29 | right.pop(0) 30 | if left: 31 | result += left 32 | if right: 33 | result += right 34 | return result 35 | 36 | # Counting Sort 37 | def counting_sort(arr, max_val): 38 | count_arr = [0] * (max_val + 1) 39 | for num in arr: 40 | count_arr[num] += 1 41 | for i in range(1, len(count_arr)): 42 | count_arr[i] += count_arr[i - 1] 43 | output_arr = [0] * len(arr) 44 | i = len(arr) - 1 45 | while i >= 0: 46 | curr_ele = arr[i] 47 | count_arr[curr_ele] -= 1 48 | new_pos = count_arr[curr_ele] 49 | output_arr[new_pos] = curr_ele 50 | i -= 1 51 | return output_arr 52 | 53 | # Sorting functions in Python 54 | arr = [1, 3, 8, 2, 9, 2, 5, 6] 55 | print(sorted(arr), arr) 56 | print(arr.sort(), arr) 57 | 58 | # Binary Search 59 | arr = [1, 2, 2, 3, 5, 6, 8, 9] 60 | def binary_search(arr, elem, prim_index = 0): 61 | mid = len(arr) / 2 62 | mid_elem = arr[mid] 63 | if mid_elem == elem: 64 | return mid + prim_index 65 | if mid_elem > elem: 66 | return binary_search(arr[:mid], elem, prim_index) 67 | if mid_elem < elem: 68 | return binary_search(arr[mid:], elem, prim_index+mid) 69 | 70 | print binary_search(arr, 9) 71 | print binary_search(arr, 5) 72 | print binary_search(arr, 3) 73 | 74 | 75 | # Upper Bound and Lower Bound Python 76 | -------------------------------------------------------------------------------- /03-Sorting/index3.py: -------------------------------------------------------------------------------- 1 | # Bubble Sort O(n^2) 2 | arr = [1, 3, 8, 2, 9, 2, 5, 6] 3 | for i, j in enumerate(arr): 4 | for a, b in enumerate(arr[:-1]): 5 | if arr[a] > arr[a + 1]: 6 | arr[a], arr[a + 1] = arr[a + 1], arr[a] 7 | print(arr) 8 | 9 | # Merge Sort 10 | arr = [1, 3, 8, 2, 9, 2, 5, 6] 11 | 12 | # Counting Sort 13 | 14 | 15 | # Sorting functions in Python 16 | arr = [1, 3, 8, 2, 9, 2, 5, 6] 17 | print(sorted(arr), arr) 18 | print(arr.sort(), arr) 19 | 20 | # Binary Search 21 | arr = [1, 2, 2, 3, 5, 6, 8, 9] 22 | 23 | 24 | def binary_search(arr, elem, prim_index=0): 25 | mid = int(len(arr) / 2) 26 | mid_elem = arr[mid] 27 | if mid_elem == elem: 28 | return mid + prim_index 29 | if mid_elem > elem: 30 | return binary_search(arr[:mid], elem, prim_index) 31 | if mid_elem < elem: 32 | return binary_search(arr[mid:], elem, prim_index + mid) 33 | 34 | 35 | print(binary_search(arr, 9)) 36 | print(binary_search(arr, 5)) 37 | print(binary_search(arr, 3)) 38 | 39 | # Upper Bound and Lower Bound Python 40 | -------------------------------------------------------------------------------- /04-Data Structures/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/04-Data Structures/.gitkeep -------------------------------------------------------------------------------- /04-Data Structures/index.py: -------------------------------------------------------------------------------- 1 | # Data Structures 2 | 3 | # Arrays 4 | arr = [1, 2, 3, 4, 5, 6] 5 | arr.pop() 6 | arr.pop() 7 | print(arr) 8 | 9 | arr.append(1) 10 | print(arr) 11 | 12 | # Set 13 | s = set([1, 2, 3, 4, 5, 2]) 14 | print(s) 15 | 16 | s.remove(5) 17 | print(s) 18 | 19 | s.add(6) 20 | print(s) 21 | 22 | print(len(s)) 23 | 24 | # Map or Object 25 | dictionary = {} 26 | dictionary[1] = 2 27 | dictionary[2] = 3 28 | 29 | print(dictionary) 30 | print(dictionary.keys()) 31 | print(dictionary.values()) 32 | print(dictionary.items()) 33 | 34 | for i,j in dictionary.items(): 35 | print(i,j) 36 | 37 | print(2 in dictionary) 38 | print(3 in dictionary) 39 | 40 | -------------------------------------------------------------------------------- /04-Data Structures/index3.py: -------------------------------------------------------------------------------- 1 | # Data Structures 2 | 3 | # Arrays 4 | arr = [1, 2, 3, 4, 5, 6] 5 | arr.pop() 6 | arr.pop() 7 | print(arr) 8 | 9 | arr.append(1) 10 | print(arr) 11 | 12 | # Set 13 | s = {1, 2, 3, 4, 5, 2} 14 | print(s) 15 | 16 | s.remove(5) 17 | print(s) 18 | 19 | s.add(6) 20 | print(s) 21 | 22 | print(len(s)) 23 | 24 | # Map or Object 25 | dictionary = {1: 2, 2: 3} 26 | 27 | print(dictionary) 28 | print(dictionary.keys()) 29 | print(dictionary.values()) 30 | print(dictionary.items()) 31 | 32 | for i,j in dictionary.items(): 33 | print(i,j) 34 | 35 | print(2 in dictionary) 36 | print(3 in dictionary) 37 | 38 | -------------------------------------------------------------------------------- /05-Complete Search/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/05-Complete Search/.gitkeep -------------------------------------------------------------------------------- /05-Complete Search/index.py: -------------------------------------------------------------------------------- 1 | # recursive subset generation, gives all combinations of a set (not permutations) 2 | n = 5 3 | subset = [] 4 | combinations = [] 5 | def search(k): 6 | if k == n: 7 | #process subset 8 | combinations.append(subset.copy()) 9 | else: 10 | search(k+1) 11 | subset.append(k) 12 | search(k+1) 13 | subset.pop() 14 | search(0) 15 | print(combinations) 16 | 17 | #=========================================================== 18 | #recursive permutation generation, always selects n elements 19 | n = 3 20 | perm = [] 21 | permutations = [] 22 | chosen = [0]*n 23 | 24 | def p_search(): 25 | if len(perm) == n: 26 | #process permutation 27 | permutations.append(perm.copy()) 28 | else: 29 | for i in range(n): 30 | if chosen[i]: continue 31 | chosen[i] = 1 32 | perm.append(i) 33 | p_search() 34 | chosen[i] = 0 35 | perm.pop() 36 | 37 | p_search() 38 | print(permutations) 39 | 40 | #=========================================================== 41 | #backtrack solution to solving n queens on an nxn chessboard 42 | n = 4 43 | count = [0] 44 | column = [0 for _ in range(n)] 45 | diag1 = [0 for _ in range(2*n-1)] 46 | diag2 = [0 for _ in range(2*n-1)] 47 | def q_search(y): 48 | if y == n: 49 | count[0] += 1 50 | return 51 | for x in range(n): 52 | if column[x] or diag1[x+y] or diag2[x-y+n-1]: continue 53 | column[x] = diag1[x+y] = diag2[x-y+n-1] = 1 54 | q_search(y+1) 55 | column[x] = diag1[x+y] = diag2[x-y+n-1] = 0 56 | 57 | q_search(0) 58 | print(count[0]) 59 | 60 | #=========================================================== 61 | 62 | -------------------------------------------------------------------------------- /05-Complete Search/index3.py: -------------------------------------------------------------------------------- 1 | # recursive subset generation, gives all combinations of a set (not permutations) 2 | n = 5 3 | subset = [] 4 | combinations = [] 5 | 6 | 7 | def search(k): 8 | if k == n: 9 | # process subset 10 | combinations.append(subset.copy()) 11 | else: 12 | search(k + 1) 13 | subset.append(k) 14 | search(k + 1) 15 | subset.pop() 16 | 17 | 18 | search(0) 19 | print(combinations) 20 | 21 | # =========================================================== 22 | # recursive permutation generation, always selects n elements 23 | n = 3 24 | perm = [] 25 | permutations = [] 26 | chosen = [0] * n 27 | 28 | 29 | def p_search(): 30 | if len(perm) == n: 31 | # process permutation 32 | permutations.append(perm.copy()) 33 | else: 34 | for i in range(n): 35 | if chosen[i]: continue 36 | chosen[i] = 1 37 | perm.append(i) 38 | p_search() 39 | chosen[i] = 0 40 | perm.pop() 41 | 42 | 43 | p_search() 44 | print(permutations) 45 | 46 | # =========================================================== 47 | # backtrack solution to solving n queens on an nxn chessboard 48 | n = 4 49 | count = [0] 50 | column = [0 for _ in range(n)] 51 | diag1 = [0 for _ in range(2 * n - 1)] 52 | diag2 = [0 for _ in range(2 * n - 1)] 53 | 54 | 55 | def q_search(y): 56 | if y == n: 57 | count[0] += 1 58 | return 59 | for x in range(n): 60 | if column[x] or diag1[x + y] or diag2[x - y + n - 1]: continue 61 | column[x] = diag1[x + y] = diag2[x - y + n - 1] = 1 62 | q_search(y + 1) 63 | column[x] = diag1[x + y] = diag2[x - y + n - 1] = 0 64 | 65 | 66 | q_search(0) 67 | print(count[0]) 68 | 69 | # =========================================================== 70 | 71 | -------------------------------------------------------------------------------- /06-Greedy Algorithms/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/06-Greedy Algorithms/.gitkeep -------------------------------------------------------------------------------- /07-Dynamic Programming/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/07-Dynamic Programming/.gitkeep -------------------------------------------------------------------------------- /07-Dynamic Programming/coins.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | coins = [1, 3, 4] 4 | 5 | 6 | # recursive solution to minimum number of coins problem 7 | 8 | 9 | def c_solve(x): 10 | if x < 0: 11 | return math.inf 12 | elif x == 0: 13 | return 0 14 | best = math.inf 15 | for c in coins: 16 | best = min(best, c_solve(x - c) + 1) 17 | return best 18 | 19 | 20 | print(c_solve(10)) # solves quickly 21 | print(c_solve(30)) # takes several seconds 22 | # print(c_solve(100)) #times out 23 | 24 | # ====================================================================== 25 | # optimized recursive solution using dynamic programming and memoization 26 | value = {} 27 | 28 | 29 | def dp_solve(x): 30 | if x < 0: 31 | return math.inf 32 | elif x == 0: 33 | return 0 34 | try: 35 | return value[x] 36 | except: 37 | pass 38 | best = math.inf 39 | for c in coins: 40 | best = min(best, dp_solve(x - c) + 1) 41 | value[x] = best 42 | return best 43 | 44 | 45 | print(dp_solve(30)) # solves quickly 46 | print(dp_solve(100)) # solves quickly 47 | 48 | # ======================================================================== 49 | # iterative version of dynamic programming solution, with solution printed 50 | value = {0: 0} 51 | first = {} 52 | 53 | 54 | def it_solve(n): 55 | for x in range(1, n + 1): 56 | value[x] = math.inf 57 | for c in coins: 58 | if x - c >= 0 and value[x - c] + 1 < value[x]: 59 | value[x] = value[x - c] + 1 60 | first[x] = c 61 | return value[n], first[n] 62 | 63 | 64 | n = 33 65 | it_solve(n) 66 | while n > 0: 67 | print(first[n]) 68 | n -= first[n] 69 | 70 | # =========================================== 71 | # total number of solutions for each coin sum 72 | count = [1] 73 | n = 33 74 | for x in range(n + 1): 75 | count.append(0) 76 | for c in coins: 77 | if x - c >= 0: 78 | count[x] += count[x - c] 79 | print(count[33]) 80 | -------------------------------------------------------------------------------- /07-Dynamic Programming/dp_applications: -------------------------------------------------------------------------------- 1 | #Dynamic Programming applications to a variety of example problems 2 | 3 | #largest increasing subsequence 4 | A = [6,2,5,1,7,4,8,3] 5 | 6 | def sub_solve(arr): 7 | length = {} 8 | for k in range(len(arr)): 9 | length[k] = 1; 10 | for i in range(k): 11 | if arr[i] < arr[k]: 12 | length[k] = max(length[k], length[i]+1) 13 | return max(length.values()) 14 | 15 | print(sub_solve(A)) 16 | 17 | #================================================== 18 | #Path in a grid with max sum 19 | # the original solution from the text did not work 20 | # as it skipped the sums in the first row and column 21 | # of the grid, since the x,y loops started from 1 22 | # the solution below is a correction 23 | 24 | grid = [[3,7,8,2,7],[9,8,3,5,5],[1,7,9,8,5],[3,8,6,4,10],[6,3,9,7,8]] 25 | 26 | def max_sum(A): 27 | tot = A.copy() 28 | for y in range(0,len(A)): 29 | for x in range(0,len(A[0])): 30 | if x==0 and y==0: 31 | continue 32 | elif x == 0: 33 | tot[y][x] += tot[y-1][x] 34 | elif y == 0: 35 | tot[y][x] += tot[y][x-1] 36 | else: 37 | tot[y][x] += max(tot[y][x-1],tot[y-1][x]) 38 | return tot[-1][-1] 39 | 40 | print(max_sum(grid)) 41 | 42 | #================================================ 43 | #Knapsack problem 44 | 45 | def w_sum(weights): 46 | possible = {i:0 for i in range(sum(weights))} 47 | possible[0] = 1 48 | for k in weights: 49 | for x in range(sum(weights))[::-1]: 50 | if possible[x]: possible[x+k] = 1 51 | return possible 52 | 53 | weights = [1,3,3,5] 54 | print(w_sum(weights)) 55 | 56 | #================================================ 57 | #edit distance 58 | # bonus, since this code is not in the book 59 | 60 | def d_solve(w1, w2): 61 | dist = [[math.inf for _ in range(len(w1))] for _ in range(len(w2))] 62 | dist[0][0] = 0 63 | for x in range(len(w1)): 64 | for y in range(len(w2)): 65 | if x == 0 and y == 0: 66 | continue 67 | elif x == 0: 68 | dist[y][x] = dist[y-1][x] + 1 69 | elif y == 0: 70 | dist[y][x] = dist[y][x-1] + 1 71 | else: 72 | dist[y][x] = min(dist[y][x-1] + 1, dist[y-1][x] + 1, dist[y-1][x-1] + (w1[x] == w2[y])) 73 | return dist[-1][-1] 74 | 75 | word1 = 'movie' 76 | word2 = 'love' 77 | print(d_solve(word1,word2)) 78 | -------------------------------------------------------------------------------- /08-Amortized Analysis/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/08-Amortized Analysis/.gitkeep -------------------------------------------------------------------------------- /09-Range Queries/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/09-Range Queries/.gitkeep -------------------------------------------------------------------------------- /10-Bit Manipulation/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/10-Bit Manipulation/.gitkeep -------------------------------------------------------------------------------- /11-Basics Of Graphs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/11-Basics Of Graphs/.gitkeep -------------------------------------------------------------------------------- /12-Graph Traversal/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/12-Graph Traversal/.gitkeep -------------------------------------------------------------------------------- /12-Graph Traversal/graph_traversal.py: -------------------------------------------------------------------------------- 1 | graph = {1: [2, 4], 2: [1, 3, 5], 3: [2, 5], 4: [1], 5: [2, 3]} 2 | 3 | 4 | # ======================= 5 | # Depth first search 6 | 7 | def dfs(s): 8 | visited = [0] * (len(graph) + 1) 9 | if (visited[s]): return 10 | visited[s] = True 11 | print(s) 12 | for u in graph[s]: 13 | dfs(u) 14 | 15 | 16 | dfs(1) 17 | 18 | 19 | # ================== 20 | # breadth first search 21 | 22 | def bfs(): 23 | visited = [0] * (len(graph) + 1) 24 | distance = [0] * (len(graph) + 1) 25 | visited[1] = True 26 | q = [1] 27 | 28 | while len(q) > 0: 29 | s = q.pop() 30 | print(s) 31 | for u in graph[s]: 32 | if visited[u]: continue 33 | visited[u] = True 34 | distance[u] = distance[s] + 1 35 | q.append(u) 36 | print(distance[1:]) 37 | 38 | 39 | bfs() 40 | 41 | # ======================= 42 | # Bellman-Ford shortest distance 43 | import math 44 | 45 | e_list = [[1, 2, 5], [1, 4, 7], [1, 3, 3], [2, 1, 5], [2, 4, 3], [2, 5, 2], 46 | [3, 1, 3], [3, 4, 1], [4, 3, 1], [4, 1, 7], [4, 5, 2], [5, 4, 2], [5, 2, 2]] 47 | 48 | 49 | def shortest(): 50 | distance = [math.inf for _ in range(len(e_list) + 1)] 51 | distance[1] = 0 52 | for e in e_list: 53 | a, b, w = e 54 | distance[b] = min(distance[b], distance[a] + w) 55 | print(distance[1:]) 56 | 57 | 58 | shortest() 59 | 60 | # ============================================================== 61 | # Dijkstra's algo 62 | import heapq 63 | 64 | graph = {1: [(2, 5), (4, 9), (5, 1)], 2: [(1, 5)], 4: [(1, 9), (5, 1)], 5: [(1, 1), (4, 2)]} 65 | 66 | 67 | def dijkstra(): 68 | distance = [math.inf for _ in range(6)] 69 | distance[1] = 0 70 | stack = [(0, 1)] 71 | heapq.heapify(stack) 72 | while stack: 73 | w, n = heapq.heappop(stack) 74 | for e in graph[n]: 75 | if distance[n] + e[1] < distance[e[0]]: 76 | distance[e[0]] = distance[n] + e[1] 77 | heapq.heappush(stack, (e[1], e[0])) 78 | print(distance[1:]) 79 | 80 | 81 | dijkstra() 82 | -------------------------------------------------------------------------------- /13-Shortest Paths/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/13-Shortest Paths/.gitkeep -------------------------------------------------------------------------------- /14-Tree Algorithms/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/14-Tree Algorithms/.gitkeep -------------------------------------------------------------------------------- /15-Spanning Trees/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/15-Spanning Trees/.gitkeep -------------------------------------------------------------------------------- /16-Directed Graphs/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/16-Directed Graphs/.gitkeep -------------------------------------------------------------------------------- /17-Strong Connectivity/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/17-Strong Connectivity/.gitkeep -------------------------------------------------------------------------------- /18-Tree Queries/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/18-Tree Queries/.gitkeep -------------------------------------------------------------------------------- /19-Paths And Circuits/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/19-Paths And Circuits/.gitkeep -------------------------------------------------------------------------------- /20-Flows And Cuts/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/20-Flows And Cuts/.gitkeep -------------------------------------------------------------------------------- /21-Number Theory/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/21-Number Theory/.gitkeep -------------------------------------------------------------------------------- /22-Combinatorics/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/22-Combinatorics/.gitkeep -------------------------------------------------------------------------------- /23-Matrices/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/23-Matrices/.gitkeep -------------------------------------------------------------------------------- /24-Probability/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/24-Probability/.gitkeep -------------------------------------------------------------------------------- /25-Game Theory/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/25-Game Theory/.gitkeep -------------------------------------------------------------------------------- /26-String Algorithms/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/26-String Algorithms/.gitkeep -------------------------------------------------------------------------------- /27-Square Root Algorithms/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/27-Square Root Algorithms/.gitkeep -------------------------------------------------------------------------------- /28-Segment Trees Revisited/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/28-Segment Trees Revisited/.gitkeep -------------------------------------------------------------------------------- /29-Geometry/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/29-Geometry/.gitkeep -------------------------------------------------------------------------------- /30-Sweep Line Algorithms/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/30-Sweep Line Algorithms/.gitkeep -------------------------------------------------------------------------------- /Competitive-Programmer-Handbook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhishekgahlot/competitive-programmer-handbook-python/17fb9269439b6f9c3a76e32d80104d1ac9130641/Competitive-Programmer-Handbook.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Abhishek Gahlot 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Competitive-Programmer-Handbook-Python 2 | 3 | [![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/abhishekgahlot/competitive-programmer-handbook-python/graphs/commit-activity) [![GitHub issues](https://img.shields.io/github/issues/abhishekgahlot/competitive-programmer-handbook-python)](https://github.com/abhishekgahlot/competitive-programmer-handbook-python/issues) 4 | [![GitHub forks](https://img.shields.io/github/forks/abhishekgahlot/competitive-programmer-handbook-python?style=social)](https://github.com/abhishekgahlot/competitive-programmer-handbook-python/network) [![GitHub stars](https://img.shields.io/github/stars/abhishekgahlot/competitive-programmer-handbook-python?style=social)](https://github.com/abhishekgahlot/competitive-programmer-handbook-python/stargazers) 5 | 6 | Python 3 Implementation 7 | 8 | >Existing code will be converted to python 3. 9 | 10 | Code in Python for all the Competitive Programmer Handbook 11 | 12 | Tried to rewrite code written in Competitive Programmer Handbook in Python 13 | 14 | >Book Link: https://cses.fi/book/book.pdf 15 | 16 | All Credits goes to the original author of the book `Antti Laaksonen`. 17 | 18 | `MIT License`, Feel Free to use the implementation 19 | 20 | 21 | --------------------------------------------------------------------------------