Monday, August 11, 2014

How to implement a Node for use in a graph

Today I'd like to discuss a very basic construct called Node which is the basic building block of graphs, as well as a few other more advanced data structures. In future posts I'll discuss in more depth what data structures it is used in as well as how and why one would use them. For today though, I'm just going to introduce the concept of a graph and what a node actually is, along with a little bit of code to demonstrate.

What is a Node?

A node is an individual object that can be connected to one or more other nodes. If you are familiar with social networking such as facebook, then your profile is a node within the network and each of your friends are their own nodes. You are connected to your friends and we call those connections edges between nodes in the network, and the network is often referred to as a graph. 

This is a fairly simple concept, so let's take a look at what a node might look like when put into code. For this example I've chosen javascript, however you should be able to do this in just about any language.

First off, we need a node object so that it can encapsulate and store it's value as well as any connections to other nodes. To do this we'll give the node it's own node value and we'll also create a list to store any connections that might exist for a given node. The code for this looks like the following.

function Node(node){
    this.node = node;
    this.connections = [];
}

Now that we have a basic node object set up, we need some functions to help access the values. In Javascript adding in the functions isn't really necessary since all values of the object can be accessed via dot notation. For the sake of a little clarity as well as anyone looking to do this in other languages that may be a little more strict, we'll add methods to the Node class that will return the instance variable values. 

var this.getNode = function(){
    return this.node;
}

var this.getConnections = function(){
    return this.connections;
}

This is all great, but how do we create connections? To do that we simply need to add a function to the class that will take a node as argument and push it onto the connections array. But since we will be adding connections, we should probably also enable removing connections. So we'll implement both of these like so:

var this.addConnection = function(node){
    this.connections.push(node);
}

var this.removeConnection = function(node){
    var idx = connections.indexOf(node);
    connections.splice(idx, 1);
}

That's all there is to it. So now our Node class in it's entirety should look like the following:

function Node(obj){
    this.node = obj;
    this.connections = [];

    var this.getNode = function(){
      return this.node;
    }

    var this.getConnections = function(){
      return this.connections;
    }

    var this.addConnection = function(node){
        this.connections.push(node);
    }

    var this.removeConnection = function(node){
      var idx = connections.indexOf(node);
      connections.splice(idx, 1);
    }
}

Now we have a fully functional node class that can have a connection to any number of nodes.

No comments:

Post a Comment