zoukankan      html  css  js  c++  java
  • DataStructGraphpart1

      1 package graphic;
      2 import queue.*;
      3 
      4 public class AdjacencyGraph implements Graph {
      5     
      6     private final int MaxValue = 1000;    // for not conjoint
      7     private int nNode;                  // numbers of node
      8     private int nEdge;                    // numbers of edge
      9     private int type;             // type of graphic
     10     private int[][] adjacency;          // asume the type of element is int
     11     
     12     public int MaxValue() {
     13         return MaxValue;                // return the const MaxValue
     14     }
     15     
     16     public int[][] getArray() {
     17         return adjacency;                // return adjacency 
     18     }
     19     
     20     public AdjacencyGraph(int nNode, int type) {
     21         if (type < 1 || type > 3) {
     22             System.out.print("Initial failed!\n");
     23             System.exit(1);
     24         }
     25         
     26         this.nNode = nNode;
     27         nEdge = 0;
     28         this.type = type;
     29         this.adjacency = new int[nNode][nNode];
     30         
     31         for (int i = 0; i < nNode; i++) {
     32             for (int j = 0; j < nNode; j++) {
     33                 if (i == j) {
     34                     adjacency[i][j] = 0;
     35                 } else if (type == 0 || type == 2) {
     36                     adjacency[i][j] = 0;
     37                 } else {
     38                     adjacency[i][j] = MaxValue;
     39                 }
     40             }
     41         }
     42     }
     43     
     44     @Override
     45     public void createGraph(EdgeElement[] d) {  
     46         // TODO Auto-generated method stub
     47         int i = 0;
     48         for (; i < d.length; i++) {
     49             int v1 = d[i].fromVex;
     50             int v2 = d[i].endVex; 
     51             verify(v1, v2);
     52             if (type == 0) {
     53                 adjacency[v1][v2] = adjacency[v2][v1] = 1;
     54             } else if (type == 1) {
     55                 adjacency[v1][v2] = adjacency[v2][v1] = d[i].weight;
     56             } else if (type == 2) {
     57                 adjacency[v1][v2] = 1;
     58             } else {
     59                 adjacency[v1][v2] = d[i].weight;
     60             }
     61         }
     62         nEdge = i;
     63     }
     64 
     65     @Override
     66     public int graphType() {
     67         // TODO Auto-generated method stub
     68         return type;
     69     }
     70 
     71     @Override
     72     public int vertices() {
     73         // TODO Auto-generated method e
     74         return nNode;
     75     }
     76 
     77     @Override
     78     public int edges() {
     79         // TODO Auto-generated method stub
     80         return nEdge;
     81     }
     82 
     83     @Override
     84     public boolean find(int i, int j) {
     85         // TODO Auto-generated method stub
     86         
     87         if (adjacency[i][j] != 0 && adjacency[i][j] != MaxValue) {
     88             return true;
     89         }
     90         return false;
     91     }
     92 
     93     @Override
     94     public void putEdge(EdgeElement edge) {
     95         // TODO Auto-generated method stub
     96         int v1 = edge.fromVex;
     97         int v2 = edge.endVex;
     98         verify(v1, v2);
     99         if (adjacency[v1][v2] == 0 || adjacency[v1][v2] == MaxValue) {
    100             nEdge++;
    101         } else {
    102             System.out.print("the edge is already exits!");
    103             System.exit(1);
    104         }
    105         if (type == 0) {
    106             adjacency[v1][v2] = adjacency[v2][v1] = 1;
    107         } else if (type == 1) {
    108             adjacency[v1][v2] = adjacency[v2][v1] = edge.weight;
    109         } else if (type == 2) {
    110             adjacency[v1][v2] = 1;
    111         } else {
    112             adjacency[v1][v2] = edge.weight;
    113         }
    114     }
    115 
    116     @Override
    117     public void removeEdge(int i, int j) {
    118         // TODO Auto-generated method stub
    119         verify(i, j);
    120         if (adjacency[i][j] == 0 || adjacency[i][j] == MaxValue) {
    121             System.out.println("the edge you want to remove is not exits!");
    122             System.exit(1);
    123         }
    124         if (type == 0) {
    125             adjacency[i][j] = adjacency[j][i] = 0;
    126         } else if (type == 1) {
    127             adjacency[i][j] = adjacency[j][i] = MaxValue;
    128         } else if (type == 2) {
    129             adjacency[i][j] = 0;
    130         } else {
    131             adjacency[i][j] = MaxValue;
    132         }
    133         nEdge--;         // do not forget
    134     }
    135 
    136     @Override
    137     public int degree(int i) {
    138         int nDegree = 0;
    139         if (type < 2) {
    140             for (int j = 0; j < nNode; j++) {
    141                 if (adjacency[i][j] != 0 || adjacency[i][j] == MaxValue) {
    142                     nDegree++;
    143                 }
    144             }
    145         } else {
    146             nDegree = inDegree(i) + outDegree(i);
    147         }
    148         return nDegree;
    149     }
    150     
    151     @Override
    152     public int inDegree(int i) {
    153         // TODO Auto-generated method stub
    154         verify(i);
    155         int nDegree = 0;
    156         if (type < 2) {
    157             nDegree = -1;
    158         } else {
    159             for (int j = 0; j < nNode; j++) {
    160                 if (adjacency[j][i] != 0 && adjacency[j][i] != MaxValue) {
    161                     nDegree++;
    162                 }
    163             }
    164         }
    165         
    166         return nDegree;
    167     }
    168 
    169     @Override
    170     public int outDegree(int i) {
    171         // TODO Auto-generated method stub
    172         verify(i);
    173         int nDegree = 0;
    174         if (type < 2) {
    175             nDegree = -1;
    176         } else {
    177             for (int j = 0; j < nNode; j++) {
    178                 if (adjacency[i][j] != 0 && adjacency[i][j] != MaxValue) {
    179                     nDegree++;
    180                 }
    181             }
    182         }
    183         
    184         return nDegree;
    185     }
    186 
    187     @Override
    188     public void depthFirstSearch(int v) {    // start search from node v
    189         // TODO Auto-generated method stub
    190         boolean[] visited = new boolean[nNode];
    191         for (int i = 0; i < nNode; i++) {
    192             visited[i] = false;
    193         }
    194         dfs(v, visited);
    195         System.out.println();
    196     }
    197 
    198     @Override
    199     public void breadthFirstSearch(int v) {
    200         // TODO Auto-generated method stub
    201         boolean[] visited = new boolean[nNode];
    202         for (int i = 0; i < nNode; i++) {
    203             visited[i] = false;
    204         }
    205         bfs(v, visited);
    206         System.out.println();
    207     }
    208     
    209     private void dfs(int i, boolean[] visited) {
    210         System.out.print(i + " ");
    211         visited[i] = true;
    212         for (int j = 0; j < nNode; j++) {
    213             if (adjacency[i][j] != 0 && adjacency[i][j] != MaxValue && !visited[j]) {
    214                 dfs(j, visited);
    215             }
    216         }
    217     }
    218     
    219     private void bfs(int i, boolean[] visited) {
    220         SequenceQueue queue = new SequenceQueue();
    221         System.out.print(i + " ");
    222         visited[i] = true;
    223         queue.add(i);
    224         
    225         while (!queue.isEmpty()) {
    226             int k = (Integer)queue.remove();
    227             for (int j = 0; j < nNode; j++) {
    228                 if (adjacency[k][j] != 0 && adjacency[k][j] != MaxValue && !visited[j]) {
    229                     System.out.print(j + " ");
    230                     visited[j] = true;
    231                     queue.add(j);
    232                 }
    233             }
    234         }
    235     }
    236     
    237     public void output() {
    238         int i, j;
    239         System.out.print("V = {");
    240         for (i = 0; i < nNode-1; i++) {
    241             System.out.print(i + ", ");
    242         }
    243         System.out.println(nNode-1 + "}");
    244         if (type == 0 || type == 2) {
    245             for (i = 0; i < nNode; i++) {
    246                 for (j = 0; j < nNode; j++) {
    247                     if (type == 0) {    // unweighted, undirected
    248                         if (i < j) {
    249                             System.out.print("(" + i + ", " + j + "), ");
    250                         }
    251                     } else {            // unweighted, directed
    252                         System.out.print("<" + i + ", " + j + ">, ");
    253                     }
    254                 }
    255             }
    256         } else {
    257             for (i = 0; i < nNode; i++) {
    258                 for (j = 0; j < nNode; j++) {
    259                     if (adjacency[i][j] != 0 && adjacency[i][j] != MaxValue) {
    260                         if (type == 1) {// weighted, undirected
    261                             System.out.print("(" + i + ", " + j + "), " + adjacency[i][j] + ", ");
    262                         } else {        // weighted, directed
    263                             System.out.print("<" + i + ", " + j + ">, " + adjacency[i][j] + ", ");
    264                         }
    265                     }
    266                 }
    267             }
    268         }
    269         System.out.println("}");
    270     }
    271     
    272     public void verify(int... args) {        // some refactor
    273         if (args.length == 1) {
    274             if (args[0] > nNode-1 || args[0] < 0) {
    275                 System.out.println("the invalid position!");
    276                 System.exit(1);
    277             }
    278         } else {
    279             if (args[0] < 0 || args[0] > nNode-1 || args[1] < 0 || args[1] > nNode-1 || args[0] == args[1]) {
    280                 System.out.println("invalid position!");
    281                 System.exit(1);
    282             }
    283         }
    284     }
    285     
    286     public static void main(String[] args) {
    287         Graph graph = new AdjacencyGraph(5, 3);
    288         int[][] a = {
    289                 {0, 1, 2}, {0, 2, 3}, {0, 3, 8}, {1, 3, 12}, 
    290                 {2, 0, 6}, {2, 3, 6}, {2, 4, 1}, {3, 4, 4}
    291         };
    292         
    293         EdgeElement[] matrix = new EdgeElement[a.length];
    294         for (int i = 0; i < matrix.length; i++) {
    295             matrix[i] = new EdgeElement(a[i][0], a[i][1], a[i][2]);
    296         }
    297         
    298         graph.createGraph(matrix);
    299         
    300         System.out.println("the sequence of deep first search start from 0: ");
    301         graph.depthFirstSearch(0);
    302         
    303         System.out.println("the sequence of bread frist search start from 0: ");
    304         graph.breadthFirstSearch(0);
    305         
    306         graph.output();
    307         
    308         System.out.println("the degree, indegree & outdegree of 0: ");
    309         System.out.println(graph.degree(0) + ", " + graph.inDegree(0) + ", " + graph.outDegree(0));
    310         
    311         graph.putEdge(new EdgeElement(4, 1, 5));
    312         
    313         graph.removeEdge(0, 2);
    314
    315 System.out.println("Is there exits a edge: (2, 3)?" + graph.find(2, 3));
    316
    317         graph.output();
    318         
    319         System.out.println("the type of the graph: " + graph.graphType() + "\n" +
    320                            "the vertices of the graph: " + graph.vertices() + "\n" + 
    321                            "the edges of the graph: " + graph.edges());
    322     }
    323 }

     图的边类:

     1  1 package graphic;
     2  2 
     3  3 public class EdgeElement {
     4  4     int fromVex;
     5  5     int endVex;
     6  6     int weight;
     7  7     
     8  8     public EdgeElement(int from, int end) {
     9  9         fromVex = from;
    10 10         endVex = end;
    11 11         weight = 1;
    12 12     }
    13 13     
    14 14     public EdgeElement(int from, int end, int wgt) {
    15 15         fromVex = from;
    16 16         endVex = end;
    17 17         weight = wgt;
    18 18     }
    19 19 }
    1 1 package graphic;
    2 2 
    3 3 public enum GraphType {
    4 4     unweighted_undirected_graph,  // refer to 0
    5 5     weighted_undirected_graph,    // ... 1
    6 6     unweighted_directed_graph,    // ... 2
    7 7     weighted_directed_graph          // ... 3
    8 8 }
     1 package graphic;
     2 
     3 public interface Graph {
     4     
     5     void createGraph(EdgeElement[] d);    // 根据边集数组参数d建立一个图
     6      7     int graphType();                    // 返回图类型
     8     
     9     int vertices();                        // 返回图中顶点数
    10     
    11     int edges();                        // 返回图中边数 
    12     
    13     boolean find(int i, int j);            // 查找(i, j)边是否存在
    14     
    15     void putEdge(EdgeElement thEdge);    // 插入一条边
    16     
    17     void removeEdge(int i, int j);        // 删除一条(i, j)边
    18     
    19     int degree(int i);
    20     
    21     int inDegree(int i);                // 返回顶点i的入度
    22     
    23     int outDegree(int i);                // 返回顶点i的出度
    24     
    25     void output();                        // 输出
    26     
    27     void depthFirstSearch(int v);        // 从v开始深度优先
    28     
    29     void breadthFirstSearch(int v);        // 从v开始广度优先
    30 }
     先贴下源码,下次给注解。。。
  • 相关阅读:
    <转>css选择器基本语法
    Pycharm错误提示
    Python继承Selenium2Library
    对于框架设计的一点总结
    <转>自动化框架设计思想
    svn检出项目报错
    eclipse查看jar包源文件
    plsql连接远程数据库快捷方式
    plsql过期注册
    hql语句cast用法
  • 原文地址:https://www.cnblogs.com/thoupin/p/2975611.html
Copyright © 2011-2022 走看看