From: Neil Smith Date: Thu, 4 Dec 2014 23:20:25 +0000 (+0000) Subject: Done algorithm examples X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=c83ef2510a03a1727b4ef7367edd179fab6c37a6;p=cas-master-teacher-training.git Done algorithm examples --- diff --git a/algorithms.html b/algorithms.html index f5dbe1f..57fdbae 100644 --- a/algorithms.html +++ b/algorithms.html @@ -280,6 +280,14 @@ Given a wordlist and a phrase, find another phrase that's an anagram. --- +# Sorting with cards + +Classroom activity? + +Don't forget bogosort! + +--- + # Dealing with complex problems: some terms to know (M269 Unit 5) @@ -298,16 +306,82 @@ Given a wordlist and a phrase, find another phrase that's an anagram. Build a soluion bottom-up from subparts -## Heuristics +--- + +# Heuristics * Posh word for "guess" * Greedy algorithms * Backtracking + --- -# Sorting with cards +# Making change -Classroom activity? +Given + +* some money and a set of coin denominations, + +find + +* the smallest number of coins to make that total. + +## Approach 1: greedy + +``` +Repeat: + Pick the biggest coin you can +``` + +Only works in some cases + +(What the best way to make 7p if you have 1p, 3p, 4p, 5p coins?) + +--- + +# Making change 2: exhaustive search + +.float-right[![Ticket to Ride](make-change.dot.png)] + +Make a tree of change-making. + +Pick the optimal. + +--- + +# Making change 3: dynamic programming + +Bottom-up approach + +Initial: + +0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... +-|-|-|-|-|-|-|-|-|-|-|- +(0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | ... + +-- +---- +0* | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... +-|-|-|-|-|-|-|-|-|-|-|- +(0, 0, 0) | (1, 0, 0) | (0, 1, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 1) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | ... + +-- +---- +0 | 1* | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... +-|-|-|-|-|-|-|-|-|-|-|- +(0, 0, 0) | (1, 0, 0) | (0, 1, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 1) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | ... + +-- +---- +0 | 1 | 2* | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... +-|-|-|-|-|-|-|-|-|-|-|- +(0, 0, 0) | (1, 0, 0) | (0, 1, 0) | (1, 1, 0) | (0, 2, 0) | (0, 0, 1) | (0, 0, 0) | (0, 1, 1) | (0, 0, 0) | (0, 0, 0) | (0, 0, 0) | ... + +-- +---- +0 | 1 | 2 | 3* | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... +-|-|-|-|-|-|-|-|-|-|-|- +(0, 0, 0) | (1, 0, 0) | (0, 1, 0) | (1, 1, 0) | (0, 2, 0) | (0, 0, 1) | (0, 0, 0) | (0, 1, 1) | (1, 1, 1) | (0, 0, 0) | (0, 0, 0) | ... --- @@ -315,19 +389,21 @@ Classroom activity? ## Travelling Salesman Problem -Online: http://www.math.uwaterloo.ca/tsp/games/tspOnePlayer.html +.float-right[![Ticket to Ride](ticket-to-ride-small.jpg)] + +Online: + +http://www.math.uwaterloo.ca/tsp/games/tspOnePlayer.html * But many implementations are Java applets, now a problem with security settings Try Android/iOS apps? -## Change-finding problem - -Same as the Knapsack problem: M269 Unit 5 Section 3.2, especially Activity 5.9 +## Ticket to Ride boardgame -Dynamic programming to find solution +Graph algorithms -## Ticket to Ride boardgame +* Find shortest path --- diff --git a/make-change.dot b/make-change.dot new file mode 100644 index 0000000..cba93a1 --- /dev/null +++ b/make-change.dot @@ -0,0 +1,36 @@ +digraph G { + ten [label="10\n0, 0, 0"]; + nine [label="9\n1, 0, 0"]; + eight [label="8\n 0, 1, 0"]; + eighta [label="8\n2, 0, 0"]; + five [label="5\n0, 0, 1"]; + seven [label="7\n1, 1, 0"]; + four [label="4\n1, 0, 5"]; + six [label="6\n0, 2, 0"]; + three [label="3\n0, 1, 1"]; + five [label="5\n0, 0, 1"]; + zero [label="0\n0, 0, 2"]; + sixa [label="6\n2, 1, 0"]; + fivea [label="5\n1, 2, 0"]; + two [label="2\n1, 1, 1"]; + + ten -> nine; + ten -> eight; + ten -> five; + + nine -> eighta; + nine -> seven; + nine -> four; + + seven -> sixa; + seven -> fivea; + seven -> two; + + eight -> seven; + eight -> six; + eight -> three; + + five -> four; + five -> three; + five -> zero; +} diff --git a/make-change.dot.png b/make-change.dot.png new file mode 100644 index 0000000..04edcf1 Binary files /dev/null and b/make-change.dot.png differ diff --git a/ticket-to-ride-small.jpg b/ticket-to-ride-small.jpg new file mode 100644 index 0000000..557deb0 Binary files /dev/null and b/ticket-to-ride-small.jpg differ diff --git a/ticket-to-ride.jpg b/ticket-to-ride.jpg new file mode 100644 index 0000000..4d2ac00 Binary files /dev/null and b/ticket-to-ride.jpg differ