大数据搜索,求助!

yyouyou 2006-11-12 10:46:52
假设每个QQ有好友100个,想找出其中任意两个QQ是否可以通过好友的好友的好友......而进行交流。
应该属于图的遍历,但是不管是深度遍历还是广度遍历,好像数据量都太大了
就算是只找出仅限于中间有10个联系人100*100*100*100*.....
感觉计算机也算不完啊:(
要实现这种功能数据库该如何设计,程序又该如何设计呢?
请大侠指点
...全文
221 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
山林73 2006-11-14
  • 打赏
  • 举报
回复
要排除多余10的,可以用:
where not exist (
select * from s s11 where s11.id not in( s1.id, s2.id, s3.id,....s10.id ) and s11.id = s10.fr_id
)
山林73 2006-11-14
  • 打赏
  • 举报
回复
select * from s s1
inner join s s2 where s2.id not in( s1.id ) and s2.id = s1.fr_id
inner join s s3 where s3.id not in( s1.id, s2.id ) and s3.id = s2.fr_id
inner join s s4 where s4.id not in( s1.id, s2.id, s3.id ) and s4.id = s3.fr_id
...
inner join s s10 where s10.id not in( s1.id, s2.id, s3.id,....s9.id ) and s10.id = s9.fr_id



yyouyou 2006-11-14
  • 打赏
  • 举报
回复
yaozw_mountain(山林)

运行时间可能会很长,如果只是查找通过一个中间人肯定没问题,但是中间联系人多了时间问题就出来了,就是100^n
yyouyou 2006-11-14
  • 打赏
  • 举报
回复
sten(近视进士)
我并没有说“任意两个QQ能保证通过无限的"好友"而关连”,
我是说“任意两个QQ是否”,只是判断有没有这种连接的可能。
山林73 2006-11-14
  • 打赏
  • 举报
回复
循环调用
山林73 2006-11-14
  • 打赏
  • 举报
回复
这个命题好像是网络状拓扑;
如果用start with / connect by 不行的话,


用中间临时表 t
insert t(id)
select distinct id from s
where s.id not in (
select id from t
) and s.id in (
select s2.fr_id from s s2
inner join t where t.id= s2.id
)

)

Tiger_Zhao 2006-11-14
  • 打赏
  • 举报
回复
SELECT COUNT(*) FROM
(SELECT ID, FID FROM t
START WITH id = 12345
CONNECT BY PRIOR ID = FID
)N1
WHERE ID = 67890

先查找出 id = 12345 的所有可连通的好友,再看 id = 67890 是否属于该集合
山林73 2006-11-14
  • 打赏
  • 举报
回复
如果任何记录永远存在id<>fr_id, 可以简化:
select * from s s1
inner join s s2 where s2.id = s1.fr_id
inner join s s3 where s3.id not in( s1.id ) and s3.id = s2.fr_id
inner join s s4 where s4.id not in( s1.id, s2.id ) and s4.id = s3.fr_id
...
inner join s s10 where s10.id not in( s1.id, s2.id, s3.id,....s8.id ) and s10.id = s9.fr_id



where not exist (
select * from s s11 where s11.id not in( s1.id, s2.id, s3.id,....s9.id ) and s11.id = s10.fr_id
)

sten 2006-11-13
  • 打赏
  • 举报
回复
感觉lz你的命题有点问题
细想下,任意两个QQ能保证通过无限的"好友"而关连吗
比如:(= 表示好友关系,<>表示非好友关系)
a = b ;b = c; d = e; f = g;
如何能保证a,d,g三个QQ中任何两个在好友关系中长出?
深度也好广度也好,从哪开始?
学习!

3,492

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧