README.md (2134B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # CompactAdjacencyMatrix 22 23 > Compact adjacency matrix constructor. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var CompactAdjacencyMatrix = require( '@stdlib/utils/compact-adjacency-matrix' ); 31 ``` 32 33 #### CompactAdjacencyMatrix( N ) 34 35 Returns a compact adjacency matrix instance. 36 37 ```javascript 38 var adj = new CompactAdjacencyMatrix( 4 ); 39 // returns <CompactAdjacencyMatrix> 40 41 // ... 42 43 adj.addEdge( 0, 1 ); 44 adj.addEdge( 0, 2 ); 45 adj.addEdge( 1, 2 ); 46 adj.addEdge( 2, 3 ); 47 ``` 48 49 </section> 50 51 <!-- /.usage --> 52 53 <section class="examples"> 54 55 ## Examples 56 57 <!-- eslint no-undef: "error" --> 58 59 ```javascript 60 var CompactAdjacencyMatrix = require( '@stdlib/utils/compact-adjacency-matrix' ); 61 62 // Create a new adjacency matrix: 63 var adj = new CompactAdjacencyMatrix( 4 ); 64 65 // Add edges: 66 adj.addEdge( 1, 0 ); 67 adj.addEdge( 1, 2 ); 68 adj.addEdge( 0, 2 ); 69 adj.addEdge( 2, 3 ); 70 71 // Compute the indegrees and outdegrees for each vertex: 72 var id; 73 var od; 74 var i; 75 for ( i = 0; i < 4; i++ ) { 76 id = adj.inDegree( i ); 77 od = adj.outDegree( i ); 78 console.log( 'vertex: %d. indegree: %d. outdegree: %d.', i, id, od ); 79 } 80 81 // Print the list of all edges: 82 console.log( adj.edges ); 83 // => [ [ 0, 2 ], [ 1, 0 ], [ 1, 2 ], [ 2, 3 ] ] 84 85 // Convert the adjacency matrix to an adjacency list representation: 86 console.log( adj.toAdjacencyList() ); 87 // => [ [ 2 ], [ 0, 2 ], [ 3 ], [] ] 88 89 // Compute a topological ordering: 90 console.log( adj.toposort() ); 91 // => [ [ 1, 0, 2, 3 ], null ] 92 ``` 93 94 </section> 95 96 <!-- /.examples --> 97 98 <section class="links"> 99 100 </section> 101 102 <!-- /.links -->