-
- Poetic Walks
实现的类型是Graph,带有标记顶点的可变加权有向图的抽象数据类型。即需要实现添加新节点、添加新边、移除节点、获得所有的节点集合等
-
-
- Get the code and prepare Git repository
-
从所给网址出直接下载。
然后输入以下指令建立仓库
git init
Git remote add origin git@github.com:ComputerScienceHIT/Lab2-1190201909.git
git pull origin master
git add .
git commit -m “1”
git push origin master
-
-
- Problem 1: Test Graph <String>
-
对于add函数,测试已存入和未存入的点
对于set函数,测试权值是否为0
对于remove函数,测试已存入和未存入的点,还有其他相邻的点
对于vertices函数,测试空图和非空图
对于sources和targets函数,测试有无边链接的点
-
-
- Problem 2: Implement Graph <String>
-
以下各部分,请按照MIT页面上相应部分的要求,逐项列出你的设计和实现思路/过程/结果。
-
-
-
- Implement ConcreteEdgesGraph
-
-
实现Edge
1、public Edge(String source, String target, int weight)构造方法
2、getSource() 得到起点
3、getTarget() 得到终点
4、getWeight() 得到权值
5、toString() 字符串连接起来
实现ConcreteEdgeGraph
1、add 调用vertices.add添加顶点(若vertices()中已包含vertex,返回false,否则新建一个顶点将其加入vertices)
2、set(String source, String target, int weight)设置一条边,若不存在则添加它(先判断顶点在不在集合中),若为0则删掉。
3、remove遍历所有的点若存在则删除点再删除边,不存在则返回false
4、Set vertices() 返回顶点
5、Map<String, Integer> sources 如果找到了目标的target点,就返回对应的source,如果没找到,返回一个空的map
6、Map<String, Integer> targets 如果找到了目标的sourse点,就返回对应的target,如果没找到,返回一个空的map
3.1.3.2Implement ConcreteVerticesGraph
-
-
-
- Implement ConcreteVerticesGraph
-
-
实现Vertex
1、Vertex构造方法
2、String getName() 得到该点
3、Map<String,Integer> getSource() 存储所有以当前点为终点的边,key表示边的起点,value表示边权
4、Map<String,Integer> getTarget() 存储所有以当前点为起点的边,key表示边的终点,value表示边权
5、public void checkRep() 检查当前点的rep是否非空。
6、String toString() 以字符串的形式直观地展示有哪些边以当前点为终点\起点
7、int setSource(String s, int weight) Vertex V ,设置V的源点返回weight
8、int setTarget(String t, int weight) Vertex V ,设置V的终点返回weight
-
-
- Problem 3: Implement generic Graph<L>
- Make the implementations generic
- Problem 3: Implement generic Graph<L>
-
使用泛型实现即可:
在修改过程中通过eclipse提示的错误修改即可完成:
方法的修改同理
-
-
-
- Implement Graph.empty()
-
-
调用一个具体的实现:
-
-
- Problem 4: Poetic walks
- Test GraphPoet
- Problem 4: Poetic walks
-
对于add函数,测试已存入和未存入的点
对于set函数,测试权值是否为0
对于remove函数,测试已存入和未存入的点,还有其他相邻的点
对于vertices函数,测试空图和非空图
对于sources和targets函数,测试有无边链接的点
-
-
-
- Implement GraphPoet
-
-
1、public GraphPoet(File corpus) throws IOException 将文档中句子整行输入,用split分割,每个单词建立顶点,两个相邻单词间添加有向边,权重设为1,若边出现多次则权重为出现的次数
2、public void checkRep() 检查图不空
3、public String poem(String input) 输入部分诗句,获取其中单词通过其target找下一个单词,直到把诗句拼完整。
4、public String toString() 输出
-
-
-
- Graph poetry slam
-
-
程序如下
-
-
- 使用Eclemma检查测试的代码覆盖度
-
-
-
- Before you’re done
-
git add .
git commit -m “2”
git push origin master