Docsity

Topological Sorting: Understanding and Implementing Algorithms, Slides of Computer science

Partial preview of the text

Download Topological Sorting: Understanding and Implementing Algorithms and more Slides Computer science in PDF only on Docsity!

SEARCHING,

SORTING,

TOPOLOGICAL SORTS

Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take a lot of computer time, or a small amount of computer time, depending on the algorithm used.

If a list of 1000 data items needs to be searched for some key value, in the worst case it will take 1000 comparisons. What do we mean by the worst case? The worst case is the case when the key value is the last in the list or the key value is not found in the list. A collection of data items stored in a computer in a sequential contiguous list is called an array.

What is my algorithm?

  1. Guess the number in the middle.
  2. If that is not it, ask if the number is higher or lower.
  3. Ignore the half of the list the number cannot lie in, and guess the middle of the remaining list. Why is this algorithm so much more efficient than random or sequential guessing?

The answer lies in how many times we can cut an array, or list, in half. We can cut a number n in half, log 2 n times. As n grows, the log of n grows very slowly.

Sometime the “sorting” of a list of data is not unique. That’s right! You can get one ordering and your neighbor can get another. This occurs when a data value must precede some other data values but does not have a specific position. Many real world jobs require tasks that behave this way. Example: building a house, dressing a baby, scheduling your classes.

TOPOLOGICAL SORTING

Ex. Dressing Baby diaper must precede undershirt, dress, coat socks must precede shoes undershirt must precede dress hat can be put on anytime undershirt and dress must precede coat

7 ordered pairs: (2,3) (2,7) (2,6) (4,5) (7,6) (3,7) (3,6) (that is just a coincidence !!!!) We have 7 tasks: 1,2,3,4,5,6,7 and A topological sort is a listing of the order in which these tasks can be performed. For example: 4,5,1,2,3,7, Is this the only topological sort that will get the job done? No. Find another.

A topological sort is easy to find using a graph. View the “Topological Sort” power point presentation on our website. What is the algorithm used to find a topological sort in the presentation? 1: Draw a node for each task. 2: Connect tasks with arrows that indicate precedence. 3: Delete a node that has no successor.

  1. Place that node’s name at the beginning of the list.
  2. Repeat until all nodes are in the list.
  1. Place all nodes with zero predecessors into another array, called bag.
  2. While the bag is not empty, (a) remove a node from the bag and place it in the sorted list. (b) update the predecessor count and update the bag (if any node now has zero predecessors).
  1. When the bag is empty, one topological sort has been found. Remove the last item in the sorted list.
  2. Return to the previous version of the bag and select an item which had not been selected first, if there is any, to add to the sorted list.
  3. Repeat from step 5.
  4. When each item in a version of the bag has been marked as having been selected as first, discard the bag. When all are discarded, all sorts have been found.