Skip to main content
deleted 54 characters in body
Source Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323
G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

If "quick" was meant as "quick at runtime", then you should better avoid Graph and use SparseArray:

componentEdges[edges_] := Module[{B},
   B = Transpose[With[{m = Length[edges], n = Max[edges]},
      (* this builds a sparse array directly from row pointers, column indices, and nonzero values; undocumentd *)
      SparseArray @@ {Automatic, {m, n}, 0, {1, {
          Range[0, 2 m, 2],
          Partition[Flatten[edges], 1]
          },
         ConstantArray[1, 2 m]}}
      ]];
   Select[
    Table[
     edges[[
      Flatten[edges[[Flatten[(SparseArray[Partition[c, 1] -> 1, Length[B]].B)["NonzeroPositions"]]]],
     {c, SparseArray`StronglyConnectedComponents[(# - 
           DiagonalMatrix[Diagonal[#]]) &[BSparseArray`StronglyConnectedComponents[B.B\[Transpose]]]B\[Transpose]]}],
    Length[#] > 0 &]
   ];

Just for comparison, the Graph-based approach:

componentEdges0[edges_] := With[{G = Graph[edges]},
   Select[
    List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
    Length[#] > 0 &]
   ];

And here a usage example along with timings:

edges = Developer`ToPackedArray[List @@@ EdgeList[RandomGraph[{10000, 60000}]]];
a = componentEdges0[edges]; // RepeatedTiming // First
b = componentEdges[edges]; // RepeatedTiming // First
Sort[Sort /@ a] == Sort[Sort /@ b]

0.13

0.00980084

True

G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

If "quick" was meant as "quick at runtime", then you should better avoid Graph and use SparseArray:

componentEdges[edges_] := Module[{B},
   B = Transpose[With[{m = Length[edges], n = Max[edges]},
      (* this builds a sparse array directly from row pointers, column indices, and nonzero values; undocumentd *)
      SparseArray @@ {Automatic, {m, n}, 0, {1, {
          Range[0, 2 m, 2],
          Partition[Flatten[edges], 1]
          },
         ConstantArray[1, 2 m]}}
      ]];
   Select[
    Table[
     edges[[
      Flatten[(SparseArray[Partition[c, 1] -> 1, Length[B]].B)["NonzeroPositions"]]]],
     {c, SparseArray`StronglyConnectedComponents[(# - 
           DiagonalMatrix[Diagonal[#]]) &[B.B\[Transpose]]]}],
    Length[#] > 0 &]
   ];

Just for comparison, the Graph-based approach:

componentEdges0[edges_] := With[{G = Graph[edges]},
   Select[
    List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
    Length[#] > 0 &]
   ];

And here a usage example along with timings:

edges = Developer`ToPackedArray[List @@@ EdgeList[RandomGraph[{10000, 60000}]]];
a = componentEdges0[edges]; // RepeatedTiming // First
b = componentEdges[edges]; // RepeatedTiming // First
Sort[Sort /@ a] == Sort[Sort /@ b]

0.13

0.0098

True

G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

If "quick" was meant as "quick at runtime", then you should better avoid Graph and use SparseArray:

componentEdges[edges_] := Module[{B},
   B = Transpose[With[{m = Length[edges], n = Max[edges]},
      (* this builds a sparse array directly from row pointers, column indices, and nonzero values; undocumentd *)
      SparseArray @@ {Automatic, {m, n}, 0, {1, {
          Range[0, 2 m, 2],
          Partition[Flatten[edges], 1]
          },
         ConstantArray[1, 2 m]}}
      ]];
   Select[
    Table[
     edges[[Flatten[(SparseArray[Partition[c, 1] -> 1, Length[B]].B)["NonzeroPositions"]]]],
     {c, SparseArray`StronglyConnectedComponents[B.B\[Transpose]]}],
    Length[#] > 0 &]
   ];

Just for comparison, the Graph-based approach:

componentEdges0[edges_] := With[{G = Graph[edges]},
   Select[
    List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
    Length[#] > 0 &]
   ];

And here a usage example along with timings:

edges = Developer`ToPackedArray[List @@@ EdgeList[RandomGraph[{10000, 60000}]]];
a = componentEdges0[edges]; // RepeatedTiming // First
b = componentEdges[edges]; // RepeatedTiming // First
Sort[Sort /@ a] == Sort[Sort /@ b]

0.13

0.0084

True

added 1310 characters in body
Source Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323
G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

If "quick" was meant as "quick at runtime", then you should better avoid Graph and use SparseArray:

componentEdges[edges_] := Module[{B},
   B = Transpose[With[{m = Length[edges], n = Max[edges]},
      (* this builds a sparse array directly from row pointers, column indices, and nonzero values; undocumentd *)
      SparseArray @@ {Automatic, {m, n}, 0, {1, {
          Range[0, 2 m, 2],
          Partition[Flatten[edges], 1]
          },
         ConstantArray[1, 2 m]}}
      ]];
   Select[
    Table[
     edges[[
      Flatten[(SparseArray[Partition[c, 1] -> 1, Length[B]].B)["NonzeroPositions"]]]],
     {c, SparseArray`StronglyConnectedComponents[(# - 
           DiagonalMatrix[Diagonal[#]]) &[B.B\[Transpose]]]}],
    Length[#] > 0 &]
   ];

Just for comparison, the Graph-based approach:

componentEdges0[edges_] := With[{G = Graph[edges]},
   Select[
    List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
    Length[#] > 0 &]
   ];

And here a usage example along with timings:

edges = Developer`ToPackedArray[List @@@ EdgeList[RandomGraph[{10000, 60000}]]];
a = componentEdges0[edges]; // RepeatedTiming // First
b = componentEdges[edges]; // RepeatedTiming // First
Sort[Sort /@ a] == Sort[Sort /@ b]

0.13

0.0098

True

G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

If "quick" was meant as "quick at runtime", then you should better avoid Graph and use SparseArray:

componentEdges[edges_] := Module[{B},
   B = Transpose[With[{m = Length[edges], n = Max[edges]},
      (* this builds a sparse array directly from row pointers, column indices, and nonzero values; undocumentd *)
      SparseArray @@ {Automatic, {m, n}, 0, {1, {
          Range[0, 2 m, 2],
          Partition[Flatten[edges], 1]
          },
         ConstantArray[1, 2 m]}}
      ]];
   Select[
    Table[
     edges[[
      Flatten[(SparseArray[Partition[c, 1] -> 1, Length[B]].B)["NonzeroPositions"]]]],
     {c, SparseArray`StronglyConnectedComponents[(# - 
           DiagonalMatrix[Diagonal[#]]) &[B.B\[Transpose]]]}],
    Length[#] > 0 &]
   ];

Just for comparison, the Graph-based approach:

componentEdges0[edges_] := With[{G = Graph[edges]},
   Select[
    List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
    Length[#] > 0 &]
   ];

And here a usage example along with timings:

edges = Developer`ToPackedArray[List @@@ EdgeList[RandomGraph[{10000, 60000}]]];
a = componentEdges0[edges]; // RepeatedTiming // First
b = componentEdges[edges]; // RepeatedTiming // First
Sort[Sort /@ a] == Sort[Sort /@ b]

0.13

0.0098

True

added 13 characters in body
Source Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323
G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
DeleteCases[Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], {}
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
DeleteCases[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], {}
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

G = Graph[{{1, 2}, {1, 3}, {2, 5}, {1, 5}, {6, 7}, {6, 9}, {4, 6}}];
Select[
 List @@@ EdgeList[Subgraph[G, #]] & /@ ConnectedComponents[G], 
 Length[#]>0 &
 ]

{{{4, 6}, {6, 7}, {6, 9}}, {{1, 2}, {1, 3}, {2, 5}, {1, 5}}}

added 477 characters in body
Source Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323
Loading
Source Link
Henrik Schumacher
  • 109.5k
  • 7
  • 186
  • 323
Loading