A graph is a data structure comprised of a set of nodes, also known as vertices, and a set of edges. Each node in a graph may point to any other node in the graph. This is very useful for things like describing networks, creating related nonhierarchical data, and as we will see in the lesson, describing the relationships within one's family.

In a Graph, there are Nodes, and connects between nodes are Edges.

For node:

function createNode(key) {
let children = [];
return {
key,
children, // the nodes which connect to
addChild(child) {
children.push(child)
}
}
}

Graph:

function createGraph(directed = false) {
const nodes = [];
const edges = []; return {
nodes,
edges,
directed, addNode(key) {
nodes.push(createNode(key))
}, getNode (key) {
return nodes.find(n => n.key === key)
}, addEdge (node1Key, node2Key) {
const node1 = this.getNode(node1Key);
const node2 = this.getNode(node2Key); node1.addChild(node2); if (!directed) {
node2.addChild(node1);
} edges.push(`${node1Key}${node2Key}`)
}, print() {
return nodes.map(({children, key}) => {
let result = `${key}`; if (children.length) {
result += ` => ${children.map(n => n.key).join(' ')}`
} return result;
}).join('\n')
}
}
}

Run:

const graph = createGraph(true)

graph.addNode('Kyle')
graph.addNode('Anna')
graph.addNode('Krios')
graph.addNode('Tali') graph.addEdge('Kyle', 'Anna')
graph.addEdge('Anna', 'Kyle')
graph.addEdge('Kyle', 'Krios')
graph.addEdge('Kyle', 'Tali')
graph.addEdge('Anna', 'Krios')
graph.addEdge('Anna', 'Tali')
graph.addEdge('Krios', 'Anna')
graph.addEdge('Tali', 'Kyle') console.log(graph.print()) /*
Kyle => Anna Krios Tali
Anna => Kyle Krios Tali
Krios => Anna
Tali => Kyle
*/

[Algorithm] JavaScript Graph Data Structure的更多相关文章

  1. [Algorithm] Linked List Data Structure in JavaScript

    A linked list is a collection of items where each item points to the next one in the list. Because o ...

  2. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  3. [Algorithm] Trie data structure

    For example we have an array of words: [car, done, try, cat, trie, do] What is the best data structu ...

  4. [Algorithms] Tree Data Structure in JavaScript

    In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...

  5. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

  6. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  7. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  8. HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  9. [UVA] 11995 - I Can Guess the Data Structure! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

随机推荐

  1. 《机器学习实战》笔记——AdaBoost

    笔记见备注 # _*_ coding:utf-8 _*_ from numpy import * # 简单数据集 def loadSimpData(): datMat = matrix([[1., 2 ...

  2. Jeddict研究过程中的总结

    一.与作者交流的总结 说来也是惭愧,没有太多的经验,先给大家贴两张图,看看大家能不能发现问题: 在最开始的时候,都处于Gaurav Gupta让我给材料的过程,因为我不是缺这个就是缺那个,根本说不清楚 ...

  3. struts拦截器的使用

    拦截器的使用 转自http://blog.csdn.net/woshisap/article/details/7271854 1:拦截器(Interceptor) 拦截器是Struts2最强大的特性之 ...

  4. ubuntu-64位安装tensorflow

    (一)ubuntu-64位安装tensorflow过程: 1安装Anaconda 然后执行:bash Anaconda×××-Linux-x86_64.sh ,然后一直enter键,中途会遇到([y] ...

  5. 96. Unique Binary Search Trees(I 和 II)

    Given n, how many structurally unique BST's (binary search trees) that store values 1-n? For example ...

  6. 【CCF】有趣的数 数位dp

    [思路] dp[i][j]表示前i个数为第j种状态,考虑6种状态 0: 出现且仅出现 2 1: 出现且仅出现 2 0 2: 出现且仅出现 2 3 3: 出现且仅出现 2 0 1 4: 出现且仅出现 2 ...

  7. BeanFactory到WebApplicationContext的结构 以及bean和spring容器的关系

    BeanFactory: Ioc 容器 ApplicationContext: Spring容器 WebApplicationContext需要ServletContext实例,也就是说它必须在拥有W ...

  8. UVa12333 Revenge of Fibonacci

    高精度 trie 暴力预处理出前100000个fibonacci数,将每个数的前40位数字串插入到trie中,记录每个结点最早可以由哪个数字串到达. 然后依次回答询问即可. 存fibonacci数的数 ...

  9. polyfill for Function--源码

    /** * polyfill for Function */ // from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refer ...

  10. JSTL <C:if></C:if> 和<C:ForEach></C:ForEach> 入门级~

    一.<C:If>标签:条件判断语句 <c:if test="${objList.nodetype == 1}">上级节点</c:if>   te ...