Title: SEARCHING, SORTING, TOPOLOGICAL SORTS
1SEARCHING, 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.
2If 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.
3When the data in the array is stored in some
order - ascending or descending - the amount of
time needed to search for a key value is reduced
dramatically.
Play the Guess the Number Game.
Think of a number between 1 and 1000. I can
guess your number in 10 or fewer guesses. (Much
less than 1000 guesses!)
4What 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?
5The 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.
6This shows the importance of being able to sort
data into some order ascending or descending.
There are many different sort algorithms in
computer science. View the 3 basic algorithms
Bubble Sort, Selection Sort and Insertions Sort,
found on our web page.
Write an algorithm for each of the 3 sorts.
7Sometime the sorting of a list of data is not
unique. Thats 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.
8TOPOLOGICAL 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
9Abstracting the tasks we label hat 1 diaper
2 undershirt 3 socks 4 shoes 5 coat 6 dress 7
Represent precedence rules by ordered
pairs (2,3) (2,7) (2,6) (4,5) (7,6) (3,7) (3,6)
10We have 7 tasks 1,2,3,4,5,6,7 and
7 ordered pairs (2,3) (2,7) (2,6) (4,5) (7,6)
(3,7) (3,6) (that is just a coincidence !!!!)
A topological sort is a listing of the order in
which these tasks can be performed.
For example 4,5,1,2,3,7,6
Is this the only topological sort that will get
the job done?
No. Find another.
11A 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. 4. Place that
nodes name at the beginning of the list. 5.
Repeat until all nodes are in the list.
12HOW CAN WE FIND ALL TOPOLOCIAL SORTS?
We can do this by using arrays and linked lists.
1. Create a precedence (directed) graph. 2.
Make an array containing the no. of predecessors
of each node. 3. Make an array of linked lists
of successors of each node.
134. Place all nodes with zero predecessors into
another array, called bag. 5. 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).
146. When the bag is empty, one topological sort
has been found. Remove the last item in the
sorted list. 7. 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. 8. Repeat from step 5. 9. 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.
15Assignment Create a job that has 5 or 6 tasks
with some precedence rules. Write this on one
sheet of paper. On another sheet of paper find
all topological sorts for the job.