读取有权图
读取有权图跟读取一般图没有什么大的区别,只是在添加边的时候,需要把边的权重传入而已。(稠密图使用矩阵实现,稀疏图使用邻接表实现)
代码实现
const fs = require('fs');
const readline = require('readline');
const SparseGraph = require('./sparse-graph');
const rl = readline.createInterface({
input: fs.createReadStream('./testG1.txt'),
output: process.stdout,
terminal: false
});
const length = 8;
const sg = new SparseGraph(length, false);
let index = 0;
rl.on('line', (line) => {
if (index === length - 1) {
console.log(sg.g);
}
if (index !== 0) {
const [v, w, weight] = line.split(/\s/);
sg.addEdge(v, w, weight);
}
index++;
});
FEATURED TAGS
前端开发
H5
JavaScript
设计模式
browser
jQuery
源码分析
生活
leetcode
Array
Stack
Queue
Linked List
剑指offer
Binary Search Tree
Binary Tree
Breadth-First Search
Depth-First Search
String
Set
Binary Search
Sliding Window
Backtracking
Dynamic Programming
Two Pointers
Union Find
Sort
Bit Operation
Recursion
Map
Graph
Search
Hash
LinkedList
复盘
QuickSort
Trie
Design
MinHeap
Traverse
Min Heap
Node.js
BackEnd
SQL
MySQL
Design Patterns
Network
计算机网络
Python
SVG