0
$\begingroup$

This question already has an answer here:

Context

Below is just an arbitrary example. It is not perfect, but hopefully it serves the point of what I would like to accomplish.

Say you have a sparse adjacency matrix, $\bf{A}$, where a value of $1$ at position $\{i,j\}$ denotes a directed path from $i$ to $j$, e.g. $i \rightarrow j$. Let's say that the indices $i$ and $j$ represent cities.

Further, suppose you have a set $C$ of nominal edge types, which you simplistically encode numerically e.g. if $C= \{\text{by-car},\text{by-boat},...\}$, then after encoding $C = \{1,2,...\}$.

Thus it is clear that while many edges will be of one type, some might have multiple options, e.g. if we let $i = \text{Chicago}$ and $j = \text{Saint Louis}$, then we could get from $i \rightarrow j$ via car, bus, or by plane.

Question

How could one construct a SparseArray with the values being a list of edge types? e.g.

S = SparseArray[{{1,5}->{2}, {3,7}->{1}, {4,7}->{2,4}]

Note: an unacceptable answer is combintaroically creating a coding scheme for multiple edges e.g. if $|C| = 3$ then the power set $\bf{P}$$(C)$ would be:

$\{\{\emptyset\}, \{1\},\{2\},\{3\},\{1,2\},\{1,3\},\{2,3\},\{1,2,3\}\}$

and thus we could encode $4 \rightarrow \{1,2\}, 5 \rightarrow \{1,3\}, 6 \rightarrow \{2,3\}, 7 \rightarrow \{1,2,3\}$, where it should be clear that $0\rightarrow\{\emptyset\}$. This is unacceptable as the order of the power set, $\bf{P}$$(s)$, given that $s$ has order $n$ is $2^n$, and I am not likely to have a edge for each encoded value.

$\endgroup$

marked as duplicate by Szabolcs, SumNeuron, m_goldberg, happy fish, user31159 Sep 19 '16 at 15:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ WeightedAdjacencyMatrix[]? $\endgroup$ – Feyre Sep 19 '16 at 10:01
  • 2
    $\begingroup$ Don't use lists. Use some other head. E.g., SparseArray[{{1, 5} -> c[2], {3, 7} -> c[1], {4, 7} -> c[2, 4]}]. Most list manipulation operations work on any head, not just List. But SparseArray elements simply cannot be lists. $\endgroup$ – Szabolcs Sep 19 '16 at 12:00
  • 2
    $\begingroup$ You can also consider using bit masks to represent the types and BitAnd for testing. {1,3} would be 2^^101, i.e. 1st and 3rd bits set. Test the kth bit: BitAnd[x, 2^k] == 0 $\endgroup$ – Szabolcs Sep 19 '16 at 12:06
  • $\begingroup$ @Szabolcs Thanks. I didn't see Mr. Wizards previous post. I'll close this question. Also, what resources did you you to become such a Mathematica wizard? $\endgroup$ – SumNeuron Sep 19 '16 at 12:29
  • $\begingroup$ It's not easy to find duplicates ... my questions get closed as duplicates too. Resources? StackExchange :-) Before that, MathGroup. These forums really help. I don't think I can recommend anything else that you don't already know or can't easily find. $\endgroup$ – Szabolcs Sep 19 '16 at 12:37