* Reorganized instruction text on multiple challenges * Fixed spaces * Fixed spaces again * Update curriculum/challenges/english/08-coding-interview-prep/data-structures/add-elements-at-a-specific-index-in-a-linked-list.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Update curriculum/challenges/english/08-coding-interview-prep/data-structures/find-the-minimum-and-maximum-height-of-a-binary-search-tree.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * fix: added code tags
3.1 KiB
3.1 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
587d8256367417b2b2512c77 | Adjacency List | 1 |
Description
Node1: Node2, Node3Above is an undirected graph because
Node2: Node1
Node3: Node1
Node1
is connected to Node2
and Node3
, and that information is consistent with the connections Node2
and Node3
show. An adjacency list for a directed graph would mean each row of the list shows direction. If the above was directed, then Node2: Node1
would mean there the directed edge is pointing from Node2
towards Node1
.
We can represent the undirected graph above as an adjacency list by putting it within a JavaScript object.
var undirectedG = {
Node1: ["Node2", "Node3"],
Node2: ["Node1"],
Node3: ["Node1"]
};
This can also be more simply represented as an array where the nodes just have numbers rather than string labels.
var undirectedGArr = [
[1, 2], // Node1
[0], // Node2
[0] // Node3
];
Instructions
James
, Jill
, Jenny
, and Jeff
. There are edges/relationships between James and Jeff, Jill and Jenny, and Jeff and Jenny.
Tests
tests:
- text: <code>undirectedAdjList</code> should only contain four nodes.
testString: assert(Object.keys(undirectedAdjList).length === 4, '<code>undirectedAdjList</code> should only contain four nodes.');
- text: There should be an edge between <code>Jeff</code> and <code>James</code>.
testString: assert(undirectedAdjList.James.indexOf("Jeff") !== -1 && undirectedAdjList.Jeff.indexOf("James") !== -1, 'There should be an edge between <code>Jeff</code> and <code>James</code>.');
- text: There should be an edge between <code>Jill</code> and <code>Jenny</code>.
testString: assert(undirectedAdjList.Jill.indexOf("Jenny") !== -1 && undirectedAdjList.Jill.indexOf("Jenny") !== -1, 'There should be an edge between <code>Jill</code> and <code>Jenny</code>.');
- text: There should be an edge between <code>Jeff</code> and <code>Jenny</code>.
testString: assert(undirectedAdjList.Jeff.indexOf("Jenny") !== -1 && undirectedAdjList.Jenny.indexOf("Jeff") !== -1, 'There should be an edge between <code>Jeff</code> and <code>Jenny</code>.');
Challenge Seed
var undirectedAdjList = {};
Solution
var undirectedAdjList = {
James: ['Jeff'],
Jill: ['Jenny'],
Jenny: ['Jill', 'Jeff'],
Jeff: ['James', 'Jenny']
};