Networks

Use overall knowledge of data structures to solve problems. You will model a caffeine molecule as a graph and present that computer model. Edges that are single bonds should be presented with a weight of 1, with a double bond - they should have a weight of 2. Build your model as a Graph. The graph should be able to print all of its Nodes with each adjacent node. You can have a print button or simply call it when the Graph is built. Output: H (C) <- there will be 9 of these C (N,H) <- there will be 3 of these N (C, C, C) <- there are 3 of these N (C, C) <- only one of these etc.... etc... for each element and combination of connected element.

by Neil Daley

HTML

<h1>
Caffeine Molecule Graph
</h1>

Molecular Formula: 
<b>C<sub>8</sub>H<sub>10</sub>N<sub>4</sub>O<sub>2</sub></b>
<br/><br/>

Click the button to print the stucture of atoms in the Caffeine molecule.<br/><br/>

<input type="button" onclick="createGraph();" value="Print Structure" style="color:white; background-color:blue" /><br/><br/>

<div id="nodes"></div>
<br/>

JavaScript

var Node = function(_source, _type) {
  this.number = _source;
  this.type = _type;
  this.edges = [];
  return this;
}

var Edge = function(_amount) {
  this.from = null;
  this.to = null;
  this.amount = null;
  return this;
}

var Graph = function() {
  this.nodes = [];
  this.edges = [];
  this.type = "";
  this.paths = [];

  this.addNode = function(_source, _type) {
    var node = new Node(_source, _type);
    this.nodes.push(node);
    return node;
  }

  this.addEdge = function(_from, _to, _amount) {
    var edge = new Edge(_amount);
    edge.from = _from;
    edge.to = _to;
    edge.amount = _amount;
    this.edges.push(edge);
    return edge;
  }

  this.printNodes = function() {
    var s = "<b>Atoms in a caffeine molecule</b> <br/><br/>";
    for (var i = 0; i < this.nodes.length; i++) {
      s = s + this.nodes[i].number + " => atom: " + this.nodes[i].type + " and adjacent(s): " + this.printAdjacent(i) + "</br>";
    }
    return s;
  }

  this.printAdjacent = function(i) {
    var n = "";
    for (var j = 0; j < this.edges.length; j++) {
    	if (this.edges[j].from.number === this.nodes[i].number) {
      	n += this.edges[j].to.type + ", ";
      }
      else if (this.edges[j].to.number === this.nodes[i].number) {
      	n += this.edges[j].from.type + ", ";
      }      
    }
    return n;
  }
}

var nodeList = document.getElementById("nodes");

var graph = new Graph();

function createGraph() {

	var n00 = graph.addNode('0','O');
  var n01 = graph.addNode('1','O');
  var n02 = graph.addNode('2','N');
  var n03 = graph.addNode('3','N');
  var n04 = graph.addNode('4','N');
  var n05 = graph.addNode('5','N');
  var n06 = graph.addNode('6','C');
  var n07 = graph.addNode('7','C');
  var n08 = graph.addNode('8','C');
  var n09 = graph.addNode('9','C');
  var n10 = graph.addNode('10','C');
  var n11 = graph.addNode('11','C');
  var n12 = graph.addNode('12','C');
  var n13 = graph.addNode('13','C');
  var n14 = graph.addNode('14','H');
  var n15 =...