JSFiddle - React, Tailwind, and code Playground

JavaScript

function Graph (v) {
	this.vertices = v; 
	this.edges = 0; 
	this.adj = [];
	this.marked = [];
	this.addEdge = addEdge;
	this.showGraph = showGraph;
	this.dfs = dfs;
    this.init = init;
}
function addEdge(v,w) {  
	this.adj[v].push(w);
	this.adj[w].push(v);
	this.edges++;
}
function showGraph() {
	var str = "";
	for(var i = 0; i < this.vertices; ++i) {
		str = i + "-->";
		for(var j = 0; j < this.vertices; ++j) {
			if(this.adj[i][j] != undefined) {
				str += this.adj[i][j] + " ";
			}
		}
		console.log(str);
	}
}
function init() {
	for(var i = 0; i < this.vertices; ++i) {
		this.adj[i] = [];  
		this.marked[i] = false;
	}
}
function dfs(v) {
	this.marked[v] = true;
	var adj = this.adj[v],nextAdj;
	if(adj != undefined) {
		console.log("Visited vertex: " + v);
	}
	for(var w in adj) {
		var nextAdj = this.adj[v][w];
		if(!this.marked[nextAdj]) {
			this.dfs(nextAdj);
		}

	}
}

var g = new Graph(5);
g.init();
g.addEdge(0,1);
g.addEdge(0,2);
g.addEdge(1,3);
g.addEdge(2,4);
g.showGraph();
g.dfs(0);