20162323周楠 2017-2018-1 《程序设计与数据结构》实验报告四

20162323周楠 2017-2018-1 《程序设计与数据结构》实验报告四

目录预览

实验四-图的实现与应用-1


图的实现与应用-1

实验结果

实验过程

根据老师给的PPT


//插入边
    public
    void insertEdge(int i, int j, int weight)       
    {
        if (i != j)                                          //不能表示自身环
        {
            if (weight <= 0 || weight > MAX_WEIGHT)            
                weight = MAX_WEIGHT;
            this.matrix.set(i, j, weight);                  
        } else throw new IllegalArgumentException("不能插入自身环,i=" + i + ",j=" + j);
    }



//插入顶点
    public
    int insertVertex(T x)                           //插入元素为x的顶点,返回x顶点序号
    {
        int i = this.vertexlist.insert((T) x);                 //顶点顺序表尾插入x,返回x序号,自动扩容
        if (i >= this.matrix.getRows())                    //若邻接矩阵容量不够,
            this.matrix.setRowsColumns(i + 1, i + 1);           //矩阵扩容。保持邻接矩阵行列数同图的顶点数
        for (int j = 0; j < i; j++)                            //初始化第i行、列元素值为∞。i==j值已为0
        {
            this.matrix.set(i, j, MAX_WEIGHT);
            this.matrix.set(j, i, MAX_WEIGHT);
        }
        return i;                                          //返回插入顶点序号
    }


//删除边
    public
    void removeEdge(int i, int j)                   //删除边〈vi,vj〉,忽略权值
    {
        if (i != j) this.matrix.set(i, j, MAX_WEIGHT);             //设置边的权值为∞。若i、j越界,抛出序号越界异常
    }

    public
    void removeEdge(Exm4.Triple edge)                    //删除边,忽略权值
    {
        this.removeEdge(edge.row, edge.column);
    }

//删除顶点
public
void removeVertex(int i) //删除顶点vi及其所有关联的边
{
int n = this.vertexCount(); //原顶点数
if (i >= 0 && i < n) {
this.vertexlist.remove(i); //删除顶点顺序表第i个元素,顶点数减1。 //顺序表删除,若i越界,返回null
for (int j = i + 1; j < n; j++) //第i+1〜n-1行元素上移一行,n为原顶点数
for (int k = 0; k < n; k++)
this.matrix.set(j - 1, k, this.matrix.get(j, k));
for (int j = 0; j < n; j++)
for (int k = i + 1; k < n; k++) //第i+1〜n-1列元素左移一列
this.matrix.set(j, k - 1, this.matrix.get(j, k));
this.matrix.setRowsColumns(n - 1, n - 1); //邻接矩阵少一行一列
} else throw new IndexOutOfBoundsException("i=" + i); //抛出序号越界异常
}




![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126160820046-1132646595.png)









***


<h2 id="2">实验四-图的实现与应用-2</h2>

***

### 实验四-图的实现与应用-2
![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126161339468-564452846.png)




### 实验结果
![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126161135546-910048011.png)

#### 实验过程

public void insertVertex(){
for(int i=0;i<this.graphSize;i++){
this.edge[i][graphSize]=MAXWEIGHT;
}
for(int i=0;i<this.graphSize;i++){
this.edge[graphSize][i]=MAXWEIGHT;
}
this.edge[graphSize][graphSize]=0;
this.graphSize++;
System.out.println("插入成功!");
return ;
}


//删除顶点
public void deleteVertex(int v){
if(v>=this.graphSize){
System.out.println("无此顶点");
return;
}
for(int i=0;i<this.graphSize;i++){
this.edge[v][i]=0;
this.edge[i][v]=0;
}
if(v==this.graphSize-1){
this.graphSize--;
return;
}
for(int i=v+1;i<this.graphSize;i++){
for(int j=0;j<this.graphSize;j++){
this.edge[i-1][j]=this.edge[i][j];
}
}
this.graphSize--;
}


//插入一条边
public void insertEdge(int v1,int v2,int weight){
if(v1==v2||v1>this.graphSize||v2>this.graphSize||this.edge[v1][v2]!=MAXWEIGHT){
System.out.println("插入失败!");
return;
}
this.edge[v1][v2]=weight;
System.out.println("插入成功!");
return;
}


//插入一条边
public void insertEdge(int v1,int v2,int weight){
if(v1==v2||v1>this.graphSize||v2>this.graphSize||this.edge[v1][v2]!=MAXWEIGHT){
System.out.println("插入失败!");
return;
}
this.edge[v1][v2]=weight;
System.out.println("插入成功!");
return;
}


//删除一条边
public void deleteEdge(int v1,int v2){
if(v1v2||v1>this.graphSize||v2>this.graphSize||this.edge[v1][v2]MAXWEIGHT){
System.out.println("删除失败!");
return;
}
this.edge[v1][v2]=MAXWEIGHT;
System.out.println("删除成功");
return;
}

![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126162253750-1479583423.png)





***

<h2 id="3">实验四-图的实现与应用-3</h2>
![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126162800546-1748058938.png)




***

### 实验结果
![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126162921171-361374306.png)




#### 实验过程

if(m == null || m.length == 0|| m[0].length ==0 ||
m[0][0] != 1 || m[m.length-1][m[0].length - 1] != 1){
return 0;
}


int res = 0;
int[][] map = new int[m.length][m[0].length];
map[0][0] = 1;

Queue rQ = (Queue ) new LinkedList();
Queue cQ = (Queue ) new LinkedList();
rQ.add(0);
cQ.add(0);

int r = 0;
int c = 0;
while (!rQ.isEmpty()){
r = rQ.poll();
c = cQ.poll();
if(r == m.length- 1 && c == m[0].length - 1){
return map[r][c];
}
walkTo(map[r][c], r - 1, c, m, map, rQ, cQ);
walkTo(map[r][c], r + 1, c, m, map, rQ, cQ);
walkTo(map[r][c], r, c - 1, m, map, rQ, cQ);
walkTo(map[r][c], r, c + 1, m, map, rQ, cQ);
}
return res;
}


public void walkTo(int pre, int toR, int toC, int


int
if(toR < 0 || toR == m.length || toC < 0 || toC == m
m
return;
}
map
rQ.add(toR);
cQ.add(toC);
}

![](https://images2018.cnblogs.com/blog/1062821/201711/1062821-20171126182004312-1424863350.png)





***

<h2 id="4">资料参考</h2>

***
[路由选择算法](http://blog.csdn.net/down_load_111/article/details/53107630)
[Java实现模拟路由功能]( http://blog.csdn.net/dotnetstudio/article/details/47206129 )
[一个用Dijkstra算法实现的路由算法的java程序——8 GraphMain类](http://blog.csdn.net/ffee/article/details/527732)
[求最短通路值散列法(hash法、关键字地址计算法)](http://blog.csdn.net/hqm12345qw/article/details/52136008)
[[数据结构]散列表-链接法和开放寻址法 线性探查](http://blog.csdn.net/lady_lili/article/details/52374248)


***

<h2 id="5">代码托管</h2>

***

[代码托管](https://gitee.com/pdds2017/zn20162323_JavaFoundations2nd/commit/f96fbf00c598d4b858a73ae67cdff0df8484c68e)

***


### PSP(Personal Software Process)时间
|        步骤    | 耗时 | 百分比 |
| --------   | :----------------:|:----------------:|
| 需求分析        | 40min         |      6%      | 
| 代码实现      | 500min          |   75.8%           | 
| 测试      | 90min          |   13.6%          | 
| 总结      | 30min          |   4.6%         | 








***

posted on 2017-11-26 18:39  GiggleKV  阅读(275)  评论(0)    收藏  举报

导航