[CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构
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 大型社交网站的数据结构的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- Choose Concurrency-Friendly Data Structures
What is a high-performance data structure? To answer that question, we're used to applying normal co ...
- [翻译]MapReduce: Simplified Data Processing on Large Clusters
MapReduce: Simplified Data Processing on Large Clusters MapReduce:面向大型集群的简化数据处理 摘要 MapReduce既是一种编程模型 ...
- 《MapReduce: Simplified Data Processing on Large Cluster 》翻译
Abstract MapReduce是一种编程模型和一种用来处理和产生大数据集的相关实现.用户定义map函数来处理key/value键值对来产生一系列的中间的key/value键值对.还要定义一个re ...
随机推荐
- python启动服务器
3.* python -m http.server [port] & 2.* python -m SimpleHTTPServer [port] ...
- ubuntu wireshark finish
http://blog.csdn.net/cumirror/article/details/4694283 安装编译工具: $sudo apt-get install build-essential ...
- Android开源项目汇总【转】
主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView ...
- JavaScript Patterns 4.5 Immediate Functions
The immediate function pattern is a syntax that enables you to execute a function as soon as it is d ...
- js中location.href的用法
Javascript中的location.href有很多种用法,主要如下: self.location.href="/url" 当前页面打开URL页面 ocation.href=& ...
- C++ new(2)
1. new与operator new C++中有很多语法让人难以理解,如:new operator(操作符,下同)和operator new之间差异,确切的说,应该是new与operator new ...
- Armstrong数
题目描述 在三位的整数中,例如153可以满足1^3 + 5^3 + 3^3 = 153,这样的数称之为Armstrong数.将所有的Armstrong数按小到大排序,试写出一程序找出指定序号的三位Ar ...
- const 用法总结
在编程中我们常常会使用敞亮的概念,除了使用#define定义的宏之外,我们还有更好的选择,就是使用const关键字. 1.const关键字的意义 当我们定义一个变量,并且希望这个变量不再改变(编译器会 ...
- iscroll.js的使用
入门 Scroll是一个类,每个需要使用滚动功能的区域均要进行初始化.每个页面上的iScroll实例数目在设备的CPU和内存能承受的范围内是没有限制的. 尽可能保持DOM结构的简洁.iScroll使用 ...
- unix基本命令日记
鉴于命令经常忘记,网站文章鱼龙混杂,很多不适合自己,现在记录方便自己查看.每个人的基础不同,需要合适的文章也不一样. 用户管理 useradd 功能说明:建立用户帐号. 语 法:useradd [-m ...