The classical greedy strategy prim algorithm, Kruskal algorithm to find the minimum spanning tree, Dijkstra to find the shortest path
The minimum spanning tree algorithm used to check the set in the previous blog write, the graph is below, the minimum spanning tree is not the right line
/** * File name: Graphtest2.java * Time: 2max14 November 17 pm 6:2max:1max * Xiu Kang */package chapter9;import java.util.comparator;import Java.util.priorityqueue;import Java.util.scanner;import Chapter8. disjsets;//Set/** * Class Name: Edeg Description: Edge */class Edge {public int v1;public int v2;public int dist;public Edge (int v1, int v2, int Dist) {this.v1 = V1;this.v2 = V2;this.dist = dist;//weight}}class Distcomparator implements comparator<edge> {@Overrid epublic int Compare (Edge O1, Edge O2) {//TODO auto-generated method stubif (O1.dist > O2.dist) return 1;else if (O1.dis T < o2.dist) Return-1;return 0;}} public class GraphTest2 {public static final int max = integer.max_value;/** * Method Name: Dijkstra Description: Dijeskra algorithm, V for source G storage graph information, dist[i ] represents the right to V to I, prev[i] represents the previous vertex of vertex i */public static void Dijkstra (int v, int n, int[][] g, int[] dist, int[] prev) {boolean[] s = new Boolean[n + 1];for (int i = 1; I <= n; i++) {dist[i] = g[v][i];s[i] = false;if (dist[i] = = max) Prev[i] = 0;elsepr Ev[i] = V;} S[V] = true;dist[v] = 0;//to find the smallest weight from v-sEdge for (int i = 1; i < n; i++) {int temp = Max;int U = v;for (int j = 1; J <= N; j + +) if (!s[j] && temp > D IST[J]) {temp = Dist[j];u = j;} S[U] = true;//update for (int j = 1; J <= N; j + +) {if (!s[j] && g[u][j] < max) {int newdist = Dist[u] + g[u][j]; if (Newdist < dist[j]) {dist[j] = newdist;prev[j] = u;}}}} /** * Method Name: Printpath Description: Print shortest path */public static void Printpath (int[] prev, int v) {System.out.print (v + ""); while (Prev[v] ! = 0) {System.out.print (Prev[v] + ""); v = Prev[v];}} /** * Method Name: Prim Description: Minimum spanning tree Prim algorithm lowcost[i] is the smallest weight at any point in the vertex I to S set Closest[i] is the vertex with the least number of vertices connected to the I vertex in * s */public static void Prim (int[] [] g, int n) {boolean[] s = new Boolean[n + 1];int[] lowcost = new Int[n + 1];int[] Closest = new Int[n + 1];s[1] = True;l OWCOST[1] = 0;for (int i = 2; I <= n; i++) {lowcost[i] = g[1][i];closest[i] = 1;s[i] = false;} The point for (int i = 1; i < n; i++) {int min = max;int j = 1;for (int k = 2; k <= N; k++) {if (!s[) is found in the v-s with the vertices connected in s and the least weighted value. K] && Lowcost[k]< min) {min = Lowcost[k];j = k;}} S[J] = true; System.out.println (j + "" + closest[j]);//re-update lowcost lowcost[i] The minimum weight for any point in the vertex I to S collection for (int k = 2; K <= N; k++) {if (!s[k] && g[j][k] < Lowcost[k]) {lowcost[k] = g[j][k];closest[k] = j;}}} /** * Method Name: Kruskal Description: Minimum spanning tree Kruskal algorithm */public static void Kruskal (priorityqueue<edge> p, int n) {disjsets ds = new D Isjsets (n + 1);//And check the edge e;int edgesaccepted = 0;while (edgesaccepted < n-1) {e = P.poll (); int uset = Ds.find (E.V1); int vset = Ds.find (E.V2); if (uset! = vset) {edgesaccepted++; System.out.println (E.v1 + "" + e.v2);d s.union1 (Uset, Vset);}}} /** * Method Name: Main Description: Test */public static void Main (string[] args) {//TODO auto-generated method stubint[][] g = new int[][] { {max, Max, Max, Max, Max, Max, Max, Max},{Max, Max, 2, Max, 1, Max, Max, Max},{Max, Max, Max, Max, 3, Max, Max} , {max, 4, Max, Max, Max, Max, 5, Max},{Max, Max, Max, 2, Max, 2, 4},{max, Max, Max, Max, Max, Max, Max, 6},{Max , Max, Max,Max, Max, Max, Max, Max},{Max, Max, Max, Max, Max, Max, 1, max}};//Graph graph = new graph (g); int[] dist = new Int[8] ; int[] prev = new INT[8]; Scanner in = new Scanner (system.in); System.out.println ("Please enter vertex number:"), int v = in.nextint ();D Ijkstra (V, 7, G, Dist, prev); System.out.println ("Please enter the destination vertex:"); int n = in.nextint (); System.out.println (The weight of "vertex" + V + "to vertex" + N + "is" + dist[n]);p Rintpath (prev, n); int[][] g2 = new int[][] {{max, max, Max, Max, Max, Max, Max, Max},{Max, Max, 2, 4, 1, Max, Max, Max},{Max, 2, Max, Max, 3, Max, Max,},{Max, 4, Max, Max, 2, Max, 5, Max},{max, 1, 3, 2, Max, 7, 8, 4},{max, Max, Max, 7, Max, Max, 6},{Max, Max, Max, 5, 8, Max, Max, 1} , {max, Max, Max, Max, 4, 6, 1, Max}}; Prim (G2, 7); priorityqueue<edge> p = new Priorityqueue<edge> (12,new distcomparator ());p. Add (New Edge (1, 2, 2));p. Add ( New Edge (1, 4, 1));p. Add (New Edge (2, 4, 3));p. Add (New Edge (1, 3, 4));p. Add (New Edge (3, 4, 2));p. Add (New Edge (4, 5, 7));p. A DD (New Edge (2, 5, ten));p. Add(New Edge (3, 6, 5));p. Add (New Edge (4, 6, 8));p. Add (New Edge (6, 7, 1));p. Add (New Edge (4, 7, 4));p. Add (New Edge (5, 7, 6)); System.out.println ("Kruskal algorithm:"); Kruskal (P, 7);}}
Minimum spanning tree, shortest path algorithm