java数据结构----带权图

时间:2022-05-13 11:44:56

1.带权图:要引入带权图,首先要引入最小生成树,当所有的边拥有相同的权值时。问题变得简单了,算法可以选择任意一条边加入最小生成树。但是当边有不同的权值时,需要用一些算法决策来选择正确的边。

2.带权图构建最小生成树算法:

  2.1.从一个顶点开始,把它放入树的集合中,然后重复做下面的事情:

    2.1.1.找到从最新的顶点到其他顶点的所有边,这些顶点不能在树的集合中,把这些边放入优先级队列,

    2.1.2.找出权值最小的边,把它和它所到达的顶点放入树的集合中。重复直到所有顶点都在树的集合中。

3.实现代码:

  3.1.Edge.java

java数据结构----带权图java数据结构----带权图
 1 package com.cn.powergraph;
2 /**
3 * 带权图的边类
4 * @author Administrator
5 *
6 */
7 public class Edge {
8 public int srcVert;
9 public int destVert;
10 public int distance;
11 public Edge(int sv,int dv,int d){
12 srcVert = sv;
13 destVert = dv;
14 distance = d;
15 }
16 }
View Code

  3.2.PriorityQ.java

java数据结构----带权图java数据结构----带权图
 1 package com.cn.powergraph;
2 /**
3 * 优先级队列来实现带权图
4 * @author Administrator
5 *
6 */
7 public class PriorityQ {
8 private final int SIZE = 20;
9 private Edge[] queArray;
10 private int size;
11 public PriorityQ(){
12 queArray = new Edge[SIZE];
13 size = 0;
14 }
15 public void insert(Edge item){
16 int i;
17 for ( i = 0; i < size; i++) {
18 if (item.distance >= queArray[i].distance)
19 break;
20 }
21 for (int j = size - 1; j >= i; j --) {
22 queArray[j + 1] = queArray[j];
23 }
24 queArray[i] = item;
25 size ++;
26 }
27 public Edge removeMin(){
28 return queArray[-- size];
29 }
30 public void removeN(int n){
31 for (int i = n; i < size - 1; i++) {
32 queArray[i] = queArray[i + 1];
33 }
34 size --;
35 }
36 public Edge peekMin(){
37 return queArray[size - 1];
38 }
39 public int size(){
40 return size;
41 }
42 public boolean isEmpty(){
43 return size == 0;
44 }
45 public Edge peekN(int n){
46 return queArray[n];
47 }
48 public int find(int index){
49 for (int i = 0; i < size; i++) {
50 if (queArray[i].destVert == index)
51 return i;
52 }
53 return -1;
54 }
55 }
View Code

  3.3.Vertex.java

java数据结构----带权图java数据结构----带权图
 1 package com.cn.powergraph;
2 /**
3 * 带权图顶点类
4 * @author Administrator
5 *
6 */
7 public class Vertex {
8 public char lable;
9 public boolean isInTree;
10 public Vertex(char lab){
11 lable = lab;
12 isInTree = false;
13 }
14 }
View Code

  3.4.Graph.java

java数据结构----带权图java数据结构----带权图
 1 package com.cn.powergraph;
2 /**
3 * 带权图的类
4 * @author Administrator
5 *
6 */
7 public class Graph {
8 private final int MAX_VERTS = 20;
9 private final int INFINITY = 1000000;
10 private Vertex[] vertList;
11 private int adjMat[][];
12 private int nVerts;
13 private int currentVert;
14 private PriorityQ thePQ;
15 private int nTree;
16 public Graph(){
17 vertList = new Vertex[MAX_VERTS];
18 adjMat = new int[MAX_VERTS][MAX_VERTS];
19 nVerts = 0;
20 for (int i = 0; i < MAX_VERTS; i++) {
21 for (int j = 0; j < MAX_VERTS; j++) {
22 adjMat[i][j] = INFINITY;
23 }
24 }
25 thePQ = new PriorityQ();
26 }
27 public void addVertex(char lab){
28 vertList[nVerts ++] = new Vertex(lab);
29 }
30 public void addEdge(int start,int end,int weight){
31 adjMat[start][end] = weight;
32 adjMat[end][start] = weight;
33 }
34 public void displayVertex(int v){
35 System.out.print(vertList[v].lable);
36 }
37 public void mstw(){
38 currentVert = 0;
39 while (nTree < nVerts - 1){
40 vertList[currentVert].isInTree = true;
41 nTree ++;
42 for (int i = 0; i < nVerts; i++) {
43 if (i == currentVert)
44 continue;
45 if (vertList[i].isInTree)
46 continue;
47 int distance = adjMat[currentVert][i];
48 if (distance == INFINITY)
49 continue;
50 putInPQ(i,distance);
51 }
52 if (thePQ.size() == 0)
53 {
54 System.out.println("GRAPH NOT CONNECTED");
55 return;
56 }
57 Edge theedge = thePQ.removeMin();
58 int sourceVert = theedge.srcVert;
59 currentVert = theedge.destVert;
60 System.out.print(vertList[sourceVert].lable);
61 System.out.print(vertList[currentVert].lable);
62 System.out.print(" ");
63 }
64 for (int i = 0; i < nVerts; i++) {
65 vertList[i].isInTree = false;
66 }
67 }
68 public void putInPQ(int newVert,int newDist){
69 int queueIndex = thePQ.find(newDist);
70 if (queueIndex != -1){
71 Edge tempEdge = thePQ.peekN(queueIndex);
72 int oldDist = tempEdge.distance;
73 if (oldDist > newDist){
74 thePQ.removeN(queueIndex);
75 Edge theEdge = new Edge(currentVert, newVert,newDist);
76 thePQ.insert(theEdge);
77 }
78 }
79 else{
80 Edge theEdge = new Edge(currentVert, newVert,newDist);
81 thePQ.insert(theEdge);
82 }
83 }
84 }
View Code

  3.5.GTest.java

java数据结构----带权图java数据结构----带权图
 1 package com.cn.powergraph;
2 /**
3 * 带权图的测试
4 * @author Administrator
5 *
6 */
7 public class GTest {
8 public static void main(String[] args) {
9 Graph g = new Graph();
10 g.addVertex('a');
11 g.addVertex('b');
12 g.addVertex('c');
13 g.addVertex('d');
14 g.addVertex('e');
15 g.addVertex('f');
16 g.addEdge(0, 1, 3);
17 g.addEdge(2, 1, 5);
18 g.addEdge(3, 1, 8);
19 g.addEdge(3, 2, 5);
20 g.addEdge(5, 4, 4);
21 g.addEdge(0, 4, 10);
22 g.mstw();
23 }
24 }
View Code