java数据结构--图

1.    建立图的顶点

[java] view plain copy
  1. package com.jzm.Graph;  
  2.   
  3. public class GraphNode {  
  4.     public GraphNode   link;  
  5.     public int   info;  
  6. }  

2.  建立图的前驱和后继点

[java] view plain copy
  1. package com.jzm.Graph;  
  2.   
  3. public class GraphList {  
  4.     public GraphNode first;  
  5.     public GraphNode last;  
  6.     public boolean   visitable;  
  7.     public int getAjd(int[] ajd) {  
  8.         GraphNode current = first;  
  9.         int length = 0;  
  10.         while(current != null) {  
  11.             ajd[length++] = current.info;  
  12.             current = current.link;  
  13.         }  
  14.         return length;  
  15.     }  
  16.     public void addNode(int v) {  
  17.         GraphNode node = new GraphNode();  
  18.         node.info = v;  
  19.         if(first == null) {  
  20.             first = node;  
  21.             last = node;  
  22.         } else {  
  23.             last.link = node;  
  24.             last = node;  
  25.         }  
  26.     }  
  27. }  


3. 写一个队列

[java] view plain copy
  1. package com.jzm.Graph;  
  2.   
  3. public class Queue {  
  4.     public GraphNode first;  
  5.     public GraphNode last;  
  6.     public int count;  
  7.     public void addQueue(int info) {  
  8.         GraphNode node = new GraphNode();  
  9.         node.info = info;  
  10.         if(first == null) {  
  11.             first = node;  
  12.             last = node;  
  13.         } else {  
  14.             last.link = node;  
  15.             last = last.link;  
  16.         }  
  17.         count++;  
  18.     }   
  19.       
  20.     public void deleteQueue() {  
  21.         if(first == null) {  
  22.             System.out.println("null queue");  
  23.         } else {  
  24.             first = first.link;  
  25.             count--;  
  26.         }  
  27.     }  
  28.       
  29.     public boolean isEmpty() {  
  30.         return count == 0;  
  31.     }  
  32.       
  33.     public int front() {  
  34.         if(first == null) {  
  35.             return -1;  
  36.         }  
  37.         return first.info;  
  38.     }  
  39.       
  40.     public int back() {  
  41.         if(last == null) {  
  42.             return -1;  
  43.         }  
  44.         return last.info;  
  45.     }  
  46. }  

4.   建立图

[java] view plain copy
  1. package com.jzm.Graph;  
  2.   
  3. public class Graph {  
  4.     private int length;  
  5.     private GraphList[] list;  
  6.     private int[][]     weight;  
  7.       
  8.     public Graph(int length) {  
  9.         this.length = length;  
  10.         list = new  GraphList[length];  
  11.         weight = new int[length][length];  
  12.     }  
  13.   
  14.       
  15.     public void   dfs(int v) {  
  16.         int[] ajd = new int[length];  
  17.         int ajdlength = list[v].getAjd(ajd);  
  18.         list[v].visitable = true;  
  19.         System.out.print(v + " ");  
  20.         for (int i = 0; i < ajdlength; i++) {  
  21.             int w = ajd[i];  
  22.             if (!list[w].visitable) {  
  23.                 dfs(w);  
  24.             }  
  25.         }  
  26.     }  
  27.   
  28.       
  29.       
  30.     // 深度优先遍历  
  31.     public void dfsTravel() {  
  32.         for (int i = 0; i < length; i++) {  
  33.             list[i].visitable = false;  
  34.         }  
  35.         for (int i = 0; i < length; i++) {  
  36.             if (!list[i].visitable) {  
  37.                 dfs(i);  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     // 广度优先遍历  
  43.     public void bfsTravel() {  
  44.         for (int i = 0; i < length; i++) {  
  45.             list[i].visitable = false;  
  46.         }  
  47.         bfs();  
  48.     }  
  49.   
  50. private void bfs() {  
  51.         Queue   queue =  new Queue();  
  52.         for (int index = 0; index < length; index++) {  
  53.             if (!list[index].visitable) {  
  54.                 queue.addQueue(index);  
  55.                 list[index].visitable = true;  
  56.                 System.out.print(index + " ");  
  57.                 while (!queue.isEmpty()) {  
  58.                     int temp = queue.front();  
  59.                     queue.deleteQueue();  
  60.                     int[] ajd = new int[length];  
  61.                     int ajdlength = list[temp].getAjd(ajd);  
  62.                     for (int i = 0; i < ajdlength; i++) {  
  63.                         int w = ajd[i];  
  64.                         if (!list[w].visitable) {  
  65.                             System.out.print(w + " ");  
  66.                             queue.addQueue(w);  
  67.                             list[w].visitable = true;  
  68.                         }  
  69.                     }  
  70.                 }  
  71.             }  
  72.   
  73.         }  
  74.     }  
  75.   
  76.     // 最短路径  
  77.     private int[] shortPath(int v) {  
  78.         int[] shortPath = new int[length];  
  79.         boolean[]   weightFound = new boolean[length];  
  80.         for (int i = 0; i < length; i++) {  
  81.                                                 // 趋近无穷  
  82.             shortPath[i] =   9999;  
  83.             weightFound[i] = false;  
  84.         }  
  85.               
  86.         shortPath[v] = 0;  
  87.         weightFound[v] = true;  
  88.         Queue queue = new Queue();  
  89.         queue.addQueue(v);  
  90.           
  91.         while (!queue.isEmpty()) {  
  92.             int temp = queue.front();  
  93.             queue.deleteQueue();  
  94.             int[]  ajd = new int[length];  
  95.             int ajdlength =   list[temp].getAjd(ajd);  
  96.               
  97.               
  98.             for (int i = 0; i < ajdlength; i++) {  
  99.                 int w = ajd[i];  
  100.                 if (!weightFound[w]) {  
  101.                     if (shortPath[w] > shortPath[temp] + weight[temp][w]){  
  102.                         shortPath[w] = shortPath[temp] + weight[temp][w];  
  103.                     }  
  104.                 }  
  105.             }  
  106.               
  107.             int minWeightNode = 0;    
  108.             for (int i = 0; i < length; i++) {  
  109.                 if (!weightFound[i]) {  
  110.                     minWeightNode = i;  
  111.                     for (int j = 0; j < length; j++) {  
  112.                         if (!weightFound[j]) {  
  113.                             if (shortPath[j] < shortPath[minWeightNode]) {  
  114.                                 minWeightNode = j;  
  115.                             }  
  116.                         }  
  117.                     }  
  118.                     break;  
  119.                 }  
  120.             }  
  121.   
  122.             if (!weightFound[minWeightNode]) {  
  123.                 weightFound[minWeightNode] = true;  
  124.                 queue.addQueue(minWeightNode);  
  125.             }  
  126.         }  
  127.                   return shortPath;  
  128.     }  
  129.       
  130.     // 长度  
  131.     public int  length() {  
  132.         System.out.println("length="+length);  
  133.         return length;  
  134.     }  
  135.       
  136.       
  137.     public boolean isEmpty() {  
  138.         return  length == 0;  
  139.     }  
  140.   
  141.       
  142.     // 添加节点  
  143.     public void addGraph(int info) {  
  144.         for (int i = 0; i < length; i++) {  
  145.             if (list[i] == null) {  
  146.                 GraphList g = new GraphList();  
  147.                 g.addNode(info);  
  148.                 list[i] = g;  
  149.                 break;  
  150.             }  
  151.         }  
  152.     }  
  153.   
  154.       
  155.     // 添加边  
  156.     public void addEdge(int vfrom, int vto, int value) {  
  157.         list[vfrom].addNode(vto);  
  158.         weight[vfrom][vto] = value;  
  159.     }  
  160.   
  161.     // 打印图  
  162.     public void print() {  
  163.         for (int i = 0; i < length; i++) {  
  164.             GraphNode current =  list[i].first;  
  165.             while (current != null) {  
  166.                 System.out.print(current.info + " ");  
  167.                 current = current.link;  
  168.             }  
  169.                System.out.println("");  
  170.         }  
  171.     }  
  172.   
  173.   
  174. public static void main(String[] args) {  
  175.         Graph graph = new Graph(5);    
  176.         System.out.println("create graph start");  
  177.         for (int i = 0; i < 5; i++) {  
  178.               graph.addGraph(i);  
  179.         }  
  180.         graph.addEdge(0116);  
  181.         graph.addEdge(032);  
  182.         graph.addEdge(043);  
  183.         graph.addEdge(347);  
  184.         graph.addEdge(3112);  
  185.         graph.addEdge(4110);  
  186.         graph.addEdge(435);  
  187.         graph.addEdge(424);  
  188.         graph.addEdge(213);  
  189.         graph.addEdge(125);  
  190.         graph.print();  
  191.         System.out.println("create graph end");   
  192.           
  193.           
  194.         int[] shortPath =  graph.shortPath(0);  
  195.         for (int i = 0; i < shortPath.length; i++) {  
  196.              System.out.print(shortPath[i] + " ");  
  197.         }  
  198.     }  
  199. }  

http://blog.csdn.net/love_ubuntu/article/details/6689898

猜你喜欢

转载自m635674608.iteye.com/blog/2288485
今日推荐