# Color-code edges by order in edge list

Given a graph with vertex coordinates list V and edge list E, how can I visualize the order of appearance of edges in E? It is similar to this and this question which ask how to color-code points in a point plot by color. It does not matter to me if the output is a graph, plot, etc. as long as the edge ordering is encoded somehow.

ClearAll[edgeColorList, legends]
edgeColorList[cf_ : Automatic][g_Graph] :=
Switch[cf,
Automatic, ColorData[97] /@ Range[EdgeCount[g]],
_String, ColorData[{cf, {1, EdgeCount@g}}] /@ Range[EdgeCount[g]],
_Integer, ColorData[cf] /@ Range[EdgeCount[g]],
_, cf];

legends[cf_ : Automatic][g_Graph] := Grid[Transpose @
Prepend[{ "index  ", "edge  ", "color  "}]@
Transpose[{EdgeIndex[g, #] & /@ EdgeList[g], EdgeList[g],
Graphics[{#, Disk[{0, 0}, Offset[10]]}, ImageSize -> 20] & /@
edgeColorList[cf][g]}],
Spacings -> 1];


Examples:

SeedRandom[1];
rg = RandomGraph[BernoulliGraphDistribution[9, 0.4]]


Use default colors (ColorData[97]):

SetProperty[rg, {EdgeLabels -> Placed["Index", .4],
EdgeStyle -> {e_ :> Directive[AbsoluteThickness[5], Opacity[1],
edgeColorList[][rg][[EdgeIndex[rg, e]]]]},
PlotLabel -> legends[][rg], ImageSize -> Large,
VertexLabels -> Placed["Name", Center], VertexSize -> Medium}]


Shuffle edges of rg:

SeedRandom[1];
rg2 = Graph @ RandomSample @ EdgeList @ rg;

SetProperty[#, {EdgeLabels -> Placed["Index", .4],
EdgeStyle -> {e_ :> Directive[AbsoluteThickness[5], Opacity[1],
edgeColorList[][#][[EdgeIndex[#, e]]]]},
PlotLabel -> legends[][#], ImageSize -> Large,
VertexLabels -> Placed["Name", Center], VertexSize -> Medium}] & @ rg2


Use ColorData["Rainbow"]:

SetProperty[#, {EdgeLabels -> Placed["Index", .4],
EdgeStyle -> {e_ :> Directive[AbsoluteThickness[5], Opacity[1],
edgeColorList["Rainbow"][#][[EdgeIndex[#, e]]]]},
PlotLabel -> legends["Rainbow"][#], ImageSize -> Large,
VertexLabels -> Placed["Name", Center], VertexSize -> Medium}] & @ rg2


Use a list of colors to specify the edge styles:

SeedRandom[1];
colors = RandomColor[EdgeCount @ rg];

SetProperty[#, {EdgeLabels -> Placed["Index", .4],
EdgeStyle -> {e_ :> Directive[AbsoluteThickness[5], Opacity[1],
edgeColorList[colors][#][[EdgeIndex[#, e]]]]},
PlotLabel -> legends[colors][#], ImageSize -> Large,
VertexLabels -> Placed["Name", Center], VertexSize -> Medium}] & @ rg2


Note: In versions 12.0+, you can replace SetProperty above with Annotate.

Alternatively, if you start with a list of edges and colors, you can use

edges = EdgeList[rg2];

{edges, Directive[AbsoluteThickness[5], Opacity[1], #]& /@ colors}],
EdgeLabels -> Placed["Index", .4],
VertexLabels -> Placed["Name", Center],
VertexSize -> Medium,
ImageSize -> Large]


same picture

You can replace Style with Property[#, EdgeStyle -> #2]& to get the same picture.

As I understand, you want to colour each edge based on its position in the edge list. With IGraph/M,

Needs["IGraphM"]

g = RandomGraph[{10, 20}]

Graph[g, GraphStyle -> "ThickEdge"] //
IGEdgeMap[ColorData["Rainbow"], EdgeStyle -> Rescale@*Range@*EdgeCount]
`