function Graph (v) {
This.vertices = v;
Initializing vertices
this.edges = 0;
The number of edges is set to 0 first
This.adj = [];
Prepares a linked list for each vertex, indicating its relationship to all nodes
for (var i = 0; i < this.vertices.length; i++) {
This.adj[i] = [];
This.adj[i].push ("");
}
This.addedge = Addedge;
this.tostring = toString;
}
function Addedge (V, W) {
Two vertices to form an edge
This.adj[v].push (w);
This.adj[w].push (v);
this.edges++;
}
function Graph (v) {
This.vertices = v;
Initializing vertices
this.edges = 0;
The number of edges is set to 0 first
This.adj = [];
Prepares a linked list for each vertex, indicating its relationship to all nodes
for (var i = 0; i < this.vertices.length; i++) {
This.adj[i] = [];
This.adj[i].push ("");
}
This.addedge = Addedge;
this.tostring = toString;
}
function Addedge (V, W) {
Two vertices to form an edge
This.adj[v].push (w);
This.adj[w].push (v);
this.edges++;
}
function Showgraph () {
for (var i = 0; i < this.vertices; i++) {
Print (i + "=");
for (var j = 0; j < this.vertices; ++j) {
if (this.adj[i][j]! = "undefined") {
Print (This.adj[i][j]);
}
}
}
}
function Dfs (v) {
THIS.MARKED[V] = true;
if (this.adj[v]! = undefined) {
Print (v);
}
for (var key in Adj[v]) {
if (!this.marked[w]) {
This.dfs (w);
}
}
}
function BFS (node) {
var queue = [];
This.marked[node] = true;
Queue.push (node);
while (Queue.length > 0) {
var v = queue.shift ();//move out of queue
if (v = = undefined) {
Print (v);
}
For (Var w in This.adj[v]) {
if (!this.marked[w]) {
This.edgeto (W) = V;
THIS.MARKED[W] = true;
Queue.push (w);
}
}
}
}
The depth-breadth-first traversal of JavaScript implementation graphs