       • To: mathgroup at smc.vnet.net
• Subject: [mg36576] RE: [mg36561] creating adjacency matrices
• From: "DrBob" <drbob at bigfoot.com>
• Date: Fri, 13 Sep 2002 01:13:37 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```I'll assume you have the information in a matrix like x:

TableForm[x = {{1, A }, {1, B }, {2, B}, {3, D}, {4, D}, {5, C}}]

First find the highest actor number (or use what you've input
elsewhere):

m = Last[Union[x[[All,1]]]]

5

Define the incidence function:

f[x_, a_, b_] /; a == b := 0
f[x_, a_, b_] :=
If[{} \$B!b(B Intersection[Cases[x, {a, y_} -> y], Cases[x, {b, y_} -> y]],
1, 0]

Here's the incidence matrix:

Array[f[x, #1, #2] &, {m, m}]

{{0, 1, 0, 0, 0}, {1, 0, 0, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {
0, 0, 0, 0, 0}}

Once you have the incidence function, the only reason to store the
incidence matrix is to avoid computing over again the same answers, and
that can be accomplished in other ways.  If you won't have other x
matrices, it's convenient to define f this way:

f[a_, b_] /; a == b := f[a, b] = 0
f[a_, b_] /; a < b := f[a, b] =
If[{} \$B!b(B Intersection[Cases[x, {a, y_} -> y], Cases[x, {b, y_} ->
y]], 1,  0]
f[a_, b_] := f[b, a]

Array[f, {m, m}]

{{0, 1, 0, 0, 0}, {1, 0, 0, 0, 0}, {0, 0, 0, 1, 0}, {0, 0, 1, 0, 0}, {0,
0, 0, 0, 0}}

Whenever you would want the {j,k} element of the adjacency matrix, just
use f[j,k] instead.  Each pair is computed only once.  In this version,
I've taken advantage of the symmetry of the problem, too, to cut the
work in half.

Bobby Treat

-----Original Message-----
From: Moliterno, Thomas [mailto:TMoliter at gsm.uci.edu]
To: mathgroup at smc.vnet.net
Subject: [mg36576] [mg36561] creating adjacency matrices

I need to create an adjacency matrix from my data, which is currently in
the form of a .txt file and is basically a two column incidence list.
For example:

1 A
1 B
2 B
3 C
. .
. .
. .
m n

Where 1 to m represent actors and A to n represent events. My goal is to
have an (m x m) matrix where cell i,j equals 1 if two actors are
incident to the same event (in the sample above, 1 and 2 are both
incident to B) and 0 otherwise (w/ zeros on the diagonal).

I'm new to Mathmatica, and so I'm on the steep part of the learning
curve ... All I've been able to figure out so far is how to get my
incidence list into the program using Import["filename.txt"]. But then
what? How do I convert to the adjacency matrix? I've found the
ToAdjacencyMatrix[] command in DiscreteMath`Combinatorica`, but I can't
seem to get it to work ...

Thanks to any and all in advance.

Tom

**********************************************
Thomas P. Moliterno