81
Chapter 5: Algorithms and Heuristics CS105: Great Insights in Computer Science

Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Chapter 5:Algorithms and Heuristics

CS105: Great Insights in Computer Science

Page 2: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

The Story So Far....• Introduction to Algorithms via Socks

• Decision Problems on Lists

• Analysis of Algorithms

• Song Growth Rates

• Big O Notation

• Exponential Misuses

• Graphs

Page 3: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

The Story So Far....• Introduction to Algorithms via Socks

• Decision Problems on Lists

• Analysis of Algorithms

• Song Growth Rates

• Big O Notation

• Exponential Misuses

• Graphs

What?

Page 4: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

The Story So Far....• Introduction to Algorithms via Socks

• Decision Problems on Lists

• Analysis of Algorithms

• Song Growth Rates

• Big O Notation

• Exponential Misuses

• Graphs

What?

How Fast?

Page 5: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

The Story So Far....• Introduction to Algorithms via Socks

• Decision Problems on Lists

• Analysis of Algorithms

• Song Growth Rates

• Big O Notation

• Exponential Misuses

• Graphs

What?

How Fast?

Examples!

Page 6: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Still to Come...• Graph Algorithms

• Sorting!

• Uncomputable Things

• Halting Problem

• Huffman Codes

• Robots

Page 7: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Still to Come...• Graph Algorithms

• Sorting!

• Uncomputable Things

• Halting Problem

• Huffman Codes

• Robots

Examples!

Page 8: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Still to Come...• Graph Algorithms

• Sorting!

• Uncomputable Things

• Halting Problem

• Huffman Codes

• Robots

Examples!

Limits!

Page 9: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Still to Come...• Graph Algorithms

• Sorting!

• Uncomputable Things

• Halting Problem

• Huffman Codes

• Robots

Examples!

Limits!

Cool

Applications!

Page 10: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Graphical

A

B C

D E

F

Page 11: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Graphs Everywhere

Page 12: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Graphs Everywhere

NJPA

NY

NH

VT

ME

CT

MA

RI

Page 13: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Graphs Everywhere

NJPA

NY

NH

VT

ME

CT

MA

RIRI

Page 14: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Graphs Everywhere

NJPA

NY

NH

VT

ME

CT

MA

RIRI

Page 15: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Algorithms on Graphs

We can represent a graph in the computer by a list of nodes, and a function that, given a node i, returns the list of nodes to which i is linked.

Page 16: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Reachable• A node j is reachable from a node i if there is a path

that begins at i and ends at j.

• Let’s list all the nodes reachable from i.

• Any node that is reachable from a node that i is linked to is also reachable.

Page 17: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Don’t Revisit!

• What goes wrong? Once we realize we can reach some node, we should mark is as “reached” and never pursue it again.

Page 18: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

To-Do List Helps

• Keep track of what you need to do later once your current activity is done.

• Stack. (See ‘paintcan’ video.)

Page 19: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

Page 20: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo:

Page 21: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo: A

Page 22: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo: B

Page 23: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo: C,D

Page 24: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo: D,F

Page 25: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo: F,E

Page 26: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo: E

Page 27: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Breadth First Search

A

B C

D E

F

ToDo:

Page 28: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 29: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 30: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 31: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 32: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 33: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 34: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 35: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 36: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 37: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 38: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 39: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 40: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Depth First

A

B C

D E

F

Page 41: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Back to Google• So, how does Google do it?

I. Web crawl: download known pages, collect links to other pages, repeat.

II. Indexing: Build a giant index that associates each word with a list of pages on which it appears.

III.Distributed search: Use lots and lots and lots of computers to do fast lookups.

Page 42: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Sorting Algorithms• Another name for the lecture is “Google II”.

• Sorting is a great topic in CS:

- relatively simple

- extremely important

- illustrates lots of different algorithms and analysis techniques

There’s more than one way to skin a cat.

Page 43: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Three Pages of Words3 html3 head3 title3 monica3 babes3 rutgers3 title3 head3 frameset3 rows3 frame3 name3 banner3 scrolling3 no3 noresize3 target3 contents3 src3 header3 htm3 frameset3 cols3 frame3 name3 contents3 target3 main3 src3 left3 htm3 frame3 name3 main3 src3 right3 htm3 frameset3 noframes3 body

3 p3 this3 page3 uses3 frames3 but3 your3 browser3 doesn3 t3 support3 them3 p3 body3 noframes3 frameset3 html4 doctype4 html4 public4 w4 c4 dtd4 html4 final4 en4 html4 head4 title4 index4 of4 mlittman4 courses4 cs4 python4 title4 head4 body4 h4 index

4 of4 mlittman4 courses4 cs4 python4 h4 ul4 li4 a4 href4 mlittman4 courses4 cs4 parent4 directory4 a4 li4 a4 href4 add4 py4 add4 py4 a4 li4 a4 href4 differences4 old4 py4 differences4 old4 py4 a4 li4 a4 href4 differences4 py4 differences

4 py4 a4 li4 a4 href4 exception4 py4 exception4 py4 a4 li4 a4 href4 ml4 py4 ml4 py4 a4 li4 a4 href4 ml4 b4 py4 ml4 b4 py4 a4 li4 a4 href4 nim4 nim4 a4 li4 a4 href4 nim4 py4 nim

4 py4 a4 li4 a4 href4 nim4 py4 nim4 py4 a4 li4 a4 href4 nim4 py4 nim4 py4 a4 li4 a4 href4 python4 walkthru4 pdf4 python4 walkthru4 pdf4 a4 li4 a4 href4 socks4 py4 socks4 py4 a4 ul4 address4 apache4 server

4 at4 www4 cs4 rutgers4 edu4 port4 address4 body4 html9 header9 title9 google9 test9 unlinked9 page9 title9 header9 html9 h9 how9 does9 google9 find9 pages9 h9 this9 page9 has9 little9 purpose9 other9 than9 to9 include9 the9 word9 googleblackout9 this9 is9 a

9 word9 sort9 of9 that9 i9 concocted9 on9 january9 and9 verified9 that9 it9 was9 unknown9 to9 google9 your9 search9 b9 googleblackout9 b9 did9 not9 match9 any9 documents9 p9 there9 is9 a9 primary9 page9 http9 www9 cs9 rutgers9 edu9 mlittman9 courses9 cs

9 googletest9 html9 that9 is9 explicitly9 linked9 from9 the9 course9 homepage9 it9 has9 its9 own9 special9 term9 which9 consists9 of9 concatenating9 google9 and9 discovery9 p9 visit9 the9 primary9 page9 for9 the9 search9 history9 p9 this9 page9 is9 part9 of9 a9 href

9 index9 html9 great9 insights9 in9 computer9 science9 a9 html

Page 44: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

What Can We Do?• All the information is there, and we can sift

through it.

• But, it’s slow and error-prone to skim through every page every time we want to find something.

• If there are N words (total) on the web pages, how long would it take to sift through them each time? (Use “big O” notation.)

• How can we organize the data to simplify?

Page 45: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Sort, Remove Duplicates4 a9 a4 add4 address9 and9 any4 apache4 at4 b9 b3 babes3 banner3 body4 body3 browser3 but4 c3 cols9 computer9 concatenating9 concocted9 consists3 contents

9 course4 courses9 courses4 cs9 cs9 did4 differences4 directory9 discovery4 doctype9 documents9 does3 doesn4 dtd4 edu9 edu4 en4 exception9 explicitly4 final9 find9 for3 frame

3 frames3 frameset9 from9 google9 googleblackout9 googletest9 great4 h9 h9 has3 head4 head3 header9 header9 history9 homepage9 how4 href9 href3 htm3 html4 html9 html

9 http9 i9 in9 include4 index9 index9 insights9 is9 it9 its9 january3 left4 li9 linked9 little3 main9 match4 ml4 mlittman9 mlittman3 monica3 name4 nim

3 no3 noframes3 noresize9 not4 of9 of4 old9 on9 other9 own3 p9 p3 page9 page9 pages4 parent9 part4 pdf4 port9 primary4 public9 purpose4 py

4 python3 right3 rows3 rutgers4 rutgers9 rutgers9 science3 scrolling9 search4 server4 socks9 sort9 special3 src3 support3 t3 target9 term9 test9 than9 that9 the3 them

9 there3 this9 this3 title4 title9 title9 to4 ul9 unknown9 unlinked3 uses9 verified9 visit4 w4 walkthru9 was9 which9 word4 www9 www3 your9 your

Page 46: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Unsorted / Sorted 262, 201, 918, 301, 187, 762, 397, 277, 645, 306, 765, 798, 689, 867, 276, 402, 124, 545, 907, 569, 259, 152, 399, 481, 977, 947, 774, 727, 292, 285, 173, 599, 464, 212, 147, 696, 242, 559, 155, 569, 806, 784, 415, 321, 820, 126, 469, 225, 646, 438

124, 126, 147, 152, 155, 173, 187, 201, 212, 225, 242, 259, 262, 276, 277, 285, 292, 301, 306, 321, 397, 399, 402, 415, 438, 464, 469, 481, 545, 559, 569, 569, 599, 645, 646, 689, 696, 727, 762, 765, 774, 784, 798, 806, 820, 867, 907, 918, 947, 977

Page 47: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Sorting Helps

• Phonebook, look for a last name vs. look for a first name.

• “Is there a pair that sums to 86?” Don’t have to consider all pairs.

• Is there a repeated number in the list?

• Not to mention min, max, median.

Page 48: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Selection Sort• Idea is quite simple. We go through the list

one item at a time.

• We keep track of the smallest item we’ve found.

• When we’re through the list, we pull the smallest item out and add it to a list of sorted items.

• We repeat until all the items have been removed.

Page 49: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Selection Code

About a 2 1/2 min. to sort 100 items.

Page 50: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Selection Sort Analysis

• How many comparisons does Selection Sort do in the worst case? Assume the list is length N. Hint: What song is it like? You can use “big O” notation.

• Does it matter whether the list is sorted or not?

Page 51: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Other Sorting Approaches

• How else can you imagine sorting?

• Fewer comparisons than O(N2)?

- bubblesort

- counting sort

- insertion sort

- Shell sort

Page 52: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Guess Who?• Each player picks a

character.

• Players take turns asking each other yes/no questions.

• First player to uniquely identify the other player’s character wins!

Page 53: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Mindreader: Set Cards

Page 54: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Mindreader: Set Cards

A BC

D

E F G H

I J K L

M N O P

Page 55: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Cross-Hatched?

Page 56: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Cross-Hatched?

Page 57: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Cross-Hatched?

Page 58: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Squiggle?

Page 59: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Squiggle?

Page 60: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Squiggle?

Page 61: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight

Page 62: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

Page 63: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

Page 64: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

Page 65: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

Page 66: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

• n=1, questions = 0

Page 67: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

• n=1, questions = 0

• n=2, questions = 1

Page 68: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

• n=1, questions = 0

• n=2, questions = 1

• n=4, questions = 2

Page 69: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

• n=1, questions = 0

• n=2, questions = 1

• n=4, questions = 2

• n=8, questions = 3

Page 70: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

• n=1, questions = 0

• n=2, questions = 1

• n=4, questions = 2

• n=8, questions = 3

• n=16, questions = 4

Page 71: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Insight• Each question splits

the remaining set of possibilities into two subsets (yes and no).

• We want to pick a question so that the larger of the two subsets is as small as possible.

• Half!

• How many questions?

• n=1, questions = 0

• n=2, questions = 1

• n=4, questions = 2

• n=8, questions = 3

• n=16, questions = 4

• n, questions = lg n.

Page 72: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search

a b c d e f g

Page 73: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

a b c d e f g

Page 74: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

• How many comparisons do we need to make to find where a new item belongs in the list?

a b c d e f g

Page 75: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

• How many comparisons do we need to make to find where a new item belongs in the list?

• Can start at the bottom and compare until the new item is bigger. a b c d e f g

Page 76: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

• How many comparisons do we need to make to find where a new item belongs in the list?

• Can start at the bottom and compare until the new item is bigger.

• Maximum number of comparisons?

a b c d e f g

Page 77: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

• How many comparisons do we need to make to find where a new item belongs in the list?

• Can start at the bottom and compare until the new item is bigger.

• Maximum number of comparisons?

• One for each position: n.

a b c d e f g

Page 78: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

• How many comparisons do we need to make to find where a new item belongs in the list?

• Can start at the bottom and compare until the new item is bigger.

• Maximum number of comparisons?

• One for each position: n.

• We can ask better questions: bigger than the halfway mark?

a b c d e f g

Page 79: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search• Let’s say we have a

sorted list of n items.

• How many comparisons do we need to make to find where a new item belongs in the list?

• Can start at the bottom and compare until the new item is bigger.

• Maximum number of comparisons?

• One for each position: n.

• We can ask better questions: bigger than the halfway mark?

• That gets us: lg (n+1)!

a b c d e f g

Page 80: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Search Sort• Using O(lg N)

comparisons, can find where to insert the next item.

• Since we insert N items, comparisons is O(N lg N) in total.

• Can’t quite implement it that way, though: Once we find the spot, O(N) to stick it in.

• However, other algorithms are really O(N lg N).

• Hillis mentions Quick Sort and Merge Sort.

Page 81: Chapter 5 - Rutgers University · Reachable • A node j is reachable from a node i if there is a path that begins at i and ends at j. • Let’s list all the nodes reachable from

Binary Sort Code

About a 1/2 min. to sort 100 items.