CST370 - Week 7
This was our seventh week in CST 370 - Algorithms.
Counting Sort
Counting Sort is a sorting algorithm that doesn't directly compare elements to sort them.
First, we loop through the unsorted array once. During this loop we get the range of values in the array (for example, integers 10-15), we count how many times each number appears, then we calculate their distribution by adding the frequency of each number with the previous.
Then, we loop through the unsorted array again, starting from the end. For each number, we place it at the index listed in its distribution value, decrement the distribution value by 1, then continue to the next number.
The time efficiency of Counting Sort is O(n + k), where k is the range of values in the input. Counting Sort is also stable, numbers with the same value in the appear in the same order in both the input and output.
Radix Sort
Radix Sort is another sorting algorithm that doesn't sort by directly comparing numbers. Instead, it runs Counting Sort on each digit of the input values, starting at the first digit, or Least Significant Digit.
This process is repeated until the last digit, or Most Significant Digit, at which point the array will be sorted. The time efficiency of Radix Sort is O(d * (n + k)), where d is the number of digits of the number in the input. This sorting algorithm runs in linear time, which means it can be very efficient.
Dynamic Programming
Dynamic Programming is a general algorithm design technique for solving a problem with overlapping subproblems. It has the following steps:
- Set up a recurrence relation that describes a solution to a problem with smaller sub-problems.
- Solve the smaller sub-problems and record the solutions in a table.
- Solve the original problem using the table.
Fibonacci Numbers
A brute force approach to calculating Fibonacci numbers would be to use a recursive function.
- f(0) = 0
- f(1) = 1
- f(n) = f(n - 1) + f(n - 2), where n >= 2
However, this approach is inefficient, as we need to call the function many times.
A dynamic approach would be to store the numbers generated in an array, and to reference the array when getting previous Fibonacci numbers.
Coin-Collecting Problem
In the Coin-Collecting Problem, we have board of n rows and m columns. Each cell of the board can have 0 or 1 coins. We have a robot start at (1, 1) of the board. The robot can move one space to the right, or one space down. How should the robot move so that it reaches the bottom right cell of the board with as many coins as possible?
We can solve this problem recursively:
With the dynamic programming approach:
Then we start at the bottom-right cell and move backwards, always picking the cell with the higher value. We reverse the path we took back to the start, and that is our optimal value.
Coin-Row Problem
In the Coin-Row problem we have a row of n coins whose values are some positive integers. The goal is to pick up the maximum amount of money, with the limitation that no two adjacent coins can be picked up.
Using recursion:
Using Dynamic Programming:
Then to find the optimal path we backtrack the calculations.
Warshall's Algorithm (Transitive Closure)
Warshall's Algorithm computes the transitive closure of a directed graph. The Transitive Closure of a directed graph is an adjacency matrix that is updated to include indirect connections between two nodes (when one node can reach another by passing other nodes).
Floyd's Algorithm (All Pairs Shortest Paths)
Floyd's Algorithm calculates the shortest paths between all pairs in a weighted graph. The basic idea is similar to the Warshall's Algorithm.
- It considers each vertext one by one and updates the matrix accordingly.
- D(k) contains shortest distance of all pairs after considering vertices from 1 to k.
- D(n) is the final solution of all pairs' shortest distance, as it considers all n vertices.
Greedy Technique
The Greedy Technique is used to solve problems with a sequence of choices (such as a sequence of coins with different values.
Each choice must be:
- Feasible (satisfies the problem's constraints)
- Locally Optimal (Greedy) (selects the best choice among all feasible choices available at that step)
- Irrevocable (the choice cannot be changed later)
This technique is simple and useful for optimal or fast approximation.
Prim's Algorithm
Prim's Algorithm is used to find the Minimum Spanning Tree of a weighted graph. A Spanning Tree is a tree that visits every node on the graph.
- Start with a tree T1 consisting of one vertex.
- Grow T1 by adding one vertex at a time, such as T2, T3, ... Tn.
- Construct Ti+1 from Ti by adding a vertex not in Ti that is closest to those already in Ti (This is a greedy step).
- Stop when all the vertices are included.
Comments
Post a Comment