From cc3217d3fcd7673db49c0240f1b9460e438fc9cb Mon Sep 17 00:00:00 2001 From: Ari Trachtenberg <trachten@bu.edu> Date: Sun, 3 Mar 2024 11:05:21 -0500 Subject: [PATCH 1/2] Initial version --- .idea/.gitignore | 8 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ hw2p1.iml | 11 +++ src/edu/bu/ec504/hw2/p1/GraphColorer.java | 60 ++++++++++++ src/edu/bu/ec504/hw2/p1/Main.java | 93 +++++++++++++++++++ src/edu/bu/ec504/hw2/p1/edges/Edge.java | 30 ++++++ .../bu/ec504/hw2/p1/edges/UndirectedEdge.java | 62 +++++++++++++ .../colored_edges/ColoredUndirectedEdge.java | 21 +++++ .../p1/edges/weighted_edges/WeightedEdge.java | 28 ++++++ .../bu/ec504/hw2/p1/graphs/EdgeListGraph.java | 81 ++++++++++++++++ src/edu/bu/ec504/hw2/p1/graphs/Graph.java | 74 +++++++++++++++ src/edu/bu/ec504/hw2/p1/support/Color.java | 34 +++++++ src/edu/bu/ec504/hw2/p1/support/UID.java | 18 ++++ src/edu/bu/ec504/hw2/p1/support/Weighted.java | 45 +++++++++ src/edu/bu/ec504/hw2/p1/vertices/Vertex.java | 46 +++++++++ .../ec504/hw2/p1/vertices/WeightedVertex.java | 31 +++++++ 18 files changed, 662 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 hw2p1.iml create mode 100644 src/edu/bu/ec504/hw2/p1/GraphColorer.java create mode 100644 src/edu/bu/ec504/hw2/p1/Main.java create mode 100644 src/edu/bu/ec504/hw2/p1/edges/Edge.java create mode 100644 src/edu/bu/ec504/hw2/p1/edges/UndirectedEdge.java create mode 100644 src/edu/bu/ec504/hw2/p1/edges/colored_edges/ColoredUndirectedEdge.java create mode 100644 src/edu/bu/ec504/hw2/p1/edges/weighted_edges/WeightedEdge.java create mode 100644 src/edu/bu/ec504/hw2/p1/graphs/EdgeListGraph.java create mode 100644 src/edu/bu/ec504/hw2/p1/graphs/Graph.java create mode 100644 src/edu/bu/ec504/hw2/p1/support/Color.java create mode 100644 src/edu/bu/ec504/hw2/p1/support/UID.java create mode 100644 src/edu/bu/ec504/hw2/p1/support/Weighted.java create mode 100644 src/edu/bu/ec504/hw2/p1/vertices/Vertex.java create mode 100644 src/edu/bu/ec504/hw2/p1/vertices/WeightedVertex.java diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..07115cd --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK"> + <output url="file://$PROJECT_DIR$/out" /> + </component> +</project> \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..809a0fa --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="ProjectModuleManager"> + <modules> + <module fileurl="file://$PROJECT_DIR$/hw2p1.iml" filepath="$PROJECT_DIR$/hw2p1.iml" /> + </modules> + </component> +</project> \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="VcsDirectoryMappings"> + <mapping directory="$PROJECT_DIR$" vcs="Git" /> + </component> +</project> \ No newline at end of file diff --git a/hw2p1.iml b/hw2p1.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/hw2p1.iml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<module type="JAVA_MODULE" version="4"> + <component name="NewModuleRootManager" inherit-compiler-output="true"> + <exclude-output /> + <content url="file://$MODULE_DIR$"> + <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> + </content> + <orderEntry type="inheritedJdk" /> + <orderEntry type="sourceFolder" forTests="false" /> + </component> +</module> \ No newline at end of file diff --git a/src/edu/bu/ec504/hw2/p1/GraphColorer.java b/src/edu/bu/ec504/hw2/p1/GraphColorer.java new file mode 100644 index 0000000..419a9b5 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/GraphColorer.java @@ -0,0 +1,60 @@ +package edu.bu.ec504.hw2.p1; + +import edu.bu.ec504.hw2.p1.edges.UndirectedEdge; +import edu.bu.ec504.hw2.p1.graphs.EdgeListGraph; +import edu.bu.ec504.hw2.p1.graphs.Graph; +import edu.bu.ec504.hw2.p1.support.Color; +import edu.bu.ec504.hw2.p1.vertices.ColoredVertex; + +/** + * Attempts to color a graph + */ +public class GraphColorer { + + /** + * Colors the vertices of an undirected G so that no two adjacent vertices have the same color. + * @param G The graph to color + * @modifies The vertex colors of G may be modified by this method. + * @return The number of different colors used to color the graph. + */ + static int color(Graph<ColoredVertex, UndirectedEdge<ColoredVertex>> G) { + for (int ii = 0; ii < G.numVertices(); ii++) + G.ithVertex(ii).setColor(Color.first()); + + return maxColor.ordinal()+1; + } + + public static void main(String[] args) { + Graph<ColoredVertex, UndirectedEdge<ColoredVertex>> G = new EdgeListGraph<ColoredVertex, UndirectedEdge<ColoredVertex>>(); + + // Build a K_{3,3} + ColoredVertex[] partA = { + ColoredVertex.of(Color.first()), + ColoredVertex.of(Color.first()), + ColoredVertex.of(Color.first()), + }; + ColoredVertex[] partB = { + ColoredVertex.of(Color.first()), + ColoredVertex.of(Color.first()), + ColoredVertex.of(Color.first()), + }; + + // build the graph + // ... vertices + for (ColoredVertex v : partA) { + G.addVertex(v); + } + for (ColoredVertex v : partB) { + G.addVertex(v); + } + // ... edges + for (ColoredVertex v : partA) + for (ColoredVertex w : partB) { + G.addEdge(UndirectedEdge.of(v,w)); + } + + // color it + System.out.println("Colored with: "+color(G)+" colors."); + System.out.println(G); + } +} diff --git a/src/edu/bu/ec504/hw2/p1/Main.java b/src/edu/bu/ec504/hw2/p1/Main.java new file mode 100644 index 0000000..2b469ac --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/Main.java @@ -0,0 +1,93 @@ +package edu.bu.ec504.hw2.p1; + + +import edu.bu.ec504.hw2.p1.edges.UndirectedEdge; +import edu.bu.ec504.hw2.p1.edges.colored_edges.ColoredDirectedEdge; +import edu.bu.ec504.hw2.p1.edges.weighted_edges.WeightedHyperEdge; +import edu.bu.ec504.hw2.p1.graphs.EdgeListGraph; +import edu.bu.ec504.hw2.p1.graphs.Graph; +import edu.bu.ec504.hw2.p1.support.Color; +import edu.bu.ec504.hw2.p1.vertices.ColoredVertex; +import edu.bu.ec504.hw2.p1.vertices.Vertex; + +public class Main { + // graphs + static Graph<Vertex, UndirectedEdge<Vertex>> G; + static Graph<ColoredVertex, ColoredDirectedEdge<ColoredVertex>> H; + static Graph< ColoredVertex, WeightedHyperEdge<ColoredVertex, String>> I; + + // create graphs + + static void createSimpleGraph() { + G = new EdgeListGraph<>(); + + // vertices + Vertex Vertex0 = G.addVertex(Vertex.of()); + Vertex Vertex1 = G.addVertex(Vertex.of()); + Vertex Vertex2 = G.addVertex(Vertex.of()); + + // edges + G.addEdge(UndirectedEdge.of(Vertex0, Vertex1)); + G.addEdge(UndirectedEdge.of(Vertex1, Vertex2)); + G.addEdge(UndirectedEdge.of(Vertex2, Vertex0)); + + System.out.println("Your simple graph is:\n"+G); + + // check edges + System.out.println("Edge of " + Vertex0 + " and " + Vertex1 + "? "+G.adjQ(Vertex0, Vertex1)); + System.out.println("Edge of " + Vertex0 + " and " + Vertex2 + "? "+G.adjQ(Vertex0, Vertex2)); + System.out.println("\n\n\n"); + } + + static void createColoredGraph() { + H = new EdgeListGraph<>(); + + // vertices + ColoredVertex ColoredVertex0 = H.addVertex(ColoredVertex.of(Color.RED)); + ColoredVertex ColoredVertex1 = H.addVertex(ColoredVertex.of(Color.GREEN)); + ColoredVertex ColoredVertex2 = H.addVertex(ColoredVertex.of(Color.BLUE)); + + // edges + H.addEdge(ColoredDirectedEdge.of(ColoredVertex0, ColoredVertex1, Color.YELLOW)); + H.addEdge(ColoredDirectedEdge.of(ColoredVertex0, ColoredVertex0, Color.YELLOW)); + H.addEdge(ColoredDirectedEdge.of(ColoredVertex1, ColoredVertex2, Color.ORANGE)); + + System.out.println("Your colored graph is:\n"+H); + + // check edges + System.out.println("Edge from " + ColoredVertex0 + " to " + ColoredVertex0 + "? "+ H.adjQ(ColoredVertex0, ColoredVertex0)); + System.out.println("Edge from " + ColoredVertex0 + " to " + ColoredVertex1 + "? "+ H.adjQ(ColoredVertex0, ColoredVertex1)); + System.out.println("Edge from " + ColoredVertex0 + " to " + ColoredVertex2 + "? "+ H.adjQ(ColoredVertex0, ColoredVertex2)); + System.out.println("\n\n\n"); + } + + static void createHyperGraph() { + I = new EdgeListGraph<>(); + + // vertices + ColoredVertex ColoredVertex0 = I.addVertex(ColoredVertex.of(Color.RED)); + ColoredVertex ColoredVertex1 = I.addVertex(ColoredVertex.of(Color.GREEN)); + ColoredVertex ColoredVertex2 = I.addVertex(ColoredVertex.of(Color.BLUE)); + + // edges + I.addEdge(WeightedHyperEdge.of("Nothing")); + I.addEdge(WeightedHyperEdge.of("One thing",ColoredVertex0)); + I.addEdge(WeightedHyperEdge.of("All things",ColoredVertex0, ColoredVertex1, ColoredVertex2)); + + System.out.println("Your hypergraph is:\n"+I); + + // check edges + System.out.println("Edge with " + ColoredVertex0 + " and " + ColoredVertex0 + "? "+I.adjQ(ColoredVertex0, ColoredVertex0)); + System.out.println("Edge from " + ColoredVertex0 + " to " + ColoredVertex1 + "? "+I.adjQ(ColoredVertex0, ColoredVertex1)); + System.out.println("Edge with " + ColoredVertex0 + " and " + ColoredVertex1 + " and " + ColoredVertex2 + "? "+I.adjQ(ColoredVertex0, ColoredVertex1, ColoredVertex2)); + System.out.println("\n\n\n"); + } + + public static void main(String[] args) { + createSimpleGraph(); + createColoredGraph(); + createHyperGraph(); + + + } +} diff --git a/src/edu/bu/ec504/hw2/p1/edges/Edge.java b/src/edu/bu/ec504/hw2/p1/edges/Edge.java new file mode 100644 index 0000000..25e0c27 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/edges/Edge.java @@ -0,0 +1,30 @@ +package edu.bu.ec504.hw2.p1.edges; + +import edu.bu.ec504.hw2.p1.support.UID; +import edu.bu.ec504.hw2.p1.vertices.ColoredVertex; +import edu.bu.ec504.hw2.p1.vertices.Vertex; +import edu.bu.ec504.hw2.p1.vertices.WeightedVertex; + +import java.util.ArrayList; + +/** + * A generic edge connecting {@link Vertex} objects. + * Each edge has a unique ID number. + * @param <VertexClass> The class of vertices connected by this edge. + * For example, this could be an edge between two + * {@link WeightedVertex} + * or {@link ColoredVertex} + * objects. + */ +public abstract class Edge<VertexClass extends Vertex> extends UID<Edge<VertexClass>> { + + /** + * @return true if this edge contains vertex V + */ + public abstract boolean contains(VertexClass V); + + /** + * @return true iff this edge consists of only the vertiex in Varr and no other vertices. + */ + public abstract boolean contains(ArrayList<VertexClass> Varr); +} diff --git a/src/edu/bu/ec504/hw2/p1/edges/UndirectedEdge.java b/src/edu/bu/ec504/hw2/p1/edges/UndirectedEdge.java new file mode 100644 index 0000000..5a63165 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/edges/UndirectedEdge.java @@ -0,0 +1,62 @@ +package edu.bu.ec504.hw2.p1.edges; + +import edu.bu.ec504.hw2.p1.vertices.Vertex; + +import java.util.ArrayList; + +/** + * One undirected edge between two vertices. + */ +public class UndirectedEdge<VertexClass extends Vertex> extends Edge<VertexClass> { + /** + * Creates an undirected edge between <code>VertexA</code> and <code>VertexB</code> + * + * @param theVA One vertex for the edge. + * @param theVB The other vertex for the edge. + */ + public UndirectedEdge(VertexClass theVA, VertexClass theVB) { + myVA = theVA; + myVB = theVB; + } + + /** + * Factory method for producing an undirected edge. + * @return an undirected graph joining vertex theVA to theVB. + */ + public static <VC extends Vertex> UndirectedEdge<VC> of(VC theVA, VC theVB) { + return new UndirectedEdge<>(theVA, theVB); + } + + // METHODS + @Override + public boolean contains(VertexClass V) { + return V.equals(myVA) || V.equals(myVB); + } + + /** + * @return true iff this edge consists of VA and VB. + */ + public boolean contains(VertexClass VA, VertexClass VB) { + return contains(VA) && contains(VB); + } + + /** + * @param Varr An array of vertices to consider. + * @return true iff this edge consists of the vertices in <code>Varr</code> . + */ + @Override + public boolean contains(ArrayList<VertexClass> Varr) { + if (Varr.size()!=2) + return false; + else return contains(Varr.get(0), Varr.get(1)); + } + + @Override + public String toString() { + return "Edge joining [" + myVA + "] and [" + myVB + "] "; + } + + // FIELDS + final public VertexClass myVA; + final public VertexClass myVB; +} diff --git a/src/edu/bu/ec504/hw2/p1/edges/colored_edges/ColoredUndirectedEdge.java b/src/edu/bu/ec504/hw2/p1/edges/colored_edges/ColoredUndirectedEdge.java new file mode 100644 index 0000000..27a362f --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/edges/colored_edges/ColoredUndirectedEdge.java @@ -0,0 +1,21 @@ +package edu.bu.ec504.hw2.p1.edges.colored_edges; + +import edu.bu.ec504.hw2.p1.edges.weighted_edges.WeightedUndirectedEdge; +import edu.bu.ec504.hw2.p1.support.Color; +import edu.bu.ec504.hw2.p1.vertices.Vertex; + +public class ColoredUndirectedEdge<VertexClass extends Vertex> extends WeightedUndirectedEdge<VertexClass, Color> { + + /** + * Creates a weighted undirected edge between <code>source</code> and <code>dest</code>. + * + * @param source The initial vertex for the edge. + * @param dest The final vertex for the edge. + * @param theClr The color of the edge + */ + public ColoredUndirectedEdge(VertexClass source, VertexClass dest, Color theClr) { + super(source, dest, theClr); + } + + Color getColor() { return myWt.get(); } +} diff --git a/src/edu/bu/ec504/hw2/p1/edges/weighted_edges/WeightedEdge.java b/src/edu/bu/ec504/hw2/p1/edges/weighted_edges/WeightedEdge.java new file mode 100644 index 0000000..fa50d89 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/edges/weighted_edges/WeightedEdge.java @@ -0,0 +1,28 @@ +package edu.bu.ec504.hw2.p1.edges.weighted_edges; + +import edu.bu.ec504.hw2.p1.edges.Edge; +import edu.bu.ec504.hw2.p1.vertices.Vertex; + +/** + * Represents a weighted edge. + * @param <VertexClass> The class of the vertices associated with this edge. + * @param <WeightClass> The class of the weights associated with this edge. + */ +public abstract class WeightedEdge<VertexClass extends Vertex, WeightClass> extends Edge<VertexClass> { + + /** + * @return The weight of this edge. + */ + public WeightClass getWt() { + return myWt; + } + + public void setWt(WeightClass wt) { + this.myWt = wt; + } + + /** + * The weight of the edge. + */ + protected WeightClass myWt; +} diff --git a/src/edu/bu/ec504/hw2/p1/graphs/EdgeListGraph.java b/src/edu/bu/ec504/hw2/p1/graphs/EdgeListGraph.java new file mode 100644 index 0000000..9af7978 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/graphs/EdgeListGraph.java @@ -0,0 +1,81 @@ +package edu.bu.ec504.hw2.p1.graphs; + +import edu.bu.ec504.hw2.p1.edges.Edge; +import edu.bu.ec504.hw2.p1.vertices.Vertex; + +import java.util.ArrayList; +import java.util.List; + +/** + * Implements an Edge-List representation of a Graph. + * @param <VertexClass> The class of the Vertices of the graph. + * @param <EdgeClass> The class of the Edges of the graph. + */ +public class EdgeListGraph <VertexClass extends Vertex, EdgeClass extends Edge<VertexClass>> + extends Graph<VertexClass,EdgeClass> { + // CONSTRUCTOR + /** + * Constructs a new EdgeListGraph + */ + public EdgeListGraph() { + edgeList = new ArrayList<>(); + } + + // METHODS + + // ... QUERY + + /** + * @inheritDoc + */ + @Override + public boolean adjQ(ArrayList<VertexClass> Vlist) { + for (EdgeClass edge : edgeList) { + if (edge.contains(Vlist)) + return true; + } + return false; + } + + /** + * @return true iff v1 and v2 are adjacent + */ + public boolean adjQ(VertexClass v1, VertexClass v2) { + return adjQ(new ArrayList<>(List.of(v1, v2))); + } + + @Override + public String toString() { + final String TAB = " "; + + StringBuilder result = new StringBuilder(); + result.append(TAB+"Vertices:\n"); + for (VertexClass vert: vertices) { + result.append(TAB+TAB+vert+"\n"); + } + result.append("\n"+TAB+"Edges:\n"); + for (EdgeClass edge: edgeList) { + result.append(TAB+TAB+edge); + result.append('\n'); + } + return result.toString(); + } + + // ... MANIPULATION + + @Override + public VertexClass addVertex(VertexClass v) { + vertices.add(v); + return v; + } + + @Override + public void addEdge(EdgeClass theEdge) { + edgeList.add(theEdge); + } + + // DATA FIELDS + + /** The list of edges for the graph. */ + protected final ArrayList<EdgeClass> edgeList; +} diff --git a/src/edu/bu/ec504/hw2/p1/graphs/Graph.java b/src/edu/bu/ec504/hw2/p1/graphs/Graph.java new file mode 100644 index 0000000..f085235 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/graphs/Graph.java @@ -0,0 +1,74 @@ +package edu.bu.ec504.hw2.p1.graphs; + +import edu.bu.ec504.hw2.p1.edges.Edge; +import edu.bu.ec504.hw2.p1.vertices.Vertex; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Represents an undirected, unweighted graph. + * @author Ari Trachtenberg + */ + +public abstract class Graph<VertexClass extends Vertex, EdgeClass extends Edge<VertexClass>> { + + // SUBCLASSES + + // METHODS + + // ... QUERY + /** + * @return the number of vertices currently in the graph. + */ + public int numVertices() { + return vertices.size(); + } + + /** + * @param ii An integer between 0 [inclusive] and numVertices() [exclusive] + * @return The ii-th vertex that was inserted into the graph. + */ + public VertexClass ithVertex(int ii) { return vertices.get(ii); } + + /** + * @return true iff there is an edge connecting exactly the vertices in Vlist + */ + abstract public boolean adjQ(ArrayList<VertexClass> Vlist); + + /** + * An alternate form of {@link #adjQ(ArrayList)}. + */ + @SafeVarargs + public final boolean adjQ(VertexClass... V) { + List<VertexClass> ls = Arrays.asList(V); + return adjQ(new ArrayList<>(ls)); + } + + /** + * @return A human-readable version of the graph + */ + public abstract String toString(); + + // ... MANIPULATION + + /** + * Adds a new vertex to the graph (not part of any edges). + * @param theVertex the new vertex to add + * @return theVertex + */ + public abstract VertexClass addVertex(VertexClass theVertex); + + /** + * Adds an edge to the graph + * @param theEdge the edge to add + */ + public abstract void addEdge(EdgeClass theEdge); + + + // DATA FIELDS + + /** The vertices of the graph. */ + protected final ArrayList<VertexClass> vertices = new ArrayList<>(); +} diff --git a/src/edu/bu/ec504/hw2/p1/support/Color.java b/src/edu/bu/ec504/hw2/p1/support/Color.java new file mode 100644 index 0000000..60c96e7 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/support/Color.java @@ -0,0 +1,34 @@ +package edu.bu.ec504.hw2.p1.support; + +/** + * An enumerated class representing a color. + */ +public enum Color { + RED, + ORANGE, + YELLOW, + GREEN, + BLUE, + INDIGO, + VIOLET; + + /** + * @return the next color from this one, cycling back to the first color. + */ + public Color next() { + Color[] colors = Color.values(); + return colors[(this.ordinal()+1)%colors.length]; + } + + /** + * @return The first color in this enum. + */ + static public Color first() { + return Color.values()[0]; + } + + @Override + public String toString() { + return "the color " + name(); + } +} diff --git a/src/edu/bu/ec504/hw2/p1/support/UID.java b/src/edu/bu/ec504/hw2/p1/support/UID.java new file mode 100644 index 0000000..81ca152 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/support/UID.java @@ -0,0 +1,18 @@ +package edu.bu.ec504.hw2.p1.support; + +/** + * Represents objects with a unique ID. + * No different objects of this class should have the same ID. + * Objects of the same Base class should have successively incrementing IDs. + */ +public class UID<Base> { + public UID() { + myID = currID++; + } + final public int myID; + + /** + * The current ID, incremented with each new object of type Base. + */ + private static int currID = 0; +} diff --git a/src/edu/bu/ec504/hw2/p1/support/Weighted.java b/src/edu/bu/ec504/hw2/p1/support/Weighted.java new file mode 100644 index 0000000..e3e1d08 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/support/Weighted.java @@ -0,0 +1,45 @@ +package edu.bu.ec504.hw2.p1.support; + +/** + * Adds weight to an object. + * @param <WeightClass> The class of the weight, for example Integer, Float, or String. + */ +public class Weighted<WeightClass> { + + /** + * Factory method for producing an object with the given weight. + * @param wt The weight of the object to be produced. + * @return An object with the given weight. + * @param <WC> The class of the weight that is being applied to an object. + */ + public static <WC> Weighted<WC> of(WC wt) { + Weighted<WC> newWeight = new Weighted<>(); + newWeight.set(wt); + return newWeight; + } + + /** + * @return The weight associated with this object. + */ + public WeightClass get() { + return myWt; + } + + /** + * Set this object to the given weight. + * @param wt The weight to set to the object. + */ + public void set(WeightClass wt) { + this.myWt = wt; + } + + @Override + public String toString() { + return "weight ["+myWt+"]"; + } + + /** + * The weight of the edge. + */ + WeightClass myWt; +} diff --git a/src/edu/bu/ec504/hw2/p1/vertices/Vertex.java b/src/edu/bu/ec504/hw2/p1/vertices/Vertex.java new file mode 100644 index 0000000..b6f25eb --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/vertices/Vertex.java @@ -0,0 +1,46 @@ +package edu.bu.ec504.hw2.p1.vertices; + +import edu.bu.ec504.hw2.p1.support.UID; + +/** + * A generic vertex in a graph. + * Each Vertex has a unique ID number. + */ + +public class Vertex extends UID<Vertex> { + + /** + * Use the factory method {@link #of()} to create vertices. + */ + protected Vertex() { + super(); + } + + /** + * @return A new Vertex + */ + public static Vertex of() { + return new Vertex(); + } + + /** + * @return If this Vertex has the same ID as <code>otherV</code> + */ + @Override + public boolean equals(Object otherV) { + // self-comparison + if (this == otherV) return true; + + // null or mismatched classes + if (otherV == null || getClass() != otherV.getClass()) return false; + + // Cast to the correct type + Vertex other = (Vertex) otherV; + + // Check vertex IDs + return myID == other.myID; + + } + + public String toString() { return "Vertex #"+myID; } +} diff --git a/src/edu/bu/ec504/hw2/p1/vertices/WeightedVertex.java b/src/edu/bu/ec504/hw2/p1/vertices/WeightedVertex.java new file mode 100644 index 0000000..956f421 --- /dev/null +++ b/src/edu/bu/ec504/hw2/p1/vertices/WeightedVertex.java @@ -0,0 +1,31 @@ +package edu.bu.ec504.hw2.p1.vertices; + +import edu.bu.ec504.hw2.p1.support.Weighted; + +/** + * Represents a vertex with an associated weight - analogous to weighted edges. + * @param <WeightClass> + */ +public class WeightedVertex<WeightClass> extends Vertex { + + /** + * Creates a vertex whose weight is assigned to <code>wt</code>. + * @param wt The weight of the vertex. + */ + WeightedVertex(WeightClass wt) { + super(); + myWt = Weighted.of(wt); + } + + @Override + public String toString() { + return super.toString()+" with " + myWt; + } + + public static <WC> WeightedVertex<WC> of(WC wt) { + return new WeightedVertex<>(wt); + } + + // FIELDS + Weighted<WeightClass> myWt; +} -- GitLab From 63ce1e075c974c8df440141fd254259493d30609 Mon Sep 17 00:00:00 2001 From: Ari Trachtenberg <trachten@bu.edu> Date: Sun, 3 Mar 2024 11:12:06 -0500 Subject: [PATCH 2/2] Initial version --- README.md | 95 ++------------------------------------------------ src/.gitignore | 29 +++++++++++++++ 2 files changed, 31 insertions(+), 93 deletions(-) create mode 100644 src/.gitignore diff --git a/README.md b/README.md index 3eb2786..2ae339f 100644 --- a/README.md +++ b/README.md @@ -1,93 +1,2 @@ -# hw2p1 - - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://agile.bu.edu/gitlab/ec504/hw2/hw2p1.git -git branch -M master -git push -uf origin master -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://agile.bu.edu/gitlab/ec504/hw2/hw2p1/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README - -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +# Homework Two, Problem 1 +Please complete the missing code in your own version of this repository. \ No newline at end of file diff --git a/src/.gitignore b/src/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/src/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file -- GitLab