PyGraphML is a Python library designed to parse GraphML file.
GraphML
is a comprehensive and easy-to-use file format for graphs. It consists of a language core to describe the structural properties of a graph and a flexible extension mechanism to add application-specific data. Its main features include support of:
Unlike many other file formats for graphs, GraphML does not use a custom syntax. Instead, it is based on XML and hence ideally suited as a common denominator for all kinds of services generating, archiving, or processing graphs.
Note: Above description is coming from GraphML official website: http://graphml.graphdrawing.org/.
PyGraphML is a small library designed to parse GraphML files. This library has been written in Python. It's main feature are:
NetworkX
In [1]:
%matplotlib inline
import tempfile
import os
import sys
sys.path.append("../")
from pygraphml import GraphMLParser
from pygraphml import Graph
In [2]:
g = Graph()
n1 = g.add_node("A")
n2 = g.add_node("B")
n3 = g.add_node("C")
n4 = g.add_node("D")
n5 = g.add_node("E")
g.add_edge(n1, n3)
g.add_edge(n2, n3)
g.add_edge(n3, n4)
g.add_edge(n3, n5)
Out[2]:
You can use breadth-first search and depth-first search:
In [3]:
# Set a root
g.set_root(n1)
nodes = g.BFS()
for node in nodes:
print(node)
nodes = g.DFS_prefix()
for node in nodes:
print(node)
In [4]:
g.show()
In [5]:
# Create graph
g = Graph()
n1 = g.add_node("A")
n2 = g.add_node("B")
n3 = g.add_node("C")
n4 = g.add_node("D")
n5 = g.add_node("E")
g.add_edge(n1, n3)
g.add_edge(n2, n3)
g.add_edge(n3, n4)
g.add_edge(n3, n5)
fname = tempfile.mktemp()
parser = GraphMLParser()
parser.write(g, fname)
In [6]:
# Visualize the GraphML file
with open(fname) as f:
print(f.read())
In [7]:
parser = GraphMLParser()
g = parser.parse(fname)
g.show()
In [8]:
g = Graph()
n = g.add_node('label')
# Add attribute
n['color'] = 'red'
# Read attribute
print(n['color'])
All attributes will be copied in GraphML file. As well when you read a GraphML file, attributes are available by the same way.