20192321 2020-2021-1《数据结构与面向对象程序设计》实验九报告
20192321 2020-2021-1《数据结构与面向对象程序设计》实验九报告
课程:《程序设计与数据结构》
班级:1923
姓名:李锦程
学号:20192321
实验教师:王志强
实验日期:2020年12月17日
必修/选修:必修
1.实验内容
图的综合实践
(1) 初始化:根据屏幕提示(例如:输入1为无向图,输入2为有向图)初始化无向图和有向图(可用邻接矩阵,也可用邻接表),图需要自己定义(顶点个数、边个数,建议先在草稿纸上画出图,然后再输入顶点和边数)(2分)
(2) 图的遍历:完成有向图和无向图的遍历(深度和广度优先遍历)(4分)
(3) 完成有向图的拓扑排序,并输出拓扑排序序列或者输出该图存在环(3分)
(4) 完成无向图的最小生成树(Prim算法或Kruscal算法均可),并输出(3分)
(5) 完成有向图的单源最短路径求解(迪杰斯特拉算法)(3分)
2.实验过程及结果
(一) 初始化:根据屏幕提示(例如:输入1为无向图,输入2为有向图)初始化无向图和有向图(可用邻接矩阵,也可用邻接表),图需要自己定义(顶点个数、边个数,建议先在草稿纸上画出图,然后再输入顶点和边数)
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class TopSort {
Graph g ;
public TopSort( Graph g)
{
this.g = g;
g.buildGraphyou();
}
public void topSort()
{
Queue<Vertex> queue= new LinkedList<Vertex>();
int length = g.getLength();
for(int i=0;i<length;i++)
{
if(g.getEnterEdgeNumber(g.v[i])==0)
{
queue.add(g.v[i]);
}
}
System.out.print("拓扑顺序为:");
while(!queue.isEmpty())
{
Vertex ver = queue.poll();
ArrayList<Vertex> al = g.getAdjacentVertex(ver);
for (Vertex vertex : al) {
System.out.print(ver.from + " ");
if (--vertex.indegree == 0)
queue.add(vertex);
}
}
if (!queue.isEmpty()){
System.out.println("存在环!");
}
}
}
(二)图的遍历:完成有向图和无向图的遍历(深度和广度优先遍历)
(三)完成有向图的拓扑排序,并输出拓扑排序序列或者输出该图存在环
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class TopSort {
Graph g ;
public TopSort( Graph g)
{
this.g = g;
g.buildGraphyou();
}
public void topSort()
{
Queue<Vertex> queue= new LinkedList<Vertex>();
int length = g.getLength();
for(int i=0;i<length;i++)
{
if(g.getEnterEdgeNumber(g.v[i])==0)
{
queue.add(g.v[i]);
}
}
System.out.print("拓扑顺序为:");
while(!queue.isEmpty())
{
Vertex ver = queue.poll();
ArrayList<Vertex> al = g.getAdjacentVertex(ver);
for (Vertex vertex : al) {
System.out.print(ver.from + " ");
if (--vertex.indegree == 0)
queue.add(vertex);
}
}
if (!queue.isEmpty()){
System.out.println("存在环!");
}
}
}
(四)完成无向图的最小生成树(Prim算法或Kruscal算法均可),并输出
public class PrimTest {
public static void main(String[] args) {
bookGraph();
}
public static void bookGraph(){
Prim prim = new Prim (6);
shiyan9.Edge[] edges = new shiyan9.Edge[10];
edges[0] = new shiyan9.Edge(0,1,6);
edges[1] = new shiyan9.Edge(1,4,3);
edges[2] = new shiyan9.Edge(4,5,6);
edges[3] = new shiyan9.Edge(5,3,2);
edges[4] = new shiyan9.Edge(3,0,5);
edges[5] = new shiyan9.Edge(0,2,1);
edges[6] = new shiyan9.Edge(1,2,5);
edges[7] = new shiyan9.Edge(4,2,6);
edges[8] = new shiyan9.Edge(5,2,4);
edges[9] = new shiyan9.Edge(3,2,5);
for(int i = 0;i<10;i++){
prim.insertEdge(edges[i]);
}
prim.bianli();
prim.Prim();
}
public static void randomGraph(){
Prim prim = new Prim(100);
for(int i = 0;i<1000;){
int preV = (int)(Math.random()*100);
int folV = (int)(Math.random()*100);
int weight = (int)(Math.random()*100+1);
if(preV != folV){
shiyan9.Edge edge = new Edge(preV,folV,weight);
try{
prim.insertEdge(edge);
i++;
}catch(Exception e){
continue;
}
}
}
prim.bianli();
prim.Prim();
}
}
(五)完成有向图的单源最短路径求解(迪杰斯特拉算法)
其他(感悟、思考等)
在这次实验过程中,我遇到了许多问题,其中既有知识上的漏洞,也有不细心导致的马虎,这一切都补充,完善,丰富,扩展了我的计算机知识体系。在这个过程中,我还进一步熟悉了IDEA这个平台的使用与运行方式,提高了自己自主学习的能力,为我接下来学习数据结构以及JAVA语言程序设计打下了坚实的基础,并在不断探索的过程中逐步提升了自己。