Data Structures and Algorithms in JavaScript

# Adding Nodes and Edges Solution

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Adding Nodes and Edges Solution" Lesson is part of the full, Data Structures and Algorithms in JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

## Bianca walks through the solution to the Adding Nodes and Edges exercise.

Get Unlimited Access Now

Transcript from the "Adding Nodes and Edges Solution" Lesson

[00:00:00]
>> Bianca Gandolfo: So what was your experience creating this adjacency matrix?
>> Speaker 2: I'm feeling like I still don't quite get it.
>> Speaker 3: I don't understand it.
>> Bianca Gandolfo: Yeah, can we turn that into a question, like a specific question? [LAUGH]
>> Speaker 2: [LAUGH] So I tried to add a node that was a value.

[00:00:22] And so I came up with, if this dot nodes, and then like the index of value. That would mean it was already there.
>> Speaker 2: In my, or yeah, this dot underscore nodes equals object.
>> Bianca Gandolfo: Okay, is there a reason you're initializing as an object and not an array?

[00:00:48]
>> Speaker 2: That's what was here in the github.
>> Bianca Gandolfo: You're just looking at the solution?
>> Bianca Gandolfo: But that's not, that's a different representation.
>> Speaker 2: No wonder this was so hard.
>> Bianca Gandolfo: Yeah, yeah. It's not in the exercise. It's an unrelated exercise.

[00:01:11]
>> Speaker 2: Okay, cool cool. I think it should be an array then.
>> Bianca Gandolfo: Or we can leave it like that, yeah. So I think the easiest way to do it is just initialize your 2D array.
>> Bianca Gandolfo: Something like that.
>> Bianca Gandolfo: So just initialize it all at zero, and then just imagine this is ten.

[00:01:58]
>> Bianca Gandolfo: So we initialize it, it's empty.
>> Bianca Gandolfo: And a lot of times, when you're doing different graph processing things, you're given the graph and then so they're like, here's the graph, now figure out these things. And so, when you're given the data ahead of time and you're not dynamically adding different nodes to the graph, it's pretty easy, right, like you probably are given like a list of nodes, and a list of edges and then from that you can just loop through it and create your 2D array.

[00:02:46] Pretty straightforward and that would be one way to initialize it, and here's another way. We're just initializing it empty, assuming that we have a certain size for our graph, and then as we add nodes we will change.
>> Bianca Gandolfo: Just the relationship here, right? And then we might also wanna represent, if we've added the node or not.

[00:03:29]
>> Bianca Gandolfo: You know what I mean? So we're initializing an empty thing. So, if we want to check if our graph contains a value, right now,
>> Bianca Gandolfo: We don't really know if it does. How could we do that?
>> Bianca Gandolfo: Cuz that seems like an easy thing that we could look up, in this kind of data structure, right?

[00:03:55] This kind of representation of our graph is just like, does it exist at all? Because we have arrays. Arrays are kind of easy for that kind of look up.
>> Bianca Gandolfo: Should we look at a picture?
>> Speaker 4: Can you repeat again?
>> Bianca Gandolfo: Hm?
>> Speaker 4: One more repeat, please.
>> Bianca Gandolfo: One more time?

[00:04:18]
>> Speaker 4: I didn't catch the question.
>> Bianca Gandolfo: Yeah. So the question is, how is one way that we could represent that dynamically, right? So this is a graph that's already been created. We know there's one through six, we have one through six. And we're adding values.
>> Bianca Gandolfo: How can we just represent that something has been added?

[00:04:47]
>> Bianca Gandolfo: Versus where you're just initializing this empty graph. Does that make sense?
>> Speaker 3: So if you're initializing an empty graph, are you actually saying that the points exist? But they just don't have any connections yet.
>> Bianca Gandolfo: Yeah, so if we don't account for this, that's what it's saying, right.

[00:05:10] So this graph is saying we have one through six.
>> Speaker 3: Right, so if we're
>> Bianca Gandolfo: If we initialize it empty this is one way to do it, if we initialize it just empty how do we know that these aren't just four unconnected nodes in a graph? You know what I'm saying?

[00:05:31]
>> Speaker 3: They are four unconnected nodes in a graph
>> Bianca Gandolfo: But what if we just want to say that this is an empty graph?
>> Speaker 3: This is an empty graph? Then don't put zeroes in there? If a zero is going to be used as a placeholder for something that exists?

[00:05:49]
>> Bianca Gandolfo: True. So that would be one way, but then whenever we add a node, we would have to suddenly put all zeroes, right?
>> [INAUDIBLE]
>> Bianca Gandolfo: So-
>> Speaker 3: I mean you could make a list of the things that you have added.
>> Bianca Gandolfo: Mm-hm, you could have another auxiliary list, yeah totally.

[00:06:09] These are all things to think about. You can also just put an x, so this is one and one, that one exists. Right, we're not gonna have a connection with our selves.
>> Speaker 3: Yeah, that's kind of what I meant.
>> Bianca Gandolfo: Yeah, or you can literally just have another list here.

[00:06:27] But again this already takes up so much space. Then you're taking up more space. So that's if we're adding the value for the index. The order is a little bit easier, right? We can just keep a count. Anything beyond that count, we haven't gotten to yet. Cool. Adding an edge, we talked about that.

[00:06:51] Overall, adding an edge is the easiest thing you could do in a matrix.
>> Bianca Gandolfo: Deleting edges, searching if something exists is another easy thing.
>> Bianca Gandolfo: Adjacency matrix. Any questions?
>> Bianca Gandolfo: No, okay.
>> Speaker 2: So essentially, in the constructor, you're setting nodes to a multidimensional array. That is basically what you'd wanna do, right.

[00:07:30] Because in the pseudocode there, that's supposed to be a picture of a graph, right?
>> Bianca Gandolfo: Yeah.
>> Speaker 2: But
>> Bianca Gandolfo: Yeah
>> Speaker 2: But what you,
>> Speaker 2: But it's just a multi-dimensional array, right?
>> Bianca Gandolfo: Yeah, that's what a matrix is.
>> Speaker 2: And then the,
>> Speaker 2: Okay, I think so.
>> Bianca Gandolfo: And then the index represents the node.

[00:07:55] This value represents the edge.
>> Speaker 2: The value represents the edge, but the index represents the node, okay.
>> Bianca Gandolfo: Yep and this is easy when we already know what's going to be in the graph. It's like, okay, we are given all of the things that's already going to be in the graph, all of the nodes and all the edges, and then we just want to do some calculations on it, right.

[00:08:23] So we don't have to worry about adding nodes, because as we've seen, it's a little bit complicated to do that, especially once we reach the size, then we have to resize. It gets complicated