Check out a free preview of the full Tree and Graph Data Structures course:
The "Graph Exercise" Lesson is part of the full, Tree and Graph Data Structures course featured in this preview video. Here's what you'd learn in this lesson:

Bianca instructs students to write methods for operating on graphs, using an adjacency list for the data structure.

Get Unlimited Access Now

Transcript from the "Graph Exercise" Lesson

>> Bianca Gandolfo: In an interview setting, I recommend using the adjacency list, but it is important to understand the pros and cons between the adjacency list and the adjacency matrix. So as you saw, right, inserting a node into the adjacency matrix is much more work than the adjacency list, right.

[00:00:22] Constant versus linear. Also, adjacency list takes up a lot more space, right? You have to build that whole matrix even if you don't have a lot of edges, could be just full of zeroes, right? So you have all this space for nothing. Whereas the adjacency list is only going to track the edges.

[00:00:39] However, a pro of the adjacency matrix is, if you're into matrix math-type stuff, you can do cool stuff with that. But that's way over my head. But if you're into that, you can look into that. So that's just a cool thing. So with that said, in real life, practically speaking and adjacency list is the one you should use.

[00:01:08] However, if the adjacency matrix is, makes more sense to you because it just kind of looks more like a real life tangible thing. You can use it in your interview, but just note that you should talk about the pros and cons with your interviewer ahead of time. And just be prepared if you're like, I'm only gonna use an adjacency matrix when I have a graph traversal question or whatever, just explain the pros and the cons.

[00:01:39] And also, practice it first because, even though it might seem easier, it can actually be a bit trickier. But everyone's brain is different. Okay, so right now for this exercise, the graph exercise, we are going to implement the constructor. Adding a node, adding an edge, and removing a node of an adjacency list.