CST 370 Week 7 Learning Journal Entry

 Non-comparison Sorting, Dynamic programming, and the Greedy Technique

Non-Comparison Sorting

Non-comparison sorting is when sorting happens without comparing elements to each other. One method discussed this week is the counting sort. In counting sort, the frequency and distribution of elements are calculated for a set of inputs, and they are used to sort them in ascending or descending order. Below are the steps to complete a counting sort and an example of using a counting sort to place a set of integers in ascending order. 
  1. Create a table with a range of values and the frequency of each value. 
  2. Calculate the distribution of each element by adding up the frequency of the current element to the previous element.  
  3. Start with the last number in the original list of values and use the distribution to place an element in a specific position in an array
  4. Reduce the distribution for that element
  5. Repeat steps 3-4 for the next element in the list. 

Counting Sort

Dynamic Programming

Dynamic programming is a design technique that solves problems with overlapping subproblems. In order this design technique must have the following three steps: 
  1. Set up a recurrence relation that describes a solution to a problem with smaller sub-problems
  2. Solve the smaller sub-problems and record their solution in a table
  3. Use the table created in Step 2 to solve the original problem
Two algorithms discussed that are examples of dynamic programming are Warshall's algorithm and Floyd's algorithm. 

Warshall's algorithm consists of completing a transitive closure of a directed graph. A transitive closure is a table that describes vertices that one vertice can reach in a graph. To get the transitive closure, we need to create an adjacency matrix. Then, we need to use each row as an intermediate node (or connector) to determine if a path exists from one node to another. A '1' means that a path is present, and a '0' means a path is not present. If a path exists, the matrix will be updated. This is considered the recurrence part of the algorithm, and the computing matrix where each node is the intermediate node would be considered the smaller sub-problems. Once all nodes have been used as intermediate nodes, the end result will be the transitive closure matrix. An example of this can be seen below.
Warshall's Algorithm

Floyd's algorithm is very similar to Warshall's algorithm in terms of process, but instead of determining whether a path exists, it also takes into consideration the cost of the path. This algorithm is used to determine the shortest path between all pairs of vertices. In order to use this algorithm, we must have a weighted graph to create the adjacent matrix. In the matrix, we use 'infinity' symbols to represent that a path does not exist, and each node cannot go to itself as we are determining the shortest path between all pairs of vertices. We can complete the same process as mentioned in Warshall's algorithm, where each node is a determinate node, to develop a matrix that shows the shortest path between all vertices. An example of this process can be found below. 

Floyd's Algorithm

Greedy Technique

The Greedy technique is a technique different from dynamic programming. In this technique, we solve problems that have a sequence of choices where the choices are feasible, logically optimal, and irrevocable. This technique is used for optimal or fast approximation, such as finding the minimal spanning tree of a graph. When finding the minimal spanning tree of a weighted graph, we can use Prim's algorithm, which is a greedy technique. To apply the algorithm, we do the following: 
  1. Pick a starting vertex and list the remaining vertex in the form destination(starting, cost). If the destination has not been reached yet, we use destination(-, 'infinity'). 
  2. Add one vertex at a time to our tree vertices list based on which is the closest/shortest. 
  3. Nodes in the remaining tree list may need to be updated as we move down the list.
  4. stop when all vertices have been included. 
An example of the Prim's algorithm can be found below. 








Comments

Popular posts from this blog

CST 334 Week 1 Journal Entry

CST 311 Week 8 Journal Entry

CST 311 Week 5 Journal Entry