A14 Stupid Graph
by scotp71
HTML
<input type="button" onclick="createGraph();" value="Create Graph" />
<br/>
<br/> Select two nodes:
<br />From
<select id="from"></select>
To
<select id="to"></select>
<input type="button" onclick="calculate();" value="Calculate" />
<br/>
<p id="nodes"></p>
<br/>
<p id="edges"></p>
<br/>
<br/><span id="result" />
JavaScript
function LinkedList(){
this.head = null;
this.tail = null;
this.length = 0;
}
function Node(){
this.next = null;
this.prev = null;
this.content = null;
this.cost = null;
}
LinkedList.prototype.add = function(_content, _cost) {
var node = new Node();
node.content = _content;
node.cost = _cost;
if (this.head == null) {
this.head = node; this.length = 1;
return node;
}
if (this.tail == null) {
this.tail = node;
this.tail.prev = this.head;
this.head.next = this.tail;
this.length = 2;
return node;
}
this.tail.next = node;
node.prev = this.tail;
this.tail = node;
this.length++;
return node;
}
//var AdjList = [];
//create graph
function Graph(noOfVertices){
this.noOfVertices = noOfVertices;
this.AdjList = new Map();
this.paths = [];
this.visited = [];
}
//add vertex to the map
Graph.prototype.addVertex = function(v){
//initalize the adjacent list with a null array
var list = new LinkedList();
this.AdjList.set(v, []);
}
//add edge to the graph
Graph.prototype.addEdge = function(v, w, c){
//debugger;
//get the list for vertex v and put the vertex w denoting edge between v and w
this.AdjList.get(v).push(list.add(w, c));
}
//print graph
Graph.prototype.printGraph = function(){
//get all the vertices
var get_keys = this.AdjList.keys();
//iterate over the keys
for(var i of get_keys){
//get the corresponding adjacency list for the vertex
var get_values = this.AdjList.get(i);
var conc = "";
//iterate over the adjacency list and concate the values to a string
for(var j of get_values)
conc += j.content + "/cost: " + j.cost + ", ";
console.log(i + " -> " + conc);
}
}
//main DFS method
Graph.prototype.dfs = function(startingNode){
this.reset();
//debugger;
var output = startingNode + this.DFSUtil(startingNode);
return output;
}
//recursive function which processes and explores all the adjacent vertex
//of the vertex with whichg it was...