flashcards (1)
Change Making Problem
Goal: Find minimum number of coins to make a given amount
Properties:
- Dynamic programming problem
- Greedy approach works for some currency systems but not all
- Can be solved optimally with DP in O(nW) time, n is number of coins, W is target amount
Example: Making 30¢ with [25,10,5,1] coins → [25,5] is optimal
Shortest Path Problem
Goal: Find minimum weight path between nodes in weighted graph
Key Algorithms:
- Dijkstra’s (no negative weights) O(nlogn)
- Bellman-Ford (handles negative weights) O(n*e)
Properties:
- Fundamental graph problem
- Many variations (single-source, all-pairs)
- Core component in navigation systems
Minimum Spanning Tree
Goal: Connect all vertices with minimum total edge weight
Key Algorithms:
- Prim’s (builds tree from single vertex)
Properties:
- Time complexity O(E log V)
- Tree has exactly V-1 edges
- No cycles permitted
PageRank
Goal: Measure importance of nodes based on incoming connections
Properties:
- Used in Google’s search algorithm
- Accounts for both quantity and quality of links
- Uses random surfer model with damping factor
Implementation:
- Iterative matrix multiplication algorithm
- Converges to steady state
- Time complexity O(E) per iteration
- Number of iterations for convergence depends on:
• Desired precision
• Graph structure
• Damping factor (typically 0.85)
Graph Coloring
Goal: Color vertices so no adjacent vertices share colors
Properties:
- NP-complete
- Minimum colors needed is chromatic number
Applications:
- Scheduling problems
- Register allocation
- Map coloring
Traveling Salesman Problem
Goal: Find shortest route visiting each city exactly once
Properties:
- NP-hard optimization problem
- No known polynomial time solution
- Small instances solvable exactly (<20 cities)
Approaches:
- Branch and bound for exact solution
- Approximation algorithms
- Heuristics like simulated annealing
Knapsack Problem
Goal: Select items maximizing value while respecting weight limit
Types:
- 0/1 Knapsack (take item or not)
- Fractional Knapsack (can take portions)
Properties:
- NP-complete (0/1 version)
- Greedy solution works for fractional version
- Dynamic programming pseudo-polynomial solution, making it o(nW)
P, NP, NP-Complete and NP-Hard
P: Problems solvable in polynomial time (like sorting)
NP: Problems whose solutions can be verified in polynomial time (like Sudoku)
NP-Complete: Problems that are both NP and if solved in polynomial, would solve all NP problems (like Boolean Satisfiability/SAT)
NP-Hard: Problems at least as hard as NP-Complete problems but might not be in NP (like Halting Problem)
P ⊆ NP ⊆ NP-Complete ⊆ NP-Hard
NP-Hard Problems
Definition: At least as hard as hardest NP problems
Properties:
- May not be in NP
- No polynomial time solution known
- Reducing any NP problem to it is possible
Examples:
- TSP optimization
- Halting problem
- Graph isomorphism
NP-Complete
Definition: Both NP-hard and in NP
Properties:
- Hardest problems in NP
- All NP-complete problems reducible to each other
- Finding polynomial solution proves P = NP
Famous Examples:
- Boolean satisfiability (SAT)
- Hamiltonian cycle
- Graph coloring
Dijkstra’s Algorithm
Goal: Find shortest paths from source to all vertices
Properties:
- Cannot handle negative weights
- Greedy approach
- Uses priority queue
Complexity:
- O(V²) basic implementation
- O(E log V) with binary heap
- O(E + V log V) with Fibonacci heap
Floyd-Warshall
Goal: Find shortest paths between all pairs of vertices
Properties:
- Dynamic programming approach
- Can handle negative weights
- Cannot handle negative cycles
- Simple to implement
Complexity: O(V³) time
Bellman-Ford
Goal: Single-source shortest paths with negative weights
Properties:
- Can detect negative cycles
- Simpler than Dijkstra’s
- Uses edge relaxation
Complexity:
- O(VE) time
- Requires V-1 iterations to find paths
Merge Sort
Strategy: Divide and conquer sorting
Properties:
- Stable sort
- O(n log n) time complexity
- Requires O(n) extra space
Steps:
1. Split array in half
2. Recursively sort halves
3. Merge sorted halves
Quick Sort
Strategy: Divide and conquer sorting using pivots
Properties:
- In-place but not stable
- Average O(n log n)
- Worst case O(n²)
Steps:
1. Choose pivot
2. Partition around pivot
3. Recursively sort partitions