1
$\begingroup$

So I tried

SyrianGovernment = {"Al-Safira"};
FSA = {"Atarib"}; 
ISIS = {"Al-Bab","Dayr Hafir","Jarabulus", "Manbij"}; 
YPG = {"Afrin","Ayn Al-Arab"}; 
Contested = {"South Mount Simeon", "North Mount Simeon", "Aleppo", "Azaz"};

vs = Join @@ MapThread[Thread[#1 -> #2] &, {{SyrianGovernment, FSA, ISIS, YPG}, 
                                            {Red, Green, Black, Yellow, Gray}}];
mm = RandomChoice[{0, 1}, {65, 65}];

AdjacencyGraph[mm, VertexSize -> 0.4, VertexStyle -> vs]

I'm sure you can see what I'm trying to do here (I'm trying to colour the vertices or nodes of a Graph). I'm trying to colour my Graph four colours depending on what group they're in. I'd also like to know if I can program it such that nodes which I do not name can be coloured a certain colour as the rest.

This works:

SyrianGovernment = {"Al-Safira", "Al-Nabk", "Qatana", "Al-Qutayfah", "Al-Tall","Yabrud", "Zabadani", "Izra", "Al-Sanamayn"};FSA = {"Atarib", "South Mount Simeon", "Abu Kamal", "Mayadin"};ISIS = {"Al-Bab", "Dayr Hafir", "Jarabulus", "Manbij"};YPG = {"Afrin", "Ayn Al-Arab"};Contested = {"South Mount Simeon", "North Mount Simeon", "Aleppo","Azaz", "Darayya", "Duma", "Qudsaya", "Markaz Rif Dimashq","Daraa", "Deir ez-Zor"};

SeedRandom@5;
list = Flatten[{SyrianGovernment, FSA, ISIS, YPG, Contested}, 1];
vs = Flatten[MapThread[Thread[Rule[#1, #2]] &, {{_, SyrianGovernment, FSA, ISIS,YPG}, {Gray, Red, Green, Black, Yellow}}], 1];
g = Graph[(DirectedEdge @@@ DeleteDuplicates@RandomChoice[list, {65, 2}]), VertexSize -> .5,VertexLabels -> "Name", ImagePadding -> 20, VertexStyle -> vs]

But when I add say "Hama" to SyrianGovernment (or anything more for that matter), the graphic disappears.

So this doesn't work:

SyrianGovernment = {"Al-Safira", "Al-Nabk", "Qatana", "Al-Qutayfah", "Al-Tall","Yabrud", "Zabadani", "Izra", "Al-Sanamayn","Hama"};FSA = {"Atarib", "South Mount Simeon", "Abu Kamal", "Mayadin"};ISIS = {"Al-Bab", "Dayr Hafir", "Jarabulus", "Manbij"};YPG = {"Afrin", "Ayn Al-Arab"};Contested = {"South Mount Simeon", "North Mount Simeon", "Aleppo","Azaz", "Darayya", "Duma", "Qudsaya", "Markaz Rif Dimashq","Daraa", "Deir ez-Zor"};

SeedRandom@5;
list = Flatten[{SyrianGovernment, FSA, ISIS, YPG, Contested}, 1];
vs = Flatten[MapThread[Thread[Rule[#1, #2]] &, {{_, SyrianGovernment, FSA, ISIS,YPG}, {Gray, Red, Green, Black, Yellow}}], 1];
g = Graph[(DirectedEdge @@@ DeleteDuplicates@RandomChoice[list, {65, 2}]), VertexSize -> .5,VertexLabels -> "Name", ImagePadding -> 20, VertexStyle -> vs]
$\endgroup$
0

2 Answers 2

2
$\begingroup$

The first problem was that by doing:

adjg = AdjacencyGraph[RandomChoice[{0, 1}, {20, 20}]];

you have VertexCount@adjg equal to 20 while you only have 12 vertices:

{"Al-Safira", "Atarib", "Al-Bab", "Dayr Hafir", "Jarabulus", "Manbij", "Afrin", 
 "Ayn Al-Arab", "South Mount Simeon", "North Mount Simeon", "Aleppo", "Azaz"}

Thus, if you want a randomly distributed Graph I would propose the following:

SyrianGovernment = {"Al-Safira", "Al-Nabk", "Qatana", "Al-Qutayfah", "Al-Tall", "Yabrud", "Zabadani", "Izra", "Al-Sanamayn", "Hama"}; 
FSA = {"Atarib", "South Mount Simeon", "Abu Kamal", "Mayadin"};
ISIS = {"Al-Bab", "Dayr Hafir", "Jarabulus", "Manbij"};
YPG = {"Afrin", "Ayn Al-Arab"};
Contested = {"South Mount Simeon", "North Mount Simeon", "Aleppo", "Azaz", "Darayya", "Duma", "Qudsaya", "Markaz Rif Dimashq", "Daraa", "Deir ez-Zor"};
SeedRandom@5;
possibleCities = {SyrianGovernment, FSA, ISIS, YPG, Contested};
list = DeleteDuplicates@RandomChoice[Flatten[possibleCities, 1], {65, 2}];
cities = DeleteDuplicates@Flatten@list;
col = (First /@ (First@Position[possibleCities, #] & /@ cities)) /. 
  {1 -> Green, 2 -> Black, 3 -> Yellow, 4 -> Red, _?IntegerQ -> Gray};
vs = Rule @@@ Thread[{cities, col}];
Graph[DirectedEdge @@@ list, VertexSize -> .5, VertexLabels -> "Name",
  ImagePadding -> 20, VertexStyle -> vs]

Mathematica graphics

  • Note that your requirement:

    I'd also like to know if I can program it such that nodes which I do not name can be coloured a certain colour as 'the rest'.

    is fulfilled by _?IntegerQ -> Gray where any integer, _?IntegerQ, is associated with Gray. Since it is placed in the end, it does not overwrite the form Rules.

  • Note also that EdgeCount@g is not equal to 65 because of the DeleteDuplicates in order prevent the following error due the repetitiveness of RandomChoice:

    Graph::supp: Mixed graphs and multigraphs are not supported.

    At last, note that all the cities are not present because of the randomness of RandomChoice.

$\endgroup$
4
  • $\begingroup$ That worked, but once I got to 30 nodes the graphic disappears. Help? $\endgroup$
    – Jason Born
    Commented May 15, 2014 at 2:35
  • $\begingroup$ @user3482534 did you add colours accordingly with nodes? You can edit your question with your 30 nodes if you want. $\endgroup$
    – Öskå
    Commented May 15, 2014 at 9:56
  • $\begingroup$ I edited as you asked. $\endgroup$
    – Jason Born
    Commented May 15, 2014 at 11:51
  • $\begingroup$ @user3482534 Should work like a charm now :) $\endgroup$
    – Öskå
    Commented May 15, 2014 at 16:49
1
$\begingroup$

You are associating 5 colors to 4 lists in vs (you forgot Contested), and I don't understand the use of Join. Try with this code: it works fine for me:

SyrianGovernment = {"Al-Safira"};
FSA = {"Atarib"};
ISIS = {"Al-Bab", "Dayr Hafir", "Jarabulus", "Manbij"};
YPG = {"Afrin", "Ayn Al-Arab"};
Contested = {"South Mount Simeon", "North Mount Simeon", "Aleppo", 
   "Azaz"};
vs = MapThread[
   Thread[#1 -> #2] &, {{SyrianGovernment, FSA, ISIS, YPG, 
     Contested}, {Red, Green, Black, Yellow, Gray}}];
mm = RandomChoice[{0, 1}, {65, 65}];
AdjacencyGraph[mm, VertexSize -> 0.4, VertexStyle -> vs]

Hope it helps.

$\endgroup$

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.