Skip to content

Comments

Graphs second refactor#24

Open
capttrousers wants to merge 10 commits intomasterfrom
graphs-refactor
Open

Graphs second refactor#24
capttrousers wants to merge 10 commits intomasterfrom
graphs-refactor

Conversation

@capttrousers
Copy link
Owner

abstracts weighted / directed flags to graph.
edges now are always directed, weighted with a default weight of 1.
a few the tests failed at first because the algos are deterministic depending on the order the edges are added to the List that is used to instantiate that graph.

return 1;
}
// is it necessary to specify "this." ?
if(edge.getWeight() > this.getWeight()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

use Double's compareTo instead

Copy link
Owner Author

Choose a reason for hiding this comment

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

so the compare to method just cares about less than, greater than, or equal to zero. it doesnt need to handle the margin of difference. since im just returning 0, -1 or 1, I could change it to Double compareTo(), but it looks like the Comparable interface says the method should return an int, if i change that would the Comparable interface not be implemented correctly and thus Collection.sort() wouldnt work?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking it would be this:
Double.compare(this.getWeight(), edge.getWeight())

I think that's the method we're looking for.

edgesForGraph11.add(edgeBetweenDandC.setWeighted(true).setWeight(1));
edgesForGraph11.add(edgeBetweenEandD.setWeighted(true).setWeight(1));
edgesForGraph11.add(edgeBetweenDandF.setWeighted(true).setWeight(1));
edgesForGraph11.add(edgeBetweenAandD.setWeight(1));
Copy link
Collaborator

Choose a reason for hiding this comment

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

why not set the weight on the edge in a constructor? or even better, declare this graph unweighted?

Copy link
Owner Author

Choose a reason for hiding this comment

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

this graph is weighted, because theres an option to go to two different edges from node Z, either to node D or to node G, and ultimately, going to D would be the shorter path, but i am tricking the algo to take the local optimum which is to node G because the other edge (Z->D) has weight 2, which adds one extra edge to the final path.

edges.add(new GraphEdge(edge.getRightNode(), edge.getLeftNode(), edge.getWeight()));
}

public GraphClass duplicateUndirectedEdges() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need this? is there any other way to add an edge besides the constructor(s) and addEdge?

Copy link
Owner Author

Choose a reason for hiding this comment

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

what i am thinking now, is change the Graph constructors to take the isDirected flag, and then set that first in the constructor, and then say if(isDirected()) this.edges = new List(edges) else for(edge : edges) addEdge(edge)
this will either just put the edges into the graphs edge list, or if it's undirected, add each edge individually with the public addEdge() method which would duplicate the edge for both directions. then I can remove the public duplicateEdges() method, and just handle the edges in the constructor, and allow any new edges to be duplicated if it's not directed.

Copy link
Collaborator

@shalka shalka left a comment

Choose a reason for hiding this comment

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

things are looking really good!


// "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
Collaborator

Choose a reason for hiding this comment

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

👍


public GraphClass(GraphClass graph) {
public Graph(Graph graph) {
edges = graph.getEdges();
Copy link
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
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?

throw new IllegalArgumentException("null start node or target node");
}
if(! (graph.getNodes().contains(start) && graph.getNodes().contains(finish))) {
return new LinkedList();
Copy link
Collaborator

Choose a reason for hiding this comment

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

it seems to me like this would be exceptional. the caller provided a graph that doesn't contain the start or finish node. empty list indicates that there is no path between start and finish, but implicitly they are both contained in the graph. my vote is a throw here (though if you feel strongly about this, i think its alright)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants