|
| 1 | +// Name: Richard Eldridge |
| 2 | +// Class: CS 3305 W03 |
| 3 | +// Term: Spring 2024 |
| 4 | +// Instructor: Carla McManus |
| 5 | +// Assignment: 12-Part-1-Prims |
| 6 | +// IDE: IntelliJ IDEA Edu |
| 7 | + |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +// Weight Node for determining weight or null |
| 11 | +class Weight { |
| 12 | + public Integer weight; |
| 13 | + Weight (Integer x) { weight = x; } |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +// Determines an edge and gives it a weight |
| 18 | +class WeightedEdge { |
| 19 | + public Weight weight; |
| 20 | + public int x, |
| 21 | + y; |
| 22 | + |
| 23 | + public WeightedEdge(int x, int y, Integer weight) { |
| 24 | + this.x = x; |
| 25 | + this.y = y; |
| 26 | + this.weight = new Weight(weight); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +class WeightedGraphs { |
| 32 | + // Print out a visual block that show the full weighted graph |
| 33 | + public static void printWeightedGraph (ArrayList<WeightedEdge> edges) { |
| 34 | + int x = 0, |
| 35 | + y = 0; |
| 36 | + |
| 37 | + for (WeightedEdge edge: edges) { |
| 38 | + if (edge.x != x) { |
| 39 | + x = edge.x; |
| 40 | + System.out.println(); |
| 41 | + } |
| 42 | + System.out.print("["+edge.x + ", " + edge.y + ", " + (edge.weight.weight == null ? edge.weight.weight : " "+edge.weight.weight) + "]"); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + // Print out a visual block that shows the Adjacency matrix |
| 48 | + public static void printAdjMatrix (ArrayList<WeightedEdge> edges) { |
| 49 | + int x = 0, |
| 50 | + y = 0; |
| 51 | + |
| 52 | + for (WeightedEdge edge: edges) { |
| 53 | + if (edge.x != x) { |
| 54 | + x = edge.x; |
| 55 | + System.out.println(); |
| 56 | + } |
| 57 | + System.out.print((edge.weight.weight == null ? edge.weight.weight+" " : " "+edge.weight.weight+" ")); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + // Prints out the order in which edges were visited |
| 63 | + public static void visitOrder (ArrayList<Integer> nVisited) { |
| 64 | + String[] aLegend = {"A","B","C","D","E","F","G","H"}; |
| 65 | + |
| 66 | + System.out.print("Adjacency Matrix visited in order: " ); |
| 67 | + for (int num: nVisited) System.out.print(aLegend[num] + " "); |
| 68 | + System.out.println(); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + // Check if an edge has been visited |
| 73 | + public static boolean bParsedEdges (boolean[] bVisited) { |
| 74 | + for (boolean bool: bVisited) if (!bool) return false; |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + // Check if all edges have been visited |
| 80 | + public static boolean bVisitedEdge (ArrayList<Integer> nVisited, int edge) { |
| 81 | + for (int num : nVisited) if (num == edge) return false; |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + // Method to run prims algorithm |
| 87 | + public static void prim(ArrayList<WeightedEdge> edges, int totalVertex, int startVertex) { |
| 88 | + String[] aLegend = {"A","B","C","D","E","F","G","H"}; |
| 89 | + |
| 90 | +// Create visited array and set as false; caveat 0 is visited |
| 91 | + boolean[] bVisited = new boolean[totalVertex]; |
| 92 | + for (int i = 0; i < bVisited.length; i++) bVisited[i] = false; |
| 93 | + bVisited[startVertex] = true; |
| 94 | + |
| 95 | + System.out.println("Starting Vertex: " + aLegend[startVertex]); |
| 96 | + |
| 97 | + |
| 98 | +// Create order visited array |
| 99 | + ArrayList<Integer> nVisited = new ArrayList<>(); |
| 100 | + nVisited.add(startVertex); |
| 101 | + |
| 102 | +// Set default total visited weight and legend |
| 103 | + int totalWeight = 0; |
| 104 | + WeightedEdge nextEdgeNode= null; |
| 105 | + |
| 106 | + while(!WeightedGraphs.bParsedEdges(bVisited)) { |
| 107 | + int nextEdge = -1, |
| 108 | + nextWeight = Integer.MAX_VALUE; |
| 109 | + |
| 110 | +// Iterate over all edges and find the next node to visit |
| 111 | + for (WeightedEdge edge: edges) { |
| 112 | + if (edge .weight.weight == null) continue; |
| 113 | + if (bVisitedEdge(nVisited, edge.x) && !bVisitedEdge(nVisited, edge.y) && edge.weight.weight < nextWeight) { |
| 114 | + nextWeight = edge.weight.weight; |
| 115 | + nextEdge = edge.x; |
| 116 | + nextEdgeNode = edge; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | +// Set for next iteration and found edges / weight |
| 121 | + bVisited[nextEdge] = true; |
| 122 | + nVisited.add(nextEdge); |
| 123 | + totalWeight += nextWeight; |
| 124 | +// System.out.println("Edge: " + aLegend[nextEdge] + " Weight: " + nextWeight); |
| 125 | + System.out.println("Edge: " + aLegend[nextEdgeNode.y] + "-" + aLegend[nextEdgeNode.x] + " Vertex: " + aLegend[nextEdge] + " Weight: " + nextWeight); |
| 126 | + } |
| 127 | + |
| 128 | +// Print out total weight and visited order |
| 129 | + System.out.println(); |
| 130 | + WeightedGraphs.visitOrder(nVisited); |
| 131 | + System.out.println("Total Weight: " + totalWeight); |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | + |
| 137 | +public class MainPrim { |
| 138 | + public static void main(String[] args) { |
| 139 | +// Adjacency matrix |
| 140 | + Integer[][] matrix = { |
| 141 | + {null, null, 4, null, null, 7, null, null}, |
| 142 | + {null, null, null, null, 9, null, null, 3}, |
| 143 | + { 4, null, null, 3, null, 2, 9, null}, |
| 144 | + {null, null, 3, null, 3, null, 7, null}, |
| 145 | + {null, 9, null, 3, null, null, 2, 7}, |
| 146 | + { 7, null, 2, null, null, null, 8, null}, |
| 147 | + {null, null, 9, 7, 2, 8, null, 3}, |
| 148 | + {null, null, 3, null, 7, null, 3, null}, |
| 149 | + }; |
| 150 | + |
| 151 | +// Create ArrayList of Weighted edges with adjacency matrix |
| 152 | + ArrayList<WeightedEdge> edges = new ArrayList<>(); |
| 153 | + for(int i = 0; i < matrix.length; i++) { |
| 154 | + for(int j = 0; j < matrix[i].length; j++) { |
| 155 | + edges.add(new WeightedEdge(i, j, matrix[i][j])); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + |
| 160 | +// WeightedGraphs.printWeightedGraph(edges); |
| 161 | +// System.out.println("\n\n"); |
| 162 | +// WeightedGraphs.printAdjMatrix(edges); |
| 163 | +// System.out.println("\n\n"); |
| 164 | + |
| 165 | + |
| 166 | +// Display results of Prim algorithm |
| 167 | + WeightedGraphs.prim(edges, 8, 0); |
| 168 | + |
| 169 | + } |
| 170 | +} |
0 commit comments