Top Banner
How Would You Move Mount Fuji ? Andrzej Hoppe
16

How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Jan 19, 2016

Download

Documents

Ethelbert White
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

How Would You Move Mount Fuji ?

Andrzej Hoppe

Page 2: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Goals

• Algorithms and Puzzles are fun

• Give a chance to win coffee• What does the title mean ?

Page 3: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Why should we like algorithms ?

• 6111 - A+ • Free pizza• Internship• 50,000 $

Page 4: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Problem definition

Input:Array(t) of length n, contains entries from the set { 1, 2, …, n-1 }

Output:One of duplicated numbers

Page 5: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Criterions

• Time complexity - Ot

• Memory complexity - Om

• Destructive

An algorithm is destructive if and only if it destroy a input.

Page 6: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

6 solutions

Time complexity Memory Complexity Destructive

n2 1 no

nlgn n yes

nlgn n no

n n no

n 1 yes

n 1 no

Page 7: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Ot(n2) , Om(1) , no

For each number check if number doesn’t occur twice

for (int i=1; i<=n; i++) // for each number int occurrence = 0;

for (int j=0; j<n;j++) // for each cell if (t[j]==i)

occurrence++;if (occurance==2)

return i;

Page 8: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Ot(nlgn) , Om(1) , yes

Sort and check if two values in sibling cells are not equal.

sort(t);for (int i=0;i<n-1;i++)

if (t[i]==t[i+1])return t[i];

Page 9: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Ot(n) , Om(n) , no

Remember numbers which occur and return number if occursrs second time.

bool tB[n+1]; //all false at the begging for (int i=0;i<n;i++)

if (tB[t[i]]==true)return t[i];

tB[t[i]]=true;

Page 10: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Ot(n) , Om(1) , yes

Traverse a table and mark a cell as visited by writing 0. If a cell has value zero then return the index of the cell.

int i=0;while (t[i]!=0)

j=t[i];t[i]=0;i=j;

return i;

Page 11: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Ot(n) , Om(1) , no

int a = t[ t[0] ];int b = t[0];do

a = t[ t[a] ]; b = t[b];

while ( a != b )

b = 0;do

a = t[a];b = t[b];

while ( a != b )return a;

Page 12: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Different abstract level

• Input 0 1 2 3 4 5

1

2

3

4

50

1 34 5 2 4

Page 13: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Animal code

a = turtle;b = greyhound; do

run ;while not meat togetherb = second turtle b - start from beggingdo

run ;while not meat togetherreturn place of meeting

Page 14: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Race

A

B

C

DAB = L/2BC = L/2CD = LDC = M-LEC = L

E

Page 15: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Puzzle to solve

How many points are there on the globe where, by walking one mile south, one mile east, and one mile north, you reach the place where you started?

Page 16: How Would You Move Mount Fuji ? Andrzej Hoppe. Goals Algorithms and Puzzles are fun Give a chance to win coffee What does the title mean ?

Future reading

• HOW WOULD YOU MOVE MOUNT FUJI? Microsoft’s Cult of Puzzle , William Poundstone

• http://www.research.ibm.com/ponder/ -

IBM Research – monthly puzzles