Algorithms
Contents
Algorithms¶
The content of this repository is also hosted at: https://peter-lucia.github.io/algorithms/
Table of Contents:¶
Graph¶
Shortest Path from every vertex to every other vertex Floyd Warshall
There are 3 basic ways to represent a graph in memory (objects and pointers, matrix, and adjacency list). Each representation and its pros + cons.
Trees / Binary Search Tree¶
Basic tree construction, traversal and manipulation algorithms
Tree traversal algorithms: BFS Adj list and DFS Adj list
The difference between inorder, postorder and preorder.
Check if a given array can represent Preorder Traversal of Binary Search Tree
116. Populating Next Right Pointers in Each Node in a Perfect Binary Tree
HashTables¶
Sorting And Searching¶
Merge Sort can be highly useful in situations where quicksort is impractical
Interpolation Search
Find Kth Smallest/Largest Element In Unsorted Array
Given a sorted array and a number x, find the pair in array whose sum is closest to x
Counting Sort Pseudocode Worst Time: O(n+k), Worst Space: O(k), k = max(nums)
Dynamic Programming¶
Longest Path In Matrix
Optimal Strategy for a Game
Boolean Parenthesization Problem
Number Theory¶
Modular multiplicative inverse
Euler’s Totient Function
Convex Hull
Basic and Extended Euclidean algorithms
Segmented Sieve
Chinese remainder theorem
Lucas Theorem
Math: Combinatorics and Probability¶
Probability problems, and other Discrete Math 101 situations.
The essentials of combinatorics and probability.
String / Array¶
Count triplets with sum smaller than a given value
Convert array into Zig-Zag fashion
Generate all possible sorted arrays from alternate elements of two given sorted arrays
Pythagorean Triplet in an array
Length of the largest subarray with contiguous elements
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
Smallest subarray with sum greater than a given value
Linked List¶
Insertion of a node in Linked List (On the basis of some constraints)
Delete a given node in Linked List (under given constraints)
Merge A Linked List Into Another Linked List At Alternate Positions
Reverse A List In Groups Of Given Size
Union And Intersection Of 2 Linked Lists
Detect And Remove Loop In A Linked List
Merge Sort For Linked Lists
BIT Manipulation¶
Magic Number
Sum of bit differences among all pairs
Swap All Odds And Even Bits
Find the element that appears once
Count total set bits in all numbers from 1 to n
Rotate bits of a number
Count number of bits to be flipped to convert A to B
Find Next Sparse Number
Python Language¶
Geometry¶
NP-Complete¶
NP-Complete means a problem is both NP-Hard and a solution is verifiable in polynomial time.
Structure of NP-Complete proofs
Demonstrate that we can validate a solution for B in polynomial time (B is in NP)
Show the reduction from a known problem, \(A \leq_p B\) (A is no harder than B and B is at least as hard as A). (B is NP_Hard)
Instance of A converted to instance of B in polynomial time
Solution of B converted to solution of A in polynomial time
If you have a solution for B you have a solution for A
If no solution for B no solution for A (or contra-positive – if you have a solution for A then you have a solution for B)