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. C语言--static全局使用示例

    前言:看到很多使用Objective-C开发IOS的大牛,有时候会使用static全局变量,相比之下,我却很少用这个,从而很少对其有着比较有实质意义的理解,甚至更别说运用它了. 今天,经过一番思考和自 ...

  2. Swift中的字典

    学习来自<极客学院:Swift中的字典> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 import Foundation //字典的声明 v ...

  3. android 之 adb 启动问题的一般解决办法

    有时,当我们打开eclipse准备运行Android项目时,虚拟机会启动不了,并且会出现下面的报错. [2015-10-07 16:47:46 - Game2048] ---------------- ...

  4. 关于Javascript中的复制

    在做项目时有一个需求,是需要复制内容到剪切板,因为有众多浏览器,所以要兼容性很重要 1.最简单的copy,只能在IE下使用 使用clipboardData方法 <script type=&quo ...

  5. 读书笔记——Windows环境下32位汇编语言程序设计(3)求复数模的子程序

    3.6.1.1中的例子 _Calc proc _dwX,_dwY local @dwResult finit fild _dwX fld st(0) fmul ;i*i fild _dwY fld s ...

  6. Linux nmap

    一.简介 Nmap(Network Mapper)是一款开放源代码的网络探测和安全审核工具.它用于快速扫描一个网络和一台主机开放的端口,还能使用TCP/IP协议栈特征探测远程主机的操作系统类型.nma ...

  7. 构建多模块的Maven项目

    在Eclipse下创建一个maven项目,该项目有多个模块组成. 1.创建父项目 File->New->Project->Maven->Maven Project(图一)   ...

  8. SQL-一道特殊的字符串分解题目

    本题不是一道直接的字符串拆解, 应用场景如下,表中有一个字段,是表示事件受影响的国家集合,使用逗号进行分隔,不幸的是,居然发现有些国家本身就带有逗号,这样在规范化的时候,如何准确地找到这些国家呢? 以 ...

  9. Sql practice

    employee表 数据准备 use tempdb go if OBJECT_ID('employee') is not null drop table employee ;with employee ...

  10. 【ASP.NET 进阶】无刷新上传图片之一:利用一般处理程序

    效果图: 源代码地址:https://github.com/YeXiaoChao/UploadThePic