Skip to content

Commit b806e6a

Browse files
committed
added assignment 12
1 parent 37b9fb4 commit b806e6a

File tree

3 files changed

+354
-0
lines changed

3 files changed

+354
-0
lines changed

assignments/12/a/Main.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class Main {
2+
public static void main(String[] args) {
3+
System.out.println("Hello world!");
4+
}
5+
}

assignments/12/b/MainKruskal.java

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
// Name: Richard Eldridge
2+
// Class: CS 3305 W03
3+
// Term: Spring 2024
4+
// Instructor: Carla McManus
5+
// Assignment: 12-ExtraCredit-1-Kruskal
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+
// Determines an edge and gives it a weight
17+
class WeightedEdge {
18+
public Weight weight;
19+
public int x,
20+
y;
21+
22+
public WeightedEdge(int x, int y, Integer weight) {
23+
this.x = x;
24+
this.y = y;
25+
this.weight = new Weight(weight);
26+
}
27+
28+
}
29+
30+
class WeightedGraphs {
31+
// Print out a visual block that show the full weighted graph
32+
public static void printWeightedGraph (ArrayList<WeightedEdge> edges) {
33+
int x = 0,
34+
y = 0;
35+
36+
for (WeightedEdge edge: edges) {
37+
if (edge.x != x) {
38+
x = edge.x;
39+
System.out.println();
40+
}
41+
System.out.print("["+edge.x + ", " + edge.y + ", " + (edge.weight.weight == null ? edge.weight.weight : " "+edge.weight.weight) + "]");
42+
}
43+
}
44+
45+
46+
// Print out a visual block that shows the Adjacency matrix
47+
public static void printAdjMatrix (ArrayList<WeightedEdge> edges) {
48+
int x = 0,
49+
y = 0;
50+
51+
for (WeightedEdge edge: edges) {
52+
if (edge.x != x) {
53+
x = edge.x;
54+
System.out.println();
55+
}
56+
System.out.print((edge.weight.weight == null ? edge.weight.weight+" " : " "+edge.weight.weight+" "));
57+
}
58+
}
59+
60+
61+
// Prints out the order in which edges were visited
62+
public static void visitOrder (ArrayList<Integer> nVisited) {
63+
String[] aLegend = {"A","B","C","D","E","F","G","H"};
64+
65+
System.out.print("Adjacency Matrix visited in order: " );
66+
for (int num: nVisited) System.out.print(aLegend[num] + " ");
67+
System.out.println();
68+
}
69+
70+
71+
// Sort ArrayList edges by weight and return new array
72+
public static ArrayList<WeightedEdge> sortByWeight (ArrayList<WeightedEdge> edges) {
73+
ArrayList<WeightedEdge> newEdges = new ArrayList<>();
74+
75+
// Loop through all of edges until only nulls remain and then dumpall into new array and return
76+
while (edges.size() > 0) {
77+
int weight = Integer.MAX_VALUE;
78+
WeightedEdge migrate = null;
79+
// find smallest weight
80+
for (WeightedEdge edge: edges) {
81+
if (edge.weight.weight != null && edge.weight.weight < weight) {
82+
weight = edge.weight.weight;
83+
migrate = edge;
84+
}
85+
}
86+
87+
// Dump entire rest of edges into new array b/c inly null remain
88+
if (migrate == null) {
89+
for (WeightedEdge edge: edges) newEdges.add(edge);
90+
edges.clear();
91+
break;
92+
}
93+
94+
// Migrate and remove lowest weight edge
95+
newEdges.add(migrate);
96+
edges.remove(migrate);
97+
}
98+
return newEdges;
99+
}
100+
101+
102+
// Recursive search to find if tree is sparse or multiple trees
103+
private static int find(int[] parent, int x) {
104+
if (parent[x] != x) parent[x] = find(parent, parent[x]);
105+
return parent[x];
106+
}
107+
108+
109+
public static void kruskal(ArrayList<WeightedEdge> edges, int totalEdges) {
110+
// Sort edges by weight
111+
edges = sortByWeight(edges);
112+
113+
// Verticies have no parents to start (set to self)
114+
int[] parent = new int[totalEdges];
115+
for (int i = 0; i < totalEdges; i++) parent[i] = i;
116+
117+
// Set inital variables
118+
int totalWeight = 0;
119+
ArrayList<Integer> nVisited = new ArrayList<>();
120+
String[] aLegend = {"A","B","C","D","E","F","G","H"};
121+
122+
123+
// Iterate over all edges and find the next node to visit
124+
System.out.println("Edges visited in order of: ");
125+
for (WeightedEdge edge : edges) {
126+
// Search for parent roots for both x and y
127+
int rootX = find(parent, edge.x);
128+
int rootY = find(parent, edge.y);
129+
130+
// If the roots dont match then these are multiple sparse trees and can be connected
131+
if (rootX != rootY) {
132+
parent[rootX] = rootY;
133+
totalWeight += edge.weight.weight;
134+
nVisited.add(edge.x);
135+
nVisited.add(edge.y);
136+
System.out.println("Edge: " + aLegend[edge.x] + "-" + aLegend[edge.y] + ", Weight: " + edge.weight.weight);
137+
}
138+
}
139+
140+
System.out.println("Total Weight: " + totalWeight);
141+
// visitOrder(nVisited);
142+
}
143+
144+
}
145+
146+
public class MainKruskal {
147+
public static void main(String[] args) {
148+
// Adjacency matrix
149+
Integer[][] matrix = {
150+
{null, null, 4, null, null, 7, null, null},
151+
{null, null, null, null, 9, null, null, 3},
152+
{ 4, null, null, 3, null, 2, 9, null},
153+
{null, null, 3, null, 3, null, 7, null},
154+
{null, 9, null, 3, null, null, 2, 7},
155+
{ 7, null, 2, null, null, null, 8, null},
156+
{null, null, 9, 7, 2, 8, null, 3},
157+
{null, null, 3, null, 7, null, 3, null},
158+
};
159+
160+
// Create ArrayList of Weighted edges with adjacency matrix
161+
ArrayList<WeightedEdge> edges = new ArrayList<>();
162+
for(int i = 0; i < matrix.length; i++) {
163+
for(int j = 0; j < matrix[i].length; j++) {
164+
edges.add(new WeightedEdge(i, j, matrix[i][j]));
165+
}
166+
}
167+
168+
169+
// WeightedGraphs.printWeightedGraph(edges);
170+
// System.out.println("\n\n");
171+
// WeightedGraphs.printAdjMatrix(edges);
172+
// System.out.println("\n\n");
173+
174+
175+
// Display results of Kruskal algorithm
176+
WeightedGraphs.kruskal(edges, 8);
177+
178+
}
179+
}

assignments/12/c/MainPrim.java

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)