Interesting Algorithms for Real World Problems

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advanced Data Structures
CSE332: Data Abstractions Lecture 9: B Trees Dan Grossman Spring 2010.
BST Data Structure A BST node contains: A BST contains
Heaps & Priority Queues Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© Peter Andreae CS4HS Algorithms Searching for an item in a list Sorting a list Searching for a word in text Analysing Networks.
A Level Computer Science Topic 9: Data Structures T eaching L ondon C omputing William Marsh School of Electronic Engineering and Computer Science Queen.
COMP 103 Priority Queues, Partially Ordered Trees and Heaps.
FACULTY OF ENGINEERING & INFORMATION TECHNOLOGIES P, NP, and Complexity Six fundamental facts One rule of thumb Three fundamental notions One fundamental.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW B Trees and B+ Trees COMP 261.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Michael Walker. From Maps to Graphs  Make Intersections Vertices  Make Roads Edges  Result is Weighted Directed Graph  Weights: Speed Limit Length.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Chapter 4: Trees Part I: General Tree Concepts Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
2015-T2 Lecture 30 School of Engineering and Computer Science, Victoria University of Wellington  Lindsay Groves, Marcus Frean, Peter Andreae, and Thomas.
2014-T2 Lecture 29 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
Sorting & Lower Bounds Jeff Edmonds York University COSC 3101 Lecture 5.
2015-T2 Lecture 28 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae and Thomas.
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
CSE373: Data Structures & Algorithms Lecture 8: AVL Trees and Priority Queues Linda Shapiro Spring 2016.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
CSE332: Data Abstractions Lecture 7: AVL Trees
Leftist Trees Linked binary tree.
Sorting With Priority Queue In-place Extra O(N) space
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
COMP261 Lecture 23 B Trees.
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
B/B+ Trees 4.7.
Top 50 Data Structures Interview Questions
Breadth-First Search: Complexity
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
October 30th – Priority QUeues
Lecture 22 Binary Search Trees Chapter 10 of textbook
Week 11 - Friday CS221.
Hashing Exercises.
Compsci 201 Priority Queues & Autocomplete
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
Heap Sort Example Qamar Abbas.
More complexity analysis & Binary Search
CMSC 341 Lecture 13 Leftist Heaps
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Artificial Intelligence
AVL Trees 11/10/2018 AVL Trees v z AVL Trees.
Phil Tayco Slide version 1.0 May 7, 2018
Building Java Programs
Quick Sort (11.2) CSE 2011 Winter November 2018.
Data Structures & Algorithms
Ch 6: Heapsort Ming-Te Chi
Draw pictures to indicate the subproblems middleMax solves at each level and the resulting maxPtr and PrevPtr for each on this linked list:
i206: Lecture 14: Heaps, Graphs intro.
CSE373: Data Structures & Algorithms Lecture 5: AVL Trees
CS6045: Advanced Algorithms
Building Java Programs
AVL Trees 2/23/2019 AVL Trees v z AVL Trees.
HEAPS.
Algorithms: Design and Analysis
CSC 143 Binary Search Trees.
CSE 373 Priority queue implementation; Intro to heaps
CSE 326: Data Structures Lecture #9 AVL II
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Heaps By JJ Shepherd.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
Discrete Mathematics CS 2610
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

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

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.

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

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

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?

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

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

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

How did you do it?

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.

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!

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 ?

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.

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

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

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:

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?

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

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

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

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

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

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)!

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

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

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!