-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_graph_algo.py
64 lines (51 loc) · 1.71 KB
/
use_graph_algo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from pyclass_infoflow import *
import os
import sqlite3
import time
from neo4j.v1 import GraphDatabase
#Configurations:
#dev = "HUAWEI_P20"
user="neo4j"
password="123"
#
def run_betweenness_centrality(tx):
label = "label" #all nodes were labeled as "label"
target_relationship = "path" #or "channel","allow"
cql = 'CALL algo.betweenness.stream(\'%s\',\'%s\',{direction:"both"}) \
YIELD nodeId, centrality \
MATCH (node:%s) WHERE id(node) = nodeId \
RETURN node.name AS node,centrality \
ORDER BY centrality DESC;'%(label,target_relationship,label)
return tx.run(cql)
def run_KshortestPath(session,src,dst):
#all paths' cost in default are set to 1.
path_num = 3 #the first path_num of shortestpaths will be returned
cql1 = "MATCH (start:label{name:\'%s\'}), (end:label{name:\'%s\'}) \
CALL algo.kShortestPaths(start, end, 3, 'cost' ,{}) \
YIELD resultCount \
RETURN resultCount" % (src,dst)
print cql1
result_paths = []
for i in range(path_num):
cql2 = 'MATCH p=()-[r:PATH_%s]->() RETURN p '%str(i)
result_paths.append(session.run(cql2))
cql3 = 'MATCH p=()-[r:PATH_%s]->() delete r'%str(i)
session.run(cql3)
#print result_paths
return result_paths
if __name__ == '__main__':
driver = GraphDatabase.driver("bolt://localhost:7687",auth = (user,password))
with driver.session() as session:
tx = session.begin_transaction()
#Call the algo to get result:
'''
#1.betweenness_centrality algorithm
#result = run_betweenness_centrality(tx) #path centrality
for record in result :
print record
'''
#2.Kshortestpath algorithm
result_list = run_KshortestPath(session,"isolated_app","system_server")
for i in result_list:
for record in i :
print record[0]._nodes[0].graph