在多对多表上递归父到子到父 [英] Recursion On A Many To Many Table Parent To Child To Parent

查看:34
本文介绍了在多对多表上递归父到子到父的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的老板给了我一张桌子.

<前>相关_项目_表项目 |配饰---------------------电视 |触角电视 |电源线电视 |偏僻的笔记本电脑 |电源线笔记本电脑 |手提箱相机 |手提箱相机 |镜片iPod |耳机

描述我的老板对结果的要求的最佳方式是走一遍流程.

  1. 用户搜索电视.

  2. 找到电视,电视配件是天线、电源线和偏僻的.

  3. 天线、电源线和配件远程现在用于查找其他相关项目.电源线也是笔记本电脑的附件.天线&遥控器不是任何其他物品的配件.

  4. 物品笔记本电脑现在用于查找该物品的配件,其中是电源线和手提箱.

  5. 配件电源线和手提箱现在用于查找其他相关项目.电源线没有发现新物品(我们已经知道电源线与电视相关笔记本电脑).手提箱也是相机配件.

  6. 物品相机现在用于查找该物品的配件,其中是手提箱和镜头.

  7. 配件手提箱 &镜头现在用于寻找其他相关项目.手提箱和镜头没有发现新项目(我们已经知道手提箱与笔记本电脑相关联).

  8. 未找到新项目以继续搜索链.最终名单返回.

<前>最终名单项目 |配饰---------------------电视 |触角电视 |电源线电视 |偏僻的笔记本电脑 |电源线笔记本电脑 |手提箱相机 |手提箱相机 |镜片

处理这个问题的最佳方法是什么?我不确定正确的术语是什么,所以也许我在搜索中错过了它.任何建议表示赞赏.

解决方案

看起来你的表格呈现的是一个无向图,你需要从用户搜索的 Item 开始遍历这个图.

考虑使用广度优先搜索 (BFS) 算法.

每个访问过的节点都是您需要的结果列表.

My boss has given me a single table.

Related_Items_Table

Item        | Accessory 
---------------------
TV          | Antennae 
TV          | Power Cord 
TV          | Remote 
Laptop      | Power Cord 
Laptop      | Carrying Case 
Camera      | Carrying Case 
Camera      | Lens 
iPod        | Headphones

The best way to describe what my boss wants for results is to walk through the process.

  1. The user searches for TV.

  2. TV is found and the accessories for TV are Antennae, Power Cord & Remote.

  3. The accessories Antennae, Power Cord & Remote are now used to find other related items. Power Cord also an accessory for Laptop. Antennae & Remote are not accessories for any other item.

  4. The item Laptop is now used to find that item's accessories, which are Power Cord & Carrying Case.

  5. The accessories Power Cord & Carrying Case are now used to find other related items. Power Cord finds no new items (we already know Power Cord is associated with TV & Laptop). Carrying Case is also an accessory for Camera.

  6. The item Camera is now used to find that item's accessories, which are Carrying Case & Lens.

  7. The accessories Carrying Case & Lens are now used to find other related items. Carrying Case & Lens find no new items (we already know Carrying Case is associated with Laptop).

  8. No new items are found to continue search chain. Final list returned.

Final List 

Item        | Accessory 
---------------------
TV          | Antennae 
TV          | Power Cord 
TV          | Remote 
Laptop      | Power Cord 
Laptop      | Carrying Case 
Camera      | Carrying Case 
Camera      | Lens 

What is the best way to handle this problem? I'm not sure what the correct terminology would be for this so perhaps I missed it in my searches. Any advice is appreciated.

解决方案

It looks like your table presents a undirected graph and you need to traverse this graph starting from the Item user searched.

Consider using breadth-first search (BFS) algorithm.

Every visited node is a resulting list you need.

这篇关于在多对多表上递归父到子到父的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆