Graphs

Graphs can be confused with trees, however there is a difference between the two as graphs do not have a root node but instead have links to other nodes without a structure. There are many way to represent a graph in a visual format with the use of graph diagrams.

Here is an example of a graph diagram, as you can see there is no root node but all nodes are connected by edges.

A way to actually implement a graph is through an adjacency matrix which can be seen here:

Here is a graph diagram and code for an adjacency matrix, the arrows within the diagram represent the edges within the matrix. The 1’s and 0’s within the code show what whether or a not a node has an edge to a another node or not. If a 1 is present then there is an edge, if it is a 0 then there is no edge. A benefit to using an adjacency matrix is that is quite easy to add and remove edges as you just need to access an index within a list, as you can see I have done this within my code. However, it is very inefficient to have an adjacency matrix as it stores ‘non-edges’ which takes up unnecessary memory.

Also to find a nodes neighbour takes more time than other data structures due to the fact that you have to loop through all of the matrix’s rows and columns to find the neighbour of a specific node.

Another data structure that is better at finding the neighbour of a node is an adjacency list as this structure stores the nodes neighbours within a dictionary and you just have to search for the key of the node you want to see the neighbours of and you will get the result straight away instead of looping through all of the rows and columns that you do within an adjacency matrix.

Collection Types

I have decided to write the function for returning the smallest positive integer that does not occur within an array.

I have used a list to store the array of numbers as this can store many different elements, I have a Boolean to loop the code until it has found a number that is not in the list, I have used this data type as the loop only needs to be one or the other which is what a Boolean is so that is why I have used it for the loop. Finally I have an integer called count to track what numbers the code has looped over as this is iterable and can count up in whole numbers.

The algorithm works by having an infinite loop until a number is not found within the array and then breaks out of the loop, the count variable adds 1 to itself when a number is found so that it can then check for the next number. For example, within my code snippet it will return the number 2 as it will enter the loop and then check if count which is 1 at this point is in any position within the numbers array as it is it will add 1 to count and loop again, this time when it enters the loop it will see that count now equals 2 and that is not in the numbers array so therefore will continue to the else block and print the current count and then break out of the loop.

Data Types & Memory

There are many data types that exist that let the computer know what data the programmer is using so that the program can compile and execute methods around that data type.

There are many different methods that only apply to certain data types, for
example, a string in Python has a .split() method which when used splits all of the string up into a list, if I were to have a variable t = “Hello, my
name is Billy, I am 19″ and then use the .split() method on it like so,
t.split(“,”), this will find all occurrences of a comma in the string
and split the whole string into a list. However, a data type such as float does
not have the .split() function so will give you an error if you were to try and
execute it.

There are 4 primitive data structures in Python: String, Integer, Float and Boolean which means these are the most basic types to be used within the language, there are however non-primitive data structures such as, arrays, lists, tuples, dictionaries etc.

Operators can be used on different data types if the data type has been casted to the correct one. For example, this code will not work since you cannot compare a string and an integer and produces a TypeError.

However if you change the code to cast the string to an integer, as you see here, it will work perfectly and display “Successful” in the console

Memory

When a computer compiles a program and executes it, it will store local variables on a stack within the memory and when the program enters a function that uses one of these variables it can find where the variable is allocated then when the program is out of the function the space is freed up within the memory.