在图上做随机游走

 

Here’s the table of contents:

  1. 随机游走
    1. Graph Demo
    2. Random Walk-1
    3. Random Walk-2
    4. 参数注释
  2. 随机游走

随机游走

【neo4j-graph-data-science-1.1.1-standalone】在ongdb-3.5.17不支持高并发计算

Graph Demo

CREATE  (home:Page {name: 'Home'}), (about:Page {name: 'About'}), (product:Page {name: 'Product'}), (links:Page {name: 'Links'}), (a:Page {name: 'Site A'}), (b:Page {name: 'Site B'}), (c:Page {name: 'Site C'}), (d:Page {name: 'Site D'}), (home)-[:LINKS]->(about), (about)-[:LINKS]->(home), (product)-[:LINKS]->(home), (home)-[:LINKS]->(product), (links)-[:LINKS]->(home), (home)-[:LINKS]->(links), (links)-[:LINKS]->(a), (a)-[:LINKS]->(home), (links)-[:LINKS]->(b), (b)-[:LINKS]->(home), (links)-[:LINKS]->(c), (c)-[:LINKS]->(home), (links)-[:LINKS]->(d), (d)-[:LINKS]->(home)

Random Walk-1

-

  • steps 表示随机游走路径包含多少步
  • walks 表示需要返回多少条游走路径
    MATCH (home:Page {name: 'Home'})
    CALL gds.alpha.randomWalk.stream({nodeProjection:'*',relationshipProjection:{LINKS: {type:'LINKS',projection:'UNDIRECTED'}},start: id(home),steps: 3,walks: 1 }) YIELD nodeIds
    UNWIND nodeIds AS nodeId RETURN gds.util.asNode(nodeId).name AS page
    
    ╒═════════╕
    │"page"   │
    ╞═════════╡
    │"Home"   │
    ├─────────┤
    │"Product"│
    ├─────────┤
    │"Home"   │
    ├─────────┤
    │"About"  │
    └─────────┘
    

    Random Walk-2

  • nodeQuery 用于查找符合条件的节点cypher语句
  • relationshipQuery 用于查找符合条件的边的cypher语句
    MATCH (home:Page {name:'Home'})
     CALL gds.alpha.randomWalk.stream({nodeQuery:'MATCH (p:Page) RETURN id(p) AS id',relationshipQuery: 'MATCH (p1:Page)-[:LINKS]->(p2:Page) RETURN id(p1) AS source, id(p2) AS target',start: id(home),steps: 5,walks: 1 }) YIELD nodeIds
     UNWIND nodeIds AS nodeId RETURN gds.util.asNode(nodeId).name AS page
    
    ╒═══════╕
    │"page" │
    ╞═══════╡
    │"Home" │
    ├───────┤
    │"About"│
    ├───────┤
    │"Home" │
    ├───────┤
    │"Links"│
    ├───────┤
    │"Home" │
    ├───────┤
    │"Links"│
    └───────┘
    

参数注释

https://neo4j.com/docs/graph-data-science/current/alpha-algorithms/random-walk/
  • 在股权网络上执行随机游走算法
    MATCH (horg:HORGShareHold {hcode:'HORGb80f53dce4d05d8a00eb8def3f405f4b'})
     CALL gds.alpha.randomWalk.stream({nodeProjection:'*',relationshipProjection:{持股: {type:'持股',projection:'OUT'}},start: id(horg),steps:2,walks: 100,mode:'random',path:false,concurrency:4,readConcurrency:4})
    YIELD startNodeId, nodeIds, path RETURN startNodeId, nodeIds, path
    
  • 加载随机游走的返回的节点【原始图】
    MATCH (horg:HORGShareHold {hcode:'HORGb80f53dce4d05d8a00eb8def3f405f4b'})
     CALL gds.alpha.randomWalk.stream({nodeProjection:'*',relationshipProjection:{持股: {type:'持股',projection:'OUT'}},start: id(horg),steps:2,walks: 100,mode:'random',path:false,concurrency:4,readConcurrency:4})
    YIELD startNodeId, nodeIds, path WITH startNodeId, nodeIds, path
    UNWIND nodeIds AS id
    WITH COLLECT(id) AS ids
    MATCH (n) WHERE ID(n) IN ids RETURN n
    
  • 返回随机游走的虚拟图
    MATCH (horg:HORGShareHold {hcode:'HORGb80f53dce4d05d8a00eb8def3f405f4b'})
     CALL gds.alpha.randomWalk.stream({nodeProjection:'*',relationshipProjection:{持股: {type:'持股',projection:'OUT'}},start: id(horg),steps:2,walks: 100,mode:'random',path:true,concurrency:4,readConcurrency:4})
    YIELD startNodeId, nodeIds, path RETURN startNodeId, nodeIds, path
    

    -

    MATCH (horg:HORGShareHold {hcode:'HORGb80f53dce4d05d8a00eb8def3f405f4b'})
     CALL gds.alpha.randomWalk.stream({nodeProjection:'*',relationshipProjection:{持股: {type:'持股',projection:'OUT'}},start: id(horg),steps:2,walks: 100,mode:'random',path:true,concurrency:4,readConcurrency:4})
    YIELD startNodeId, nodeIds, path RETURN startNodeId, nodeIds, path
    
    MATCH (horg:HORGShareHold {hcode:'HORG2a1714857fc3790bc6f28365c1c2d35c'})
    CALL gds.alpha.randomWalk.stream({nodeQuery:'MATCH (p:HORGShareHold) RETURN id(p) AS id',relationshipQuery: 'MATCH (p1:HORGShareHold)-[:持股]->(p2:HORGShareHold) RETURN id(p1) AS source, id(p2) AS target',start: id(horg),steps:2,walks: 100,mode:'random',path:true,concurrency:1,readConcurrency:4})
    YIELD startNodeId, nodeIds, path RETURN startNodeId, nodeIds, path
    

    随机游走

    【graph-algorithms-algo-3.5.4.0】在ongdb-3.5.17支持高并发计算

  • 加载一个网络
    MATCH (n:HORGShareHold) WHERE n.is_issue_bond=true AND n.hcode='HORG450ff74858cc73e697cc8373dbd517a6' AND n.name='深圳前海联捷商业保理有限公司'
    CALL apoc.path.expand(n,'<持股','HORGShareHold',-1,5) YIELD path RETURN path
    

    对此股权网络中的某节点进行随机游走【不返回虚拟路径】【】

    MATCH (horg:HORGShareHold) WHERE horg.is_issue_bond=true AND horg.hcode='HORG450ff74858cc73e697cc8373dbd517a6' AND horg.name='深圳前海联捷商业保理有限公司' WITH ID(horg) AS horgId
    CALL algo.randomWalk.stream(horgId, 20, 10,{graph:'heavy'
     , nodeQuery:"HORGShareHold"
     , relationshipQuery:"持股"
     , direction:'BOTH'
     , mode:'node2vec'
    , inOut: 0.8, return: 0.2
     , path:false, concurrency:20}) YIELD startNodeId,nodeIds as id
     return startNodeId,id
    

    对此股权网络中的某节点进行随机游走【返回虚拟路径】

    MATCH (horg:HORGShareHold) WHERE horg.is_issue_bond=true AND horg.hcode='HORG450ff74858cc73e697cc8373dbd517a6' AND horg.name='深圳前海联捷商业保理有限公司' WITH ID(horg) AS horgId
    CALL algo.randomWalk.stream(horgId, 20, 10,{graph:'heavy'
     , nodeQuery:"HORGShareHold"
     , relationshipQuery:"持股"
     , direction:'BOTH'
     , mode:'node2vec'
    , inOut: 0.8, return: 0.2
     , path:false, concurrency:20}) YIELD startNodeId,nodeIds as id
     return startNodeId,id
    

    在图上做随机游走算法参数