转载:http://blog.youkuaiyun.com/yxmmao/article/details/51586540
1 . 创建图的邻接矩阵数据结构
public class MGraph {
int vexs;
char data[];
int [][]weight;
public MGraph(int ve){
vexs=ve;
data=new char[ve];
weight=new int[ve][ve];
}
}
2 . 创建图的邻接矩阵,显示邻接矩阵,实现图的深度遍历
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class GraphMetrix {
public void CreateGraph(MGraph graph,int vexs,char data[],int [][]weight){
int i,j;
for(i=0;i<vexs;i++){
graph.data[i]=data[i];
for(j=0;j<vexs;j++){
graph.weight[i][j]=weight[i][j];
}
}
}
public void ShowGraph(MGraph graph){
int vexs=graph.vexs;
int [][]weight=graph.weight;
int i,j;
for(i=0;i<vexs;i++){
System.out.print(graph.data[i]+":");
for(j=0;j<vexs;j++){
System.out.print(weight[i][j]+" ");
}
System.out.println();
}
}
public int GetFirst(MGraph graph,int k){
int i;
if(k<0||k>graph.vexs-1){
System.out.println("参数k值超出范围");
return -1;
}
for(i=0;i<graph.vexs;i++){
if(graph.weight[k][i]==1)
return i;
}
return -1;
}
public int GetNext(MGraph graph,int k,int t){
int i;
if(k<0||k>graph.vexs-1||t<0||t>graph.vexs-1){
System.out.println("参数k或t值超出范围");
return -1;
}
for(i=t+1;i<graph.vexs;i++){
if(graph.weight[k][i]==1)
return i;
}
return -1;
}
public void DFSVGraph(MGraph graph,int k,int visited[]){
int u;
System.out.print(graph.data[k]+", ");
visited[k]=1;
u=GetFirst(graph,k);
while(u!=-1){
if(visited[u]==0){
DFSVGraph(graph,u,visited);
}
u=GetNext(graph,k,u);
}
}
public void BFSVGraph(MGraph graph,int k,int visited[]){
Queue <Integer>queue=new LinkedList <Integer>();
int u;
queue.add(k);
visited[k]=1;
while(!queue.isEmpty()){
u=queue.remove();
System.out.print(graph.data[u]+", ");
int v=GetFirst(graph,u);
while(v!=-1){
if(visited[v]==0){
queue.add(v);
visited[v]=1;
}
v=GetNext(graph,u,v);
}
}
}
public static void main(String args[]){
char []data=new char[]{'A','B','C','D','E'};
int vexs=data.length;
int visited[]=new int [vexs];
int [][]weight=new int[][]{
{0,1,0,0,1},
{1,0,1,1,0},
{0,1,0,0,0},
{0,1,0,0,1},
{1,0,0,1,0}
};
for(int i=0;i<vexs;i++){
visited[i]=0;
}
MGraph graph=new MGraph(vexs);
GraphMetrix gam=new GraphMetrix();
gam.CreateGraph(graph,vexs,data,weight);
gam.ShowGraph(graph);
gam.BFSVGraph(graph, 0, visited);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124