BUAA_OO_第三单元作业总结

第九次作业

3、作业思路

算法选择

图论算法

public class UnionFindSet {
private HashMap<Integer, Integer> parent;
private int unionNum;//维护连通块的数量，可通过o(1)查询

public UnionFindSet() {
parent = new HashMap<Integer, Integer>();
unionNum = 0;
}

public void insert(int id) {
parent.put(id, id);
unionNum++;
}

private int getFather(int id) {
int root = id;
while (true) {
int father = parent.get(root);
if (father == root) {
break;
}
root = father;
}
int now = id;
//路径压缩，可将查找效率优化到o(1)
while (true) {
int father = parent.get(now);
if (father == now) {
break;
}
parent.put(now, root);
now = father;
}
return root;
}

public boolean inUnion(int id1, int id2) {
int f1 = getFather(id1);
int f2 = getFather(id2);
return f1 == f2;
}

public void link(int id1, int id2) {
int f1 = getFather(id1);
int f2 = getFather(id2);
if (f1 == f2) {
return;
}
unionNum--;
parent.put(f1, f2);
}

public int getUnionNum() {
return unionNum;
}
}

数据结构算法

Group中维护了ageSumageSumSqvalueSum来提高相关指令的查询效率

第十次作业

3、作业思路

算法选择

public class Kruskal {
private UnionFindSet ufs;
private ArrayList<Relation> relations;
private HashSet<Integer> allPeople;
private int id;
private int leastSum;

public Kruskal(int id) {
ufs = new UnionFindSet();
relations = new ArrayList<>();
allPeople = new HashSet<Integer>();
this.id = id;
leastSum = 0;
ufs.insert(id);
}
//遍历所有边加入该连通分支中的点与边
int id1 = relation.getId1();
int id2 = relation.getId2();
if (!allPeople.contains(id1)) {
ufs.insert(id1);
}
if (!allPeople.contains(id2)) {
ufs.insert(id2);
}
}
//求值
public int getLeastSum() {
Collections.sort(relations);
for (Relation relation : relations) {
if (ufs.getUnionNum() == 1) {
break;
}
if (!ufs.inUnion(relation.getId1(), relation.getId2())) {
leastSum += relation.getValue();
}
}
return leastSum;
}
}


第十一次作业

3、作业思路

public class Dijkstra {
private MyPerson person1;
private MyPerson person2;
private PriorityQueue<Distance> pq;
private HashMap<Integer, Integer> minDis;
private HashSet<Integer> visit;
private static int INF = 0x7fffffff;

public Dijkstra(MyPerson person1, MyPerson person2) {
this.person1 = person1;
this.person2 = person2;
pq = new PriorityQueue<Distance>();
minDis = new HashMap<Integer, Integer>();
visit = new HashSet<>();
}

public int minDistance() {
minDis.put(person1.getId(), 0);
while (!pq.isEmpty()) {
Distance dis = pq.poll();//取出并弹出堆顶
MyPerson person = dis.getPerson();
int distance = dis.getDistance();
if (person.equals(person2)) { //已经访问到person2
return distance;
}
if (visit.contains(person.getId())) {
continue;
}
for (Person next : linkPerson.values()) {
int oriDistance = getDistance(next.getId());
if (oriDistance > distance + person.queryValue(next)) { //更新距离
int newDistance = distance + person.queryValue(next);
minDis.put(next.getId(), newDistance);
}
}
}
return -1;
}

public int getDistance(int id) {
if (minDis.containsKey(id)) {
return minDis.get(id);
}
return INF;
}
}


NetWork拓展

题目要求

• Person：吃瓜群众，不发广告，不买东西，不卖东西

发送广告

/*@ public normal_behavior
@ assignable people[*].messages
@ ensures (\forall int i; 0 <= i && i < people.length; (getPerson(id).isLinked(people[i])) ==> (people[i].messages.length == \old(people[i].messages.length) + 1 && people[i].messages[0] == \old(getPerson(id).getAdvertisement(advertisementId)) && (\forall int j; 1 <= j && j < people[i].messages.length; people[i].messages[j] == \old(people[i].messages[j - 1]))));
@ ensures (\forall int i; 0 <= i && i < people.length; !(getPerson(id).isLinked(people[i])) ==> (people[i].messages.length == \old(people[i].messages.length && (\forall int j; 0 <= j && j < people[i].messages.length; people[i].messages[j] == \old(people[i].messages[j]))));
@ also
@ public exceptional_behavior
@ signals (PersonIdNotFoundException e) (\forall int i; 0 <= i && i < people.length; people[i].getId() != id || (people[i].getId() == id && !people[i] instanceof Advertiser));
@*/


设置偏好

/*@ public normal_behavior
@ requires contains(personId);
@ requires containsProduct(productId);
@ ensures getPerson(personId).isFavorable(productId) == true;
@*/
public /*@ pure @*/void saleProduct(int personId, int productId);


购买商品

/*@ public normal_behavior
@ requires (containsCustomer(id1) && containsProducer(id2) && num > 0 && ReadyToBuy(id1, id2));
@ assignable getProducer(id2).sales, containsCustomer(id1).money;
@ ensures getProducer(id2).sales == \old(getProducer(id2).sales) + getProducer(id2).productPrice * num;
@ ensures getCustomer(id1).money == \old(getCustomer(id1).money) - getProducer(id1).productPrice * num;
@ ensures \result == true;
@ also
@ public normal_behavior
@ requires (containsCustomer(id1) && containsProducer(id2) && num < 0 && && ReadyToBuy(id1, id2));
@ assignable \nothing;
@ ensures \result == false;
@ also
@ public exceptional_behavior
@ signals (CustomerIdNotFoundException e) !containsCustomer(id1));
@ signals (ProducerIdNotFoundException e) (containsCustomer(id1) && !containsProducer(id2));