17
Algorithms and Data Structures, Fall 2011 Sorting and searching case studies Rasmus Pagh Based on slides by Kevin Wayne, Princeton Algorithms, 4 th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 Monday, October 10, 11

Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Algorithms and Data Structures, Fall 2011

Sorting and searching case studies

Rasmus Pagh

Based on slides by Kevin Wayne, PrincetonAlgorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011

Monday, October 10, 11

Page 2: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Today’s lecture

We will focus on these intended learning outcomes:• Choose among and make use of the most important algorithms and data

structures in libraries, based on knowledge of their complexity.• Design algorithms for ad hoc problems by using and combining known

algorithms and data structures.

The lecture will be based on a number of case studies, many of which have previously been posed as problems in the course.

In SW section 3.5 you can find further examples.

2

Monday, October 10, 11

Page 3: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: A small database

Id Name Salary 1 Joe White 60,000 2 Will Jones 55,000 … … …

Id BossID Project 1 1 APL 2 4 GUI … … …

Id Year Profit APL 1999 -100,000 GUI 2000 30,000 … … …

Employees

ProjectAssignment

Projects

3

Monday, October 10, 11

Page 4: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: A small database

Some database queries• Given the name of an employee, find his/her id and salary.• Find the total profit of all projects in the year 2000.• Find employees who have a higher salary than another employee who was

their boss in some project.

Some database updates• Change the salary of an employee.• Add a new employee or project.• Assign an employee to work on a project.

Task: Consider possibilities for indexing.Notation: na assignments, ne employees.

4

Id Name Salary 1 Joe White 60,000 2 Will Jones 55,000 … … …

Id BossID Project 1 1 APL 2 4 GUI … … …

Id Year Profit APL 1999 -100,000 GUI 2000 30,000 … … …

Employees

ProjectAssignment

Projects

Monday, October 10, 11

Page 5: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Goal. Index a PC (or the web).Simplified goal: Given two or more words, report the list of (text) files in which these words occur.

Case study: File indexing

5

Monday, October 10, 11

Page 6: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: File indexing

Inverted index: For each word, list the files that contain it.

Building an inverted index:Problem session!

6

Monday, October 10, 11

Page 7: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: File indexing

Inverted index: For each word, list the files that contain it.

Building an inverted index:• Replace file names by small ID numbers: Use a symbol table to look up.• Fill a symbol table:•Key = word•Value = unbounded array of file IDs

Can also use these symbol tables to answer queries.

Problem: What if data is so large that it does not fit in RAM: Thrashing!

7

Monday, October 10, 11

Page 8: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

8

File system model

Page. Contiguous block of data (e.g., a file or 4096-byte chunk).Probe. First access to a page (e.g., from disk to memory).

Property. Time required for a probe is much larger than time to accessdata within a page.

Cost model. Number of probes.

Goal. Access data using minimum number of probes.

slow fast

Monday, October 10, 11

Page 9: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

B-tree. Generalize 2-3 trees by allowing up to M - 1 key-link pairs per node.

• At least 2 key-link pairs at root.

• At least M / 2 key-link pairs in other nodes.

• External nodes contain client keys.

• Internal nodes contain copies of keys to guide search.

9

B-trees (Bayer-McCreight, 1972)

choose M as large as possible so

that M links fit in a page, e.g., M = 1024

Anatomy of a B-tree set (M = 6)

2-node

external3-node external 5-node (full)

internal 3-node

external 4-node

all nodes except the root are 3-, 4- or 5-nodes

* B C

sentinel key

D E F H I J K M N O P Q R T

* D H

* K

K Q U

U W X Y

each red key is a copyof min key in subtree

client keys (black)are in external nodes

Monday, October 10, 11

Page 10: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

• Start at root.

• Find interval for search key and take corresponding link.

• Search terminates in external node.

* B C

searching for E

D E F H I J K M N O P Q R T

* D H

* K

K Q U

U W X

search for E inthis external node

follow this link becauseE is between * and K

follow this link becauseE is between D and H

Searching in a B-tree set (M = 6)

10

Searching in a B-tree

Monday, October 10, 11

Page 11: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Sorting on disk

Even if B-trees are used, symbol table operations use 1 or more I/Os.Is there an alternative approach? Yes!

• Replace file names and words by small ID numbers: Use a symbol table to look up.

• Create a list of (fileID,wordID) pairs, output it to disk.

• Sort the list according to wordID.

• Traverse the sorted lists to build the inverted index (create symbol table).

Question: How does one best sort data on disk?- Mergesort, quicksort both read/write items around log n times.- Multiway versions typically read/write each item only twice!(The amount of CPU work is about the same as if we had all in RAM.)

11

Monday, October 10, 11

Page 12: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: Referential integrity

12

Monday, October 10, 11

Page 13: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: Using trees

13

Monday, October 10, 11

Page 14: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

14

Ordered symbol table API

09:00:00 Chicago 09:00:03 Phoenix 09:00:13 Houston 09:00:59 Chicago 09:01:10 Houston 09:03:13 Chicago 09:10:11 Seattle 09:10:25 Seattle 09:14:25 Phoenix 09:19:32 Chicago 09:19:46 Chicago 09:21:05 Chicago 09:22:43 Seattle 09:22:54 Seattle 09:25:52 Chicago 09:35:21 Chicago 09:36:14 Seattle 09:37:44 Phoenix

keys values

get(09:00:13)

ceiling(09:30:00)

keys(09:15:00, 09:25:00)

size(09:15:00, 09:25:00) is 5rank(09:10:25) is 7

floor(09:05:00)

min()

select(7)

max()

Examples of ordered symbol-table operations

Monday, October 10, 11

Page 15: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study

15

Monday, October 10, 11

Page 16: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Anagrams, continued

16

Monday, October 10, 11

Page 17: Algorithms and Data Structures, Fall 2011itu.dk/people/pagh/ads11/06-SortingSearchingCaseStudies.pdfToday’s lecture We will focus on these intended learning outcomes: • Choose

Case study: Analysis of trees

Task: Analyze the running time of the algorithm.If needed, suggest an alternative implementation.

17

Monday, October 10, 11