Network:Name
Password:Password
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
TA Name
TA Name
TA Name
"A repeatable process for determining the solution to a problem."
What's an algorithm that you use?
What are the steps for doing it?
var piles = {'shirts': [], 'socks': []} for (var i = 0; i < clothing.length; i++) { piles[clothing[i].type].push(clothing[i]); } for (var pile in piles) { for (var i = 0; i < pile.length; i++) { pile[i].hangUp(); } }
How long does the algorithm take?
Putting an item in a pile takes 10 seconds. Hanging an item up takes 30 seconds.
# items # Seconds 4 (4*10 + 4*30) = 160 8 (8*10 + 8*30) = 320 16 (16*10 + 16*30) = 640How long does the algorithm take?
If N = items of clothing, it takes (N*10 + N*30) => O(N).
Addition: We hang up our items in sorted order in the closet.
var piles = {'shirts': [], 'socks': []} for (var i = 0; i < clothing.length; i++) { piles[clothing[i].type].push(clothing[i]); } for (var type in piles) { for (var i = 0; i < piles[type].length; i++) { var section = closetSections[type]; for (var j = 0; j < section.length; j++) { if (pile[i].color > section[j].color) { pile[i].hangUp(); } } } }
Now we must loop through closet section items each time we hang up an item.Each look at an item takes 2 seconds.
items section items seconds 4 4 (4*10 + 4*30 + 4*4*2) 192 8 8 (8*10 + 8*30 + 8*8*2) 448 1024 1024 (1024*10 + 1024*30 + 1024*1024*2) 2138112If N = # items of clothing and # items in section, it takes (N*10 + N*30 + N*N*2) => O(N^2)
How much space does our algorithm require?
What are our factors that determine space?
Assume: piles of clothing can be infinitely high.
N = number of pieces of clothing,M = number of piles.
Worst case: M = NBest case: M = 1Average: N/2
...depending on your particular constraints.
What's the time requirement of your algorithm?
What's the space requirement of your algorithm?
Many items operate on sequences (lists/arrays)
What sort of algorithms do you do on a deck of cards?[4, Q, 5, A, J, 10, K]
Sort, search, shuffle, etc.
Rearrange items in a sequence by some property about each item.(like numerical or alphabetical order)
[4, Q, 5, A, J, 10, K]
-> [A, 4, 5, 10, J, Q, K]
A simple but not very efficient sorting algorithm.
From here.
For I = 0 to N - 2 For J = 0 to N - 2 If (A(J) > A(J + 1) Temp = A(J) A(J) = A(J + 1) A(J + 1) = Temp End-If End-For End-For
What factors determine time?
N = number of items in sequence.
What is the best case? What is the worst case? Already sorted array Reverse sorted array N comparisons. N*N comparisons. O(N) O(N2)Only requires 1 extra temporary variable.
All differ in performance, and performance often depends on sequence characteristics.
Wikipedia: Comparison of algorithm performance, Sorting Algorithms Visualization
(Who says programmers don't know how to let loose?)
Find an item with a particular value in a sorted sequence.
Find 'J' in sequence:[4, 5, J, 10, Q, K, A]
J is the 3rd element (index 2).
Find an item by repeatedly halving the search space.
To find index of element e with certain value:
BinarySearch(array, value) { low = 0 high = array.length - 1 while (low <= high) { mid = (low + high) / 2 if (A[mid] > value) high = mid - 1 else if (A[mid] < value) low = mid + 1 else return mid } return not_found }
What factors determine time?
N = number of items in sequence.
Since algorithm splits array in half every time, at most log2N steps are performed.
Performance varies depending on sequence characteristics (distribution)
Much data is actually connected node and vertices, which makes it much harder to find algorithms that compute solutions quickly.
A lot of research goes into solving graph problems.
Find a route in a graph.
Find a particular node in a connected graph.
procedure depth_first_search(graph, vertex): label vertex as discovered let S be a stack S.push(v) while S is not empty t ← S.top if there is an edge u adjacent to t that is undiscovered and unexplored S.push(u) else mark t as explored S.pop()
Time: If b is the "branching factor" and d is the deepest depth: O(bd)
Space: O(bd)
Many of these use "heuristics" to make a better guess as to what path to go down, to avoid searching the whole space.
A special variant of the graph search problem.
Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city exactly once and returns to the origin city?Considered "NP-hard" (all solutions are exponential)
Many different approaches, like "Ant Colony":
☞ Demo: Shape-Based Hit Detection, ☞ Box2d JS, ☞ Tutorial: Collision Detection with Circles, ☞ Wikipedia: Collision Detection
☞ Demo: Map symbol clustering: k-means vs noverlap, ☞ Wikipedia: K-means clustering
☞ Image Processing, ☞ fileformat: Run-length Encoding, Wikipedia: Run-length Encoding
☞ Demo: Doggy Video, ☞ Demo: Canvas Pixel Manipulation, ☞ Demo: Tron Video, Wikipedia: Edge detection