Skip to content

Instantly share code, notes, and snippets.

@amar-b
Last active February 17, 2020 00:35
Show Gist options
  • Save amar-b/344cdcd59f4f91264ef362dd80e85cae to your computer and use it in GitHub Desktop.
Save amar-b/344cdcd59f4f91264ef362dd80e85cae to your computer and use it in GitHub Desktop.
Matlab script to convert GraphML files to adjacency matrices
function A = graphML2Matrix(path)
% Reads a graphml file and represents graph as adjanency matrix
xml = xmlread(path);
graph = xml.getElementsByTagName('graph');
idMap = containers.Map('KeyType','char','ValueType','uint64');
if (graph.getLength == 1)
V = graph.item(0).getElementsByTagName('node');
n = V.getLength;
A = zeros(n,n);
for i = 1:n
idMap(char(V.item(i-1).getAttribute('id'))) = i;
end
E = graph.item(0).getElementsByTagName('edge');
for i = 1:E.getLength
src = idMap(char(E.item(i-1).getAttribute('source')));
tgt = idMap(char(E.item(i-1).getAttribute('target')));
A(src,tgt) = 1;
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment