Thursday, 12 November 2015

Low-Level Programming_Post8

Low-Level Programming: Post 8 12/11/2015


November 12th 2015 is the date of the eighth lesson on Low-Level Programming.

Before the lesson officially began, we were informed of a new approach to have we would have our break (time off). Instead of having a full 15 minutes break after the lectural side of the lesson like we would normally do, instead, we had two 5 minute breaks and one 10 minute break. This was perhaps due to having nearly two hours of consistent teaching to take in, whereas in the new method, we had to take in around half an hour each, which I felt was better, as we could also look back at potential notes taken and take it in generally.

After becoming aware of the new break system, the lesson officially began. As per usual, we began with being shown the points that we were going to learn today and the components/features that come with them. The main points are as follows...
  • Big 'O' Notation: How we define and describe complexity of algorithms.
  • Design Patterns: What are they & Examples.
  • Auto: The Auto keyword and how it works and when to use it.
  • Windows.h: Do not use (in assignments) and how we can sleep a thread instead, poll for input and clear the screen.
Big 'O' Notation ('O' stands for Order)
  • Complexity: Refers to how the number of inputs impact the processing time.
  • Algorithms: Focuses on Algorithms.
  • Models: There are mathematical models and computing science, plus various others.
  • Symbolism: Big O Notation is used to describe the asymptotic behaviour of functions i.e. how fast a function grows or shrinks.
  • Letter O: The letter O is used because the rate of growth of a function is called it's order.
  • Orders: When we examine our algorithms we can describe the growth rate of our function in "order of".
  • T(n) = 0(n^2): In this instance our function T grows at the order of n^2 i.e. the growth rate for each input for this function is exponential.
  • Computing Science: When we talk about Big O in computing science, we're referring to how complexity scales depending on the number of inputs.
  • Arrays: For example searching through an array has a complexity that's linear, as we need to potentially check all items stored in it; O(n).
Orders of Magnitude
  • Magnitude: In asymptotic analysis, we focus on the order of magnitude of a algorithm rather than the actual values themselves.
  • Abstract: Conceptually the amount of time of processing an algorithm takes is unknown, but orders allow us to show how it scales.
  • Big O: Big O notation is the way we express orders of magnitude when discussing the complexity of an algorithms for n items.
  • Traditional Usage: When two values are within an order of  magnitude of 1, they're within a factor of 10 i.e. 10 and 20 but not 10 and 200.
  • Notation: In Big O, the n defines the order, the higher the order of n, the more aggressively the complexity of the algorithm scales.
  • Algorithms: Often, you'll see references to algorithms when discussing a function's complexity.
Algorithms
  • Meaning: It's a set of rules that govern a series of calculation of problem-solving solutions. Sometimes it's abstractly considered a recipe.
  • Computing: Simply, we can think of each line, operation or function in our program as an algorithm. Rules to tell a computer what to do.
  • Unambiguous: Algorithms are unambiguous, each step does not lend itself to interpretation i.e. Add X to Y and return the result.
Constant Expression
  • Constant Expression: How do we represent a function or algorithm that regardless of the number of inputs provides us with a constant level of complexity?
  • Element Access: Accessing an element directly in a vector has constant complexity; it's irrespective of the number of elements contained in it.
  • Time Taken: The time taken to retrieve the element has no relationship with the number of items in the array, hence, it's constant Big O notation.
Linear
  • Linear Expression: How do we represent a function or algorithm that's complexity increases in line with the number of items being processed.
  • Linear Searching: Searching through a vector is an example of linear complexity. For every item added, we need to then potentially loop through it.
  • Time Taken: The time taken to retrieve the element has no relationship with the number of items in the array, hence, it's constant Big O notation.
Polynomial
  • Polynomial: How do we represent a function or algorithm that's complexity is directly proportional to the square of the size of the input set.
  • Selection Sort: A very simple and easy way to implement algorithms that sort numbers by their lowest value has an exponential complexity.
  • Complexity Growth: The deeper nested the algorithm, the higher the polynomial degree i.e. n^2, n^3, n^4 etc.
Exponential Big O
  • Exponential: An algorithm whose growth doubles with each addition to the input set. The growth curve is shallow and then rises rapidly.
  • Recursion: Recursive functions that need to traverse through a series of data inputs, cause a doubling of processing for each additional input.
Logarithmic
  • Logarithmic: The opposite of exponents. This time, the larger the input set, the smaller increase in the algorithms processing time.
  • Associative Containers: Last week, we spoke about how sets and maps use forms of binary searches to locate the item in the container.
  • Complexity Growth: Looking at the graph we can clearly see that working with a bigger data set has significantly less impact than other algorithms.
STL Search Algorithms
  • Linear: Standard search used when traversing containers using find.
  • Binary Tree: How does Binary Tree searching differ?
  • Red Black Tree: Associative Containers.
Linear Search
  • Find: One of the most used algorithms in the STL is std::find or std::find_if, both of which are linear in complexity.
  • Distance: It is can be thought of as up to linear complexity for the distance of the container being searched (first to last).
  • Best Case: In the best case scenario, the first element we check is the one we want, which would imply a constant complexity, but we Big O deals with the largest case.
Binary Tree Search
  • Associative Containers: The containers such as set and map provide an alternative search algorithm based on binary tree searches. The key in the container is stored.
  • Logarithmic: When we look at the complexity, we find it's logarithmic in nature.
  • Linked Lists: They work similar to linked lists but provide the efficiency of search in an ordered array. Each node points to the nodes left and right.
Verb Tree
  • Finding a Verb: To find the verb 'up', we traverse the tree, simplifying the search domain until we converge on our solution.
Red Black Tree Search
  • Balanced Binary Tree: Red Black Trees are a form of self balancing binary search trees designed to help the tree maintain it's logarithmic nature.
  • Node Colours: Each node in the tree is coloured, either red or black, whilst preserving the mantra, that the deepest node is not longer than twice the shortest.
  • Leaf Nodes: All nil nodes at the end of the tree will be coloured black and both children of a red should always be black.
  • Height: Every path from a node n to a descendent leaf has the same number of black nodes. The black height is denoted as bh(n).
  • T(n) = 0(log(n)): These properties or rules guarantee that the tree has a height of O(log(n)), since the order is a function of the tree's height.
  • Root Node: The root of the tree is always black, with no adjacent red nodes in the tree allowed.
  • Black Heights: With these rules in place, the root is at least h/2(each red node requires two black nodes). A subtree node has at least 2^bh(v) - 1 nodes.
Design Patterns
  • Software Architecture: How we design our systems and engines.
  • Patterns: What are Patterns?
  • State Pattern: What's the State Pattern?
Software Architecture
  • Good Design: Good design can encompass many ideals and concepts, but normally illustrates itself when adding new functionality is seamless.
  • OOP: Fundamentally, this is why OOP design continues to be the standard when building your codebase.
  • Coupling: Remember, if you can't understand one part of a program without understanding another, you have coupling.
  • Performance: All of our design decisions to make code more manageable aren't free.
  • Balance: We want a nicely designed codebase which is easy to understand, debug and extend over it's life time, but also quick.
  • Design Patterns: Design patterns are all about helping us to provide sensible interface and solutions to meet our desirable software architecture.
State Pattern
  • State Machines: As discussed last week, objects in the game world including the game itself can be broken down to it's constituent states.
  • Usage: When managing the game state for your object is becoming convoluted, now's a good time to use the state pattern.
Multiple States
  • Concurrent States: You may find that an object in your game, has multiple states unrelated to each other, or require loose coupling.
  • Hierarchical States: It's inevitable that after implementing a number of states that there will be common behaviours between some of them.
Pushdown Automata
  • Stack: It sometimes makes sense to record a series of states that have happened in order, for this we can emulate the stack principle.
  • Convenience: When transitioning between the character's state, it makes sense that we might want to revert to the last state before the current.
  • Multiple States: Store a current and a stack of previous states.
States
  • Limited: States are considered fairly limited. For more complex AI solutions, we have behaviour trees, neural nets and pathfinding algorithms.
  • Behaviour Changes: If the object being modelled behaviour changes with respective to some internal state, then states are still valid.
  • Distinct Options: The object can be easily categorized into one of several small distinct states.
  • Input Response: The entity responds to a series of inputs and/or events over time. i.e. input handling, menu screens, pausing text etc.
Factory Patterns
  • Constructor of Objects: A factory is exactly that; it produces or in this case constructs object's of our choosing. Very common in games.
  • Workings: Very simply, you ask the factory for an object of type X and it constructs it and returns a reference of the newly constructed object.
  • Similar Classes: Often our factory will look after the construction of a multiple of classes, that share the same base class.
  • Parameters: In order to construct the objects, the factory either needs to have sane construction defaults or allow the user to specify them.
  • Storage: Whilst the factory is busy constructing objects, it can use specific pools and even allocate them to a central storage location.
  • Return Type: Depending on the type of storage used and whom owns responsibility for the destruction of the object, you may get the actual object or a reference to it instead.
In one of the main points, I mentioned that we were told NOT to use the Windows.h file in our assignments. I will now display a guidance as to why we shouldn't use it and similar methods...
  • It bloats your codebase and pollutes namespaces ,defines etc (Lean and Mean config is available).
  • Using the functionality included will tie your program into a single platform, you lose the benefit or portability.
  • Stay away from the use of system commands (system ("pause") etc).
We were, however, given other features and methods that we could use in our assignments to help us.

C++11: Sleeping a Thread!
  • Sleep Function: Instead of using the system function provided by windows to sleep, let's utilise the new standards version.
  • Thread: We in effect stall the thread for a given amount of time. Once the time has elapsed, the thread resumes.
  • Literals: C++14 even allows us to specify literals to determine the sleep unit.
Storing Inputs
  • Windows.h: Windows provides some convenient ways of polling for input, but how can we implement our own 'portable' alternatives.
  • Polling: Instead of running a function that handles the check and the actions, we could store the inputs status in an 'input' class or similar.
Cleaning the Console
  • System: We could use system commands to do this for us, but i've made a point of disallowing them (slow and non-portable).
  • Alternative: Quite simply, why not just print a series of new lines to the screen, not the most graceful solution, but effective.
  • Platform Independent Code: Or we could attempt to write two different versions of console clearing modes depending on the platform we're running on.
C++11: Auto
  • Type Inference: C++11 introduces the Auto keyword. This keyword lets the compiler auto-deduce the type.
  • Shorthand: Provides us with the ability to declare variables efficiently and more quickly. i.e. iterators are longhanded, auto resolves this.
  • Usability: Only use auto when the type inference is obvious and it doesn't obfuscate the codebase.

No comments:

Post a Comment