您的位置:首页 > 理论基础 > 数据结构算法

java数据结构和算法------图

2015-07-22 09:51 447 查看
package iYou.neugle.graph;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class MyGraph1 {
private MatrixGraph graph;

public MatrixGraph getGraph() {
return graph;
}

public void setGraph(MatrixGraph graph) {
this.graph = graph;
}

// 使用邻接矩阵作为存储单元
class MatrixGraph {
public int maxNum = 10;
public int[][] edge;
public boolean[] isVisited;
public int type;// 0表示无向图,1表示有向图

public MatrixGraph(int maxNum, int type) {
this.maxNum = maxNum;
this.type = type;
this.edge = new int[this.maxNum][this.maxNum];
this.isVisited = new boolean[this.maxNum];
}
}

public MyGraph1(int maxNum, int type) {
this.graph = new MatrixGraph(maxNum, type);
}

// 创建邻接矩阵 (带权值)
public void CreateMaxtrixGraph(int i1, int i2, int w) {
this.graph.edge[i1 - 1][i2 - 1] = w;
// 如果为无向图 则对称位置也要置为w
if (this.graph.type == 0) {
this.graph.edge[i2 - 1][i1 - 1] = w;
}
}

// 创建邻接矩阵 (不带权值)
public void CreateMaxtrixGraph(int i1, int i2) {
this.graph.edge[i1 - 1][i2 - 1] = 1;
// 如果为无向图 则对称位置也要置为1
if (this.graph.type == 0) {
this.graph.edge[i2 - 1][i1 - 1] = 1;
}
}

// 遍历邻接矩阵
public void OutPutMaxtrixGraph() {
int[][] edge = this.graph.edge;
for (int i = 0; i < edge.length; i++) {
if (i == 0) {
System.out.print("  ");
for (int j = 0; j < edge[i].length; j++) {
System.out.print((j + 1) + " ");
}
System.out.println();
}
System.out.print(i + 1 + " ");
for (int j = 0; j < edge[i].length; j++) {
System.out.print(edge[i][j] + " ");
}
System.out.println();
}
}

// 深度优先遍历
public void DFSTraverse() {
// 初始化
for (int i = 0; i < this.graph.isVisited.length; i++) {
this.graph.isVisited[i] = false;
}
// 采用栈的数据结构
Stack<Integer> stack = new Stack<>();
System.out.print("深度优先遍历的结果为: ");
for (int i = 0; i < this.graph.edge.length; i++) {
if (!this.graph.isVisited[i]) {
stack.push(i);
while (!stack.isEmpty()) {
int n = stack.pop();
if (!this.graph.isVisited
) {
System.out.print(n + 1 + " ");
} else {
continue;
}
this.graph.isVisited
= true;
for (int j = this.graph.edge
.length - 1; j >= 0; j--) {
if (this.graph.edge
[j] == 1) {
stack.push(j);
}
}
}
}
}
System.out.println();
}

// 广度优先遍历
public void BFSTraverse() {
// 初始化
for (int i = 0; i < this.graph.isVisited.length; i++) {
this.graph.isVisited[i] = false;
}
// 采用队列的数据结构
Queue<Integer> queue = new LinkedList<>();
System.out.print("广度优先遍历的结果为: ");
for (int i = 0; i < this.graph.edge.length; i++) {
if (!this.graph.isVisited[i]) {
queue.add(i);
while (!queue.isEmpty()) {
int n = queue.remove();
if (!this.graph.isVisited
) {
System.out.print(n + 1 + " ");
} else {
continue;
}
this.graph.isVisited
= true;
for (int j = 0; j < this.graph.edge
.length; j++) {
if (this.graph.edge
[j] == 1) {
queue.add(j);
}
}
}
}
}
System.out.println();
}

// 主函数
public static void main(String[] args) {
MyGraph1 myGraph = new MyGraph1(5, 0);
myGraph.CreateMaxtrixGraph(1, 2);
myGraph.CreateMaxtrixGraph(1, 3);
myGraph.CreateMaxtrixGraph(1, 5);
myGraph.CreateMaxtrixGraph(2, 4);
myGraph.CreateMaxtrixGraph(3, 5);
myGraph.CreateMaxtrixGraph(4, 5);
myGraph.OutPutMaxtrixGraph();
myGraph.DFSTraverse();
myGraph.BFSTraverse();
}
}


1 2 3 4 5
1 0 1 1 0 1
2 1 0 0 1 0
3 1 0 0 0 1
4 0 1 0 0 1
5 1 0 1 1 0
深度优先遍历的结果为: 1 2 4 5 3
广度优先遍历的结果为: 1 2 3 5 4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: