图论的相关算法java实现

package Graph;
import java.util.ArrayList;
import java.util.LinkedList;
import DataStruct.Union;
import Sort.Sort;

/**
     图类 
     构造函数封装了将邻接矩阵转化临接表的方法,提供了拓扑排序,无权最短路径算法等
     所有算法都是基于临接表(拓扑排序 基于入度数组,入度数组也是基于临接表求得)
 */
public class Graph {
   
    public static final int INFINITY=-1;//代表不相连 无穷
    /**
     图的顶点
     */
    class Vertex{
        int topnum;
        Edge edge=null;

        //下面三条信息是为了搜索最短路径
        int dis=INFINITY;
        boolean arrived=false;
        Vertex path=null;

        public Vertex(int topnum) {
            this.topnum = topnum;
        }
        public Vertex(int topnum,Edge edge) {
            this.topnum = topnum;
            this.edge=edge;
        }
    }
    /**
     图的边(用于临接表)
     */
    class Edge{
        int adjacentTop;//边邻接的顶点 1->2  adjacentTop为2
        int weight = 1;     //连接两顶点的边的权值
        Edge next;
        public Edge(int adjacentTop,int weight) {
            this.adjacentTop = adjacentTop;
            this.next = null;
            this.weight=weight;
        }
    }
    class Side implements Comparable<Side>{
        int from;
        int to;
        int weight;
        public Side(int from, int to, int weight) {
            this.from = from;
            this.to = to;
            this.weight = weight;
        }
        public int compareTo(Side arg0) {
            return weight>((Side)arg0).weight? 1:weight==((Side)arg0).weight? 0:-1;
        }
    }
    private int numVertexs;//顶点数
    private int numEdges=0;//边数
    private Vertex[] NodeTable=null;//邻接表
    private int[] InDegreeList;//入度数组  数组从第一个顶点(下标为零)表示入度的边数
    /**
     * 边的集合 初始化Graph对象时 自动初始化
     */
    private ArrayList<Side> sides=new ArrayList<Graph.Side>();
    public Graph(int[][] AdjacencyMatrix) {
        this.numVertexs = AdjacencyMatrix.length;
        NodeTableInit(numVertexs);
        InDegreeList=new int[numVertexs];
        MatrixToList(AdjacencyMatrix);
    }
    /**
     * @param length
     * 邻接表初始化 边暂时都为空
     */
    
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 Java 代码实现 Prime 算法的示例: ```java import java.util.*; public class PrimeAlgorithm { private static final int INF = Integer.MAX_VALUE; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // 顶点数 int m = sc.nextInt(); // 边数 int[][] graph = new int[n][n]; // 图的邻接矩阵表示 for (int i = 0; i < n; i++) { Arrays.fill(graph[i], INF); // 初始化为无穷大 } for (int i = 0; i < m; i++) { int u = sc.nextInt() - 1; // 节点编号从 0 开始 int v = sc.nextInt() - 1; int w = sc.nextInt(); graph[u][v] = graph[v][u] = w; // 无向图,需要将 u->v 和 v->u 的权值都设置 } int[] dist = new int[n]; // 记录每个节点到已连通部分的最小距离 boolean[] visited = new boolean[n]; // 记录节点是否已经加入已连通部分 Arrays.fill(dist, INF); // 初始距离为无穷大 dist[0] = 0; // 从 0 号节点开始 int ans = 0; // 最小生成树的权值和 for (int i = 0; i < n; i++) { int u = -1; for (int j = 0; j < n; j++) { if (!visited[j] && (u == -1 || dist[j] < dist[u])) { u = j; } } visited[u] = true; // 将节点 u 加入已连通部分 ans += dist[u]; // 更新最小生成树的权值和 for (int v = 0; v < n; v++) { if (!visited[v] && graph[u][v] != INF && graph[u][v] < dist[v]) { dist[v] = graph[u][v]; // 更新节点 v 到已连通部分的最小距离 } } } System.out.println(ans); } } ``` 其中,输入格式为: ``` n m u1 v1 w1 u2 v2 w2 ... ``` 表示图中有 n 个节点,m 条边,接下来 m 行每行表示一条边,其中 ui、vi 表示边的两个端点,wi 表示边的权值。输出最小生成树的权值和。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值