Python 图、边和上下文信息

Python 图、边和上下文信息,python,graph,Python,Graph,我不熟悉图形,并试图理解如何将上下文关系映射到图形中 我看了一些有系统地介绍简单演示(1)的示例,例如: # I want to organize the seating arrangement for my party # Bob does not like John, so I can say ----------- ------------ | Bob | <--- excludes --- | John | --

我不熟悉图形,并试图理解如何将上下文关系映射到图形中

我看了一些有系统地介绍简单演示(1)的示例,例如:

# I want to organize the seating arrangement for my party
# Bob does not like John, so I can say

-----------                       ------------
|   Bob   |  <--- excludes ---    |   John   |
-----------                       ------------
#我想为我的聚会安排座位
#鲍勃不喜欢约翰,所以我可以说
-----------                       ------------

|Bob |如果您不将自己局限于一组人及其关系,而是局限于所有人的关系,您还可以对不同编号的组之间的关系进行建模-例如,再次使用图形。
解决方案是干净的,因为所有节点都是定义集的一部分。正如您已经提到的,您必须检查人员所在的所有节点

我想到了两种方法:


  • 将人对等组合成特定实体;所以事实上你会有关系的鲍勃谢谢博丹!国家模式看起来很有希望。如果我理解清楚的话,目标节点将是John的satte。国家模式似乎解决了问题的一部分。如果要找约翰和玛丽在一起时排除在外的人,我会马上找到鲍勃。然而,当寻找排除Bob的人时,我可能需要对许多对象遍历许多不同的状态。对不起,我考虑了更多。这似乎很有道理:您只需将John和Bob联系起来,然后检查是否满足排除条件。谢谢
    # solution 1: create an intermediary node. 
    # now, the problem is that, if I want to know who John excludes, 
    # I have to look at the node John + every other node he might be the child of. 
    
    
                                                        |   John  |
    -----------                       -------------     -----------
    |   Bob   |  <--- excludes ---    |   Couple  | < 
    -----------                       -------------     ------------
                                                        |   Mary   |
                                                        ------------
    
    # solution 2: target the edges, and make them cumulative
    # here if I want to know who John excludes, 
    # I just have to target the node John and then check for 
    # additional logic (in this case, check if Mary is present too).
    
    -----------                       ------------
    |   Bob   |  <--- excludes ---    |   John   |
    -----------             ^         ------------
         ^                  |
          |                 |
    excludes   <---- requires (some logic here)
          |
    ----------
    |  Mary  |
    ----------