Adding weights to a graph

Hi All,
I have the following graph.
NNode = 11;
tail = 1:NNode-1;
head = 2:NNode;
Graph = graph(tail,head);
Graph.Edges.Weight1 = randn(height(Graph.Edges),1)
Graph.Edges.Weight2 = randn(height(Graph.Edges),1)
H = Graph
H = addedge(H,1,2,Graph.Edges.Weight1(1))
H = addedge(H,3,2,Graph.Edges.Weight1(1))
plot(Graph);
When I try to add new nodes, edges and assign weights, the following error is obtained
Error using graph/addedge (line 139)
Unable to add weighted edges to an unweighted graph.
Error in Untitled (line 9)
H = addedge(H,1,2,Graph.Edges.Weight1(1))
In line 9, a weighted graph is assigned to a variable H. I am adding new edges and weights to H. I am not sure why
Unable to add weighted edges to an unweighted graph.
error appears.
Could someone look into this?

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Sept. de 2019

0 votos

Graph = graph(tail,head);
Graph creates an undirected graph without weights, using a slightly different data structure than a graph that has weights. You need to either create the graph with weights, graph(tail,head,appropriate_weights) or else you need to assign to the Weight property of the graph, such as Graph.Weight = randn(10,1) . Once the Weight structure is there, then you can use addedge() to add edges with weights.

Categorías

Más información sobre Graph and Network Algorithms en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 8 de Sept. de 2019

Respondida:

el 8 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by