Dependency Algorithm - find a minimum set of packages to install

前端 未结 10 1931
别那么骄傲
别那么骄傲 2021-02-02 17:23

I\'m working on an algorithm which goal is to find a minimum set of packages to install package \"X\".

I\'ll explain better with an example:



        
10条回答
  •  春和景丽
    2021-02-02 17:49

    I would suggest you to first transform the graph in a AND-OR Tree. Once done you can perform a search in the tree for the best (where you can choose what "best" means: shortest, lowest memory occupation of packages in nodes, etc...) path.

    A suggestion I'd make, being that the condition to install X would be something like install(X) = install(A) and (install(E) or install(C)), is to group the OR nodes (in this case: E and C) into a single node, say EC, and transform the condition in install(X) = install(A) and install(EC).

    In alternative, based on the AND-OR Tree idea, you could create a custom AND-OR Graph using the grouping idea. In this way you could use an adaptation of a graph traversal algorithm, which could be more useful in certain scenarios.

    Yet another solution could be to use Forward Chaining. You'd have to follow these steps:

    1. Transform (just re-writing the conditions here):

      A and (E or C) => X

      E and (H or Y) => A

      B and (Z or Y) => E

    into

    (A and E) or (A and C) => X
    (E and H) or (E and Y) => A
    (B and Z) or (B and Y) => E
    
    1. Set X as goal.
    2. Insert B, H, K, Y, Z as facts.
    3. Run Forward chaining and stop on the first occurrence of X (the goal). That should be the shortest way to achieve the goal in this case (just remember to keep track of the facts that have been used).

    Let me know if anything is unclear.

提交回复
热议问题