<< DiscreteMath`Combinatorica`
<< Graphics`Animation`

\(ConnectComp[\ 
          a_Graph\ ]\  := 
\ \ Module[\ {compfile, \ i, \ ab = a, \ cc1, \ 
            cc2, \ oldcolors, t, \ 
            m\ }, 
\ \ \ \ compfile\  = \ {}; 
oldcolors\  = \ {}; 
Do[	
{\(oldcolors\  = \ 
                  Append[\ 
                    oldcolors, \ {m}];\)}
, 
{\ m, \ 1, \ 
              V[a]}
]; 
\ \ \ \ For[\ \(,\)\(\ \)\(! \ 
              ConnectedQ[\ 
                ab\ ]\)\(,\)\(\ \)\(
\)\( (*Updates\ per\ frame\ *) \)\(
\)\(For\ [\ i\  = \ 0, \ 
              i\  != \ 
                5, \ 
\(i++\); 
cc1 = 
                Map[Sort, 
                  ConnectedComponents[ab]]; 
ab\  = \ 
                AddEdge[\ 
\ \ \ \ \ \ \ \ \ \ ab, \ {\ 
                    Random[Integer, \ {1, \ V[ab]}], \ 
                    Random[Integer, \ {1, \ 
                        V[ab]}]}]; \ 
cc2 = 
                Map[Sort, 
                  ConnectedComponents[ab]]; 
delt\  = \ 
                Complement[cc1, cc2]; 
t\  = \ 
                Flatten[
                  Map[\((Position[oldcolors, #])\) &, 
                    delt]]; 
If[\ 
                Length[t]\  != \ 
                  0, 
\(If[
                    Length[\ oldcolors[\([t[\([1]\)]]\)]] > 
                      Length[\ 
                        oldcolors[\([t[\([2]\)]]\)]], 
 (*Add\ the\ second\ one\ to\ the\ first\ one*) 
oldcolors[\([t[\([1]\)]]\)]\  = \ 
                      Sort[Flatten[
                          Append[oldcolors[\([t[\([1]\)]]\)], 
                            oldcolors[\([t[\([2]\)]]\)]]]]; 
oldcolors[\([t[\([2]\)]]\)]\  = \ {};
, 
oldcolors[\([t[\([2]\)]]\)]\  = \ 
                      Sort[Flatten[
                          Append[oldcolors[\([t[\([2]\)]]\)], 
                            oldcolors[\([t[\([1]\)]]\)]]]]; 
oldcolors[\([t[\([1]\)]]\)]\  = \ {};
];\), 
];
]

            
            compfile\  = \ 
\ \ \ \ \ \ \ \ Append[\ 
                compfile, \ 
\ \ \ \ \ \ \ \ \ \ Graphics[{Rectangle[\ {0, 
                        0}, \ {0.8, \ 
                        0.8}, 
ShowGraph[
\ \ \ \ \ \ \ \ \ \ \ \ Highlight[ab, \ 
                          oldcolors, \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ HighlightedVertexColors\  -> \ {Yellow, \ Blue, \ Red, \ Green, \ 
                              Pink, \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Orange}], 
PlotLabel\  -> \ "Evolution of Connected Components", \ TextStyle\  -> \ {FontSize\  -> \ 15, \ 
                            FontColor -> 
                              Purple}, 
Graphics
]], 
Text["www.combinatorica.com", \ {0.5, 0.0}, \ {TextStyle -> {FontColor -> 
                              Red}}]
}
, 
{\ ImageSize -> 350, \ 
                    AspectRatio -> 
                      1}
]]\)]; 
compfile
\ \ \ \ ]\)

ShowGraph[g  =  ChangeEdges[ GridGraph[10, 10],  {}]]

ShowAnimation[ConnectComp[ g ]]