Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@ gradle-app.setting

# gimp image file to make pngs of graphs for tests
*.xcf
.idea
26 changes: 0 additions & 26 deletions .idea/compiler.xml

This file was deleted.

3 changes: 0 additions & 3 deletions .idea/copyright/profiles_settings.xml

This file was deleted.

50 changes: 0 additions & 50 deletions .idea/misc.xml

This file was deleted.

10 changes: 0 additions & 10 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/modules/1748581304/coding-exercises.iml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/modules/1748581304/coding-exercises_main.iml

This file was deleted.

16 changes: 0 additions & 16 deletions .idea/modules/1748581304/coding-exercises_test.iml

This file was deleted.

73 changes: 73 additions & 0 deletions src/main/java/graphs/Edge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package graphs;

public class Edge implements Comparable<Edge> {
private Node leftNode;
private Node rightNode;
private double weight;

public Edge(Node leftNode, Node rightNode, double weight) {
this.leftNode = leftNode;
this.rightNode = rightNode;
setWeight(weight);
}

public Edge(Node leftNode, Node rightNode) {
this.leftNode = leftNode;
this.rightNode = rightNode;
setWeight(1);
}

public Edge(Edge edge) {
this.leftNode = edge.getLeftNode();
this.rightNode = edge.getRightNode();
this.weight = edge.getWeight();
}

public double getWeight() {
return weight;
}

public Edge setWeight(double weight) {
this.weight = weight;
return this;
}

public Node getLeftNode() {
return leftNode;
}

public Node getRightNode() {
return rightNode;
}

public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}

public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}

public boolean contains(Node node) {
return leftNode.equals(node) || rightNode.equals(node);
}

// will return null if node is not in edge, else returns opposite node
public Node getOtherNode(Node node) {
if(contains(node)) {
if(node.equals(getRightNode())) {
return getLeftNode();
} else {
return getRightNode();
}
} else {
return null;
}
}

// "Note: this class has a natural ordering that is inconsistent with equals."
public int compareTo(Edge edge) {
return Double.compare(this.getWeight(), edge.getWeight());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

}
Original file line number Diff line number Diff line change
@@ -1,60 +1,82 @@
package graphs;

import java.util.*;

public class GraphClass {
private List<GraphEdge> edges;
private List<GraphNode> nodes;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.LinkedList;
import java.util.Collections;


public class Graph {
private List<Edge> edges;
private List<Node> nodes;
private boolean isDirected;
private boolean isWeighted;

public GraphClass() {
edges = new ArrayList<GraphEdge>();
nodes = new ArrayList<GraphNode>();
public Graph() {
edges = new ArrayList<Edge>();
nodes = new ArrayList<Node>();
setDirected(false);
setWeighted(false);
}

public GraphClass(List<GraphNode> nodes, List<GraphEdge> edges) {
this.edges = edges;
public Graph(List<Node> nodes, List<Edge> edges) {
// if no flag for isDirected, default false
this(nodes, edges, false);
}

public Graph(List<Node> nodes, List<Edge> edges, boolean isDirected) {
this.nodes = nodes;
setDirected(false);
setDirected(isDirected);
if(isDirected()) {
this.edges = edges;
} else {
this.edges = new ArrayList<Edge>();
for(Edge edge : edges) {
addEdge(edge);
}
}
setWeighted(false);
}

public GraphClass(GraphClass graph) {
public Graph(Graph graph) {
edges = graph.getEdges();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intent of this copy constructor is to provide a deep copy of the specified graph? Or is it alright that we point to the same set of edges and nodes that the original graph does?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont really know why i implemented this, i guess just in case. here the new graph instance will have it's edges pointing to the same edges object of the first graph? so if i added an edge to the second graph, would the edges of the first graph update as well?

nodes = graph.getNodes();
isDirected = graph.isDirected();
isWeighted = graph.isWeighted();
}

public List<GraphEdge> getEdges() {
public List<Edge> getEdges() {
return edges;
}

public List<GraphNode> getNodes() {
public List<Node> getNodes() {
return nodes;
}

public void addNode(GraphNode node) {
public void addNode(Node node) {
nodes.add(node);
}

public void addEdge(GraphEdge edge) {
public void addEdge(Edge edge) {
edges.add(edge);
if(! isDirected()) {
edges.add(new Edge(edge.getRightNode(), edge.getLeftNode(), edge.getWeight()));
}
}

// public void addEdge(GraphNode leftNode, GraphNode rightNode) {
// public void addEdge(Node leftNode, Node rightNode) {
//// weight is the sqrt( ( left.x - right.x) ^ 2 + (left.y - right.y) ^ 2)
// double weight = Math.sqrt(Math.pow(leftNode.getX() - rightNode.getX(), 2) + Math.pow((leftNode.getY() - rightNode.getY()),2));
// edges.add(new GraphEdge(leftNode, rightNode, weight));
// edges.add(new Edge(leftNode, rightNode, weight));
// }

// returns removed node for chaining
public GraphNode removeNode(GraphNode node) {
Set<GraphEdge> toRemove = new HashSet<GraphEdge>();
for(GraphEdge edge : edges) {
public Node removeNode(Node node) {
Set<Edge> toRemove = new HashSet<Edge>();
for(Edge edge : edges) {
if(edge.contains(node)) {
toRemove.add(edge);
}
Expand All @@ -64,16 +86,13 @@ public GraphNode removeNode(GraphNode node) {
return node;
}

public void removeEdge(GraphEdge edge) {
public void removeEdge(Edge edge) {
edges.remove(edge);
}

// set directed will work on graph and all edges so can be done at the end
public void setDirected(boolean directed) {
isDirected = directed;
for(GraphEdge edge : edges) {
edge.setDirected(directed);
}
public void setDirected(boolean isDirected) {
this.isDirected = isDirected;
}

public boolean isDirected() {
Expand All @@ -89,10 +108,10 @@ public void setWeighted(boolean weighted) {
isWeighted = weighted;
}

public List<GraphEdge> getIncidentEdges(GraphNode node) {
List<GraphEdge> incidentEdges = new LinkedList<GraphEdge>();
for(GraphEdge edge : edges) {
if(edge.getLeftNode().equals(node) || (edge.getRightNode().equals(node) && ! isDirected)) {
public List<Edge> getIncidentEdges(Node node) {
List<Edge> incidentEdges = new LinkedList<Edge>();
for(Edge edge : edges) {
if(edge.getLeftNode().equals(node)) {
incidentEdges.add(edge);
}
}
Expand Down
Loading