Let’s Say I have MyClass{ private LargeMatrix mtrx; hashCode(){...}}
JGraphT (maybe all Graph data structures) seems to be using Hash table to map the vertices. So will that affect the speed when I’m using the MyClass instead of String l1,l2,l3 ?
What are the pros and cons in that case ? Should I override hashcode(remove matrix hashcode ) ? Is there a graph that uses references instead of hashtable ?
so My code was:
package ann;
import org.jgrapht.DirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleDirectedGraph;
/**
* @author marmoush
*
*/
public class Network
{
DirectedGraph<String, DefaultEdge> diGraph;
String l1="hello1";
String l2="hello1";
String l3="hello3";
/**
*
*/
public Network()
{
diGraph = new SimpleDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
diGraph.addVertex(l1);
diGraph.addVertex(l2);
diGraph.addVertex(l3);
diGraph.addEdge(l1, l2);
System.out.println(diGraph.containsEdge(l1,l2));
// TODO Auto-generated constructor stub
}
}
Exception in thread "main" java.lang.IllegalArgumentException: loops not allowed
at org.jgrapht.graph.AbstractBaseGraph.addEdge(Unknown Source)
at ann.Network.<init>(Network.java:28)
at test.TestNetwork.main(TestNetwork.java:9)
Because (I think) l1.hashCode()==l2.hashCode()
EDIT:
Matrices might be zeros sometimes or ones, they change over time so I would try to come up with something that differentiate between those objects, and that seems to be stupid solution. Why can’t the graph just select vertices by there position in a vector or something ?
Should I reinvent the wheel ? with a graph that uses Vectors instead of hashtables ? or there is a work around ?
I would create a Vertex class and implement
equals()andhashcode()appropriately. The speed impact is not that big, it can even be faster if the id of the vertex is numeric.