Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interesting Algorithms for Real World Problems

Similar presentations


Presentation on theme: "Interesting Algorithms for Real World Problems"— Presentation transcript:

1 Interesting Algorithms for Real World Problems
Peter Andreae, VUW Comp Sci

2 Abstract There are lots of interesting algorithms for problems that matter in the real world. Often these algorithms are explained with tiny examples that make the problem seem trivial, the algorithm uninteresting, and the mathematics irrelevant. With slightly larger and more realistic examples, the problems immediately become challenging, and students can see the power of good algorithms and the importance of the related mathematics. This workshop will explore several such problems and algorithms, which could be used in high school classrooms.

3 Topics Mathematics of algorithms Looking for a name in a list
Graph theory and Djikstra’s Shortest Path algorithm Google Directions Graph Theory, Combinatorics for complexity Priority Queues and Partially Ordered Trees For Djikstra’s algorithm, simulation, … Trees, logarithms and more Combinatorics The Halting problem Security for browsing and using apps Proof by contradiction

4 Looking for a Name in a List
Is the name “Stevens” in your list? How did you find it? Two algorithms: bottom ← 0, top ← n while you haven’t found it, and bottom ≤ top look at the middle value if target < middle: top ← middle if target > middle: bot ← middle

5 Looking for a Name in a List
Analysis: If the list had 1,000,000 names, how many names would you have to look at? If the list had n names, how many names would you have to look at?

6 Where’s the Maths? Writing down the algorithm – requires precision
Thinking about the general case. Logarithms log2 (n) = number of times you have to cut a number in half to get to 1 log2 (n) steps => If a list needs20 steps, doubling the list only takes 21 steps

7 Finding the Shortest Path in a Graph
Easy! Obvious that there is a path How long would it take a computer to find the shortest path? Who cares – it’s an easy problem. Start 8 13 4 4 5 8 2 1 4 3 2 7 2 2 End 1

8 Finding the Shortest Path in a Graph
3 6 Start 8 8 3 4 4 4 5 1 6 8 9 2 3 7 5 2 9 2 5 1 7 9 5 3 3 3 3 4 4 4 6 3 4 5 2 7 2 5 2 3 9 8 8 7 6 2 8 6 5 9 7 7 7 4 6 6 5 6 9 8 7 9 8 5 3 End 2 5 2

9 How did you do it?

10 Where’s the Maths? Graph Theory
There a lots of real world problems that can be abstracted into a network of nodes and links (or graphs of vertices and edges) Road maps (Google Directions) Social Networks (Facebook etc) The internet Mathematics is about abstracting stuff, and then working with the abstractions.

11 Where’s the Maths? Algorithms
One part of Graph Theory is coming up with effective algorithms for solving various problems in graphs Shortest paths Traveling salesman problem Finding cliques Max Flow Planarity Good algorithms are hard to find! Many algorithms are tricky!

12 Where’s the Maths? Working out whether an Algorithm is good
Complexity theory How many steps would the algorithm take if there were n nodes? Relevant maths for high school? basic combinatorics logarithms and exponentials There’s a large collection of problems for which no-one knows if there is an efficient algorithm or not (everyone believes not) P = NP ?

13 Djikstra’s Algorithm Given a graph (nodes and links) and a start node and a goal node, Find the shortest path from start to goal Key idea: Build outwards from the start node, building shortest paths to processed nodes. Keep a fringe of all the unprocessed neighbours of processed nodes Always choose the node on the fringe with the next shortest path Stop when you get to the goal.

14 Djikstra’s algorithm A D K F J H B C G 4 8 13 2 5 3 1 7 Start End E

15 Djikstra’s algorithm make a “fringe” – a list with (start, -, 0) on it
B C G 4 8 13 2 5 3 1 7 Start End E make a “fringe” – a list with (start, -, 0) on it repeat until goal is found: take best (node, from, length) off list if node’s length is not set set node’s length to length record node’s path as from for each edge from node to an unmarked neighbour pathLength = node’s length + edge length add (neighbour, node, pathlength) to fringe

16 Djikstra’s algorithm fringe: node: from: length: 8 13 5 2 1 4 3 7 A D
B C G 4 8 13 2 5 3 1 7 Start End E fringe: node: from: length:

17 Djikstra’s Algorithm How fast is it? How many steps?
Assume there are n nodes in the graph Look for the worst case. repeat take best node off the fringe mark it add its unmarked neighbours to the fringe How many times do we repeat? How many steps does each line take?

18 Partially Ordered Trees
Binary tree Children ≤ parent, Order of children not important Keep largest (or smallest) element at the root. Bee 35 Eel 26 Cat 19 Dog 14 Fox 3 Ant 9 Hen 23 Gnu 13 Jay 1

19 Partially Ordered Tree: add
Easy to add and remove because the order is not complete. Add: insert at bottom rightmost “push up” to correct position. (swapping) Bee 35 Cat 19 Eel 26 Dog 14 Fox 3 Hen 23 Ant 9 Jay 1 Gnu 13 Kea 24

20 Partially Ordered Tree: remove
Easy to add and remove because the order is not complete. Add: insert at bottom rightmost “push up” to correct position. Remove: “pull up” largest child and recurse. But: makes tree unbalanced! Bee 35 Kea 24 Eel 26 Dog 14 Cat 19 Hen 23 Ant 9 Jay 1 Gnu 13 Fox 3

21 Partially Ordered Tree: remove I
Easier to add and remove because the order is not complete. Add: insert at bottom rightmost “push up” to correct position. Remove: “pull up” largest child of root and recurse on that subtree. But: makes tree unbalanced! Alternative: replace root by bottom rightmost node “push down” to correct position (swapping) keeps tree balanced – and complete! Bee 35 Kea 24 Eel 26 Dog 14 Cat 19 Hen 25 Ant 9 Jay 1 Gnu 13 Fox 3

22 Partially Ordered Tree:
How fast is it to add or remove? If there are n nodes in the tree, how many levels? How many swaps to add or remove? So Djikstra’s algorithm takes n2 x log(n) steps Eel 26 Kea 24 Hen 23 Dog 14 Cat 19 Fox 3 Ant 9 Jay 1 Gnu 13

23 Halting Problem Malicious code on web pages, malicious apps: => major security problem. We would like to automatically detect bad programs. Can you do it? Not in general! Can’t even tell if a piece of code will loop for ever or halt!! How do we know? We can prove that no-one could do it (for all cases)!

24 Halting Problem: Proof by contradiction
Suppose you give me a “DoesItHalt” program: Given another program and an input Says whether the program would halt or loop forever when given the input Perhaps the simplest malicious code detector! DoesItHalt Program “Halts” (or “Loops”) Input

25 Halting Problem: Proof by contradiction
Suppose you give me a“DoesItHalt” program, Then I could make the following “Tricky” program: What does Tricky(Tricky) do? Tricky DoesItHalt Program If “Halts” => loop forever If “Loops” => stop

26 Summary There’s lots of interesting mathematics wrapped up in algorithms and algorithmic problems. If you get students to work on a problem, make sure it’s a big enough example Little examples are too easy to do using visual search Discrete mathematics is the the mathematics of the digital age!

27


Download ppt "Interesting Algorithms for Real World Problems"

Similar presentations


Ads by Google