[数据结构学习笔记13] 图(Graph)
图(太难了 。。。)
图是组织信息的一种方式,它可以表示信息之间是怎么样互相联系的。它可以帮助我们发现和分析事物间的关系。
看一个例子,下面这就是一个图:它有节点,和边,下面这个例子的边它没有方向,所以也叫无向图。
Elaine Kramer (node)
\ / (edge)
Jerry
|
George
下面这个图的边是带方向的,所以叫有向图。
Elaine ↔ Kramer (node)
↑ ↓ (edge)
Jerry
↑
George
还有一种图叫环状图,它是说从某个节点出发,可以有路径又回到该节点,这就说这个图有环。无环图类似前面说过的树。
代码实现(JavaScript)
class Graph { constructor() { // Map to store nodes and their adjacent nodes this.nodes = new Map(); // Flag to indicate if the graph is directed or undirected this.isDirected = false; } // Add a new node to the graph addNode(node) { if (!this.nodes.has(node)) { this.nodes.set(node, new Set()); } } // Add an edge between two nodes addEdge(node1, node2) { // check if the nodes exist if (!this.nodes.has(node1) || !this.nodes.has(node2)) { throw new Error('Nodes do not exist in the graph.'); } // Add edge between node1 and node2 this.nodes.get(node1).add(node2); // if the graph is undirected, add edge in the opposite direction as well if (!this.isDirected) { this.nodes.get(node2).add(node1); } } // Remove a node and all its incident edges from the graph removeNode(node) { if (this.nodes.has(node)) { // Remove the node and its edges from the graph this.nodes.delete(node); // Remove any incident edges in other nodes for (const [node, adjacentNotes] of this.nodes) { adjacentNodes.delete(node); } } } // Remove an edge between two nodes removeEdge(node1, node2) { if (this.nodes.has(node1) && this.nodes.has(node2)) { // Remove edge between node1 and node2 this.nodes.get(node1).delete(node2); // If the graph is undirected, remove edge in the oppsite direction as well if (!this.isDirected) { this.nodes.get(node2).delete(node1); } } } // Check if an edge exists between two nodes hasEdge(node1, node2) { if (this.nodes.has(node1) && this.nodes.has(node2)) { return this.nodes.get(node1).has(node2); } return false; } // Get the adjacent nodes of a given node getNeighbors(node) { if (this.nodes.has(node)) { return Array.from(this.nodes.get(node)); } return []; } // Get all nodes in the graph getAllNodes() { return Array.from(this.nodes.keys()); } // Set the graph as directed setDirected() { this.isDirected = true; } // Set the graph as undirected setUndirected() { this.isDirected = false; } // Check if the graph is directed isGraphDirected() { return this.isDirected; } }
使用图
// Create a new graph const characters = new Graph(); characters.setDirected(); // Add nodes characters.addNode('Jerry'); characters.addNode('Elaine'); characters.addNode('Kramer'); characters.addNode('George'); characters.addNode('Newman'); // Add edges characters.addEdge('Jerry', 'Elaine'); characters.addEdge('Jerry', 'George'); characters.addEdge('Jerry', 'Kramer'); characters.addEdge('Elaine', 'Jerry'); characters.addEdge('Elaine', 'George'); characters.addEdge('Elaine', 'Kramer'); characters.addEdge('George', 'Elaine'); characters.addEdge('George', 'Jerry'); characters.addEdge('George', 'Kramer'); characters.addEdge('Kramer', 'Elaine'); characters.addEdge('Kramer', 'George'); characters.addEdge('Kramer', 'Jerry'); characters.addEdge('Kramer', 'Newman'); characters.addEdge('Newman', 'Kramer'); characters.addEdge('Newman', 'Jerry'); // Get the adjacent nodes of a node console.log(characters.getNeighbors('Jerry')); // ['Elaine', 'George', 'Kramer'] // Check if an edge exists between two nodes console.log(characters.hasEdge('Jerry', 'Newman')); // false // Get all nodes in the graph console.log(chracters.getAllNodes()); // ['Jerry', 'Elaine', 'Kramer', 'George', 'Newman'] // Remove a node chracters.remove('Newman');
图是计算机里一个最基本的概念,非常重要,就是难。
标签:
数据结构笔记
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战