1
$\begingroup$

This is a self-contained version of something a bit bigger - going for brevity. It is a bit synthetic, but there are applications I have in mind where the {{1,2}} will be replaced with real data, more of it, and for a larger network.

g = CompleteGraph[4];

Scan[(PropertyValue[{g, #[[1]] <-> #[[2]] &}, EdgeWeight] = #[[1]] + #[[2]] &), {{1, 2}}]

The argument {{1,2}} passed at the end is a subset of what I want to Scan over (I expect that list of pairs to be large).

The Scan appears to work, but when I read the EdgeWeight value for node 1<->2 afterwards, thus:

PropertyValue[{g, 1 <-> 2}, EdgeWeight]

I get $Failed

If I do manually what I am trying to Scan, and then read it, it works

PropertyValue[{g, 1 <-> 2}, EdgeWeight] = 3;
PropertyValue[{g, 1 <-> 2}, EdgeWeight]

(returns 3).

Clearly there's something wrong with my Scan, but without an error message I can't tell what that is.

Edit I am specifically looking to understand why my expression does not work. An edited working expression without indication of why my expression is flawed isn't instructive to me. For example, if my nested list of data at the end is shaped differently, ## is not going to work automatically. Yes - I can live with an answer that says "that will never work, here's why", but I am mostly looking for explanations - not just something that works.

I'd appreciate some pointers on Scan; if there's a better way to do it I'd like to know what that is.

$\endgroup$
1
  • 1
    $\begingroup$ Builtin properties like EdgeWeight have easier ways to set. You can either SetProperty[g, EdgeWeight -> {1,2,3}] (for a graph with three edges), or explicitly assign to edges like SetProperty[g, EdgeWeight -> {1<->2 -> 1, 2<->3 -> 2, 1<->3 -> 3}]. This only works for builtin properties. $\endgroup$
    – Szabolcs
    Oct 14, 2015 at 7:34

1 Answer 1

2
$\begingroup$
g = CompleteGraph[4]; 
Scan[(PropertyValue[{g, UndirectedEdge @@ ##}, EdgeWeight] = Tr@##) &, {{1, 2}}]
PropertyValue[{g, 1 <-> 2}, EdgeWeight]
(* 3 *)
$\endgroup$
7
  • $\begingroup$ Check your syntax :) $\endgroup$ Oct 14, 2015 at 4:16
  • $\begingroup$ Thanks for the answer - looking for some input on why mine is bad as well. The Scan expression doesn't evaluate. Double brace after ## should be single brace, then it works $\endgroup$
    – Paul_A
    Oct 14, 2015 at 5:34
  • $\begingroup$ @Paul_A Thanks. Something went bad in the copy/paste. Yours have a misplaced ) and an spurious & $\endgroup$ Oct 14, 2015 at 11:17
  • $\begingroup$ Scan[(PropertyValue[{g, #[[1]] <-> #[[2]]}, EdgeWeight] = #[[1]] + #[[2]]) &, {{1, 2}}] $\endgroup$ Oct 14, 2015 at 11:17
  • $\begingroup$ thanks for the always-helpful identification of the error - I'm still puzzled that if the expression is invalid MMA swallows it silently - maybe it's not invalid but is just asking MMA to do something other than what I want (if any readers know what was wrong with the Scan in the original question I'd appreciate the pointers) $\endgroup$
    – Paul_A
    Oct 14, 2015 at 13:06

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.