More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
post
page
Python IDE Dashboard

Machine Learning – Top Trumps Game

The purpose of this post is to demonstrate a basic example of how machine learning works.

In this example, the computer learns how to play a game of Top Trumps more effectively to increase its chance of winning the game.

It does so by running a learning sequence. During this learning sequence its machine learning algorithm pick two random cards from the deck and compare them against a random criteria.

Based on which card wins the round, it updates its knowledge base accordingly. By repeating this process many times (number of iterations), the algorithm records in its knowledge base statistics for each category of each card. (Probability of winning the round)

The algorithm hence progressively learns which criteria has the highest probability of winning and records this information for each card of the deck.

These probabilities become more accurate when you increase the number of iterations of the learning sequence.

Use the buttons provided in the code pen below to show the impact of the learning phase on the knowledge base.

Machine Learning?


Let’s consider the following definition of Machine Learning:

“A computer program is said to learn from experience E with respect to some task T and some performance measure P, if its performance on T, as measured by P, improves with experience E.” — Tom Mitchell, Carnegie Mellon University

Let’s apply this defintion to our Top Trumps Machine Learning algorithm:

  • E: (The Experience/learning sequence): The experience consists of randomly picking two cards and comparing them against a criteria to see which card would win the round.
  • T: (The Task): Playing a full game of Top Trumps against an end-user.
  • P: (The Performance Measure): The probability of winning the game.

In our example, the probability (P) of winning a game of Top Trumps (T) increases with the number of iterations of the learning sequence (Experience E).

So we effectively have a machine learning algorithm.

Obviously, with a fixed data sets of 9 cards and 4 criteria per card, the learning is limited. More complex scenarios use a similar approach with larger data sets which are often open/limitless and where the computer can constantly learn and improve the accuracy of its knowledge base by either analysing existing data sets or populating new data by interacting with real end-users/human beings.

Next Step?


To complete this algorithm, the next step would be to implement the playing phase, where the computer should use its knowledge base to play against an end-user.

Heuristic Approaches to Problem Solving

“A heuristic technique, often called simply a heuristic, is any approach to problem solving, learning, or discovery that employs a practical method not guaranteed to be optimal or perfect, but sufficient for the immediate goals. Where finding an optimal solution is impossible or impractical, heuristic methods can be used to speed up the process of finding a satisfactory solution. Heuristics can be mental shortcuts that ease the cognitive load of making a decision. Examples of this method include using a rule of thumb, an educated guess, an intuitive judgement, guesstimate, stereotyping, profiling, or common sense.” (Source: Wikipedia)

“In computer science, a heuristic is a technique designed for solving a problem more quickly when classic methods are too slow, or for finding an approximate solution when classic methods fail to find any exact solution. This is achieved by trading optimality, completeness, accuracy, or precision for speed. In a way, it can be considered a shortcut.” (Source: Wikipedia)

The objective of a heuristic algorithm is to apply a rule of thumb approach to produce a solution in a reasonable time frame that is good enough for solving the problem at hand. There is no guarantee that the solution found will be the most accurate or optimal solution for the given problem. We often refer the solution as “good enough” in most cases.

Heuristic Algorithms?


Heuristic Algorithms can be found in:

Artificial Intelligence
E.g. When a computer algorithm plays a game of Chess (e.g. Deep Blue) or a game of Go (e.g. AlphaGo), the computer cannot investigate every single move that can be played. Instead it will apply a few rules of thumb to quickly discard some moves while focusing on key moves that are more likely to lead to a victory.
Language recognition
When analysing the meaning of a user input, for instance when a user asks a question on a search engine or interacts with a Google Home or Amazon Echo device, an algorithm tries to make sense of the user inputs. Word associations, analysis of context, previous searches history and current trends/search engine statistics can be used in a heuristic algorithm to speed up the search process.
Big Data Analysis
When there are large amounts of data to analyse. This is the case for search engines to return search results very efficiently, profiling algorithms which can be used by a marketing department to identify members of a target audience and their behaviours, data analysis in scientific research (e.g. medicine to identify cause/effect correlations between large data sets).
Shortest Path Algorithms
used by GPS systems and self-driving cars also use a heuristic approach to decide on the best route to go from A to Z (e.g. A* Search Algorithm). More advanced algorithms can also take into consideration a range of factors including the type of roads, the speed limits, live traffic data, etc. which can result in extremely complex algorithms.
Machine Learning
Artificial Intelligence algorithms based on machine learning where the computer builds up a knowledge base from previous experiences is another application of heuristic algorithms. In this case the algorithm uses a self-maintained knowledge base to inform decisions and make “educated guesses” based on previous experiences.

Let’s investigate a few basic examples where a heuristic algorithm can be used:

Noughts & CrossesGame of ChessTop TrumpsLanguage RecognitionShortest Path Algorithms
To help the computer make a decision as to where to place a token on a 3×3 noughts and crosses grid, a basic heuristic algorithm should be based on the rule of thumb that some cells of the grid are more likely to lead to a win:
heuristic-noughts-and-crosses

Based on this approach, can you think of how a similar approach could be used for an algorithm to play:

  • Othello (a.k.a. Reversi Game)
  • A Battleship game?
  • Rock/Paper/Scissors?
When playing a game of chess, expert players can “see” several moves ahead. It would be tempting to design an algorithm that could investigate every single possible move that players can make and investigate the impacts of such moves on the outcome of the game. However such an algorithm would have to investigate far too many possible moves and would quickly become too slow and too demanding in terms of memory resources in order to perform effectively.

It is hence essential to use a heuristic approach to quickly discard some moves which would most likely lead to a defeat while focusing on moves that would seem to be a good step towards a win!

heuristic-chess-move

Let’s consider the above scenario when investigating all the possible moves for this white pawn. Can the computer make a quick decision as to what would most likely be the best option?

Heuristic approaches do not always give you the best solution. Can you explain how this could be the case with this scenario?

Sometimes your algorithm need to collect or analyse data before applying heuristic to make a decision. This can be done by providing the computer with some data or by implementing an algorithm based on machine learning.

For instance in a game of Top Trumps, if your algorithm knows the average value of each card for each category, it will be able to select the criteria which is most likely going to win the round.

Alternatively, a machine learning algorithm could play the game and record and update statistics after playing each card to progressively learn which criteria is more likely to win the round for each card in the deck.
You can investigate how machine learning can be used in a game of Top Trumps by reading this blog post.

Heuristic methods can be used when developing algorithms which try to understand what the user is saying, or asking for. For instance, by looking for words associations, an algorithm can narrow down the meaning of words especially when a word can have two different meanings:

heuristic-raspberry

e.g. When using Google search a user types: “Raspeberry Pi Hardware” We can deduct that in this case Raspberry has nothing to do with the piece of fruit, so there is no need to give results on healthy eating, cooking recipes or grocery stores…

However if the user searches for “Raspeberry Pie ingredients”, we can deduct that the user is searching for a recipe and is less likely to be interested in programming blogs or computer hardware online shops.

Short Path Algorithms used by GPS systems and self-driving cars also use a heuristic approach to decide on the best route to go from A to Z. This is for instance the case for the A* Search algorithm which takes into consideration the distance as the crow flies between two nodes to decide which paths to explore first and hence more effectively find the shortest path between two nodes.

signs-distance

You can compare two different algorithms used to find the shortest route from two nodes of a graph:

A* Search Algorithm

The A* Search algorithm (pronounced “A star”) is an alternative to the Dijkstra’s Shortest Path algorithm. It is used to find the shortest path between two nodes of a weighted graph. The A* Search algorithm performs better than the Dijkstra’s algorithm because of its use of heuristics.

Before investigating this algorithm make sure you are familiar with the terminology used when describing Graphs in Computer Science.

Let’s decompose the A* Search algorithm step by step using the example provided below. (Use the tabs below to progress step by step).

Note that, in this graph, the heuristic we will use is the straight line distance (“as the crow flies”) between a node and the end node (Z). This distance will always be the shortest distance between two points, a distance that cannot be reduced whatever path you follow to reach node Z.

GraphStep 1234567
A-Star-Search-Algorithm
A-Star-Search-Algorithm-Step-1Start by setting the starting node (A) as the current node.
A-Star-Search-Algorithm-Step-2Check all the nodes connected to A and update their “Shortest Distance from A” and set their “previous node” to “A”.
Update their total distance by adding the shortest distance from A and the heuristic distance to Z.
A-Star-Search-Algorithm-Step-3Set the current node (A) to “visited” and use the unvisited node with the smallest total distance as the current node (e.g. in this case: Node C).
Check all unvisited nodes connected to the current node and add the distance from A to C to all distances from the connected nodes. Replace their values only if the new distance is lower than the previous one.

C -> D: 3 + 7 = 10 < ∞ – Change Node D
C -> E: 3 + 10 = 13 < ∞ – Change Node E

The next current node (unvisited node with the shortest total distance) could be either node B or node D. Let’s use node B.

A-Star-Search-Algorithm-Step-4
Check all unvisited nodes connected to the current node (B) and add the distance from A to B to all distances from the connected nodes. Replace their values only if the new distance is lower than the previous one.

B -> E: 4 + 12 = 16 > 13 – Do not change Node E
B -> F: 4 + 5 = 9 < ∞ – Change Node F

The next current node (unvisited node with the shortest total distance) is D.

A-Star-Search-Algorithm-Step-5
Check all unvisited nodes connected to the current node (D) and add the distance from A to D to all distances from the connected nodes. Replace their values only if the new distance is lower than the previous one.

D -> E: 10 + 2 = 12 < 13 – Change Node E

The next current node (unvisited node with the shortest total distance) is E.

A-Star-Search-Algorithm-Step-6Check all unvisited nodes connected to the current node (E) and add the distance from A to E to all distances from the connected nodes. Replace their values only if the new distance is lower than the previous one.

E -> Z: 12 + 5 = 17 < ∞ – Change Node Z

A-Star-Search-Algorithm-Step-7We found a path from A to Z, but is it the shortest one?

Check all unvisited nodes. In this example, there is only one unvisited node (F). However its total distance (20) is already greater than the distance we have from A to Z (17) so there is no need to visit node F as it will not lead to a shorter path.

We found the shortest path from A to Z.
Read the path from Z to A using the previous node column:
Z > E > D > C > A
So the Shortest Path is:
A – C – D – E – Z with a length of 17

Your Task



Graph #1Graph #2
Apply the steps of the A* Search algorithm to find the shortest path from A to Z using the following graph:
A-Star-Search-Algorithm-Graph

Node Status Shortest Distance from A Heurisitic Distance to Z Total Distance Previous Node
A 21
B 14
C 18
D 18
E 5
F 8
Z 0

Shortest Path?

Length?

Apply the steps of the A* Search algorithm to find the shortest path from A to Z using the following graph:
Graph-A-Star-Algorithm

Node Status Shortest Distance from A Heurisitic Distance to Z Total Distance Previous Node
A 11
B 8
C 8
D 4
E 2
Z 0

Shortest Path?

Length?

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Dijkstra’s Shortest Path Algorithm

Dijkstra’s Shortest Path Algorithm is an algorithm used to find the shortest path between two nodes of a weighted graph.

Before investigating this algorithm make sure you are familiar with the terminology used when describing Graphs in Computer Science.

Let’s decompose the Dijkstra’s Shortest Path Algorithm step by step using the following example: (Use the tabs below to progress step by step).

GraphStep 123456789
Dijkstra-Algorithm
Dijkstra-Algorithm-Step-1Start by setting the starting node (A) as the current node.
Dijkstra-Algorithm-Step-2Check all the nodes connected to A and update their “Distance from A” and set their “previous node” to “A”.
Dijkstra-Algorithm-Step-3Set the current node (A) to “visited” and use the closest unvisited node to A as the current node (e.g. in this case: Node C).
Dijkstra-Algorithm-Step-4Check all unvisited nodes connected to the current node and add the distance from A to C to all distances from the connected nodes. Replace their values only if the new distance is lower than the previous one.

C -> B: 2 + 1 = 3 < 4 – Change Node B
C -> D: 2 + 8 = 10 < ∞ – Change Node D
C -> E: 2 + 10 = 12 < ∞ – Change Node E

Dijkstra-Algorithm-Step-5Set the current node C status to Visited.
We then repeat the same process always picking the closest unvisited node to A as the current node.
In this case node B becomes the current node.
Dijkstra-Algorithm-Step-6B -> D 3+5 = 8 < 10 – Change Node D

Next “Current Node” will be D as it has the shortest distance from A amongst all unvisited nodes.

Dijkstra-Algorithm-Step-7D -> E 8+2 = 10 < 12 – Change Node E
D -> Z 8+6 = 14 < ∞ – Change Node Z

We found a path from A to Z but it may not be the shortest one yet. So we need to carry on the process.

Next “Current Node”: E

Dijkstra-Algorithm-Step-8E -> Z 10+5 = 15 > 14 – We do not change node Z.
Dijkstra-Algorithm-Step-9We found the shortest path from A to Z.
Read the path from Z to A using the previous node column:
Z > D > B > C > A
So the Shortest Path is:
A – C – B – D – Z with a length of 14

Your Task



Graph #1Graph #2
Apply the steps of the Dijkstra’s Algorithm to find the shortest path from A to Z using the following graph:
Dijkstra-Algorithm-Graph

Node Status Shortest Distance from A Previous Node
A
B
C
D
E
F
Z

Shortest Path?

Length?

Apply the steps of the Dijkstra’s Algorithm to find the shortest path from A to Z using the following graph:
Graph-Dijkstra-Algorithm

Node Status Shortest Distance from A Previous Node
A
B
C
D
E
F
Z

Shortest Path?

Length?

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area
Tagged with: ,

Boolean Algebra


In this blog post we are investigating different formulas than can be used to simplify a Boolean expression.

Double Negation

¬ ¬A = A

Complement Laws

A ∧ ¬A = 0
A ∨ ¬A = 1

Idempotent Laws

A ∧ A = A
A ∨ A = A

Identity Laws

A ∧ 1 = A
A ∧ 0 = 0
A ∨ 1 = 1
A ∨ 0 = A

Associative Laws

(A ∧ B) ∧ C = A ∧ (B ∧ C)
(A ∨ B) ∨ C = A ∨ (B ∨ C)

Commutative Laws

A ∧ B = B ∧ A
A ∨ B = B ∨ A

Distributive Laws

A ∧ (B ∨ C) = (A ∧ B) ∨ (A ∧ C)
A ∨ (B ∧ C) = (A ∨ B) ∧ (A ∨ C)

Absorptive Laws

A ∧ (A ∨ B) = A
A ∨ (A ∧ B) = A

De Morgan’s Rules

¬(A ∨ B) = ¬A ∧ ¬B
¬(A ∧ B) = ¬A ∨ ¬B

Boolean Algebra Practice


Use the formulas listed above to simplify the following Boolean expressions:

#1#2#3#4#5#6
Boolean Expression
A ∨ ¬(A ∧ B)

Simplified Boolean Expresssion:



View Solution
Boolean Expression
(A ∧ B) ∨ (A ∧ C)

Simplified Boolean Expresssion:



View Solution
Boolean Expression
(A ∧ B) ∨ A ∧ (B ∨ C)

Simplified Boolean Expresssion:



View Solution
Boolean Expression
¬A ∨ C ∨ (A ∧ B)

Simplified Boolean Expresssion:



View Solution
Boolean Expression
¬(¬A ∧ (B ∧ C))

Simplified Boolean Expresssion:



View Solution
Boolean Expression
¬(A ∧ ¬B) ∨ (¬A ∧ B)

Simplified Boolean Expresssion:



View Solution

Big O Notation

The question we will try to answer in this blog post is as follows: How can we measure the effectiveness/performance of an algorithm?

First let’s consider this quote from Bill Gates (Founder of Microsoft):

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”

So, according to Bill Gates the length of a program (in lines of code) is not a criteria to consider when evaluating its effectiveness to solve a problem or its performance.

  • A long program does not necessarly mean that the program has been coded the most effectively.
  • And vice-versa, a shorter program does not necessarly perform better than a longer piece of code.

Big O Notation

The Big O notation is used in Computer Science to describe the performance (e.g. execution time or space used) of an algorithm.

The Big O notation can be used to compare the performance of different search algorithms (e.g. linear search vs. binary search), sorting algorithms (insertion sort, bubble sort, merge sort etc.), backtracking and heuristic algorithms, etc. It is especially useful to compare algorithms which will require a large number of steps and/or manipulate a large volume of data (e.g. Big Data algorithms).

Best Case, Average Case or Worst Case Scenario?

The Big O notation can be used to describe either the best case, average case or worst-case scenario of an algorithm. For instance, let’s consider a linear search (e.g. finding a user by its username in a list of 100 users). In the best case scenario, the username being searched would be the first username of the list. In this case the algorithm would complete the search very effectively, in just one iteration. However, the worst case scenario would be that the username being searched is the last of the list. In this case the algorithm would require 100 iterations to find it.

Considering that the average or worst case scenario, we can deduct that a linear search amongst N records could take up to N iterations. There is a linear correlation between the number of records in the data set being searched and the number of iterations of the average case and worst case scenarios.

In this case a linear search would have the following time complexity big O notation:

  • Best Case Scenario:Constant Notation: O(1)
  • Average Case Scenario: Linear Notation: O(N)
  • Worst Case Scenario: Linear Notation: O(N)

So let’s review the different types of algorithm that can be classified using the Big O Notation:

O(1)O(N)O(N2)O(2N)O(log(N))
Big-O-Notation-Constant-Algorithm
Constant Notation: O(1)

The constant notation describes an algorithm that will always execute in the same execution time regardless of the size of the data set.

For instance, an algorithm to retrieve the first value of a data set, will always be completed in one step, regardless of the number of values in the data set.

FUNCTION getFirstElemnt(list)
    RETURN list[0]
END FUNCTION

A hashing algorithm is an O(1) algorithm that can be used to very effectively locate/search a value/key when the data is stored using a hash table. It’s a more effective way than using a linear search O(N) or binary search O(Log(N)) algorithm. (See example blog post on hashing algorithm for memory addressing)

Big-O-Notation-Linear-Algorithm
Linear Notation: O(N)

A linear algorithm is used when the execution time of an algorithm grows in direct proportion to the size of the data set it is processing.

Algorithms, such as the linear search, which are based on a single loop to iterate through each value of the data set are more likely to have a linear notation O(N) though this is not always the case (e.g. binary search).

FUNCTION linearSearch(list, value)
    FOR EACH element IN list
        IF (element == value) 
			RETURN true
		END IF	
	NEXT
    RETURN false
END FUNCTION
Big-O-Notation-Polynomial-Algorithm
Polynomial Notation: O(N2), O(N3), etc.

Polynomial algorithms include quadratic algorithms O(N2), cubic algorithms O(N3) and so on:

  • O(N2) represents an algorithm whose performance is directly proportional to the square of the size of the data set.
  • O(N3) represents an algorithm whose performance is directly proportional to the cube of the size of the data set.
  • etc.

Algorithms which are based on nested loops are more likely to have a polynomial O(N2), or O(N3), etc. depending on the level of nesting.

PROCEDURE displayTimesTable()
	FOR i FROM 1 TO 10
		FOR j FROM 1 TO 10
			product = i*j 
			OUTPUT i + " times " + j + " equals " + product
		NEXT j
	NEXT i
END PROCEDURE

Typically, O(N2) algorithms can be found when manipulating 2-dimensional arrays, O(N3) algorithms can be found when manipulating 3-dimensional arrays and so on.

PROCEDURE emptyChessboardGrid()
	FOR row FROM 0 to 7
		FOR col FROM 0 to 7
			grid[row][col] = 0
		NEXT col
	NEXT row
END PROCEDURE

Most sorting algorithms such as Bubble Sort, Insertion Sort, Quick Sort algorithms are O(N2) algorithms.

Big-O-Notation-Exponential-Algorithm
Exponential Notation: O(2N)

The exponential notation O(2N) describes an algorithm whose growth doubles with each addition to the data set.

Backtracking algorithms which test every possible “pathway” to solve a problem can be based on this notation. Such algorithms become very slow as the data set increases.

Example of exponential algorithm: An algorithm to list all the possible binary permutations depending on the number of digits (bits).

Big-O-Notation-Logarithmic-Algorithm
Logarithmic Notation: O(log(N))

A logarithmic algorithm O(log(N)) is an algorithm whose growth decreases when the data set increase following a logarithmic curve. Logarithmic algorithms are hence quite efficient especially when processing large sets of data.

A binary search is a typical example of logarithmic algorithm. In a binary search, half of the data set is discarded after each iteration. Which means that an algorithm which searches through 2,000,000 values will just need one more iteration than if the data set only contained 1,000,000 values.

binary-search-algorithm
Use a logarithmic algorithm (based on a binary search) to play the game Guess the Number.

Graph Terminology

Graphs are a data structure that can be used in computer science in a variety of context.

You can check the following Python challenges which are all being solved using a graph and a short path algorithm, one of the most useful algorithms used when manipulating graphs.


Using a graph to represent a food web.

Using a graph to represent a food web.

Using a graph to store London tube map.

Using a graph to store London tube map.

Using a graph to represent friendship relationships in a social network.

Using a graph to represent friendship relationships in a social network.

Using a graph to plan a flight route between airports.

Using a graph to plan a flight route between airports.


Graph Terminology


A graph is a collection of nodes also called vertices which are connected between one another. Each connection between two vertices is called an edge (sometimes called a branch).

When designing a graph we can make decisions as to:

  • Use a directed graph or an undirected graph,
  • Use a weighted graph or an unweighted graph.
An undirected graph is when edges have no direction.

An undirected graph is when edges have no direction.

A directed graph is when vertices have a direction.

A directed graph is when edges have a direction.

A weighted graph is when edges have a numerical value.

A weighted graph is when edges have a numerical value.

A weighted and directed graph is where edges have a direction and a numerical value!

A weighted and directed graph is where edges have a direction and a numerical value!

The weight of an edge can have different meanings:

  • It could represent the distance (e.g. in miles) between two vertices,
  • It could represent the time needed to travel (e.g. in minutes) between two vertices,
  • etc…

The direction of an edge is not always needed.

For instance, in a social network like Facebook, there is no need to have directed edges to represent friendship, as if A if a friend of B, then B is also a friend of A. So all edges are both ways, hence an undirected graph is suitable to represent friendship relationships in Facebook.

Twitter however would use a directed graph, as if A follows B, it is not necessary the case that B is following A. With Twitter the edges represent the “Follow” relationship and are directed edges.

Adjacency Matrix

An adjacency matrix is sometimes used to represent a graph. It is based on a 2D-array to represent all the vertices and edges of a graph.

Graph #1Graph #2Graph #3Graph #4Graph #5Graph #6
Graph #1

graph_4_2
Adjacency Matrix #1

A B C D E
A
B
C
D
E
Graph #2

graph_1_3
Adjacency Matrix #2

A B C D E
A 4 5
B 4 7
C 7 3 6
D
E
Graph #3

graph_3_2
Adjacency Matrix #3

Complete the following adjacency matrix:

A B C D E F
A
B
C
D
E
F
Graph #4

graph_3_3
Adjacency Matrix #4

Complete the following adjacency matrix.

A B C D E F
A
B
C
D
E
F
Graph #5

graph_4_1
Adjacency Matrix #5

Complete the following adjacency matrix.

A B C D E
A
B
C
D
E
Graph #6

graph_2_3
Adjacency Matrix #6

Complete the following adjacency matrix.

A B C D E
A
B
C
D
E

Extension Task:


European-Airports-Graph

Adjacency Matrix

Complete the following adjacency matrix:

  Amsterdam Athens Berlin Bucharest Budapest Dublin Geneva Lisbon London Madrid Oslo Paris Reykjavik Rome
Amsterdam
Athens
Berlin
Bucharest
Budapest
Dublin
Geneva
Lisbon
London
Madrid
Oslo
Paris
Reykjavik
Rome
Tagged with:

Network Security – Terminology

Click on the picture below to check your knowledge of key Network Security concepts: Forms of attacks, threats and approaches to secure a network.

network-security-dominoes

Network Security TerminologyOpen Domino Activity
 
Tagged with:

Random Access Memory using Logic Gates

RAMIn our previous blog post, “Binary Additions using Logic Gates”, we investigated how logic gates can be used together to create a circuit used in the ALU (Arithmetic & Logic Unit of the CPU) to add two binary numbers together.

In this blog post we will investigate how logic gates are used to create the RAM (primary memory), in other words how logic gates can be used to store volatile information.

Random Access Memory


Random Access Memory (RAM) is volatile memory that sits next to the CPU. (Volatile means that it is wiped out when the computer is switched off). It is used to store instructions and data currently used by the CPU.

RAM consists of billions of Data Cells, each data cell being able to store one bit of information. For instance a 2GB RAM can store 2,000,000,000 Bytes of information = 16,000,000,000 bits of information and hence consists of 16,000,000,000 data cells.

D-Type Flip-Flop Circuits


Each data cell consists of a D-Type Flip-Flop circuit that is built using four NAND logic gates connected as follows:
D-Type-Flip-Flop-Logic-Gates

We represent a D-Type Flip-Flop Circuit as follows. You can change the input values D and E by clicking on the corresponding buttons below to see the impact on the outputs Q and Q.




D-Type-Flip-Flop-Circuit




You can also test the behaviour of a D-Type flip-flop circuit using our online simulator:
Click on the above circuit to open in a new window.

A D-Type Flip-Flop Circuit is used to store 1 bit of information. It has two input pins (Called D (Data) and E (Enabler) and two output pins (Q and Q = NOT Q).

The truth table of a D-Type Flip-Flop circuit is as follows:
D-Type-Flip-Flop-Truth-Table

When the enabler input E is set to 1, the output Q can be set to the Data input D.
When the enabler input E is set to 0, the output Q cannot be changed. It remains as its previous value. In other word it retains its value. This is why this circuit is used to create memory cells (e.g in the RAM).

Random Access Memory (RAM) consists of billions of data cells, each data-cell uses a D-Type flip-flop circuit.

Random Access Memory (RAM) consists of billions of data cells, each data-cell uses a D-Type flip-flop circuit.

Clock Signal and Delaying Effect


The enabler input E is often connected to another circuit called the clock (e.g. CPU clock). The clock signal constantly and regularly alternate between two states: 0 and 1, similar to a heart beat. Inside the CPU the clock signal controls the execution of the FDE cycle.

The Clock Signal is similar to a heart beat

The Clock Signal is similar to a heart beat.

When the clock signal is applied to the Enabler input (in this case also called the clock input), the flip-flop output Q can only change values when triggered by the clock signal. The value of the flip-flop is held or delayed until the next clock signal. This delaying effect is also called a latch. This is why we call this circuit a D-Type flip flop where D stands for Delay.

In other words, the change of input (D) is not applied immediately (to the output Q) but is applied at the next “tick of the clock”. There are many applications to this delay such as the ability to create a frequency divider. (divide the clock frequency by a multiple of 2).

Delaying effect when using a D-Type Flip-Flop circuit

Delaying effect when using a D-Type Flip-Flop circuit.

Tagged with: ,

Binary Additions using Logic Gates

In our previous blog post “from transistors to processors” we found out that the CPU consists of logic gates, which are made using transistors.

In this blog post we are looking at how these logic gates can be combined to create an integrated circuit used by the ALU (Arithmetic and Logic Unit of the CPU) to add two 8-bits binary numbers together.

First let’s recap on how a binary addition works:

Half-Adder Circuit


A half-adder circuit is used to add two bits of data together and is based on the following Truth Table.
half-adder-truth-table

A half-adder circuit consists of two logic gates as follows:
Half-Adder

You can test this circuit by clicking on the picture below:

Full-Adder Circuit


A full-adder circuit is used to add three bits of data together and is based on the following Truth Table.
full-adder-truth-table

A full-adder circuit consists of two half-adder circuits and an OR gate connected as follows:
Full-Adder

You can test this circuit by clicking on the picture below:

Full Binary Addition


By connecting an half-adder circuit with 7 full-adder circuits we can create a circuit to implement a full binary addition of two 8-bits binary numbers:
Binary-addition-truth-table

Full circuit:
Binary-addition-using-binary-adder-circuits

Tagged with: ,