Message Boards Message Boards

0
|
6026 Views
|
6 Replies
|
2 Total Likes
View groups...
Share
Share this post:

[?] Change colors of specific vertices in graphs?

I want to color only the inside star of this graph red (The edges) and the rest of the edges blue. Every time I try I get the whole thing the same color. This is the code: CompleteGraph[5]

Thanks, Hafez.

POSTED BY: Hafez Rais
6 Replies
Posted 5 years ago

Hey again Marco. Quick question on the same subject. I was wondering if I can make a graph, say for example CompleteGraph[6], such that all the edges are randomly two colored, say red/blue? (Or n-colored in general). Also the same with randomly colored edges and vertices. Thanks, Hafez.

POSTED BY: Updating Name

Thank you so much! That makes sense. I'm really new to Mathematica and some of the syntax are really alien to me. Thanks again! That really helps!

POSTED BY: Hafez Rais

Dear Hafez,

the idea is really simple. EdgeStyle colours an edge - the edge needs to be given by a pair of numbers (the two vertices that are linked).

If we write

CompleteGraph[5, EdgeStyle -> {1 \[UndirectedEdge] 3 -> Red}]

that colours the edge from node 1 to 3:

enter image description here

Next, it would be good to know what the vertex labels/names are (if you do not know it already):

CompleteGraph[5, VertexLabels -> "Name"]

enter image description here

So I want to highlight the edges from 1 to 3; 2 to 4; 3 to 5; 4 to 1; 5 to 2.

It is practically linking each vertex to the one with the index two higher, until I come to vertices 4 and 5, which are linked to 1 and 2 respectively. This is because the situation is modulo 5 (i.e. circular).

So I could write:

CompleteGraph[5, EdgeStyle -> {1 \[UndirectedEdge] 3 -> RGBColor[1, 0, 0], 
 2 \[UndirectedEdge] 4 -> RGBColor[1, 0, 0], 
 3 \[UndirectedEdge] 5 -> RGBColor[1, 0, 0], 
 4 \[UndirectedEdge] 1 -> RGBColor[1, 0, 0], 
 5 \[UndirectedEdge] 2 -> RGBColor[1, 0, 0]}]

and that would do the trick. I am just terribly lazy; so I did not want to type all of that in. The table that I use generates exactly the list that I use for EdgeStyle. Writing the EdgeStyle programatically also has the advantage that this generalises easily:

For example:

CompleteGraph[23, EdgeStyle -> Flatten[Table[k \[UndirectedEdge] Mod[k + 1, 23] + 1 -> Red, {k, 1, 23}]]]

enter image description here

You can also produce graphs where all the edges that correspond different (vertex index) distance are coloured differently:

CompleteGraph[17, EdgeStyle -> Flatten[Table[colour = RandomColor[]; Table[k [UndirectedEdge] Mod[k + j, 17] + 1 -> colour, {k, 1, 17}], {j, 1, 15}]], VertexLabels -> "Name"]

enter image description here

Note that it will "write-over" some distances.... this is just an illustration.

Cheers,

Marco

POSTED BY: Marco Thiel

That is exactly what I wanted. But I don't get the meaning of the syntax after the edge style! And how can I make this with other graphs? I would really appreciate it if you can give me a quick explanation behind the logic. Thanks, Hafez.

POSTED BY: Hafez Rais

Hey Marco. I was wondering if you can help me to figure out how I can make a function that generates complete graphs with edges being 2 colored, but the colorings are random? Thanks, Hafez.

POSTED BY: Hafez Rais

Does this work for you?

CompleteGraph[5, EdgeStyle -> Flatten[Table[k \[UndirectedEdge] Mod[k + 1, 5] + 1 -> Red, {k, 1, 5}]]]

enter image description here

Cheers,

Marco

POSTED BY: Marco Thiel
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract