Data Structures and Algorithms in JavaScript

Check out a free preview of the full Data Structures and Algorithms in JavaScript course:
The "Pseudocoding an Adjacency List" 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 pseudocode for an Adjacency List. The constructor initializes the nodes array. The addNode() method adds the passed value ot the nodes array and the addEdges() method pushes the ending vertex to the starting vertices in the nodes array.

Get Unlimited Access Now

Transcript from the "Pseudocoding an Adjacency List" Lesson

[00:00:00]
>> Bianca Gandolfo: Pseudocode, what might we need in the constructor for an adjacency list? So, let's just say for now it's an array, just for simplicity.
>> Bianca Gandolfo: Okay? And if we wanted to add a node, right, we're gonna pass a value. And we wanna add that value.
>> Bianca Gandolfo: Oops, probably we'll initialize it to another empty array, so that then we can add all of our adjacent nodes or vertices.

[00:00:45]
>> Speaker 2: Yes.
>> Bianca Gandolfo: Or, if it already exists, right? We'll just put whatever that is there or we'll initialize it, right? For the case where there's already something inside of the adjacency list, and it's not the first node that we're adding.
>> Bianca Gandolfo: Cool?
>> Speaker 2: Cool.
>> Bianca Gandolfo: So we're gonna have a data structure that looks like this, we're gonna add our graph

[00:01:22]
>> Bianca Gandolfo: .addnode, and we're gonna pass.
>> Bianca Gandolfo: Just pass 1. Then, we have,
>> Bianca Gandolfo: And we're gonna have undefined. And then, here, we'll have 1, because that's just how the array will work. We initialize an array at 1 and then this doesn't already exist, so we'll add an empty array.

[00:01:52] So there's 1.
>> Speaker 2: Okay, I think I'm willing to try.
>> Bianca Gandolfo: Two?
>> Bianca Gandolfo: Zero, one, two, three, four, five. Okay, so now, we've initialized three different nodes in our graph, 1, 2 and 5. But right now they're not connected to anything else.
>> Bianca Gandolfo: That's not very useful. So how might we connect our graph?

[00:02:50]
>> Speaker 2: So, assuming we're passing v1 and v2 to edge, we could do nodes, index v1.push v2.
>> Speaker 2: It seems like the adjacency list is just a series of lists of what it's connected to.
>> Bianca Gandolfo: Yep.
>> Bianca Gandolfo: And then this is if its only one direction, right?
>> Speaker 2: Yep.

[00:03:25]
>> Bianca Gandolfo: If it's undirected, how might this be different?
>> Speaker 2: The whole thing?
>> Bianca Gandolfo: Nope, we'll just add to both. Instead of just one. So, v2 push v1.
>> Speaker 2: [INAUDIBLE].
>> Bianca Gandolfo: Yep.