Skip to content

Instantly share code, notes, and snippets.

View hagberg's full-sized avatar

Aric Hagberg hagberg

View GitHub Profile
@hagberg
hagberg / ba.py
Created April 30, 2017 23:22
BA algorithm
from networkx.utils import random_weighted_sample
def barabasi_albert_graph_rwc(n, m, seed=None):
if m < 1 or m >=n:
raise nx.NetworkXError("Barabási–Albert network must have m >= 1"
" and m < n, m = %d, n = %d" % (m, n))
if seed is not None:
random.seed(seed)
# Add m initial nodes (m0 in barabasi-speak)
def from_biadjacency_matrix(A,create_using=None):
import numpy as np
kind_to_python_type={'f':float,
'i':int,
'u':int,
'b':bool,
'c':complex,
'S':str,
'V':'void'}
try: # Python 3.x
from networkx import Graph
from networkx.exception import NetworkXException, NetworkXError
import networkx.convert as convert
class Tree(Graph):
""" A free (unrooted) tree."""
def __init__(self, data=None, **attr):
Graph.__init__(self, **attr)
if data is not None:
convert.to_networkx_graph(data, create_using=self)
@hagberg
hagberg / wrom.py
Created December 15, 2013 22:22
generate non-isomorphic free trees or rooted trees
#!/usr/bin/env python
"""Implementation of the Wright, Richmond, Odlyzko and McKay (WROM)
algorithm for the enumeration of all non-isomorphic free trees of a
given order. Rooted trees are represented by level sequences, i.e.,
lists in which the i-th element specifies the distance of vertex i to
the root."""
import sys
import networkx as nx
print nx.__version__
g = nx.gnp_random_graph(1000,0.01)
nx.write_gml(g, 'foo.gml')
for i in range(10000):
g = nx.read_gml('foo.gml')
print g.number_of_nodes(), g.number_of_edges()
@hagberg
hagberg / sample1.gml
Created October 7, 2013 23:10
sample gml
graph [
node [
id 13595
label 164966
bytesInAsClient 0
bytesOutAsServer 0
ip "10.0.0.1"
start_time "2013-09-08T13:24:28Z"
Class "Device"
coid "11111"
@hagberg
hagberg / gist:4591198
Last active December 11, 2015 11:08 — forked from jtorrents/gist:4586874
import time
import networkx as nx
from contextlib import contextmanager
@contextmanager
def hidden_nodes(G, nodes=None):
import types
if nodes is None:
nodes = []
def successors_iter(G,n):
@hagberg
hagberg / restricted_paths.py
Created January 18, 2013 17:45
restricted paths
import networkx as nx
from contextlib import contextmanager
@contextmanager
def hidden(G, nodes=None, edges=None):
if nodes is None:
nodes = []
if edges is None:
edges = []
hidden_nodes = [(u,G.node[u]) for u in nodes]
@hagberg
hagberg / results.log
Created January 6, 2013 15:16
NetworkX test log
NetworkX does not appear to be in the venv: /home/aric/Software/hagberg-networkx/networkx/__init__.pyc
Do you use setupegg.py develop?
@hagberg
hagberg / gist:3969750
Created October 28, 2012 20:20
modified GraphML
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:y="http://www.yworks.com/xml/graphml" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://www.yworks.com/xml/schema/graphml/1.1/ygraphml.xsd">
<VersionInfo createdBy="Maltego Radium" subtitle="" version="3.2.1.3453"/>
<key for="graphml" id="d0" yfiles.type="resources"/>
<key for="port" id="d1" yfiles.type="portgraphics"/>
<key for="port" id="d2" yfiles.type="portgeometry"/>
<key for="port" id="d3" yfiles.type="portuserdata"/>
<key attr.name="MaltegoEntity" for="node" id="d4" attr.type="string"/>
<key for="node" id="d5" yfiles.type="nodegraphics"/>
<key attr.name="MaltegoLink" for="edge" id="d6" attr.type="string"/>