"Object-oriented programming and data structures" Experimental Report 9

Science 20182329 No. 2019-2020-1 "Object-oriented programming and data structures" Experimental Report 9

  • Course: "Programming and Data Structures"
  • Class: 1823
  • Name: Li Yizhuo
  • Student ID: 20182329
  • Experiment Teacher: Wang Zhiqiang
  • Experiment Date: December 3, 2019
  • Compulsory / Elective: Compulsory

1. Experimental content

  • Achieve binary sort tree, and learn to write, delete, add, insert, and traverse the binary sort tree
  • Knowledge learning map, to map and understand the distinction undirected graph,
  • Learn weighted graph, weighted graph algorithm learn, the best algorithm.
  • Figure traverse a variety of learning, such as depth-first traversal and breadth-first traversal.
  • The method of learning generated the most trees
  • The method of generating learn FIG adjacency matrix, there is calculated for each node of the penetration-implemented method
  • The method of calculation of the learn each graph, there is not performed by traversing.
  • Solution has completed (Dijkstra) to a single-source shortest path of FIG.

2. Experimental procedure and results

  • Initialization: on-screen prompts, and undirected graph initialization directed graph (the adjacency matrix is ​​available, can also be used adjacent table), FIG required (the number of vertices, edges define their own number, recommended to FIG draw on rough paper, and then enter the number of vertices and edges), I begin to realize their own view of the adjacency matrix.

initialization:

 public Graph(List<Vertex> vertexs, int[][] edges) {
            this.vertexs = vertexs;
            this.topVertexs=new ArrayList<GRAPGAPI.Vertex>();
            this.edges = edges;
            this.minTree=new int[this.vertexs.size()][this.vertexs.size()];
            initUnVisited();
        }

FIG initialization must first obtain the code point

public List<Vertex> getNeighbors(Vertex v) {
            //参数检测
            if(!isInGraph(v)){
                System.out.println("当前节点不在图中");
                return null;
            }
            List<Vertex> neighbors = new ArrayList<Vertex>();
            int position = vertexs.indexOf(v);
            Vertex neighbor = null;
            int distance;
            for (int i = 0; i < vertexs.size(); i++) {
                if (i == position) {
                    //顶点本身,跳过
                    continue;
                }
                distance = edges[position][i];    //到所有顶点的距离
                if (distance < Integer.MAX_VALUE) {
                    //是邻居(有路径可达)
                    neighbor = getVertex(i);
                    if (!neighbor.isMarked()) {
                        //如果邻居没有访问过,则加入list;
                        neighbors.add(neighbor);
                    }
                }
            }
            return neighbors;
        }
  • There is completed (depth and breadth first traversal) to traverse to view and FIG no, the same undirected graph to FIG.
//深度优先
 public void  DFS(String vertexName){
            int id=getIdOfVertexName(vertexName);
            if(id==-1)return;
            vertexs.get(id).setMarked(true);
            System.out.println("遍历到"+vertexs.get(id).getName());
            List<Vertex> neighbors = getNeighbors(vertexs.get(id));
            for(int i=0;i<neighbors.size();i++){
                if(!neighbors.get(i).isMarked()){
                    DFS(neighbors.get(i).getName());
                }
            }
        }
//广度优先
public void BFS(String vertexName){
            int startID=getIdOfVertexName(vertexName);
            if(startID==-1) return;
            List<Vertex> q=new ArrayList<Vertex>();
            q.add(vertexs.get(startID));
            vertexs.get(startID).setMarked(true);
            while(!q.isEmpty()){
                Vertex curVertex=q.get(0);
                q.remove(0);
                System.out.println("遍历到"+curVertex.getName());
                List<Vertex> neighbors = getNeighbors(curVertex);
                for(int i=0;i<neighbors.size();i++){
                    if(!neighbors.get(i).isMarked()){
                        neighbors.get(i).setMarked(true);
                        q.add(neighbors.get(i));
                    }
                }

            }

        }
  • First time topological sorting, undirected graph is not done, there can only be directed graph. Topological adjacency matrix, MAX_VALUE table 0.
public void topSort(){
            int[][] tmpEdges=edges;
            int IDofNullPreVertex=getNullPreVertexID(tmpEdges);//获得当前图中无前驱的节点
            while(IDofNullPreVertex!=-1){
                vertexs.get(IDofNullPreVertex).setMarked(true);
                topVertexs.add(vertexs.get(IDofNullPreVertex));//拓扑序列增加
                //边销毁
                for(int j=0;j<this.vertexs.size();j++){
                    if(tmpEdges[IDofNullPreVertex][j]!=Integer.MAX_VALUE){
                        tmpEdges[IDofNullPreVertex][j]=Integer.MAX_VALUE;
                    }
                }
                IDofNullPreVertex=getNullPreVertexID(tmpEdges);
            }

        }
  • Undirected complete graph minimum spanning tree (Prim algorithm or algorithms can Kruscal), and outputs
    and can not be updated to the minimum spanning tree of FIG.
public int[][] getMinTree(){
            initMinTree();//初始化最小生成树
            while(!allVisited()){
                Vertex vertex = vertexs.get(getNotMarkedMinVertex());//设置处理节点
                System.out.println("处理:节点"+vertex.getName());
                //顶点已经计算出最短路径,设置为"已访问"
                vertex.setMarked(true);
                //获取所有"未访问"的邻居
                List<Vertex> neighbors = getNeighbors(vertex);
                System.out.println("邻居个数为:"+neighbors.size());
                //更新最小生成树
                updateMinEdge(vertex, neighbors);
            }
            System.out.println("search over");
            setMinTree();

            return minTree;
        }

Can be updated in accordance with changes in the minimum spanning tree of FIG.

 public void  updateMinEdge(Vertex vertex, List<Vertex> neighbors){
            //参数检测
            if(!isInGraph(vertex)){
                System.out.println("当前节点不在图中");
                return ;
            }

            for(Vertex neighbor: neighbors){
                int distance = edges[getIdOfVertexName(neighbor.getName())][getIdOfVertexName(vertex.getName())];
                if(neighbor.getAnotherIDinminEdge()==-1){
                    neighbor.setAnotherIDinminEdge(getIdOfVertexName(vertex.getName()));
                    System.out.println(neighbor.getName()+" setEdge To"+vertex.getName()+edges[neighbor.getAnotherIDinminEdge()][getIdOfVertexName(neighbor.getName())]);
                }
                else if(distance <  edges[getIdOfVertexName(neighbor.getName())][neighbor.getAnotherIDinminEdge()]){
                    neighbor.setAnotherIDinminEdge(getIdOfVertexName(vertex.getName()));
                    System.out.println(neighbor.getName()+" setEdge To"+vertex.getName()+edges[neighbor.getAnotherIDinminEdge()][getIdOfVertexName(neighbor.getName())]);
                }
            }
        }
  • Finally, the shortest path generated

Firstly to find the shortest path sentinel

public void search(){
            while(!unVisited.isEmpty()){
                Vertex vertex = unVisited.element();
                //顶点已经计算出最短路径,设置为"已访问"
                vertex.setMarked(true);
                List<Vertex> neighbors = getNeighbors(vertex);
                //更新邻居的最短路径
                updatesDistance(vertex, neighbors);
                pop();
            }
            System.out.println("最短路径");
        }

The output is then updated neighbor shortest path for the

 public List<Vertex> getNeighbors(Vertex v) {
            //参数检测
            if(!isInGraph(v)){
                System.out.println("当前节点不在图中");
                return null;
            }
            List<Vertex> neighbors = new ArrayList<Vertex>();
            int position = vertexs.indexOf(v);
            Vertex neighbor = null;
            int distance;
            for (int i = 0; i < vertexs.size(); i++) {
                if (i == position) {
                    //顶点本身,跳过
                    continue;
                }
                distance = edges[position][i];    //到所有顶点的距离
                if (distance < Integer.MAX_VALUE) {
                    //是邻居(有路径可达)
                    neighbor = getVertex(i);
                    if (!neighbor.isMarked()) {
                        //如果邻居没有访问过,则加入list;
                        neighbors.add(neighbor);
                    }
                }
            }
            return neighbors;
        }

3. Experimental problems encountered in the process and settlement process

  • Question 1: When is generated during the map, I chose to write data to map the way to read and write files

  • Problem 1 Solution: I have problems reading and writing tasks in a way, I was drawing double-loop input file to ,, and with the line "" separated, but when you enter "0" is not set in the output loop conditions, it will be an infinite loop when traveling to each "0."
  • Question 2: During enter, process the shortest path nodes, and now I'm stuck back in the first node.

  • 问题2解决方法:邻接矩阵的输入方式存在敕位问题。

其他(感悟、思考等)

在进行运算生成图的时候,选择链表进行图的生成时,后续的排序会比较麻烦,而且在进行图的顶点的各种操作时(读取),顶点的入度出度会显示。关键在进行各种排序时,还是,还有需要报错程序,这样找不到顶点可以自动返回。

参考资料

Guess you like

Origin www.cnblogs.com/lyz182329/p/11994388.html