Upload
ethelbert-white
View
217
Download
0
Embed Size (px)
Citation preview
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 $
Problem definition
Input:Array(t) of length n, contains entries from the set { 1, 2, …, n-1 }
Output:One of duplicated numbers
Criterions
• Time complexity - Ot
• Memory complexity - Om
• Destructive
An algorithm is destructive if and only if it destroy a input.
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
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;
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];
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;
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;
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;
Different abstract level
• Input 0 1 2 3 4 5
1
2
3
4
50
1 34 5 2 4
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
Race
A
B
C
DAB = L/2BC = L/2CD = LDC = M-LEC = L
E
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?
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