➕︎ Aa ➖︎ |Dark 1 ● Dark 2 ● Light|Hack ● Fira Code|Back to PT Mono✖
// CPCS 324 Algorithms & Data Structures 2
// Graph data structure starter - First Graph Object
// 2020, Dr. Muhammad Al-Hashimi
// -------------------------------------------------------
// start vertex part of a simple graph object
// use this simple array of objects to input vertices into
// user input fields (see Vertex object below)
var _v = [ {label:"one"}, {label:"two"}, {label:"four"} ];
// replace label init value with strings of your choice
// be creative (don't do "a", "b"..., or "1", "2" etc.)
// create a graph
var _g = new Graph();
// input graph into internal data structures
_g.read_graph( _v );
// -------------------------------------------------------
// Vertex object constructor
function Vertex(v)
{
// user input fields
this.label = v.label; // vertex can have label,
// example: a, v1, ali
// more fields to initialize internally
this.adjacent = null; // reference/pointer to
// adjacency list
// (fill code) vertex can be marked visited
// (useful for traversals)
}
// -------------------------------------------------------
// Graph object constructor
function Graph()
{
this.verts = []; // vertex list (array of Vertex objects)
this.nv; // number of vertices
// --------------------
// member methods use functions defined below
this.read_graph = graph_input; // default input reader
this.print_graph = list_verts;
}
// -------------------------------------------------------
// Functions used by methods of Graph object. Similar to
// normal functions but use object member fields and
// methods, depending on which object is passed by the
// method call through the self variable: this.
//
// --------------------
function list_verts() // simple vertex lister
{
// i,v function local vars, verts[] object var via this
for (var i=0; i < this.nv; i++)
{
var v = this.verts[i];
document.write( "VERTEX: ", i,
" {", v.label, "} - VISIT: ", v.visit,
" - ADJACENCY: ", v.adjacent, "<br>" );
}
}
// --------------------
function graph_input(v)
{
// (fill code below) set number of vertices field
// (fill code below) input vertices into internal
// vertex array
}