Running graph0_starter3.js

➕︎ Aa ➖︎ |Dark 1Dark 2Light|HackFira Code|Back to  PT Mono
// CPCS 324 Algorithms & Data Structures 2
// Graph data structure starter - First DFS Traversal
// 2020, Dr. Muhammad Al-Hashimi

// -----------------------------------------------------------------------
// simple graph object with linked-list edge implementation and minimal fields
// extra vertex and edge member fields and methods to be added later as needed


// graph from Figure 3.10 (Levitin, 3rd edition)
// read info to configure *user* property fields of Vertex object,
// note global var naming convention

var _v = [
   {label:"a"},
   {label:"b"},
   {label:"c"},
   {label:"d"},
   {label:"e"},
   {label:"f"},
   {label:"g"},
   {label:"h"},
   {label:"i"},
   {label:"j"}

   ];

var _e = [
   {u:0, v:3},
   {u:0, v:2},
   {u:3, v:2},
   {u:2, v:5},
   {u:5, v:4},
   {u:5, v:1},
   {u:1, v:4},
   {u:0, v:4},
   {u:6, v:7},
   {u:7, v:8},
   {u:8, v:9},
   {u:9, v:6}

   ];


// create a graph (default undirected)
var _g = new Graph();

// if _g is directed (digraph) change property BEFORE input
// _g.digraph = true;

// use global input arrays _v and _e to initialize internal data structures
_g.read_graph(_v,_e);

// (fill code below) perform a depth-first search and store result
// HINT: may need to copy code to caller page (?)





// -----------------------------------------------------------------------
// Vertex object constructor

function Vertex(v)
{
   // user input fields

   this.label = v.label;          // vertex can be labelled

   // more fields to initialize internally

   this.visit = false;            // vertex can be marked visited (useful for traversals)
   this.adjacent = new List();    // init an adjacency list
}

// -----------------------------------------------------------------------
// Graph object constructor

function Graph()
{
   this.verts = [];               // vertex list (an array of Vertex objects)
   this.nv;                       // number of vertices
   this.ne;                       // number of edges
   this.digraph = false;          // true if digraph, false otherwise (default undirected)
   this.dfs_push = [];            // DFS traversal order output array

   // --------------------
   // student property fields next


   // --------------------
   // member methods use functions defined below

   this.read_graph = better_input;  // default input reader method
   this.print_graph = list_verts;
   this.add_edge = add_edge;


   // --------------------
   // student methods next; actual functions in student code section at end

   this.DFS =                       // (fill) perform a depth-first search
   this.dfs =                       // (fill) DFS a connected component

}


// -------------------------------------------------------
// 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
{
   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.traverse(), "<br>" );
   }
}

// --------------------
function add_edge(u_i,v_i)
{

}

// --------------------
function better_input(v,e)
{

}


// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// --- begin student code section ----------------------------------------

function DFS()
{

   // mark all vertices unvisited


   // traverse unvisited connected components


}

// --------------------
function dfs(v_i)
{

   // get landing vert by id then process


   // recursively traverse unvisited adjacent vertices


}