Docsity

Algorithm 1, Searching Algorithms - Lecture Notes | CSCI 243, Study notes of Discrete Structures and Graph Theory

Partial preview of the text

Download Algorithm 1, Searching Algorithms - Lecture Notes | CSCI 243 and more Study notes Discrete Structures and Graph Theory in PDF only on Docsity!

Discrete Structures

 - 9/13/ 
  • CSci
    • Lecture
      • Algorithms

9/13/

Outline^ • Definition• Defining Characteristics• Searching Algorithms• Devising Algorithms

9/13/

Example: Searing a Steak^ •^

Pat steak dry and let sit for 15 minutes.

-^

Coat steak thinly with vegetable oil.

-^

Rub ½ tsp salt and ¼ tsp pepper into both sides.

-^

Let steak sit for 5 minutes. Meanwhile open a window.

-^

Heat pan until a water drop jumps in the pan.

-^

Cook on pan for 3 minutes. Don’t touch it.

-^

Flip and cook for 3 more minutes. Don’t touch it.

-^

Pull from pan and let rest for 5 minutes before eating.— Alton Brown,

I’m Just Here for the Food

9/13/

Language of Operations^ • From example: pat, coat, rub, heat, cook, flip• Algorithms assume a set of operations:

sear steak is not useful if you don’t know

sear

  • For computers, it’s all about shuffling data• e.g.:

sort list, vs.

move,

compare data

9/13/

Example: CISC vs. RISC^ • CISC: complex instruction set computer

  • e.g. VAX (300+ instr, polynomial eval, linked lists)• Shorter code, “powerful” instructions, simpler

compilers, easier debugging, fewer memory accesses

  • RISC: reduced instruction set computer
    • e.g. Sparc, PowerPC• Easier to optimize (fixed op times), simpler chips,

simple ops very commonly used, few args

  • Mix: convert CISC to mini-RISC
    • e.g. Pentium, Athlon• Convert CISC -> RISC, compatibility with speed

9/13/

Algorithm: Defining Characteristics^ • Input: The original data• Output: The resulting data (solution)• Definiteness: Every step is defined precisely• Correctness: Correct output for all inputs• Finiteness: A finite number of steps to result• Effectiveness: Each step exactly in finite time• Generality: Applicable to similar problems, not

just one set of inputs

9/13/

Not Definite^ • find_max(set of numbers) {

max = maximum(numbers)return max }

9/13/

Not Correct^ • find_max(set of numbers) {

max = a number in numbersforeach a_number in numbers

return a_number if a_number > max return max }

  • QC: What does this really do?• QC: For what inputs does it actually work?

9/13/

Not Effective^ • Computer languages designed to run in finite

time

  • function_halts(function foo) {

call foo()return true }

9/13/

Not General^ • find_max({5,9,13,7,2}) {

return 13 }

  • QC: When might you

want to hardcode

answers?

9/13/

Search a Random List^ • You dropped your playing cards.• How to find a joker?• find_joker(list of cards)

foreach a_card in cards

if a_card is a joker then

return a_card

return NOT_FOUND }

  • QC: Does it work for sorted lists?

9/13/

Binary Search Example (Informal)^ •

Efficient algs exploit structure in the input or problem

  • Binary search for c in
„^

a, b, c, d, e, f, g, h

Ò

-^

Split:

„^

a, b, c, d

Ò^

and

„^

e, f, g, h

Ò

-^

Compare: c <= d and choose:

„^

a, b, c, d

Ò

-^

Split:

„^

a, b

Ò^

and

„^

c, d

Ò

-^

Compare: c > b and choose:

„^

c, d

Ò

-^

Split:

„^

c^

Ò^ and

„^

d^

Ò

-^

c = c, so found it

9/13/

19

Binary Search Example (Algorithm)^ • Search for d in

a, b, c, d, e, f, g, h, i, j

Ò

  • lowpoint = 1, highpoint = 10• pivot = 5• d <= e, so highpoint = 5• pivot = 3• d > c, so lowpoint = 4• pivot = 4• d <= d, so highpoint = 4• d == d, so return 4

9/13/

Next up… Devising Algorithms

sub reformat(@){ my $columns;if ($#_ > 0 && $_[0] =~ /^\d+$/) {

$columns = shift;} else {$columns = 80;} my $text = join '\n',@_;my ($ending_newlines) = $text =~ /(\n)$/s;$ending_newlines = '' unless defined $ending_newlines;$text =~ s/\n$//;# Change all the newlines to spaces in preparation of reformatting.$text =~ s/\n/ /g;$Text::Wrap::columns = $columns;my $formatted = wrap('','',$text);# Tack a newline on the end if the original had one.$formatted .= $ending_newlines;return $formatted;}