This course has been updated into a 4 part series! We now recommend you take the A Practical Guide to Algorithms with JavaScript course.

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Exercise: Adding Nodes and Edges" 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:

In this exercise, you will implement the Graph pseudocode in JavaScript. You will create the constructor, addNode(), and addEdge() methods.

Get Unlimited Access Now

Transcript from the "Exercise: Adding Nodes and Edges" Lesson

>> Bianca Gandolfo: Do you guys want to take a stab at representing this into some real Java scripts? Again, there's some complexity here, and I'm gonna encourage you to make it easy, you know what I mean? You don't need to hash anything or whatever, just do it the easy way.

[00:00:18] Whether you want to initialize your matrix, or add a column row everytime. Or whatever resize it. You can make those decisions however you want to design this. The idea here is we wanna explore what it takes, what are the constraints? What comes easy? What's harder? And do we like it?

[00:00:40] Maybe for you for the things that you wanna do. The JDC matrix works just fine. It's simpler in your mind. Maybe, maybe not. We just wanna get familiar with this representation of the graph. It's the simplest representation. No, no we'll go with this, it's the simplest one, I don't know.

[00:01:10] Yeah, what else do I wanna say? One last point I wanna make is do you know what the space complexity of this is?
>> Speaker 2: The what now?
>> Speaker 3: N squared.
>> Bianca Gandolfo: Mm-hm, n squared. Space complex. So we talked a lot about time complexity and that's usually what we care about but space complexity is also a thing.

[00:01:34] It's how much space does it take to represent your data so as your data doubles, how much more space does it take to store these six nodes? So we have 6 nodes but we have to have 6 times 6, 36 bases here, in memory, right? Because they're arrays, they're saving a little spot in memory, or a big spot, depending, right, for this data.

[00:01:57] And so,
>> Bianca Gandolfo: It's a lot of data, especially if we don't have a lot of connections, if it's just mostly zeros, we call that a sparsely connected graph, or loosely connected. We just have a bunch of zeros, that's a waste of space, so that's another thing to think about when thinking about adjacency matrix.

[00:02:23] But we'll review that in a second, I want you guys to jump into the code and have a feel for adding nodes and deleting nodes, or not deleting, adding edges. We already know what it's like to delete nodes. We're not going there quite yet. We're still traumatized from deleting our binary search nodes so adding nodes, adding edges, representing our matrix somehow.