Search code examples
graphwolfram-mathematica

Coloring Edges in graph according to their vertexes color


  1. I working in mathematica and have a graph that every vertex is in a different color, i want to coloring the edges. for example: if two vertex in the same color, the edge will be in some color, and so on for all the edges with this property.

  2. In general, how can i get the color of vertex?

Thanks!


Solution

  • here is how to generate the graph, assuming you have access to the input:

     g = Select[
             Union@Table[ 
              RandomInteger[{1, 10}] -> RandomInteger[{1, 10}], {35}],
                ! Equal @@ # &];
     colors = Table[ 
        i -> RandomChoice[ {Red, Blue, Green, Orange} ] ,
                 {i, (Union@Flatten[List @@@ g])}];
     Graph[g,
       VertexStyle -> colors,
       EdgeStyle -> ((# -> (#[[1]]/.colors )) & /@ 
         Select[g,(#[[1]]/.colors) == (#[[2]]/.colors ) & ]) ]
    

    enter image description here

    If you only have the graph object and need to extract the style data that will be a bit of a chore.