0
$\begingroup$

So I have

graph = Graph[{"Russia" \[DirectedEdge] "Crimea", 
   "Russia" \[DirectedEdge] "Donetsk", 
   "Russia" \[DirectedEdge] "Luhansk", 
   "Russia" \[DirectedEdge] "Kharkiv", 
   "Russia" \[DirectedEdge] "Sumy", 
   "Russia" \[DirectedEdge] "Chernihiv", 
   "Crimea" \[DirectedEdge] "Kherson", 
   "Donetsk" \[DirectedEdge] "Luhansk", 
   "Luhansk" \[DirectedEdge] "Donetsk", 
   "Luhansk" \[DirectedEdge] "Kharkiv", 
   "Kharkiv" \[DirectedEdge] "Luhansk", 
   "Kharkiv" \[DirectedEdge] "Sumy", "Sumy" \[DirectedEdge] "Kharkiv",
    "Sumy" \[DirectedEdge] "Chernihiv", 
   "Chernihiv" \[DirectedEdge] "Sumy", 
   "Donetsk" \[DirectedEdge] "Kharkiv", 
   "Kharkiv" \[DirectedEdge] "Donetsk", 
   "Kherson" \[DirectedEdge] "Zaporizhzhya", 
   "Zaporizhzhya" \[DirectedEdge] "Kherson", 
   "Zaporizhzhya" \[DirectedEdge] "Donetsk", 
   "Donetsk" \[DirectedEdge] "Zaporizhzhya", 
   "Kherson" \[DirectedEdge] "Dnipropetrovsk", 
   "Dnipropetrovsk" \[DirectedEdge] "Kherson", 
   "Dnipropetrovsk" \[DirectedEdge] "Zaporizhzhya", 
   "Zaporizhzhya" \[DirectedEdge] "Dnipropetrovsk", 
   "Dnipropetrovsk" \[DirectedEdge] "Donetsk", 
   "Donetsk" \[DirectedEdge] "Dnipropetrovsk", 
   "Dnipropetrovsk" \[DirectedEdge] "Kharkiv", 
   "Kharkiv" \[DirectedEdge] "Dnipropetrovsk", 
   "Kherson" \[DirectedEdge] "Crimea", 
   "Dnipropetrovsk" \[DirectedEdge] "Poltava", 
   "Poltava" \[DirectedEdge] "Dnipropetrovsk", 
   "Poltava" \[DirectedEdge] "Kharkiv", 
   "Kharkiv" \[DirectedEdge] "Poltava", 
   "Poltava" \[DirectedEdge] "Sumy", "Sumy" \[DirectedEdge] "Poltava",
    "Poltava" \[DirectedEdge] "Chernihiv", 
   "Chernihiv" \[DirectedEdge] "Poltava", 
   "Chernihiv" \[DirectedEdge] "Kiev", 
   "Kiev" \[DirectedEdge] "Chernihiv", 
   "Kiev" \[DirectedEdge] "Cherkassy", 
   "Cherkassy" \[DirectedEdge] "Kiev", 
   "Cherkassy" \[DirectedEdge] "Kirovohrad", 
   "Kirovohrad" \[DirectedEdge] "Cherkassy", 
   "Kiev" \[DirectedEdge] "Poltava", "Poltava" \[DirectedEdge] "Kiev",
    "Poltava" \[DirectedEdge] "Cherkassy", 
   "Cherkassy" \[DirectedEdge] "Poltava", 
   "Kirovohrad" \[DirectedEdge] "Poltava", 
   "Poltava" \[DirectedEdge] "Kirovohrad", 
   "Kirovohrad" \[DirectedEdge] "Dnipropetrovsk", 
   "Dnipropetrovsk" \[DirectedEdge] "Kirovohrad", 
   "Kirovohrad" \[DirectedEdge] "Mykolayiv", 
   "Mykolayiv" \[DirectedEdge] "Kirovohrad", 
   "Mykolayiv" \[DirectedEdge] "Dnipropetrovsk", 
   "Dnipropetrovsk" \[DirectedEdge] "Mykolayiv", 
   "Mykolayiv" \[DirectedEdge] "Kherson", 
   "Kherson" \[DirectedEdge] "Mykolayiv", 
   "Mykolayiv" \[DirectedEdge] "Odessa", 
   "Odessa" \[DirectedEdge] "Mykolayiv", 
   "Odessa" \[DirectedEdge] "Kirovohrad", 
   "Kirovohrad" \[DirectedEdge] "Odessa", 
   "Odessa" \[DirectedEdge] "Vinnytsia", 
   "Vinnytsia" \[DirectedEdge] "Odessa", 
   "Vinnytsia" \[DirectedEdge] "Kirovohrad", 
   "Kirovohrad" \[DirectedEdge] "Vinnytsia", 
   "Vinnytsia" \[DirectedEdge] "Cherkassy", 
   "Cherkassy" \[DirectedEdge] "Vinnytsia", 
   "Vinnytsia" \[DirectedEdge] "Kiev", 
   "Kiev" \[DirectedEdge] "Vinnytsia", 
   "Vinnytsia" \[DirectedEdge] "Zhytomyr", 
   "Zhytomyr" \[DirectedEdge] "Vinnytsia", 
   "Zhytomyr" \[DirectedEdge] "Kiev", 
   "Kiev" \[DirectedEdge] "Zhytomyr", 
   "Vinnytsia" \[DirectedEdge] "Khmelnytsky", 
   "Khmelnytsky" \[DirectedEdge] "Vinnytsia", 
   "Khmelnytsky" \[DirectedEdge] "Zhytomyr", 
   "Zhytomyr" \[DirectedEdge] "Khmelnytsky", 
   "Khmelnytsky" \[DirectedEdge] "Rivne", 
   "Rivne" \[DirectedEdge] "Khmelnytsky", 
   "Rivne" \[DirectedEdge] "Zhytomyr", 
   "Zhytomyr" \[DirectedEdge] "Rivne", 
   "Vinnytsia" \[DirectedEdge] "Chernivtsi", 
   "Chernivtsi" \[DirectedEdge] "Vinnytsia", 
   "Chernivtsi" \[DirectedEdge] "Khmelnytsky", 
   "Khmelnytsky" \[DirectedEdge] "Chernivtsi", 
   "Chernivtsi" \[DirectedEdge] "Ternopil", 
   "Ternopil" \[DirectedEdge] "Chernivtsi", 
   "Ternopil" \[DirectedEdge] "Khmelnytsky", 
   "Khmelnytsky" \[DirectedEdge] "Ternopil", 
   "Ternopil" \[DirectedEdge] "Rivne", 
   "Rivne" \[DirectedEdge] "Ternopil", 
   "Rivne" \[DirectedEdge] "Lutsk", "Lutsk" \[DirectedEdge] "Rivne", 
   "Lutsk" \[DirectedEdge] "Lviv", "Lviv" \[DirectedEdge] "Lutsk", 
   "Lviv" \[DirectedEdge] "Ternopil", 
   "Ternopil" \[DirectedEdge] "Lviv", 
   "Lviv" \[DirectedEdge] "Ivano-Frankivsk", 
   "Ivano-Frankivsk" \[DirectedEdge] "Lviv", 
   "Ivano-Frankivsk" \[DirectedEdge] "Ternopil", 
   "Ternopil" \[DirectedEdge] "Ivano-Frankivsk", 
   "Ivano-Frankivsk" \[DirectedEdge] "Chernivtsi", 
   "Chernivtsi" \[DirectedEdge] "Ivano-Frankivsk", 
   "Ivano-Frankivsk" \[DirectedEdge] "Uzhgorod", 
   "Uzhgorod" \[DirectedEdge] "Ivano-Frankivsk", 
   "Uzhgorod" \[DirectedEdge] "Lviv", 
   "Lviv" \[DirectedEdge] "Uzhgorod"}, VertexLabels -> "Name", 
  ImagePadding -> 10]

Which produces a graph.

Then,

trans = Transpose[Normal[AdjacencyMatrix[graph]]];

Total[Normal[AdjacencyMatrix[graph]]];

Position[Total[Normal[AdjacencyMatrix[graph]]], 0] // Flatten;

input = Table[0, {l, 1, 27}]; (input[[#]] = 1) & /@    Position[Total[Normal[AdjacencyMatrix[graph]]], 0] // Flatten; input

Which gives

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

Then

grr = Graphics[{Red, PointSize[0.05], Point[{0, 0}]}]; grg = Graphics[{Green, PointSize[0.05], Point[{0, 0}]}];

AdjacencyGraph[Transpose[trans], ImageSize -> Medium,   VertexStyle -> Table[i -> If[input[[i]] == 1, Red, Green], {i, 1, 27}]]

Which doesn't give me the graph I want, which should be of Russia with a red light and the rest with green. I just get some more code:

AdjacencyGraph[Automatic, {{0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}}, ImageSize -> Medium,
VertexStyle -> {1 -> RGBColor[1, 0, 0], 2 -> RGBColor[0, 1, 0], 3 -> RGBColor[0, 1, 0], 4 -> RGBColor[0, 1, 0], 5 -> RGBColor[0, 1, 0], 6 -> RGBColor[0, 1, 0], 7 -> RGBColor[0, 1, 0], 8 -> RGBColor[0, 1, 0], 9 -> RGBColor[0, 1, 0], 10 -> RGBColor[0, 1, 0], 11 -> RGBColor[0, 1, 0], 12 -> RGBColor[0, 1, 0], 13 -> RGBColor[0, 1, 0], 14 -> RGBColor[0, 1, 0], 15 -> RGBColor[0, 1, 0], 16 -> RGBColor[0, 1, 0], 17 -> RGBColor[0, 1, 0], 18 -> RGBColor[0, 1, 0], 19 -> RGBColor[0, 1, 0], 20 -> RGBColor[0, 1, 0], 21 -> RGBColor[0, 1, 0], 22 -> RGBColor[0, 1, 0], 23 -> RGBColor[0, 1, 0], 24 -> RGBColor[0, 1, 0], 25 -> RGBColor[0, 1, 0], 26 -> RGBColor[0, 1, 0], 27 -> RGBColor[0, 1, 0]}]

$\endgroup$
2
  • $\begingroup$ Are you doing research on Ukraine? $\endgroup$ Apr 2, 2014 at 22:09
  • $\begingroup$ Yes, I'm trying to create a dynamical model for the onset of war. My main point of focus is on the Ukraine situation. $\endgroup$
    – Jason Born
    Apr 3, 2014 at 18:03

1 Answer 1

5
$\begingroup$

Graph and friends don't handle the case where VertexStyle has more entries than there are vertices. For some reason, you're using 27 as the upper limit of your Tables when defining input and iterating through input when providing VertexStyle, but you only have 26 vertices.

The best way to avoid these kinds of problems is to avoid indexing into graphs at all, by treating lists holistically with functional and rule-based programming. For instance:

styles =
  With[{totals = Total[Normal[AdjacencyMatrix[graph]]]},
   Thread[Range@Length@totals -> (
      totals /. {0 -> Red, n_?Positive -> Green})]];

AdjacencyGraph[Transpose[trans], ImageSize -> Medium, 
 VertexStyle -> styles]

This should do what you want.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.