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
相关文章推荐
- 数据结构——二叉树的遍历
- linux常用c函数 数据结构及算法篇
- 数据结构(二):线性表包括顺序存储结构(顺序表、顺序队列和顺序栈)和链式存储结构(链表、链队列和链栈)
- 数据结构与算法(4、约瑟夫环问题到循环链表)
- 00-自测5. Shuffling Machine (20)
- 00-自测4. Have Fun with Numbers (20)
- 00-自测3. 数组元素循环右移问题 (20)
- 00-自测2. 素数对猜想 (20)
- 00-自测1. 打印沙漏(20)
- 数据结构和设计模式05(字符串)
- HDU 5016 Mart Master II (树上点分治)
- 数据结构和设计模式04(hash表)
- 数据结构——士兵队列训练问题
- 数据结构——队列问题——解题分析
- JAVA学习数据结构
- 【玩转cocos2d-x之二十六】数据结构CCDictionary
- 【玩转cocos2d-x之二十五】数据结构CCArray
- 数据结构——栈
- 数据结构—栈
- 宝典——数据结构和设计模式