Last active
February 17, 2020 00:35
-
-
Save amar-b/344cdcd59f4f91264ef362dd80e85cae to your computer and use it in GitHub Desktop.
Matlab script to convert GraphML files to adjacency matrices
This file contains 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
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