/**
* 功能:java
[java] view plain copyapp
- /**
- * 思路:
- * 1)针对每一个朋友ID,找出所在机器的位置:int machine_index=getMachineIDForUser(personID);
- * 2)转到编号为#machine_index的机器。
- * 3)在那台机器上,执行:Person friend=getPersonWithID(person_id)。
- *
- * 定义一个Server类,包含一份全部机器的列表,还有一个Machine类,表明一台单独的机器。经过散列表,有效地查找数据。
- *
- */
-
- class Server{
- HashMap<Integer,Machine> machines=new HashMap<Integer, Machine>();
- HashMap<Integer,Integer> personToMachineMap=new HashMap<Integer, Integer>();
-
- public Machine getMachineWithId(int machineID){
- return machines.get(machineID);
- }
-
- public int getMachineIDForUser(int personID){
- return personToMachineMap.get(personID);
- }
-
- public Person getPersonWithId(int personID){
- Integer machineID=getMachineIDForUser(personID);
- if(machineID==null)
- return null;
- Machine machine=getMachineWithId(machineID);
- if(machine==null)
- return null;
- return machine.getPersonWithId(personID);
- }
-
- }
-
- class Machine{
- public int machineID;
- public HashMap<Integer,Person> persons=new HashMap<Integer, Person>();
-
- public Person getPersonWithId(int personID){
- return persons.get(personID);
- }
-
- }
-
- class Person{
- private int personID;
- private ArrayList<Integer> friendID;
-
- public Person(int id){
- this.personID=id;
- }
-
- public int getID(){
- return this.personID;
- }
-
- public void addFriend(int id){
- this.friendID.add(id);
- }
-
- }
-
- /**
- * 优化:减小机器间跳转次数
- * 从一台机器跳转到另一台机器的开销很昂贵,不要为了找到某个朋友就在机器之间任意跳转,而是试着批处理这些跳转动做。
- * 优化:智能划分用户和机器
- * 根据地域划分
- *
- * 问题:广度优先搜索要求标记访问过的节点,如何处理
- * 同一时间可能会执行不少搜索操做,所以直接编辑数据的作法并不稳当。能够利用散列表模仿节点的标记动做,以查询节点id,是否被访问过。
- */