How to construct an undirected graph by reading edge by edge from a text file

5 vues (au cours des 30 derniers jours)
TR RAO
TR RAO le 6 Sep 2017
Réponse apportée : TR RAO le 28 Sep 2017
Suppose I have a list of edges in a text file(dont know how many edges are there in a text file). I have to create an undirected graph from it. Finally I have to construct first order transition probability matrix from that graph. for example for the edges={a,b}, {a,c}, {a,d}, the transition matrix is [0 1/3 1/3 1/3;1 0 0 0; 1 0 0 0;1 0 0 0]
  3 commentaires
TR RAO
TR RAO le 6 Sep 2017
Modifié(e) : TR RAO le 6 Sep 2017
Kindly find the text file here. I have to form a graph by reading edges in the text file. And from that graph I have to form a first order transition matrix.
José-Luis
José-Luis le 6 Sep 2017
Modifié(e) : José-Luis le 6 Sep 2017
Are you talking about building tree structures in Matlab?

Connectez-vous pour commenter.

Réponses (2)

Josh Meyer
Josh Meyer le 6 Sep 2017
You can use textscan to read the text file into a cell array, with the first entry listing the source nodes and the second entry listing the target nodes. Then you can create the graph directly from the cell array columns.
fid = fopen('textfile.txt');
C = textscan(fid,'%s%s')
fclose(fid);
G = graph(C{1},C{2})
If the file lists duplicate edges (for an undirected graph a->b and b->a is a duplicate edge, so the example file you provided contains duplicates) you'll need to trim those away first. One method is to convert to a char array, sort the edges, find the unique rows, then convert back to a cell array:
C = cell2mat([C{1} C{2}]);
C = unique(sort(C,2),'rows');
C = num2cell(C)
G = graph(C(:,1),C(:,2))
  2 commentaires
Christine Tobler
Christine Tobler le 6 Sep 2017
I agree with the way you're constructing the graph here, Josh. To get the transition matrix from a graph, use the following code:
T = adjacency(g) ./ degree(g);

Connectez-vous pour commenter.


TR RAO
TR RAO le 28 Sep 2017
clc; clear; fileID = fopen('C:\Users\TR RAO\Desktop\rao1.txt','r'); C = textscan(fileID, '%s %s'); fclose(fileID); C1 =cell2mat(C{1,1}); C2 =cell2mat(C{1,2}); CC = [C1,C2] s=C1;t=C2; nodes = unique(CC); n_nodes = size(nodes,1) S0=eye(n_nodes); adjacency_matrix = false(n_nodes); for idx = 1:size(C1,1) adjacency_matrix(C1(idx)==nodes,C2(idx)==nodes) = 1; end adjacency_matrix n_elements = sum(adjacency_matrix,2); transition_matrix = adjacency_matrix./repmat(n_elements, 1, n_nodes); transition_matrix(isnan(transition_matrix)) = 0; P=transition_matrix;
I am reading a list of edges from a textfile. For small file with six edges it is forming a transition matrix. but for large file of 50000 edges it is giving the following error. Error using cat Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 83) m{n} = cat(1,c{:,n});
Error in readingfile1 (line 14) C1 =cell2mat(C{1,1});

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by