{"id":406,"date":"2018-10-30T22:31:22","date_gmt":"2018-10-30T22:31:22","guid":{"rendered":"http:\/\/chwe.net\/michael\/?page_id=406"},"modified":"2018-10-30T22:31:22","modified_gmt":"2018-10-30T22:31:22","slug":"mathematica-code-for-computing-who-revolts-given-thresholds-and-network","status":"publish","type":"page","link":"http:\/\/chwe.net\/michael\/index.php\/mathematica-code-for-computing-who-revolts-given-thresholds-and-network\/","title":{"rendered":"Mathematica code for computing who revolts given thresholds and network"},"content":{"rendered":"<pre>(*\r\nThe inputs to the program are two arrays: neighbors and threshold. \r\n\r\nneighbors[i] is the set of neighbors of person i (people who send \r\ninformation to person i) and threshold[i] is person i's threshold.\r\n\r\nThe function g[set] returns the people who revolt assuming that no \r\none outside the set revolts.  g[set] results from applying the function \r\nf on set repeatedly, eliminating people in succession, until a fixed \r\npoint is reached.\r\n\r\nThe heart of the program is function f.  Given a set, f selects the\r\npeople in the set which satisfy the following criterion: if the set is\r\na subset of the person's neighbors, then the person is selected if the\r\nnumber of people in the set is greater than the person's threshold; if \r\nthe set is not a subset of the person's neighbors, then the person is \r\nselected if the number of people who revolt in the intersection of the set \r\nand the person's neighbors is greater than the person's threshold.\r\n\r\nNote that function f is defined iteratively in that it depends on\r\nfunction g, which in turn depends on function f, etc.  As the code is\r\nwritten, no intermediate values of f and g are \"stored\", which makes\r\nthings slow for larger examples, but it is simple to change this.\r\n\r\nTo get the final result, one simply evaluates g on the set of everyone.\r\n*)\r\n\r\nf[set_] := Select[set, \r\n    If[SubsetQ[neighbors[#], set], Length[set] &gt;= threshold[#], \r\n    Length[g[Intersection[set, neighbors[#]]]] &gt;= threshold[#]] &amp;];\r\n\r\ng[set_] := FixedPoint[f, set];\r\n\r\n\r\n(*kite*)\r\nneighbors[1] = {1, 2, 3};\r\nneighbors[2] = {1, 2, 3};\r\nneighbors[3] = {1, 2, 3};\r\nneighbors[4] = {3, 4};\r\n\r\nthreshold[1] = 3;\r\nthreshold[2] = 3;\r\nthreshold[3] = 3;\r\nthreshold[4] = 3;\r\n\r\ng[{1, 2, 3, 4}]\r\n\r\n{1, 2, 3}\r\n\r\n\r\n(*square*)\r\nneighbors[1] = {1, 2, 4};\r\nneighbors[2] = {1, 2, 3};\r\nneighbors[3] = {2, 3, 4};\r\nneighbors[4] = {3, 4, 1};\r\n\r\nthreshold[1] = 3;\r\nthreshold[2] = 3;\r\nthreshold[3] = 3;\r\nthreshold[4] = 3;\r\n\r\ng[{1, 2, 3, 4}]\r\n\r\n{}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>(* The inputs to the program are two arrays: neighbors and threshold. neighbors[i] is the set of neighbors of person i (people who send information to person i) and threshold[i] is person i&#8217;s threshold. The function g[set] returns the people who revolt assuming that no one outside the set revolts. &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"class_list":["post-406","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/pages\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":1,"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/pages\/406\/revisions"}],"predecessor-version":[{"id":407,"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/pages\/406\/revisions\/407"}],"wp:attachment":[{"href":"http:\/\/chwe.net\/michael\/index.php\/wp-json\/wp\/v2\/media?parent=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}