10.2 How would you design the data structures for a very large social network like Facebook or Linkedln? Describe how you would design an algorithm to show the connection, or path, between two people (e.g., Me -> Bob -> Susan -> Jason -> You).

这道题让我们实现大型社交网站的数据结构,首先用户类Person需要包含好友和其他的一些信息,而且大型网站一般可能会有上百万的用户,我们一般不可能把所有的数据都存在一台机器上,所以我们在查找好友时,需要先查找好友所在的机器,再在机器上查询好友,每个好友或机器都有自己的编号,为了快速查找,均使用了哈希表来建立映射,参见代码如下:

class Person {
public:
Person(int id): _personID(id) {}
int getID() { return _personID; }
void addFriend(int id) { _friendIDs.push_back(id); } private:
vector<int> _friendIDs;
int _personID;
}; class Machine {
public:
unordered_map<int, Person*> _persons;
int _machineID;
Person* getPersonWithID(int personID) {
if (_persons.find(personID) == _persons.end()) {
return nullptr;
}
return _persons[personID];
}
}; class Server {
public:
unordered_map<int, Machine*> _machines;
unordered_map<int, int> _personToMachineMap;
Machine* getMatchineWithId(int machineID) {
if (_machines.find(machineID) == _machines.end()) {
return nullptr;
}
return _machines[machineID];
}
int getMachineIDForUser(int personID) {
if (_personToMachineMap.find(personID) == _personToMachineMap.end()) {
return -;
}
return _personToMachineMap[personID];
}
Person* getPersonWithID(int personID) {
if (_personToMachineMap.find(personID) == _personToMachineMap.end()) {
return nullptr;
}
int machineID = _personToMachineMap[personID];
Machine *machine = getMatchineWithId(machineID);
if (machine == nullptr) return nullptr;
return machine->getPersonWithID(personID);
}
};

优化:减少机器跳跃

机器之间的跳跃花费大,我们一般不会在机器之间进行随机跳跃,一般若我有好多个好友在同一个机器上,会将他们归到一起访问。

优化:智能的分类人和机器

由于人们更有可能会添加和他们来自同一个国家的人,所以将同一个城市,州,国家的人都尽量存贮到同一台机器上,这样查找时会减少机器跳跃

问题:BFS搜索需要将点标记为已读,这里怎样处理?

由于可能会有很多个搜索同时进行,所以我们不会对数据进行直接标记,但我们会使用哈希表来建立映射来标记数据是否访问过。

还有一些其他的问题可以考虑:

1. 在现实中,如果服务器崩溃了怎么办?

2. 你怎么利用好缓存功能?

3. 你会搜到图的尽头吗,你怎么决定什么时候停止搜索?

4. 实际中,每个人的朋友数都不同,有人想在你和别人之间产生一个好友链,你该怎么用这数据确定在哪开始遍历?

[CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构的更多相关文章

  1. Complex social network Partition for Balanced Subnetworks---Hao Lan Zhang,Jiming Liu,Chunyu Feng,Chaoyi Pang,Tongliang Li,Jing He阅读

    摘要:Abstract—Complex social network analysis methods have been applied extensively in various domains ...

  2. 10 Big Data Possibilities for 2017 Based on Oracle's Predictions

    2017 will see a host of informed predictions, lower costs, and even business-centric gains, courtesy ...

  3. The Swiss Army Knife of Data Structures … in C#

    "I worked up a full implementation as well but I decided that it was too complicated to post in ...

  4. 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》

    按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...

  5. Persistent Data Structures

    原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...

  6. Go Data Structures: Interfaces

    refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...

  7. Choose Concurrency-Friendly Data Structures

    What is a high-performance data structure? To answer that question, we're used to applying normal co ...

  8. [翻译]MapReduce: Simplified Data Processing on Large Clusters

    MapReduce: Simplified Data Processing on Large Clusters MapReduce:面向大型集群的简化数据处理 摘要 MapReduce既是一种编程模型 ...

  9. 《MapReduce: Simplified Data Processing on Large Cluster 》翻译

    Abstract MapReduce是一种编程模型和一种用来处理和产生大数据集的相关实现.用户定义map函数来处理key/value键值对来产生一系列的中间的key/value键值对.还要定义一个re ...

随机推荐

  1. 关于EntityFramework在vs2012无法引用的问题

    这段时间学习MVC,发现一个问题,我公司的电脑可以直接引用EntityFrameWork这个命名空间,但我家里面的电脑就不能直接引用,刚开始以为是我电脑配置问题,后重装电脑,发现问题并没有解决. 今天 ...

  2. Homebrew OS X 不可或缺的套件管理器

    Homebrew OS X 不可或缺的套件管理器,可以说Homebrew就是mac下的apt-get.yum. 1.安装homebrew brew的安装很简单,使用一条ruby命令即可,Mac系统上已 ...

  3. Windows下查看端口占用

    最近在重新安装Mysql的时候,发现3306默认端口被占用了.类似的情况常常遇到,想查看到底是哪个程序把这个端口占用了. 下面是我google找到的方法,和大家分享. 1. 首先,使用netstat ...

  4. easyui的datagrid实例实现

    功能要求如图所示: function Loading() { var editRow = undefined;//保存行的索引 var query= $("#myform").se ...

  5. 我的Windows核心编程——完成端口+套接字 图解

    ========================声明============================ 本文原创,转载请注明作者和出处,并保证文章的完整性(包括本声明). 本文不定期修改完善,为 ...

  6. ajaxFileUpload文件上传

    一.简介 ajaxFileUpload是一种异步的文件上传控件,通过ajax进行文件上传,并获取上传处理结果.在很多时候我们需要使用到文件上传的功能,但是不需要使用那些强大的上传插件.此时就可以使用a ...

  7. Spring-framework下载

    下载版本,修改版本号就行. http://repo.springsource.org/libs-release-local/org/springframework/spring/4.3.2.RELEA ...

  8. AFHTTPClient的异步回调模式

    以前第一个版本,ios的http都用的同步模式,在很多地方会导致线程阻塞,自己开发了一个简易的AFHTTPClient的异步回调模式. 回调的protocol: @protocol MyAFNetwo ...

  9. c# 参数传递

    c#类型有值类型与引用类型. 无论哪种类型的变量,作为方法的参数进行传递时,默认是以"值传递"方式来传递的. 传递给方法的形参,在执行时都会新创建一个局部变量,然后接受实参的值, ...

  10. 【Android UI设计与开发】5.底部菜单栏(二)使用Fragment实现底部菜单栏

    既然 Fragment 取代了TabActivity,当然 TabActivity 的能实现的菜单栏,Fragment 当然也能实现.主要其实就是通过菜单栏的点击事件切换 Fragment 的显示和隐 ...