Quick actions

cmd+k|ctrl+k

Navigation

Languages

Graph

Snippet info

Language

JavaScript

Visibility

public

Author

neeraj1804

Created

2020-10-25T08:55:47Z

Updated

2020-10-25T09:15:36Z

class MyGraph {
    constructor() {
        this.adjancyList = {};
        this.noOfVertices = 0;
    }
    
    addVertices(vertice) {
        if(!this.adjancyList[vertice]) {
            this.adjancyList[vertice] = [];
            this.noOfVertices++;
        }
    }
    
    addEdges(vertice1, vertice2) {
        this.adjancyList[vertice1].push(vertice2);
        this.adjancyList[vertice2].push(vertice1);
    }
    
    showConnections() {
        console.log(JSON.stringify(this.adjancyList));
    }
}

const myGraph1 = new MyGraph();
myGraph1.addVertices(1);
myGraph1.addVertices(2);
myGraph1.addVertices(3);
myGraph1.addEdges(1,2);
myGraph1.addEdges(1,3);
myGraph1.addEdges(2,3);
myGraph1.showConnections();
INFO