Clear Filters
Clear Filters

how to create undirected weighted graph with multiple sets of node labels and edge weights in MATLAB

6 views (last 30 days)
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?

Accepted Answer

Li Xue
Li Xue on 11 Jul 2017
Hi Steven, many thanks for your prompt reply. This is exactly what I am looking for.

More Answers (1)

Steven Lord
Steven Lord on 11 Jul 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)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!