Last active
October 19, 2015 12:59
-
-
Save bbengfort/8a3e3bc0a8794a590699 to your computer and use it in GitHub Desktop.
Counting edges with non-duplicate nodes in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Computes the number of edges on the Graph | |
""" | |
from __future__ import division | |
import networkx as nx | |
from collections import defaultdict | |
def choose(n, k): | |
""" | |
A fast way to calculate binomial coefficients by Andrew Dalke (contrib). | |
""" | |
if 0 <= k <= n: | |
ntok = 1 | |
ktok = 1 | |
for t in xrange(1, min(k, n - k) + 1): | |
ntok *= n | |
ktok *= t | |
n -= 1 | |
return ntok // ktok | |
else: | |
return 0 | |
def count_edges(G): | |
""" | |
Counts the edges with virtual nodes | |
""" | |
edges = 0 | |
for node in nx.nodes_iter(G): | |
data = G.node[node] | |
if data['type'] == 'virtual': | |
edges += choose(nx.degree(G, node) ,2) | |
else: | |
# All neighbors are virtual | |
nbunch = defaultdict(int) | |
for vn in G.neighbors(node): | |
for vnn in G.neighbors(vn): | |
nbunch[vnn] += 1 | |
for nb, freq in nbunch.items(): | |
if nb == node: continue | |
freq -= 1 | |
if freq > 0: | |
edges -= (freq / 2) | |
return edges | |
if __name__ == "__main__": | |
G = nx.read_graphml('graph.graphml') | |
print count_edges(G) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"> | |
<key attr.name="label" attr.type="string" for="node" id="d1" /> | |
<key attr.name="type" attr.type="string" for="node" id="d0" /> | |
<graph edgedefault="undirected"> | |
<node id="A"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="C"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="B"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="E"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="D"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="G"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="F"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="I"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="H"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="K"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="J"> | |
<data key="d0">node</data> | |
<data key="d1">A</data> | |
</node> | |
<node id="V1"> | |
<data key="d0">virtual</data> | |
<data key="d1">V1</data> | |
</node> | |
<node id="V2"> | |
<data key="d0">virtual</data> | |
<data key="d1">V2</data> | |
</node> | |
<node id="V3"> | |
<data key="d0">virtual</data> | |
<data key="d1">V3</data> | |
</node> | |
<node id="V4"> | |
<data key="d0">virtual</data> | |
<data key="d1">V4</data> | |
</node> | |
<edge source="A" target="V1" /> | |
<edge source="A" target="V2" /> | |
<edge source="C" target="V1" /> | |
<edge source="B" target="V2" /> | |
<edge source="E" target="V1" /> | |
<edge source="E" target="V3" /> | |
<edge source="D" target="V1" /> | |
<edge source="D" target="V3" /> | |
<edge source="G" target="V1" /> | |
<edge source="G" target="V3" /> | |
<edge source="G" target="V4" /> | |
<edge source="F" target="V3" /> | |
<edge source="F" target="V4" /> | |
<edge source="I" target="V1" /> | |
<edge source="I" target="V2" /> | |
<edge source="I" target="V4" /> | |
<edge source="H" target="V4" /> | |
<edge source="K" target="V3" /> | |
<edge source="J" target="V2" /> | |
<edge source="J" target="V3" /> | |
</graph> | |
</graphml> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Builds the whiteboard graph | |
""" | |
import networkx as nx | |
NODES = { | |
"V1": {"type": "virtual", "label": "V1"}, | |
"V2": {"type": "virtual", "label": "V2"}, | |
"V3": {"type": "virtual", "label": "V3"}, | |
"V4": {"type": "virtual", "label": "V4"}, | |
"A": {"type": "node", "label": "A"}, | |
"B": {"type": "node", "label": "A"}, | |
"C": {"type": "node", "label": "A"}, | |
"D": {"type": "node", "label": "A"}, | |
"E": {"type": "node", "label": "A"}, | |
"F": {"type": "node", "label": "A"}, | |
"G": {"type": "node", "label": "A"}, | |
"H": {"type": "node", "label": "A"}, | |
"I": {"type": "node", "label": "A"}, | |
"J": {"type": "node", "label": "A"}, | |
"K": {"type": "node", "label": "A"}, | |
} | |
EDGES = [ | |
('A', 'V1'), | |
('A', 'V2'), | |
('B', 'V2'), | |
('C', 'V1'), | |
('D', 'V1'), | |
('D', 'V3'), | |
('E', 'V1'), | |
('E', 'V3'), | |
('K', 'V3'), | |
('F', 'V3'), | |
('F', 'V4'), | |
('G', 'V3'), | |
('G', 'V4'), | |
('G', 'V1'), | |
('H', 'V4'), | |
('I', 'V1'), | |
('I', 'V2'), | |
('I', 'V4'), | |
('J', 'V2'), | |
('J', 'V3'), | |
] | |
if __name__ == "__main__": | |
G = nx.Graph() | |
for key, data in NODES.items(): | |
G.add_node(key, **data) | |
for edge in EDGES: | |
G.add_edge(*edge) | |
nx.write_graphml(G, 'graph.graphml') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment