用java语言建立一个以邻接矩阵形式存储的图; 2、以Prim算法获得最小生成树; 3、以Kruscal算法获得最小生成树; 4、退出系统。
时间: 2023-06-13 17:05:17 浏览: 115
好的,以下是Java语言实现的代码示例:
1. 以邻接矩阵形式存储的图
```java
public class Graph {
private int[][] matrix; // 邻接矩阵
private int numVertices; // 图中节点数
public Graph(int numVertices) {
this.matrix = new int[numVertices][numVertices];
this.numVertices = numVertices;
}
public void addEdge(int i, int j, int weight) {
matrix[i][j] = weight;
matrix[j][i] = weight;
}
public int getWeight(int i, int j) {
return matrix[i][j];
}
public int getNumVertices() {
return numVertices;
}
}
```
2. Prim算法获得最小生成树
```java
public class PrimMST {
private int[] parent; // 记录每个节点的父节点
private boolean[] visited; // 记录每个节点是否被访问过
private int[] key; // 记录每个节点与最小生成树的距离
public void primMST(Graph graph) {
parent = new int[graph.getNumVertices()];
visited = new boolean[graph.getNumVertices()];
key = new int[graph.getNumVertices()];
Arrays.fill(key, Integer.MAX_VALUE);
key[0] = 0;
for (int i = 0; i < graph.getNumVertices() - 1; i++) {
int u = getMinKeyVertex(key, visited);
visited[u] = true;
for (int v = 0; v < graph.getNumVertices(); v++) {
int weight = graph.getWeight(u, v);
if (weight != 0 && !visited[v] && weight < key[v]) {
parent[v] = u;
key[v] = weight;
}
}
}
printMST(graph);
}
private int getMinKeyVertex(int[] key, boolean[] visited) {
int min = Integer.MAX_VALUE;
int minIndex = -1;
for (int i = 0; i < key.length; i++) {
if (!visited[i] && key[i] < min) {
min = key[i];
minIndex = i;
}
}
return minIndex;
}
private void printMST(Graph graph) {
System.out.println("Edge \tWeight");
for (int i = 1; i < graph.getNumVertices(); i++) {
System.out.println(parent[i] + " - " + i + "\t" + graph.getWeight(parent[i], i));
}
}
}
```
3. Kruskal算法获得最小生成树
```java
public class KruskalMST {
private int[] parent; // 记录每个节点的父节点
public void kruskalMST(Graph graph) {
parent = new int[graph.getNumVertices()];
List<Edge> edges = new ArrayList<>();
for (int i = 0; i < graph.getNumVertices(); i++) {
for (int j = i + 1; j < graph.getNumVertices(); j++) {
int weight = graph.getWeight(i, j);
if (weight != 0) {
edges.add(new Edge(i, j, weight));
}
}
}
Collections.sort(edges);
int count = 0;
int i = 0;
while (count < graph.getNumVertices() - 1) {
Edge edge = edges.get(i++);
int u = find(edge.getU());
int v = find(edge.getV());
if (u != v) {
parent[u] = v;
count++;
}
}
printMST(graph);
}
private int find(int i) {
while (parent[i] != 0) {
i = parent[i];
}
return i;
}
private void printMST(Graph graph) {
System.out.println("Edge \tWeight");
for (int i = 1; i < graph.getNumVertices(); i++) {
System.out.println(parent[i] + " - " + i + "\t" + graph.getWeight(parent[i], i));
}
}
class Edge implements Comparable<Edge> {
private int u;
private int v;
private int weight;
public Edge(int u, int v, int weight) {
this.u = u;
this.v = v;
this.weight = weight;
}
public int getU() {
return u;
}
public int getV() {
return v;
}
public int getWeight() {
return weight;
}
@Override
public int compareTo(Edge o) {
return this.weight - o.weight;
}
}
}
```
4. 退出系统
```java
public class Main {
public static void main(String[] args) {
Graph graph = new Graph(6);
graph.addEdge(0, 1, 6);
graph.addEdge(0, 2, 1);
graph.addEdge(0, 3, 5);
graph.addEdge(1, 2, 5);
graph.addEdge(1, 4, 3);
graph.addEdge(2, 3, 5);
graph.addEdge(2, 4, 6);
graph.addEdge(2, 5, 4);
graph.addEdge(3, 5, 2);
graph.addEdge(4, 5, 6);
PrimMST primMST = new PrimMST();
primMST.primMST(graph);
KruskalMST kruskalMST = new KruskalMST();
kruskalMST.kruskalMST(graph);
}
}
```
阅读全文