Here is the interview question prompt, presented for reference.
Since a graph is one of the more difficult data structures to conceptualize in a programmatic 2D manner, let's go ahead and implement it! We'll go with an adjacency list version. Note that there's also the adjacency matrix method which we will cover later.
Recall that an adjacency list is a concept in graph theory, used to describe a representation of a graph via its nodes' adjacent (neighboring) nodes. You can think of it as each vertex maintaining a list of other vertices it's connected to. Using an unordered list data structure, each list is comprised of the node's neighbors.
Here's a skeleton of the implementation. Can you fill the rest in? What could be a good data structure to model this?
class Graph {
constructor(verticesCount) {
this.adjacencyList = {};
}
addVertex(nodeVal) {}
addEdge(src, dest) {}
removeVertex(nodeVal) {}
removeEdge(src, dest) {}
}
100000HashMap for the adjacency listO(1)O(n)You can see the full challenge with visuals at this link.
Challenges • Asked almost 8 years ago by Team AlgoDaily
This is the main discussion thread generated for Implement a Graph.