3
$\begingroup$

I need to take my graph:

g = Graph[{A \[UndirectedEdge] B, B \[UndirectedEdge] C, 
   C \[UndirectedEdge] A, C \[UndirectedEdge] D, 
   A \[UndirectedEdge] D, B \[UndirectedEdge] D}, 
  EdgeWeight -> {14, 46, 49, 48, 38, 16}, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> "Name"]

and Find the sum of the weights along an arbitrary path, for example A->B->C->-D->A I've been trying to use GraphDistance but that only lets me go a single step. Does anyone know how to specify a path instead of just two vertices?

$\endgroup$

1 Answer 1

3
$\begingroup$
Total[GraphDistance[g, #[[1]], #[[2]]] & /@ 
  Partition[{A, B, C, D, A}, 2, 1]]

Partition[{A, B, C, D, A}, 2, 1]

gives the list of individual path segments.

GraphDistance

calculates all the lengths of these individual segments.

Total 

adds them up.

$\endgroup$
1
  • $\begingroup$ Thank you very much, I was able to modify your code for the answers I needed, but I'll need to do some research to figure out what each part is actually doing. $\endgroup$
    – Wombles
    Aug 1, 2020 at 7:52

Your Answer

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

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