If we modify the input slightly, by requiring that each person is a member of his own network (i.e. the reflexive closure), then the computation can be more concise:
c=: 0;1 5 7;2 ;3;4 1 2;5 2 7;6 2;7 8;8 5 c ┌─┬─────┬─┬─┬─────┬─────┬───┬───┬───┐ │0│1 5 7│2│3│4 1 2│5 2 7│6 2│7 8│8 5│ └─┴─────┴─┴─┴─────┴─────┴───┴───┴───┘ <@~././ |: (,|."1) (I.#&>c),.;c ┌─┬───────┬───────┬─┬─────┬─────────┬───┬───────┬─────┐ │0│1 5 7 4│2 4 5 6│3│4 1 2│5 2 7 1 8│6 2│7 8 1 5│8 5 7│ └─┴───────┴───────┴─┴─────┴─────────┴───┴───────┴─────┘ On Thu, Sep 12, 2013 at 4:27 PM, Roger Hui <[email protected]>wrote: > s=: '';5 7;'';'';1 2;2 7;2;8;5 > s > ┌┬───┬┬┬───┬───┬─┬─┬─┐ > ││5 7│││1 2│2 7│2│8│5│ > └┴───┴┴┴───┴───┴─┴─┴─┘ > j -.&.>~ </./ |: (,.~j),(,|."1) (;s),.(#&>s)#j=. i.#s > ┌┬─────┬─────┬┬───┬───────┬─┬─────┬───┐ > ││4 5 7│4 5 6││1 2│1 8 2 7│2│1 5 8│7 5│ > └┴─────┴─────┴┴───┴───────┴─┴─────┴───┘ > > (;s),.(#&>s)#j are the relationships in ordered pair form; (,|."1) of that > catenates the reversed ordered pairs; and the rest of the code puts the > information back into the form in which s is specified. > > Repeated application of this processes is the transitive closure (friend > of my friend is also my friend, *ad infinitum*). > > > > On Thu, Sep 12, 2013 at 3:44 PM, Richard Gaylord <[email protected]>wrote: > >> here's some code in the Wolfram Programming Language (the language >> underlying Mathematica) illustring the use of pattern matching. >> >> >> >> Society is a list of the attribute lists of people. As an illustration, we >> will create a society consisting of eight people, each with two >> attributes. >> In each person's attribute list, >> the first element (which is a number) represents the person's name , >> >> the second element which is a list of numbers) is a list of the names of >> the members of the person's social network. >> >> >> society = {{1, {5, 7}}, >> {2, {}}, >> {3, {}}, >> {4, {1, 2}}, >> {5, {2, 7}}, >> {6, {2}}}, >> {7, {8}}, >> {8, {5}}} >> >> We can modify society so that anyone who is in another person's social >> network includes that person in their own social network (i.e., reciprocal >> social connections). >> >> symSoc = society /. >> {a_Integer, b_} :> {a, Union[b, Cases[society, {x_, {___, a, ___}} :> >> x]]} >> >> symSoc = {{1, {4, 5, 7}}, >> {2, {4, 5, 6}}, >> {3, {}}, >> {4, {1, 2}}, >> {5, {1, 2, 7, 8}}, >> {6, {2}}, >> {7, {1, 5, 8}}, >> {8, {5, 7}}} >> >> how would this be done in J? >> >> note: a pdf of the note set on the Wolfram Language in which the >> expressions for society and symSoc appear on p. 42 can be downloaded at >> >> http://library.wolfram.com/infocenter/MathSource/5216/ >> ---------------------------------------------------------------------- >> For information about J forums see http://www.jsoftware.com/forums.htm >> > > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
