zoukankan      html  css  js  c++  java
  • cs61b lab13

    part1:length2Paths():只能想出三层loop的办法,实在想不出快一些的办法了。但是这种算法时间复杂度是θ(n2),而且part2中length越长,时间复杂度越高,如果length是五的话,就是θ(n5),感觉过于慢了,应该是有更快的方法的。

    code:

    public UDGraph length2Paths() {
        UDGraph newGraph = new UDGraph(vertices);
        for(int i=0;i<vertices;i++){
            for(int j=0;j<vertices;j++){
                if(hasEdge(i,j)){
                    for(int k=0;k<vertices;k++){
                        if(hasEdge(j,k)){
                            newGraph.addEdge(i, k);
                        }
                            
                    }
                }
            }
        }
        return newGraph;
      }

    part2:在part一的基础上进行recursion,在length-1的基础上再进行一次类似于length2path的操作。

    代码:

     public UDGraph paths(int length) {
        UDGraph newGraph = new UDGraph(vertices);
        if(length<=1){
            return null;
        }
        else if(length==1){
              for(int i=0;i<vertices;i++){
                    for(int j=0;j<vertices;j++){
                        if(hasEdge(i,j)){
                            newGraph.addEdge(i, j);
                                    }
                        }
                    }
                }
        else if(length==2){
            newGraph=length2Paths();
        }
        else if(length>2){
            for(int i=0;i<vertices;i++)
                for(int j=0;j<vertices;j++){
                    if(paths(length-1).hasEdge(i, j)){
                        for(int k=0;k<vertices;k++){
                            if(hasEdge(j,k)){
                                newGraph.addEdge(i, k);
                            }
                        }
                    }
                }
        }
        return newGraph;
      }

    运行结果:

     *** Square the unweighted directed graph! *** 
    
    Creating a graph with 11 vertices
    
    The original graph is
    11 vertices and 17 edges
    . . . . . . . . t . .
    t . . t . . . . . . .
    t . . . . . . . . . .
    . . t . . t . . . . .
    . . t . . t . . . . .
    . . . . . . . t . t .
    . . . . t . . t . . .
    . . . . . . . . . . .
    . . . . t . t . . . t
    . t . . . . . . . . .
    . . . . . . t . . . .
    
    Testing length-2 paths.
    The graph of length-2 paths is
    11 vertices and 25 edges
    . . . . t . t . . . t
    . . t . . t . . t . .
    . . . . . . . . t . .
    t . . . . . . t . t .
    t . . . . . . t . t .
    . t . . . . . . . . .
    . . t . . t . . . . .
    . . . . . . . . . . .
    . . t . t t t t . . .
    t . . t . . . . . . .
    . . . . t . . t . . .
    
    Testing length-3 paths.
    The graph of length-3 paths is
    11 vertices and 34 edges
    . . t . t t t t . . .
    t . . . t . t t . t t
    . . . . t . t . . . t
    . t . . . . . . t . .
    . t . . . . . . t . .
    t . . t . . . . . . .
    t . . . . . . t . t .
    . . . . . . . . . . .
    t . t . t t . t . t .
    . . t . . t . . t . .
    . . t . . t . . . . .
    
    Testing length-4 paths.
    The graph of length-4 paths is
    11 vertices and 49 edges
    t . t . t t . t . t .
    . t t . t t t t t . .
    . . t . t t t t . . .
    t . . t t . t . . . t
    t . . t t . t . . . t
    . . t . . t . . t . .
    . t . . . . . . t . .
    . . . . . . . . . . .
    t t t . . t . t t t .
    t . . . t . t t . t t
    t . . . . . . t . t .
    
    Testing length-5 paths.
    The graph of length-5 paths is
    11 vertices and 63 edges
    t t t . . t . t t t .
    t . t t t t t t . t t
    t . t . t t . t . t .
    . . t . t t t t t . .
    . . t . t t t t t . .
    t . . . t . t t . t t
    t . . t t . t . . . t
    . . . . . . . . . . .
    t t . t t . t t t t t
    . t t . t t t t t . .
    . t . . . . . . t . .
    
     *** Good Job! *** 
  • 相关阅读:
    vim 常用命令
    centos 安装mysql
    centos部署ftp
    centos 6.8部署nginx
    ndk学习16: unix domain socket
    ndk学习14: 进程
    ndk学习13: proc
    ndk学习11: linux内存管理
    ndk学习10: linux文件系统
    ndk学习9: 动态使用共享库
  • 原文地址:https://www.cnblogs.com/lyz1995/p/7272734.html
Copyright © 2011-2022 走看看