* 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.5 KiB
3.5 KiB
id, title, challengeType
| id | title | challengeType |
|---|---|---|
| 587d8256367417b2b2512c78 | Adjacency Matrix | 1 |
Description
1 2 3Above is a very simple, undirected graph where you have three nodes, where the first node is connected to the second and third node. Note: The numbers to the top and left of the matrix are just labels for the nodes. Below is a JavaScript implementation of the same thing.
------
1 | 0 1 1
2 | 1 0 0
3 | 1 0 0
var adjMat = [
[0, 1, 1],
[1, 0, 0],
[1, 0, 0]
];
Unlike an adjacency list, each "row" of the matrix has to have the same number of elements as nodes in the graph. Here we have a three by three matrix, which means we have three nodes in our graph. A directed graph would look similar. Below is a graph where the first node has an edge pointing toward the second node, and then the second node has an edge pointing to the third node.
var adjMatDirected = [
[0, 1, 0],
[0, 0, 1],
[0, 0, 0]
];
Graphs can also have weights on their edges. So far, we have unweighted edges where just the presence and lack of edge is binary (0 or 1). You can have different weights depending on your application.
Instructions
Tests
tests:
- text: <code>undirectedAdjList</code> should only contain five nodes.
testString: assert((adjMatUndirected.length === 5) && adjMatUndirected.map(function(x) { return x.length === 5 }).reduce(function(a, b) { return a && b }) , '<code>undirectedAdjList</code> should only contain five nodes.');
- text: There should be an edge between the first and fourth node.
testString: assert((adjMatUndirected[0][3] === 1) && (adjMatUndirected[3][0] === 1), 'There should be an edge between the first and fourth node.');
- text: There should be an edge between the first and third node.
testString: assert((adjMatUndirected[0][2] === 1) && (adjMatUndirected[2][0] === 1), 'There should be an edge between the first and third node.');
- text: There should be an edge between the third and fifth node.
testString: assert((adjMatUndirected[2][4] === 1) && (adjMatUndirected[4][2] === 1), 'There should be an edge between the third and fifth node.');
- text: There should be an edge between the fourth and fifth node.
testString: assert((adjMatUndirected[3][4] === 1) && (adjMatUndirected[4][3] === 1), 'There should be an edge between the fourth and fifth node.');
Challenge Seed
var adjMatUndirected = [];
Solution
var adjMatUndirected = [
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[0, 0, 1, 1, 0]
];