Can mathematica's GraphDistance handle complex weights? Or unknown parameters eg. 'x'? - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-11-14T23:48:55Z https://mathematica.stackexchange.com/feeds/question/121026 https://creativecommons.org/licenses/by-sa/4.0/rdf https://mathematica.stackexchange.com/q/121026 2 Can mathematica's GraphDistance handle complex weights? Or unknown parameters eg. 'x'? Se314en https://mathematica.stackexchange.com/users/29986 2016-07-19T10:27:18Z 2016-07-19T11:16:12Z <p>The EdgeWeight property states that the EdgeWeight can be any expression and the GraphDistance function states that in a weighted graph, it will return the minimum of the sum of the weights.</p> <p>So if I do:</p> <pre><code>g = Graph[{ob1 \[DirectedEdge] ob2}]; PropertyValue[{g, ob1 \[DirectedEdge] ob2}, EdgeWeight] = 1/4 ; GraphDistance[g, ob1, ob2] </code></pre> <p>I get <code>0.25</code> as expected, but</p> <pre><code>g = Graph[{ob1 \[DirectedEdge] ob2}]; PropertyValue[{g, ob1 \[DirectedEdge] ob2}, EdgeWeight] = I/4 ; GraphDistance[g, ob1, ob2] </code></pre> <p>is returned unevaluated. The same thing happens if I try to use an edge weight containing a variable such as 'x'. </p> https://mathematica.stackexchange.com/questions/121026/-/121028#121028 3 Answer by Se314en for Can mathematica's GraphDistance handle complex weights? Or unknown parameters eg. 'x'? Se314en https://mathematica.stackexchange.com/users/29986 2016-07-19T11:16:12Z 2016-07-19T11:16:12Z <p>As pointed out by @Szabolcs in the comments to the question, I was trying to do something stupid. I figured I should write it up in case anyone else comes across this.</p> <p>GraphDistance tries to find the minimal sum of weights of edges between two points for a weighted graph. Therefore it doesn't make sense to use complex weights, as mathematica will not be able to compare them to find the smallest. The same is obviously true for weights involving an unknown parameter.</p> <p>I really only needed to know whether any such path existed (which I why I didn't think about the comparison problem) and would be better using the FindPath function, which will return a path I can then find the weights for:</p> <pre><code>g = Graph[{ob1 \[DirectedEdge] ob2}]; PropertyValue[{g, ob1 \[DirectedEdge] ob2}, EdgeWeight] = (I u)/4 ; FindPath[g, ob1, ob2]; PropertyValue[{g, ob1 \[DirectedEdge] ob2}, EdgeWeight] </code></pre> <p>returns <code>(I u)/4</code> as required.</p>