Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Peter Andreae CS4HS Algorithms Searching for an item in a list Sorting a list Searching for a word in text Analysing Networks.

Similar presentations


Presentation on theme: "© Peter Andreae CS4HS Algorithms Searching for an item in a list Sorting a list Searching for a word in text Analysing Networks."— Presentation transcript:

1 © Peter Andreae CS4HS Algorithms Searching for an item in a list Sorting a list Searching for a word in text Analysing Networks.

2 © Peter Andreae CS4HS Searching in a list. “Find the name ‘Stevens, D’ in the list” What algorithm should you use? Linear Search for target item from position 1 to position 300 if item at the position is target, exit with “found” exit with “not found” Binary Search lower ← 1, upper ← 300, while (lower ≤ upper) position ← (upper + lower)/2 if target is at position: exit with “found” if target < item at position: upper = position-1 if target > item at position: lower = position + 1 exit with “not found”

3 © Peter Andreae CS4HS Searching in a list. Interpolation Search for target item lower ← 1, upper ← 300, while (lower ≤ upper) position ← compute estimate of position of target based on items at lower and upper if target = item at position: exit with “found” if target < item at position: upper = position-1 if target > item at position: lower = position + 1 exit with “not found” fraction ← (target – item@lower)/(item@upper – item@lower) position ← lower + fraction ∗ (upper-lower)

4 © Peter Andreae CS4HS Sorting a list of items? Which algorithm did you use? Insertion Sort insert each item in turn into its right place in the growing list Selection Sort Repeatedly find the smallest (or largest) and put it next in the sorted list Radix Sort Put items in piles based on the first letter Sort each pile Quick Sort Separate into lists of the smaller items and larger items, (by comparing each item to the first item – “pivot”) Sort each list Merge Sort Lay out items as a sequence of of one item lists Repeatedly merge adjacent lists into larger lists.

5 © Peter Andreae CS4HS Does it matter which you use?

6 © Peter Andreae CS4HS Searching for a string in text Find the string “T A N D W H A” in the text. Remember, the computer can only look at one character in the text at a time! How did you do it? Can we do it faster? Simplified Boyer-Moore Algorithm: Key idea: search from the END of the string if there is a mismatch, you can jump ahead Doesn’t even look at every character in the text once! Needs a “jump” table to say how far to move. 7 6 5 4 3 2 1

7 © Peter Andreae CS4HS Simplified Boyer-Moore Algorithm Find the string “T A N D W H A” in the text. W H E N A L I C E H A D A H A T A N D W H A T A H A T T A N D W H A

8 © Peter Andreae CS4HS Simplified Boyer-Moore Algorithm Make a “jump” table: “T A N D W H A” textpos ← 7 (length of string) stringpos ← 0 while textpos ≤ end of text jump ← look up character at textpos in the table while stringpos < length of string if text char at (textpos – stringpos) = string char at stringpos stringpos ← stringpos + 1 else stringpos ← 0 textpos ← textpos + jump go back to outer loop exit with “found” exit with “not found” A: 5 D: 3 H: 1 N: 4 T: 6 W: 2 else: 7 6 5 4 3 2 1 0

9 © Peter Andreae CS4HS Analysing Networks Minimum “Spanning Trees” How could I connect all these towns by fibre optic cables at the minimum cost! (cost might depend on distance, terrain, and land ownership) Shortest Path from here to there How does Google maps find the shortest path in the network of roads? How do the NPC’s in games work out where to go? Lots more “graph” algorithms Articulation Points critical nodes that would disconnect the network Maximum Flow given a network of railways/pipes/… with maximum capacities, what is the maximum amount of freight/oil/… that can be pushed through the network from A to B.

10 © Peter Andreae CS4HS Find Minimum Spanning Tree. 6 4 5 9 4 1 5 8 3 7 3 8 9 7 6 6 9 2 5 2 3 5 7 6 4 1 8 3 4 9 5 2 8 8 5 7 9 2 7 5 4 6 7 3 2 3 5 9 8 4 2 6 2 5 3 3 4 4 5 2 7 8 6 9 8 6 2 3 7 2 9

11 © Peter Andreae CS4HS Minimum Spanning Tree How did you do it? Prim’s Algorithm: Build connected subset outwards, always adding the smallest edge to an unconnected node Kruskal’s Algorithm: Add the next smallest edge that doesn’t make a loop (have to keep track of the connected subsets)

12 © Peter Andreae CS4HS Find shortest path in graph. 6 4 5 9 4 1 5 8 3 7 3 8 9 7 6 6 9 2 5 2 3 5 7 6 4 1 8 3 4 9 5 2 8 8 5 7 9 2 7 5 4 6 7 3 2 3 5 9 8 4 2 6 2 5 3 3 4 4 5 2 7 8 6 9 8 6 2 Start End 3 7

13 © Peter Andreae CS4HS Shortest Path How did you do it? Dijkstra’s Algorithm: Build connected subset outwards, always adding the edge for the shortest PATH to unconnected node Stop when you reach the goal A* search Algorithm: Same as Dijkstra, but have heuristic look ahead to aim towards goal.


Download ppt "© Peter Andreae CS4HS Algorithms Searching for an item in a list Sorting a list Searching for a word in text Analysing Networks."

Similar presentations


Ads by Google