Skip to main content
 首页 » 编程设计

python之图、边和上下文信息

2025年04月02日13开发

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

我看过系统介绍简单demo(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,但不喜欢在他女朋友 Mary 的陪伴下待在他身边(你知道,他们一直接吻,你不能再和他们说话了)。我想过两种解决方案:

# 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  | 
---------- 

请注意,我没有找到任何这样的示例,甚至想知道直接在图中处理此类问题是否是正确的解决方案。任何想法?谢谢。

(1) 例如关于 neo4j,http://www.slideshare.net/thobe/django-and-neo4j-domain-modeling-that-kicks-ass/25-The_Neo4j_Graph_data_model

请您参考如下方法:

我想到了两种方法:

  1. 将人对等组合成特定的实体;所以实际上你会有 Bob <- John 和 Bob <- John + Mary 的关系。但是如果数据量很大,我想它可能看起来很难看。
  2. 创建状态机:您将能够针对不同的情况跟踪不同的状态。因此,例如,状态“约翰一个人来”将过渡到“坐在约翰身边”,而“约翰和玛丽一起来”则不会。