Connect nodes to maximize total edge weight

前端 未结 5 1128
情深已故
情深已故 2021-01-01 13:23

I am working on a problem which could be reduced to a graph optimization problem as below.

  • A set of colored nodes is given. They are all unconnected i.e. th

5条回答
  •  孤城傲影
    2021-01-01 13:38

    Did you think maybe about greedy approach ? For all colors and corresponding colorA->colorB values, if for each colored edge you will do

    edge_color  :  sort_in_descending_order(edge_color_To_color_Prices)
    example:
        red: red->black  30
             red->white  20
             red->yellow 10
        black: black->green 15
               black->grey 10
    

    iterate until there is room for new edge (4 per node) and take biggest possible edge value(mark it as visited as well it will help you latter) (I assume you can link nodeA with nodeB only once). We can assume that edge is not directed from what you have said. In each node you have to store those chosen values, so when you are iterating over next edge that you already used you have to be aware of the chosen edges (black node has to be aware of red->black 30 chosen by red)

    red: 1st edge: red->black 30 -> stores that in black node list as 
    [red,30]
         2nd edge: ...........20 ->...
         3rd edge: ...........10
         4th edge: ...........5
         ---------------------------
         5th edge: ...........4   (after update from black it will be 4th 
    edge)
    black: 1st edge: black->white   50  > [red,30] 
           2nd edge: .............. 40
           3rd edge: .............. 35
           4th edge: .............. 34
           ---------------------------
           5th edge  .............. 30 (this is our red->black edge)
    

    replace by (black->white 50) and go to red node to update it with the next max (because we just removed red->black 30 by black->white 50 - we will replace it only if we reached the limit of edges in Black node following the min value criteria - we are removing/replacing lowest priced edge from Black node)

    That should work

提交回复
热议问题