zoukankan      html  css  js  c++  java
  • java数据结构和算法------图

      1 package iYou.neugle.graph;
      2 
      3 import java.util.LinkedList;
      4 import java.util.Queue;
      5 import java.util.Stack;
      6 
      7 public class MyGraph1 {
      8     private MatrixGraph graph;
      9 
     10     public MatrixGraph getGraph() {
     11         return graph;
     12     }
     13 
     14     public void setGraph(MatrixGraph graph) {
     15         this.graph = graph;
     16     }
     17 
     18     // 使用邻接矩阵作为存储单元
     19     class MatrixGraph {
     20         public int maxNum = 10;
     21         public int[][] edge;
     22         public boolean[] isVisited;
     23         public int type;// 0表示无向图,1表示有向图
     24 
     25         public MatrixGraph(int maxNum, int type) {
     26             this.maxNum = maxNum;
     27             this.type = type;
     28             this.edge = new int[this.maxNum][this.maxNum];
     29             this.isVisited = new boolean[this.maxNum];
     30         }
     31     }
     32 
     33     public MyGraph1(int maxNum, int type) {
     34         this.graph = new MatrixGraph(maxNum, type);
     35     }
     36 
     37     // 创建邻接矩阵 (带权值)
     38     public void CreateMaxtrixGraph(int i1, int i2, int w) {
     39         this.graph.edge[i1 - 1][i2 - 1] = w;
     40         // 如果为无向图 则对称位置也要置为w
     41         if (this.graph.type == 0) {
     42             this.graph.edge[i2 - 1][i1 - 1] = w;
     43         }
     44     }
     45 
     46     // 创建邻接矩阵 (不带权值)
     47     public void CreateMaxtrixGraph(int i1, int i2) {
     48         this.graph.edge[i1 - 1][i2 - 1] = 1;
     49         // 如果为无向图 则对称位置也要置为1
     50         if (this.graph.type == 0) {
     51             this.graph.edge[i2 - 1][i1 - 1] = 1;
     52         }
     53     }
     54 
     55     // 遍历邻接矩阵
     56     public void OutPutMaxtrixGraph() {
     57         int[][] edge = this.graph.edge;
     58         for (int i = 0; i < edge.length; i++) {
     59             if (i == 0) {
     60                 System.out.print("  ");
     61                 for (int j = 0; j < edge[i].length; j++) {
     62                     System.out.print((j + 1) + " ");
     63                 }
     64                 System.out.println();
     65             }
     66             System.out.print(i + 1 + " ");
     67             for (int j = 0; j < edge[i].length; j++) {
     68                 System.out.print(edge[i][j] + " ");
     69             }
     70             System.out.println();
     71         }
     72     }
     73 
     74     // 深度优先遍历
     75     public void DFSTraverse() {
     76         // 初始化
     77         for (int i = 0; i < this.graph.isVisited.length; i++) {
     78             this.graph.isVisited[i] = false;
     79         }
     80         // 采用栈的数据结构
     81         Stack<Integer> stack = new Stack<>();
     82         System.out.print("深度优先遍历的结果为: ");
     83         for (int i = 0; i < this.graph.edge.length; i++) {
     84             if (!this.graph.isVisited[i]) {
     85                 stack.push(i);
     86                 while (!stack.isEmpty()) {
     87                     int n = stack.pop();
     88                     if (!this.graph.isVisited[n]) {
     89                         System.out.print(n + 1 + " ");
     90                     } else {
     91                         continue;
     92                     }
     93                     this.graph.isVisited[n] = true;
     94                     for (int j = this.graph.edge[n].length - 1; j >= 0; j--) {
     95                         if (this.graph.edge[n][j] == 1) {
     96                             stack.push(j);
     97                         }
     98                     }
     99                 }
    100             }
    101         }
    102         System.out.println();
    103     }
    104 
    105     // 广度优先遍历
    106     public void BFSTraverse() {
    107         // 初始化
    108         for (int i = 0; i < this.graph.isVisited.length; i++) {
    109             this.graph.isVisited[i] = false;
    110         }
    111         // 采用队列的数据结构
    112         Queue<Integer> queue = new LinkedList<>();
    113         System.out.print("广度优先遍历的结果为: ");
    114         for (int i = 0; i < this.graph.edge.length; i++) {
    115             if (!this.graph.isVisited[i]) {
    116                 queue.add(i);
    117                 while (!queue.isEmpty()) {
    118                     int n = queue.remove();
    119                     if (!this.graph.isVisited[n]) {
    120                         System.out.print(n + 1 + " ");
    121                     } else {
    122                         continue;
    123                     }
    124                     this.graph.isVisited[n] = true;
    125                     for (int j = 0; j < this.graph.edge[n].length; j++) {
    126                         if (this.graph.edge[n][j] == 1) {
    127                             queue.add(j);
    128                         }
    129                     }
    130                 }
    131             }
    132         }
    133         System.out.println();
    134     }
    135 
    136     // 主函数
    137     public static void main(String[] args) {
    138         MyGraph1 myGraph = new MyGraph1(5, 0);
    139         myGraph.CreateMaxtrixGraph(1, 2);
    140         myGraph.CreateMaxtrixGraph(1, 3);
    141         myGraph.CreateMaxtrixGraph(1, 5);
    142         myGraph.CreateMaxtrixGraph(2, 4);
    143         myGraph.CreateMaxtrixGraph(3, 5);
    144         myGraph.CreateMaxtrixGraph(4, 5);
    145         myGraph.OutPutMaxtrixGraph();
    146         myGraph.DFSTraverse();
    147         myGraph.BFSTraverse();
    148     }
    149 }
      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 
  • 相关阅读:
    质因数分解
    P1939 【模板】矩阵加速(数列)
    UVALive
    Python操作MySQL:pymysql模块
    Mysql数据库基础
    Redis管道和发布订阅
    Redis常用操作
    Redis操作集合,有序集合
    Redis操作list
    Redis操作hash
  • 原文地址:https://www.cnblogs.com/niuxiaoha/p/4666458.html
Copyright © 2011-2022 走看看