3
$\begingroup$

Is there a convenient way to build a table as generated by the code below where a 5th column (at the far right) could be added to the table that is the sum of the edge weights for the nodes in a given row. Then also be able to sort that table from smallest edge weight sum to largest edge weight sum?

Clear[edges,g]
edges = {N1 -> N2, N1 -> N3, N1 -> N4,
         N2 -> N5, N3 -> N5, N3 -> N6,
         N4 -> N6, N5 -> N7, N6 -> N7};

g = Graph[
      edges, 
      VertexLabels -> "Name", 
      EdgeWeight -> {1, 2, 3, 4, 5, 6, 7, 8, 9}, 
      EdgeLabels -> {"EdgeWeight"},
      EdgeLabelStyle -> Directive[Red, 20]
    ]

WeightedAdjacencyMatrix[g] // MatrixForm

TableForm[FindPath[g, N1, N7, Infinity, All]]

table with Ni

I'm looking for the sum of the edge weights of each row in the above table. For example, the last row would be generated as N1 N2 N5 N7 13, where 13 is the sum of the edge weights 1 (N1 -> N2) + 4 (N2 -> N5) + 8 (N5 -> N7) = 13. So, 13 would be the 5th column computed for each row in the above table generated by the last line of code in the above.

$\endgroup$
2
  • $\begingroup$ I'm looking for the sum of the edge weights of each row in the above table. For example, the last row would be generated as N1 N2 N5 N7 13, where 13 is the sum of the edge weights 1 (N1->N2) + 4 (N2->N5) + 8 (N5->N7) = 13. So, 13 would be the 5th column computed for each row in the above table generated by the last line of code in the above. $\endgroup$
    – user42700
    Jan 13, 2021 at 1:17
  • 2
    $\begingroup$ You comment should be part of the question. $\endgroup$ Jan 13, 2021 at 2:11

1 Answer 1

6
$\begingroup$
paths = FindPath[g, N1, N7, Infinity, All]; 

totalweights = Total[PropertyValue[{g, DirectedEdge @@ #}, EdgeWeight] & /@ 
     Partition[#, 2, 1]] & /@ paths;

Join[paths, List /@ totalweights, 2] // MatrixForm

enter image description here

$\endgroup$
3
  • $\begingroup$ kglr ... I wrote the above clarification $\endgroup$
    – user42700
    Jan 13, 2021 at 1:19
  • $\begingroup$ @PRG, please see the update. $\endgroup$
    – kglr
    Jan 13, 2021 at 2:33
  • $\begingroup$ kglr ... superbly done ... thank u very much! ... prg $\endgroup$
    – user42700
    Jan 13, 2021 at 3:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.