Skip to content

Instantly share code, notes, and snippets.

View hagberg's full-sized avatar

Aric Hagberg hagberg

View GitHub Profile
import networkx as nx
import sys
G=nx.read_gexf(sys.argv[1])
for u,d in G.nodes(data=True):
print(u,d)
for u,v,d in G.edges(data=True):
print(u,v,d)
print(G.graph)
print "G",type(G)
nx.write_gexf(G,'test.gexf')
@hagberg
hagberg / gist:3751844
Created September 19, 2012 19:53
networkx depth first traversal in sorted order
"""
DFS with children visited in sorted order.
Nodes must be sortable.
Basic algorithms for depth-first searching.
Based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
by D. Eppstein, July 2004.
"""
__author__ = """\n""".join(['Aric Hagberg <hagberg@lanl.gov>'])
@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"/>
@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 / 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 / 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 / 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"
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 / 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
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)