图数据库-Neo4j-常用算法

2年前 (2022) 程序员胖胖胖虎阿
218 0 0

<div class="article-entry" itemprop="articleBody">

    <p>本次主要学习图数据库中常用到的一些算法,以及如何在<code>Neo4j</code>中调用,所以这一篇偏实战,每个算法的原理就简单的提一下。</p>

<h3 id="1-图数据库中常用的算法"><a href="#1-图数据库中常用的算法" class="headerlink" title="1. 图数据库中常用的算法"></a>1. 图数据库中常用的算法</h3><ul> <li><p>PathFinding &amp; Search</p> <p>一般用来发现Nodes之间的最短路径,常用算法有如下几种</p> <p><a href="https://www.google.com/search?newwindow=1&amp;ei=yKxrW77lH9mB-QbQ3ZWABA&amp;q=bellman_ford+%E7%AE%97%E6%B3%95+dijkstra+folyd&amp;oq=bellman_ford+%E7%AE%97%E6%B3%95+dijkstra+folyd&amp;gs_l=psy-ab.3..33i160k1.3896.6372.0.6588.6.6.0.0.0.0.164.729.1j5.6.0....0...1c.1.64.psy-ab..0.5.564...0i30k1.0.1ROtXHlgXdQ" target="_blank" rel="noopener">Google Search Results</a></p> <ul> <li>Dijkstra - 边不能为负值</li> <li>Folyd - 边可以为负值,有向图、无向图</li> <li>Bellman-Ford</li> <li>SPFA</li> </ul> </li> <li><p>Centrality</p> <p>一般用来计算这个图中节点的中心性,用来发现比较重要的那些Nodes。这些中心性可以有很多种,比如</p> <ul> <li>Degree Centrality - 度中心性</li> <li>Weighted Degree Centrality - 加权度中心性</li> <li>Betweenness Centrality - 介数中心性</li> <li>Closeness Centrality - 紧度中心性</li> </ul> <a id="more"></a> </li> <li><p>Community Detection </p> <p><a href="https://bigdata-ny.github.io/2016/08/12/graph-of-thrones-neo4j-social-network-analysis/" target="_blank" rel="noopener">基于社区发现算法和图分析Neo4j解读《权力的游戏》</a></p> <p>用于发现这个图中局部联系比较紧密的Nodes,类似我们学过的聚类算法。</p> <ul> <li>Strongly Connected Components</li> <li>Weakly Connected Components (Union Find)</li> <li>Label Propagation</li> <li>Lovain Modularity</li> <li>Triangle Count and Average Clustering Coefficient</li> </ul> <!--more--> </li> </ul> <h3 id="2-路径搜索算法"><a href="#2-路径搜索算法" class="headerlink" title="2. 路径搜索算法"></a>2. 路径搜索算法</h3><ul> <li><p>Shortest Path</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">MATCH (start:Loc{name:"A"}), (end:Loc{name:"F"})</span><br><span class="line">CALL algo.shortestPath.stream(start, end, "cost")</span><br><span class="line">YIELD nodeId, cost</span><br><span class="line"></span><br><span class="line">MATCH (other:Loc) </span><br><span class="line">WHERE id(other) = nodeId</span><br><span class="line">RETURN other.name AS name, cost</span><br></pre></td></tr></tbody></table></figure> <p><img src="http://on8r6i4gg.bkt.clouddn.com/18-8-18/88942165.jpg" alt=""></p> </li> <li><p>Single Source Shortest Path</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">MATCH (n:Loc {name:"A"})</span><br><span class="line">CALL algo.shortestPath.deltaStepping.stream(n, "cost", 3.0</span><br><span class="line">YIELD nodeId, distance</span><br><span class="line"></span><br><span class="line">MATCH (destination) WHERE id(destination) = nodeId</span><br><span class="line">RETURN destination.name AS destination, distance</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>All Pairs Shortest Path</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.allShortestPaths.stream("cost",{nodeQuery:"Loc",defaultValue:1.0})</span><br><span class="line">YIELD sourceNodeId, targetNodeId, distance</span><br><span class="line">WITH sourceNodeId, targetNodeId, distance</span><br><span class="line">WHERE algo.isFinite(distance) = true</span><br><span class="line"></span><br><span class="line">MATCH (source:Loc) WHERE id(source) = sourceNodeId</span><br><span class="line">MATCH (target:Loc) WHERE id(target) = targetNodeId</span><br><span class="line">WITH source, target, distance WHERE source &lt;&gt; target</span><br><span class="line">RETURN source.name AS source, target.name AS target, distance</span><br><span class="line">ORDER BY distance DESC</span><br><span class="line">LIMIT 10</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>Minimum Weight Spanning Tree</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">MATCH (n:Place {id:"D"})</span><br><span class="line">CALL algo.spanningTree.minimum("Place", "LINK", "cost", id(n),</span><br><span class="line"> {write:true, writeProperty:"MINST"})</span><br><span class="line">YIELD loadMillis, computeMillis, writeMillis, effectiveNodeCount</span><br><span class="line">RETURN loadMillis, computeMillis, writeMillis, effectiveNodeCount;</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>CASE</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">MERGE (a:Loc {name:"A"})</span><br><span class="line">MERGE (b:Loc {name:"B"})</span><br><span class="line">MERGE (c:Loc {name:"C"})</span><br><span class="line">MERGE (d:Loc {name:"D"})</span><br><span class="line">MERGE (e:Loc {name:"E"})</span><br><span class="line">MERGE (f:Loc {name:"F"})</span><br><span class="line">MERGE (a)-[:ROAD {cost:50}]-&gt;(b)</span><br><span class="line">MERGE (a)-[:ROAD {cost:50}]-&gt;(c)</span><br><span class="line">MERGE (a)-[:ROAD {cost:100}]-&gt;(d)</span><br><span class="line">MERGE (b)-[:ROAD {cost:40}]-&gt;(d)</span><br><span class="line">MERGE (c)-[:ROAD {cost:40}]-&gt;(d)</span><br><span class="line">MERGE (c)-[:ROAD {cost:80}]-&gt;(e)</span><br><span class="line">MERGE (d)-[:ROAD {cost:30}]-&gt;(e)</span><br><span class="line">MERGE (d)-[:ROAD {cost:80}]-&gt;(f)</span><br><span class="line">MERGE (e)-[:ROAD {cost:40}]-&gt;(f);</span><br></pre></td></tr></tbody></table></figure> </li> </ul> <h3 id="3-中心性算法"><a href="#3-中心性算法" class="headerlink" title="3. 中心性算法"></a>3. 中心性算法</h3><ul> <li><p>PageRank</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.pageRank.stream("Page", "LINKS",</span><br><span class="line">{iterations:20})</span><br><span class="line">YIELD nodeId, score</span><br><span class="line">MATCH (node) WHERE id(node) = nodeId</span><br><span class="line">RETURN node.name AS page,score</span><br><span class="line">ORDER BY score DESC</span><br></pre></td></tr></tbody></table></figure> <p><img src="http://on8r6i4gg.bkt.clouddn.com/18-8-18/94277608.jpg" alt=""></p> </li> <li><p>Degree Centrality</p> </li> <li><p>Betweenness Centrality</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.betweenness.stream("User", "MANAGES", {direction:"out"})</span><br><span class="line">YIELD nodeId, centrality</span><br><span class="line">MATCH (user:User) WHERE id(user) = nodeId</span><br><span class="line">RETURN user.id AS user,centrality</span><br><span class="line">ORDER BY centrality DESC;</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>Closeness Centrality</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.closeness.stream("Node", "LINK")</span><br><span class="line">YIELD nodeId, centrality</span><br><span class="line">MATCH (n:Node) WHERE id(n) = nodeId</span><br><span class="line">RETURN n.id AS node, centrality</span><br><span class="line">ORDER BY centrality DESC</span><br><span class="line">LIMIT 20;</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>CASE</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">MERGE (home:Page {name:"Home"})</span><br><span class="line">MERGE (about:Page {name:"About"})</span><br><span class="line">MERGE (product:Page {name:"Product"})</span><br><span class="line">MERGE (links:Page {name:"Links"})</span><br><span class="line">MERGE (a:Page {name:"Site A"})</span><br><span class="line">MERGE (b:Page {name:"Site B"})</span><br><span class="line">MERGE (c:Page {name:"Site C"})</span><br><span class="line">MERGE (d:Page {name:"Site D"})</span><br><span class="line">MERGE (home)-[:LINKS]-&gt;(about)</span><br><span class="line">MERGE (about)-[:LINKS]-&gt;(home)</span><br><span class="line">MERGE (product)-[:LINKS]-&gt;(home)</span><br><span class="line">MERGE (home)-[:LINKS]-&gt;(product)</span><br><span class="line">MERGE (links)-[:LINKS]-&gt;(home)</span><br><span class="line">MERGE (home)-[:LINKS]-&gt;(links)</span><br><span class="line">MERGE (links)-[:LINKS]-&gt;(a)</span><br><span class="line">MERGE (a)-[:LINKS]-&gt;(home)</span><br><span class="line">MERGE (links)-[:LINKS]-&gt;(b)</span><br><span class="line">MERGE (b)-[:LINKS]-&gt;(home)</span><br><span class="line">MERGE (links)-[:LINKS]-&gt;(c)</span><br><span class="line">MERGE (c)-[:LINKS]-&gt;(home)</span><br><span class="line">MERGE (links)-[:LINKS]-&gt;(d)</span><br><span class="line">MERGE (d)-[:LINKS]-&gt;(home)</span><br></pre></td></tr></tbody></table></figure> </li> </ul> <h3 id="4-社区发现算法"><a href="#4-社区发现算法" class="headerlink" title="4. 社区发现算法"></a>4. 社区发现算法</h3><ul> <li><p>Strongly Connected Components</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.scc.stream("User","FOLLOWS")</span><br><span class="line">YIELD nodeId, partition</span><br><span class="line">MATCH (u:User) WHERE id(u) = nodeId</span><br><span class="line">RETURN u.id AS name, partition</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>Weakly Connected Components (Union Find)</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.unionFind.stream("User", "FRIEND", {})</span><br><span class="line">YIELD nodeId,setId</span><br><span class="line">MATCH (u:User) WHERE id(u) = nodeId</span><br><span class="line">RETURN u.id AS user, setId</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>Label Propagation</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.labelPropagation.stream("User", "FOLLOWS",</span><br><span class="line"> {direction: "OUTGOING", iterations: 10})</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>Lovain Modularity</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.louvain.stream("User", "FRIEND", {})</span><br><span class="line">YIELD nodeId, community</span><br><span class="line">MATCH (user:User) WHERE id(user) = nodeId</span><br><span class="line">RETURN user.id AS user, community</span><br><span class="line">ORDER BY community;</span><br></pre></td></tr></tbody></table></figure> </li> <li><p>Triangle Count and Average Clustering Coefficient</p> <figure class="highlight plain"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">CALL algo.triangle.stream("Person","KNOWS")</span><br><span class="line">YIELD nodeA,nodeB,nodeC</span><br><span class="line">MATCH (a:Person) WHERE id(a) = nodeA</span><br><span class="line">MATCH (b:Person) WHERE id(b) = nodeB</span><br><span class="line">MATCH (c:Person) WHERE id(c) = nodeC</span><br><span class="line">RETURN a.id AS nodeA, b.id AS nodeB, c.id AS node</span><br></pre></td></tr></tbody></table></figure> </li> </ul> <h3 id="5-References"><a href="#5-References" class="headerlink" title="5. References"></a>5. References</h3><ul> <li><a href="https://www.slideshare.net/SamchuLi/neo4j-in-depth-session1" target="_blank" rel="noopener">Neo4j in deep</a></li> <li>官方文档:Comprehensive-Guide-to-Graph-Algorithms-in-Neo4j-ebook</li> </ul>

  原文地址:https://chenson.cc/2018/08/18/%E5%9B%BE%E6%95%B0%E6%8D%AE%E5%BA%93-Neo4j-%E5%B8%B8%E7%94%A8%E7%AE%97%E6%B3%95/
</div>

版权声明:程序员胖胖胖虎阿 发表于 2022年11月1日 下午3:40。
转载请注明:图数据库-Neo4j-常用算法 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...