Skip to main content
added 245 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Update: a greedy approach:

am = WeightedAdjacencyMatrix[g1];
DeleteDuplicates[am["NonzeroPositions"][[Ordering[-am["NonzeroValues"]]]][[;; ;; 2]], 
  IntersectingQ]

{{4, 5}, {1, 2}, {6, 8}}

Original answer:

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
  constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
      Thread[0 <= # <= 1] & /@ Transpose[dm]];
  constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
  constraints3 = {Total[dm, 2] == #2};
  constraints4 = DeleteCases[Thread[Join@@dm ==Join@@(dm  Unitize[am])], True];
  objective = Total[dm WeightedAdjacencyMatrix[#], 2];
  Maximize[{objective, ## & @@ 
   Join[constraints1, constraints2, constraints3, constraints4]}, Join @@ dm, Integers]]&;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

 HighlightGraph[g1, Style[f[g1, 4][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]] 

enter image description here

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
  constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
      Thread[0 <= # <= 1] & /@ Transpose[dm]];
  constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
  constraints3 = {Total[dm, 2] == #2};
  constraints4 = DeleteCases[Thread[Join@@dm ==Join@@(dm  Unitize[am])], True];
  objective = Total[dm WeightedAdjacencyMatrix[#], 2];
  Maximize[{objective, ## & @@ 
   Join[constraints1, constraints2, constraints3, constraints4]}, Join @@ dm, Integers]]&;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

 HighlightGraph[g1, Style[f[g1, 4][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]] 

enter image description here

Update: a greedy approach:

am = WeightedAdjacencyMatrix[g1];
DeleteDuplicates[am["NonzeroPositions"][[Ordering[-am["NonzeroValues"]]]][[;; ;; 2]], 
  IntersectingQ]

{{4, 5}, {1, 2}, {6, 8}}

Original answer:

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
  constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
      Thread[0 <= # <= 1] & /@ Transpose[dm]];
  constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
  constraints3 = {Total[dm, 2] == #2};
  constraints4 = DeleteCases[Thread[Join@@dm ==Join@@(dm  Unitize[am])], True];
  objective = Total[dm WeightedAdjacencyMatrix[#], 2];
  Maximize[{objective, ## & @@ 
   Join[constraints1, constraints2, constraints3, constraints4]}, Join @@ dm, Integers]]&;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

 HighlightGraph[g1, Style[f[g1, 4][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]] 

enter image description here

added 296 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
    constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
       Thread[0 <= # <= 1] & /@ Transpose[dm]];
    constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
    constraints3 = {Total[dm, 2] == #2};
  constraints4 = DeleteCases[Thread[Join@@dm ==Join@@(dm  Unitize[am])], True];
  objective = Total[dm WeightedAdjacencyMatrix[#], 2];
    Maximize[{objective, ## & @@ 
       Join[constraints1, constraints2, constraints3]constraints3, constraints4]}, Join @@ dm, Integers]] &;Integers]]&;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

 HighlightGraph[g1, Style[f[g1, 4][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]] 

enter image description here

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
    constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
       Thread[0 <= # <= 1] & /@ Transpose[dm]];
    constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
    constraints3 = {Total[dm, 2] == #2};
    objective = Total[dm WeightedAdjacencyMatrix[#], 2];
    Maximize[{objective, ## & @@ 
       Join[constraints1, constraints2, constraints3]}, Join @@ dm, Integers]] &;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
  constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
      Thread[0 <= # <= 1] & /@ Transpose[dm]];
  constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
  constraints3 = {Total[dm, 2] == #2};
  constraints4 = DeleteCases[Thread[Join@@dm ==Join@@(dm  Unitize[am])], True];
  objective = Total[dm WeightedAdjacencyMatrix[#], 2];
  Maximize[{objective, ## & @@ 
   Join[constraints1, constraints2, constraints3, constraints4]}, Join @@ dm, Integers]]&;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

 HighlightGraph[g1, Style[f[g1, 4][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]] 

enter image description here

added 226 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
    constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
       Thread[0 <= # <= 1] & /@ Transpose[dm]];
    constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
    constraints3 = {Total[dm, 2] == #2};
    objective = Total[dm WeightedAdjacencyMatrix[#], 2];
    Maximize[{objective, ## & @@ 
       Join[constraints1, constraints2, constraints3]}, Join @@ dm]]dm, Integers]] &;

Examples:Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
     constraints2, constraints3, objective},
    constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
       Thread[0 <= # <= 1] & /@ Transpose[dm]];
    constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
    constraints3 = {Total[dm, 2] == #2};
    objective = Total[dm WeightedAdjacencyMatrix[#], 2];
    Maximize[{objective, ## & @@ 
       Join[constraints1, constraints2, constraints3]}, Join @@ dm]] &;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

ClearAll[f]
f = Module[{dm = Array[a, {1, 1} VertexCount[#], 1], constraints1, 
      constraints2, constraints3, objective},
    constraints1 = Join @@ Join[Thread[0 <= # <= 1] & /@ dm, 
       Thread[0 <= # <= 1] & /@ Transpose[dm]];
    constraints2 = 0 <= Total[{#, Reverse /@ #}, 2] <= 1 & /@ dm;
    constraints3 = {Total[dm, 2] == #2};
    objective = Total[dm WeightedAdjacencyMatrix[#], 2];
    Maximize[{objective, ## & @@ 
       Join[constraints1, constraints2, constraints3]}, Join @@ dm, Integers]] &;

Examples:

edges = Property[#, EdgeWeight -> #2] & @@@ 
   Thread[{{1 <-> 2, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 
      6 <-> 7, 6 <-> 8, 7 <-> 8}, {3, 2, 1, 6, 5, 4, 1, 3, 2}}];
g1 = Graph[Range[8], edges, EdgeLabels -> "EdgeWeight", 
  VertexLabels -> Placed["Name", Center], VertexSize -> Large, ImageSize -> Large]

enter image description here

f[g1, 3] /. r : Rule[_, 1] :> Style[r, Red, Bold]

enter image description here

HighlightGraph[g1, Style[f[g1, 3][[2]] /. 
  Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

HighlightGraph[g1, Style[f[g1, 2][[2]] /. 
   Rule[a[b_, c_], 1] :> UndirectedEdge[b, c], {Red, Thickness[.02]}]]

enter image description here

Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading