Building blocks of code

Randy Taylor
4 min readDec 5, 2020

When you are learning software you will have to pick and choose what. There is just too much to pick it all. So much is really cool and fulfilling but some is more tedious and feels like work. However there are things you will have to know; the fundamentals are a requirement no matter what you pick. Whether its Machine Learning and Neural Networks or Web Development and REST APIs you will need to know the fundamentals for both.

Data Structures are fairly fundamental and knowledge of them is crucial. If you are new to code you are already working with data structures like arrays and object literals without even knowing it. As you dig deeper you will learn the costs and benefits of using different types. There is a reason arrays are not the be all and end all of data structures.

Arrays [] are very user friendly and come with out of the box functionality. If you want to only pull out specific elements from an array? Array.prototype.filter lets you do that. JavaScript is prototypal and thus it will look on your array and not find the filter method. It will then look to it’s parent Array prototype where the filter method lives. Want to add an element to the start of your array? Array.prototype.unshift will let you do this. However that will have unexpected costs. Lets think about why. What does that mean for your array in that last case? The first element is the newly unshifted element into the array, but now the second element was originally the first element. The second element was pushed into the second slot. This happens for every element in the array. Now every element has to move and it costs memory and time. It has BigO(n) time complexity, n meaning the number of elements in the array. If your array is small it is negligible but if your array is 1000s of elements it will cause time and memory issues. Lets thing about fixing this then. Remember fundamentals and if you needed to come up with a custom data structure to solve this what would you create?

Dan Abramov creator of Redux and boy genius(he was about 22 when he created Redux) used functions called reducers and that had switch statement logic which returned objects to create redux. That’s a pretty good hint on what to use; at the end of the day you have to use the fundamentals.

Lets create an object that will replace our Array to store information. This array will have to store data but should also be able to find the other other data we will need to store. This will mean we will need to add a line, pointer, or edge to that data. In mathematics a vertex is a point where multiple lines or edges converge to a single point. So we will use ES6 syntax to create a class Vertex. Classes in JavaScript are just syntactic sugar for Object factories. Our vertex will have a data property. We are the creators of our data structure so lets add some methods to add pointers to the data. To start out lets make our Vertex and adjacent property and a data property.

Vertex.js

Above is a standard implementation of a Node in a linked List. These Nodes are also in Trees and Graphs with modifications for children or adjacency respectively.

Graph in python

We could take that idea and make a graph. In Math the definition is abstractly vertices connected my edges. Anything could be a graph. A city could be a graph where buildings are the vertices and edges are the roads or a building could be a graph where the rooms are vertices and edges are the halls and elevators. Here we have our data stored in a Node class and the graph will be represented by the Graph class. The Adjacency Matrix property of the class will represent the graph. The Matrix will be a rows and columns where 1 will indicate adjacency and 0 will be non-adjacent. If two buildings can be visited by traveling a road then they have adjacency otherwise they are non-adjacent.

adjacency matrix

Here two building are on the same street. As you can see from their addresses the are adjacent as represented by the 1 in the matrix.

#row 1 column 1 is 0 because building1 cannot be adjacent to itself
[
[0,1],
[1,0]
]
#row 2 column 1 is a 1 because building2 is adjacent to building 1

These are just some of the ways to store data and connect them. There are many more ways to code this and represent data/store data/retrieve data.

--

--