Re: [jgrapht-users] add edge to directed graph in both directions
Brought to you by:
barak_naveh,
perfecthash
From: krutor <mat...@gm...> - 2015-06-24 11:03:09
|
Hi everyone,I ended up using undirected graph (WeightedPseudograph) with my Edge Implementation that has "directed" flag. I hid this WeightedPseudograph behind my own interface that knows about mixed nature of the graph and modifies graph behaviour for its clients (it also deals with multithreading and other staff...). For example I had to modify computation of In Edges for vertex to account for edges directed flag like this: public Set getInEdges(Node node) { Set inEdges = new HashSet<>(); try { readLock(); Set edges = graphImpl.edgesOf(node); // graphImpl = WeightedPseudograph<Node, Edge> for (Edge edge : edges) { if (!edge.isDirected() || edge.getTarget().equals(node)) { inEdges.add(edge); } } return Collections.unmodifiableSet(inEdges); } finally { readUnlock(); }} However this way you are not able to use JGraphT algorithms for directed graphs - for example DFS algorithm now treats the graph as undirected, so it finds path between target and source node even on directed edge (which is ok in my case)...I chose this implementation only because I need to differentiate between case when there is undirected edge between two nodes and when there is actually two opposite directed edges between them... Otherwise I would use directed graph and represent undirected edge as two opposite directed edges, which is IMHO cleaner solution. -- View this message in context: http://jgrapht-users.107614.n3.nabble.com/add-edge-to-directed-graph-in-both-directions-tp4024989p4024994.html Sent from the jgrapht-users mailing list archive at Nabble.com. |