-
Notifications
You must be signed in to change notification settings - Fork 0
Graphs second refactor #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
8ebb66c
6aa66c5
b5309c6
c359145
49a60ac
1be5c14
e8dee03
ada9e37
c83f993
dbb6d5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,3 +53,4 @@ gradle-app.setting | |
|
|
||
| # gimp image file to make pngs of graphs for tests | ||
| *.xcf | ||
| .idea | ||
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| 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()); | ||
| } | ||
|
|
||
| } | ||
| 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(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
| } | ||
|
|
@@ -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() { | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍