Filter löschen
Filter löschen

A = [ 1 2 1;1 3 1;1 4 1;2 5 2;3 5 3;4 5 4] ; For above matrix first i need out put like all possible path like 1-2-5 and its total weight (1+2 =3); path 1-3-5 and its total weight (1+3 = 4), Path 1-4-5 and its total weight (1+4 =5);

2 Ansichten (letzte 30 Tage)
Finding Longest Path in Directed Acyclic Graph
  2 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 30 Jul. 2018
Bearbeitet: KALYAN ACHARJYA am 30 Jul. 2018
What are the path represents? Matrix element values?
DHARMAN J
DHARMAN J am 30 Jul. 2018
In each Row, First two element for connection between two nodes and third elements for its weight for that connection

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Magdy Saleh
Magdy Saleh am 30 Jul. 2018
You can try this:
A = [ 1 2 1;1 3 1;1 4 1;2 5 2;3 5 3;4 5 4];
cntr = 1;
path = [];
for i = 1:length(A)
path(i).startNode = A(i, 1);
path(i).path = [A(i, 2)];
path(i).weight = A(i, 3);
end
for i = 1:length(A)
for j = 1:length(A)
if(path(j).path(end) == A(i, 1))
path(j).path = [path(j).path, A(i, 2)];
path(j).weight = path(j).weight + A(i, 2);
end
end
end
But this assumes that your paths are at most of length two. Otherwise you might want to consider a recursive approach.
  11 Kommentare
DHARMAN J
DHARMAN J am 30 Jul. 2018
Hi sir, Thanks for reply. I couldn't implement such algorithms. S only posted here
Guillaume
Guillaume am 30 Jul. 2018
I suggest you make a start at implementing and when you have a specific problem with it, come back and ask for help, showing what you have come up with so far.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Networks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by