© peter andreae cs4hs algorithms searching for an item in a list sorting a list searching for a...
TRANSCRIPT
© Peter Andreae
CS4HS
Algorithms
• Searching for an item in a list• Sorting a list• Searching for a word in text• Analysing Networks.
© 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 300if 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)/2if target is at position: exit with “found”if target < item at position: upper = position-1if target > item at position: lower = position + 1
exit with “not found”
© Peter Andreae
CS4HS
Searching in a list.
• Interpolation Search for target itemlower ← 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-1if target > item at position: lower = position + 1
exit with “not found”
fraction ← (target – item@lower)/(item@upper – item@lower) position ← lower + fraction ∗ (upper-lower)
© 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.
© 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.
© Peter Andreae
CS4HS
Find Minimum Spanning Tree.
6
4
5
9
4
1
5
83
7
3
8
976
6
9
25
2
35
7
6
4
1
8 3
4
9
5
2
8
8
5
7
927
5
4 67
3
2
3
5
984
26
2
5 33 4
45
2
7
8
6
98
62
3
7
2
9
© 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)
© Peter Andreae
CS4HS
Find shortest path in graph.
6
4
5
9
4
1
5
83
7
3
8
976
6
9
25
2
35
7
6
4
1
8 3
4
9
5
2
8
8
5
7
927
5
4 67
3
2
3
5
984
26
2
5 33 4
4
5
2
7
8
6
98
62
Start
End
3
7
© 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.