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. ruby直接底层连接数据库

    class MysqlTest #Code here require "mysql" def testMysql dbc=Mysql.real_connect('localhost ...

  2. win10中文简体繁体切换快捷键

    win10中文简体繁体切换快捷键Ctrl+Shift+F

  3. 【mysql】索引的优化

    写在前面的话 查询容易,优化不易,且写且珍惜 mysql结构 从MySQL逻辑架构来看,MySQL有三层架构,第一层连接,第二层查询解析.分析.优化.视图.缓存,第三层,存储引擎 MySQL有哪些索引 ...

  4. Oracle-创建服务器参数文件

    允许使用传统的init.ora或SPFILE作为配置文件.但是建议所有数据库创建和使用一个SPFILE.可以从init.ora创建SPFILE SQL> CREATE spfile FROM p ...

  5. java常用开发工具类之 图片水印,文字水印,缩放,补白工具类

    import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  6. Lua环境

    1.前言 Lua将其所有的全局变量保存在一个常规的table中,这个table称为“环境”.这种组织结构的优点在于,其一,不需要再为全局变量创造一种新的数据结构,因此简化了Lua的内部实现:另一个优点 ...

  7. HTTP详解2-请求、响应、缓存

    1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干.HTTP协议传输的消息 ...

  8. 迅为iTOP-4412嵌入式开发板实现中断驱动例程

    本文转自迅为:www.topeetboard.com 大家好,今天我们来学习一下 linux 中断处理驱动的编写,本节我们实现的功能是通过开发板上的按键来控制 led 发光二极管,在之前的章节我们学习 ...

  9. Sample: Write And Read data from HDFS with java API

    HDFS: hadoop distributed file system 它抽象了整个集群的存储资源,可以存放大文件. 文件采用分块存储复制的设计.块的默认大小是64M. 流式数据访问,一次写入(现支 ...

  10. NOIP2008普及组传球游戏(动态规划)——yhx

    题目描述 上体育课的时候,小蛮的老师经常带着同学们一起做游戏.这次,老师带着同学们一起做传球游戏. 游戏规则是这样的:n个同学站成一个圆圈,其中的一个同学手里拿着一个球,当老师吹哨子时开始传球,每个同 ...