Azzera filtri
Azzera filtri

Problem with highlight every edge between two next nodes by its minimum weight.

1 visualizzazione (ultimi 30 giorni)
I created a code where first start node is specified (in this case it's node 18)
I wanted to highlight one outedge which have the lowest weight:
... from node by using function findedge.
Then I remove edge to avoid a situation where outedge from node is the edge in different direction to previous node
tovisit = G.Nodes;
...
min_node1 = e(id,1).EndNodes(1);
min_node2 = e(id,1).EndNodes(2);
s = findedge(G, min_node1, min_node2); %edge in direction where finds shortest path
t = findedge(G, min_node2, min_node1); %edge in different direction
highlight(p,'Edges', s, 'EdgeColor', 'red')
k = min_node1;
tovisit(k,:)
tovisit(k,:) = [];
G = rmedge(G,min_node1,min_node2)
if t %if edge in different direction exists
highlight(p,'Edges', t, 'EdgeColor', 'black')
G = rmedge(G,min_node2,min_node1)
Everything worked fine for edges s and t, but on node 14, edge t in different direction from node 15 to node 14 (min_node2 = 15, min_node1 = 14) isn't highlight and shows different edge from different node:
In the end after few loops I've got an error:
Error using Test1 (line 32)
tovisit(k,:)
Row index exceeds table dimensions.
I have a suspicion that problem is caused by eid (edge id), because few of them have the same eid (both outedges and inedges), but I don't think so.
What should I do to highlight correctly this edge and not get this error?
Of course I don't want use functions like shortestpath.

Risposte (1)

Kartik
Kartik il 17 Apr 2023
Hi,
It's possible that the error you're getting is caused by removing a node from "tovisit" and then attempting to access it again in the next iteration of the loop. When you remove a row from a table, the remaining rows are shifted up to fill the gap, so the index of any subsequent rows changes. To avoid this issue, you can use a different approach to track which nodes you still need to visit.
One approach is to use a boolean vector to indicate whether each node has been visited or not. You can initialize this vector with "false" values for all nodes, and then set the value to "true" for each node that you visit. You can use this vector to determine which nodes still need to be visited, instead of relying on the "tovisit" table.

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by