SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.

Slides:



Advertisements
Similar presentations
Some Graph Algorithms.
Advertisements

CSE Lecture 3 – Algorithms I
M180: Data Structures & Algorithms in Java
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Skip List & Hashing CSE, POSTECH.
Topological Sort Example This job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3.
Data Structures: A Pseudocode Approach with C
Topological Sort and Hashing
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CPS120: Introduction to Computer Science Searching and Sorting.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Binary Search Introduction to Trees. Binary searching & introduction to trees 2 CMPS 12B, UC Santa Cruz Last time: recursion In the last lecture, we learned.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Programming Logic and Design Fourth Edition, Comprehensive
Chapter 3: Arrays, Linked Lists, and Recursion
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Chapter 5 Ordered List. Overview ● Linear collection of entries  All the entries are arranged in ascending or descending order of keys.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 16: Searching, Sorting, and the vector Type.
Problem Solving and Algorithms
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 19: Searching and Sorting Algorithms
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Intro to Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 2.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Data Structures Types of Data Structure Data Structure Operations Examples Choosing Data Structures Data Structures in Alice.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CSCI 51 Introduction to Programming March 12, 2009.
Lecture on Binary Search and Sorting. Another Algorithm Example SEARCHING: a common problem in computer science involves storing and maintaining large.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Data Structure and Algorithms
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Lists and Sorting Algorithms
Topological Sorting.
Data Structures I (CPCS-204)
Lesson Objectives Aims Understand the following “standard algorithms”:
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Graph Algorithms.
Arrays and Linked Lists
Bubble, Selection & Insertion sort
Binary Search and Intro to Sorting
"Learning how to learn is life's most important skill. " - Tony Buzan
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Invitation to Computer Science 5th Edition
Presentation transcript:

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.

When 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 I can guess your number in 10 or fewer guesses. (Much less than 1000 guesses!)

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.

This 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.

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

Abstracting the tasks we label: hat 1 diaper 2 undershirt3 socks4 shoes5 coat6 dress7 Represent precedence rules by ordered pairs: (2,3) (2,7) (2,6) (4,5) (7,6) (3,7) (3,6)

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,6 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. 4. Place that node’s name at the beginning of the list. 5. Repeat until all nodes are in the list.

HOW 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.

4. 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).

6. 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 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.

Assignment: 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.