# 20162316刘诚昊 2017-2018-2 《Java程序设计》第四次实验 图的实现与应用

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

### 实验过程：

http://blog.csdn.net/kelvinmao/article/details/51519284

http://blog.csdn.net/liangxiamoyi/article/details/52153972

public Graph_Matrix(){
graphSize = 0;
vertexkist = new String[graphSize];
this.edge=new int[100][100];
}


//插入顶点，返回其在顶点顺序表中的下标
public int insertVertex(T x){
//当创建的顶点顺序表长度不够，扩增
this.graphSize++;
if(graphSize == MAXGRAPHSIZE){
MAXGRAPHSIZE = MAXGRAPHSIZE *2;
Object[] N = new Object[MAXGRAPHSIZE];
int a = 0;
for(Object element : vertexkist){
N[a] = element;
a++;
}
vertexkist = N;
}
vertexkist[graphSize - 1] = x;

for(int i=0;i<this.graphSize;i++){
this.edge[i][graphSize-1]=0;
}
for(int i=0;i<this.graphSize;i++){
this.edge[graphSize-1][i]=0;
}
this.edge[graphSize-1][graphSize-1]=0;
return (graphSize);
}

//删除顶点
public void removeVertex(int v) {
if(v>=this.graphSize){
System.out.println("无此顶点");
return;
}
for(int a = v;a<graphSize;a++){
vertexkist[a] = vertexkist[a+1];
}
vertexkist[graphSize-1] = null;
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) throws ElementNotFoundException {
if(v1==v2||v1>this.graphSize||v2>this.graphSize||this.edge[v1][v2]!=0) {
throw new ElementNotFoundException ("插入失败 "
+ "参数错误");
}
this.edge[v1][v2]=1;
System.out.println("添加边成功");
return;
}

//删除边
public void deleteEdge(int v1,int v2) throws ElementNotFoundException {
if(v1==v2||v1>this.graphSize||v2>this.graphSize||this.edge[v1][v2]==0) {
throw new ElementNotFoundException ("插入失败 "
+ "参数错误");
}
this.edge[v1][v2]=0;
System.out.println("删除边成功");
return;
}


//深度遍历
public void DFS(){
boolean[] visited = new boolean[graphSize];
DFSafter(0,visited);
}
void DFSafter(int k,boolean visited[]){
int u;
System.out.print(vertexkist[k]+", ");
visited[k]=true;
u=GetFirst(k);
while(u!=-1) {
if (visited[u] != true) {  /
DFSafter(u, visited);
}
u = GetNext(k, u);
}
}

//广度遍历
public void BFS(int v,int[] visited) {
int next;
visited[v]=1;
while (!queue.isEmpty()) {
next=queue.remove();
System.out.print(next + ", ");
int vex = GetFirst(next);
while (vex!=-1) {
if (visited[vex]==0) {
visited[vex]=1;
}
vex=GetNext(next, vex);
}
}

}


## 实验四 图的实现与应用-2:

### 实验过程：

• 节点类：
private class VertexNode {
char vertex;
EdgeNode firstIn;
EdgeNode firstOut;
}

private class EdgeNode {
int tailvex;
}

• 边与点的操作：
 public int insertVertex(char data){
vlen++;
if (ver.length == 0){
ver = new char[1];
}
if(vlen == ver.length) {
int num = ver.length * 2;
char[] n = new char[num];
int b =0;
for (char element : ver){
n[b] = element;
b++;
}

ver = n;
}
ver[ver.length-1] = data;
return vlen;
}

public void inserEdge(int a,int b){
char[][] OLD = edges;

char[][] NEW = new char[OLD.length+1][OLD.length+1];
int c = 0;
for(char[] element:edges){
NEW[c] = element;
}

edges = NEW;

OListDG(ver,edges);
}
public void OListDG(char[] vexs, char[][] edges) {
vlen = vexs.length;
elen = edges.length;

// 初始化顶点,建立顶点表
vertexNodeList = new VertexNode[vlen];
for (int i = 0; i < vlen; i++) {
vertexNodeList[i] = new VertexNode();
vertexNodeList[i].vertex = vexs[i];
vertexNodeList[i].firstIn = null;
vertexNodeList[i].firstOut = null;
}

// 初始化边，利用头插法建立十字链表
for (int i = 0; i < elen; i++) {
EdgeNode edgeNode_1 = new EdgeNode();
EdgeNode edgeNode_2 = new EdgeNode();
int vi = getPosition(edges[i][0], vexs);
int vj = getPosition(edges[i][1], vexs);

edgeNode_1.tailvex = vi;
vertexNodeList[vi].firstOut = edgeNode_1;

edgeNode_2.tailvex = vi;
vertexNodeList[vj].firstIn = edgeNode_2;

}
}

• size与isEmpty
 public int size()
return vlen;
}

public boolean isEmpty(){
return ver.length == 0;
}


## 实验四 图的实现与应用-3：

实现PP19.9

### 点此返回目录

posted @ 2017-11-26 23:36  20162316刘诚昊  阅读(101)  评论(2编辑  收藏