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! *** 

 

posted @ 2017-08-02 10:55  想做码农的熊孩子  阅读(298)  评论(0编辑  收藏  举报