how to create undirected weighted graph with multiple sets of node labels and edge weights in MATLAB
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Each edge of my graphs has multiple sets of weights. For example, 1) the distance between two nodes and 2) the correlation between these two nodes. Each node of my graphs also has multiple sets of features (attributes or labels). For example, 1) color, 2) acidity and so on.
I do not think the build-in function "graph" can do it. Do I then have to write my own code for it? Is there something in MATLAB that corresponds to networkx in python?
0 commentaires
Réponse acceptée
Plus de réponses (1)
Steven Lord
le 11 Juil 2017
You can add custom attributes to the nodes and edges in a graph or digraph object. The attribute Weight is considered "special" by some of the methods of graph or digraph (for example, shortestpath will use the Weight variable of the Edges table if it exists) but you could set that attribute to be the same as one of the other attributes before using a method that respects Weight.
% Make a random digraph using an adjacency matrix
rng default
A = rand(10) < 0.5;
D = digraph(A);
% Give the edges some weights
D.Edges.W1 = randi(10, numedges(D), 1);
D.Edges.W2 = randi(10, numedges(D), 1);
D.Edges.W3 = randi(10, numedges(D), 1);
% Because D.Edges has no Weight variable, shortestpath considers it to be unweighted
noweight = shortestpath(D, 1, 3)
% When we give the edges a Weight attribute, the shortest path changes
% The additional weight on the edges that made up the previous best path (noweight)
% make another route a shorter option
D.Edges.Weight = D.Edges.W3;
withweight = shortestpath(D, 1, 3)
0 commentaires
Voir également
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!