活跃农民
- 积分
- 517
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-9-2
- 最后登录
- 1970-1-1
|
用BFS找圖的最短路徑
我假設夫妻之間的距離為0
並把夫妻當成一個 node 來看
也就是說原本 Abe 和 Mona 在圖中應該是兩個不同的 node,但我把他們用同個 node 來代表,此 node 命名為 abeMona
![]()
- import java.util.*;
- public class Simpson {
- public static void main(String[] args){
- Map<PersonNode,Set<PersonNode>> familyTree = new HashMap<>();
- PersonNode abe = new PersonNode("Abe");
- familyTree.put(abe,new HashSet<>());
- PersonNode mona = new PersonNode("Mona");
- familyTree.put(mona,new HashSet<>());
- PersonNode clancy = new PersonNode("Clancy");
- familyTree.put(clancy,new HashSet<>());
- PersonNode jacqueline = new PersonNode("Jacqueline");
- familyTree.put(jacqueline,new HashSet<>());
- PersonNode herb = new PersonNode("Herb");
- familyTree.put(herb,new HashSet<>());
- PersonNode homer = new PersonNode("Homer");
- familyTree.put(homer,new HashSet<>());
- PersonNode marge = new PersonNode("Marge");
- familyTree.put(marge,new HashSet<>());
- PersonNode patty = new PersonNode("Patty");
- familyTree.put(patty,new HashSet<>());
- PersonNode selma = new PersonNode("Selma");
- familyTree.put(selma,new HashSet<>());
- PersonNode bart = new PersonNode("Bart");
- familyTree.put(bart,new HashSet<>());
- PersonNode lisa = new PersonNode("Lisa");
- familyTree.put(lisa,new HashSet<>());
- PersonNode maggie = new PersonNode("Maggie");
- familyTree.put(maggie,new HashSet<>());
- PersonNode ling = new PersonNode("Ling");
- familyTree.put(ling,new HashSet<>());
- /*We view couple as one single node*/
- PersonNode abeMona = new PersonNode("AbeMona");
- familyTree.put(abeMona,new HashSet<>());
- PersonNode clancyJacqueline = new PersonNode("ClancyJacqueline");
- familyTree.put(clancyJacqueline,new HashSet<>());
- PersonNode homerMarge = new PersonNode("HoerMarge");
- familyTree.put(homerMarge,new HashSet<>());
- /*construct edges*/
- familyTree.get(abeMona).add(herb);
- familyTree.get(abeMona).add(homerMarge);
- familyTree.get(clancyJacqueline).add(homerMarge);
- familyTree.get(clancyJacqueline).add(patty);
- familyTree.get(clancyJacqueline).add(selma);
- familyTree.get(herb).add(abeMona);
- familyTree.get(homerMarge).add(abeMona);
- familyTree.get(homerMarge).add(clancyJacqueline);
- familyTree.get(homerMarge).add(bart);
- familyTree.get(homerMarge).add(lisa);
- familyTree.get(homerMarge).add(maggie);
- familyTree.get(patty).add(clancyJacqueline);
- familyTree.get(selma).add(clancyJacqueline);
- familyTree.get(selma).add(ling);
- familyTree.get(bart).add(homerMarge);
- familyTree.get(lisa).add(homerMarge);
- familyTree.get(maggie).add(homerMarge);
- familyTree.get(ling).add(selma);
- /*If someone is married, put that person and his/her spouse into this map*/
- HashMap<PersonNode,PersonNode> couple = new HashMap<>();
- couple.put(abe,abeMona);
- couple.put(mona,abeMona);
- couple.put(clancy,clancyJacqueline);
- couple.put(jacqueline,clancyJacqueline);
- couple.put(homer,homerMarge);
- couple.put(marge,homerMarge);
- Simpson s = new Simpson();
- int distance = s.findShortestDistance(abe,mona, familyTree,couple);
- System.out.println(distance);
- }
- int findShortestDistance(PersonNode A, PersonNode B, Map<PersonNode,Set<PersonNode>> familyTree, HashMap<PersonNode,PersonNode> couple){
- //if person A and person B are couple, return 0;
- if(couple.containsKey(A) && couple.containsKey(B) && couple.get(A).equals(couple.get(B)))
- return 0;
- //if person A is married but B is not his/her spouse, we use A's spouse to search instead of A
- if(couple.containsKey(A))
- A = couple.get(A);
- //if person B is married but A is not his/her spouse, we use B's spouse to search instead of B
- if(couple.containsKey(B))
- B = couple.get(B);
- Queue<PersonNode> myQ = new LinkedList<>();
- Set<PersonNode> visited = new HashSet<>();
- int distance = 0;
- myQ.add(A);
- visited.add(A);
- while(!myQ.isEmpty()){
- int qSize = myQ.size();
- for(int i=0; i<qSize; i++){
- PersonNode curPerson = myQ.poll();
- if(curPerson == B)
- return distance;
- for(PersonNode p : familyTree.get(curPerson)){
- if(!visited.contains(p)){
- myQ.add(p);
- visited.add(curPerson);
- }
- }
- }
- distance++;
- }
- //we cannot find a path from A to B
- return -1;
- }
- }
- class PersonNode{
- String name;
- PersonNode(String name){
- this.name = name;
- }
- public boolean equals(Object o){
- PersonNode p = (PersonNode) o;
- return this.name.equals(p.name);
- }
- public int hashCode(){
- return name.hashCode();
- }
- }
复制代码
|
|