JSFiddle - React, Tailwind, and code Playground
by wooozy
HTML
<h1>Assignment 12</h1>
<p><input type="button" id = "createG" onclick="DisplayGraph();" value="Display Graph" /></p>
<p>Pick Two Nodes<br>
From: <select id="from"></select><br>
To: <select id="to"></select><br>
</p>
<input type="button" onclick="total();" value="total" />
<p id="nodes"></p>
<p id="edges"></p>
<p id="test"></p>
<span id="result"></span>
JavaScript
var createdStructure = function(){
this.can = [];
this.add = function(val){
this.can.push(val);
};
this.remove = function(){
var val = null;
try {
val = this.can[0];
if (this.can.length === 1){
this.can = [];
}
else{
this.can = this.can.slice(1,this.can.length);
}
}
catch (err){
}
return val;
};
this.empty = function(){
var out = "";
out = false;
if (this.can.length === 0) {
out = true;
}
return out;
};
};
function createPaths(graph,distances,start,destination,q,d){
var str = "";
q.add([start]);
d.add([0]);
while (q.empty() === false){
i = -1;
path = q.remove();
dist = d.remove();
l_node = path[path.length - 1];
if (l_node === destination){
str = str + "Path: " + path.toString() + " Cost = " + dist.reduce(function(a, b) { return a + b; }, 0) + "<br>";
document.getElementById("test").innerHTML = str;
}
debugger;
var temp = graph[l_node];
for (var j = 0; j < temp.length; j++){
i = i + 1;
var n = temp[j];
if (path.indexOf(n) === -1) {
new_path = path.concat([n]);
new_dist = dist.concat([distances[l_node][i]]);
q.add(new_path);
d.add(new_dist);
}
}
}
}
var Node = function(_name) {
this.name = _name;
this.edges = [];
return this;
};
var Edge = function(_cost) {
this.from = null;
this.to = null;
return this;
};
var Graph = function() {
this.nodes = [];
this.edges = [];
this.paths = [];
this.nodeList = {};
this.distances = {};
this.addNode = function(_name) {
var node = new Node(_name);
this.nodes.push(node);
this.nodeList[_name] = [];
this.distances[_name] = [];
return node;
};
this.addEdge = function(_from, _to, _cost) {
var edge = new Edge(_cost);
edge.from = _from;
edge.to = _to;
edge.cost = _cost;
this.edges.push(edge);
var a = this.distances[_from.name];
var b =...