# 基于社区发现算法和图分析Neo4j解读《权力的游戏》上篇

Source,Target,Weight
Aemon,Grenn,5
Aemon,Samwell,31
Aerys,Jaime,18 ...

(:Character {name})-[:INTERACTS]->(:Character {name})

CREATE CONSTRAINT ON (c:Character) ASSERT c.name IS UNIQUE;

LOAD CSV WITH HEADERS FROM "https://www.macalester.edu/~abeverid/data/stormofswords.csv" AS rowMERGE (src:Character {name: row.Source})MERGE (tgt:Character {name: row.Target})MERGE (src)-[r:INTERACTS]->(tgt)SET r.weight = toInt(row.Weight)

CALL apoc.meta.graph

MATCH p=(:Character)-[:INTERACTS]-(:Character) RETURN p

MATCH (c:Character) RETURN count(c)

count(c)

107

`MATCH (c:Character)-[:INTERACTS]->() WITH c, count(*) AS num RETURN min(num) AS min, max(num) AS max, avg(num) AS avg_characters, stdev(num) AS stdev`

// Find maximum diameter of network // maximum shortest path between two nodes
`MATCH (a:Character), (b:Character) WHERE id(a) > id(b) MATCH p=shortestPath((a)-[:INTERACTS*]-(b)) RETURN length(p) AS len, extract(x IN nodes(p) | x.name) AS path ORDER BY len DESC LIMIT 4`

// Shortest path from Catelyn Stark to Khal Drogo
`MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}) MATCH p=shortestPath((catelyn)-[INTERACTS*]-(drogo)) RETURN p`

// All shortest paths from Catelyn Stark to Khal Drogo
`MATCH (catelyn:Character {name: "Catelyn"}), (drogo:Character {name: "Drogo"}) MATCH p=allShortestPaths((catelyn)-[INTERACTS*]-(drogo)) RETURN p`

// Find all pivotal nodes in network
`MATCH (a:Character), (b:Character) MATCH p=allShortestPaths((a)-[:INTERACTS*]-(b)) WITH collect(p) AS paths, a, b MATCH (c:Character) WHERE all(x IN paths WHERE c IN nodes(x)) AND NOT c IN [a,b] RETURN a.name, b.name, c.name AS PivotalNode SKIP 490 LIMIT 10`

MATCH (a:Character {name: "Drogo"}), (b:Character {name: "Ramsay"}) MATCH p=allShortestPaths((a)-[:INTERACTS*]-(b)) RETURN p