add subtree below more than one nodes in a graph - java

I am using Java JUNG 2.0.1 version. I understand basics of JUNG API.
I have a tree with root vertex as 1 [see Input graph].
Basically, I want to remove an edge (from:1 to:3) i.e. have sub-tree where root is vertex 3 and add it below vertex 2 and vertex 5 separately [see Output graph].
I used getSubTree() and addSubTree() of TreeUtils.java.
But, it gives error with stack trace :
java.lang.IllegalArgumentException: Tree must not already contain child a.b.c
at edu.uci.ics.jung.graph.DelegateTree.addChild(DelegateTree.java:182)
at edu.uci.ics.jung.graph.DelegateTree.addEdge(DelegateTree.java:102)
at edu.uci.ics.jung.graph.util.TreeUtils.addFromSubTree(TreeUtils.java:139)
at edu.uci.ics.jung.graph.util.TreeUtils.addSubTree(TreeUtils.java:100)
Input graph :
Output graph :

Marco13# is correct. Graph elements (nodes and edges) are required to be unique; think of them as elements of a set.
If you want two nodes to have the same label (or value, or some other associated data), there are several ways to go about doing this, as discussed here: http://sourceforge.net/apps/trac/jung/wiki/JUNGManual#UserData

Related

Finding Index of a Node in a Huffman Tree

Assume we have a tree of nodes (Huffman-tree) that hold string values in them. How would I go about traversing the tree and spitting out an index of a specific node if I had a tree like this? The numbers I drew inside the circles would be the index I want (especially 12 or 13).
Note: Due to miscommunication, I will repeat: the #'s I wrote inside the circles are not the values that the nodes hold. They are the index of that node. My problem was that I couldn't find the index, since the trees are structured weirdly - not a classic BST tree, and the values inside aren't numerical.
Edit: I redrew the image to make my question more clear.
Either way, I figured it out. I'll write up the answer after my finals.
The tree you are showing is not a binary search tree. The central property of a binary search tree that allows it to be searched efficiently is that the left descendants of a node are smaller, and the right descendants bigger than the node itself (in terms of the index value).
If you have a proper binary search tree, you can find a node with given index by comparing with nodes and following the corresponding branch, starting with the root.

gremlin query to retrieve vertices which are having multiple edges between them

Consider the above graph. I would like a gremlin query that returns all nodes that have multiple edges between them as shown in the graph.
this graph was obtained using neo4j cypher query:
MATCH (d:dest)-[r]-(n:cust)
WITH d,n, count(r) as popular
RETURN d, n
ORDER BY popular desc LIMIT 5
for example:
between RITUPRAKA... and Asia there are 8 multiple edges hence the query has returned the 2 nodes along with the edges, similarly for other nodes.
Note: the graph has other nodes with only a single edge between them, these nodes will not be returned.
I would like same thing in gremlin.
I have used given below query
g.V().as('out').out().as('in').select('out','in').groupCount().unfold().filter(select(values).is(gt(1))).select(keys)
it is displaying
out:v[1234],in:v[3456] .....
but instead of displaying Ids of the node I want to display values of the node
like out:ICIC1234,in:HDFC234
I have modified query as
g.V().values("name").as('out').out().as('in').values("name").select('out','in').
groupCount().unfold().filter(select(values).is(gt(1))).select(keys)
but it is showing the error like classcastException, each vertex to be traversed use indexes for fast iteration
Your graph doesn't seem to indicate bi-directional edges are possible so I will answer with that assumption in mind. Here's a simple sample graph - please consider including one on future questions as it makes it much easier than pictures and textual descriptions for those reading your question to understand and to get started writing a Gremlin traversal to help you:
g.addV().property(id,'a').as('a').
addV().property(id,'b').as('b').
addV().property(id,'c').as('c').
addE('knows').from('a').to('b').
addE('knows').from('a').to('b').
addE('knows').from('a').to('c').iterate()
So you can see that vertex "a" has two outgoing edges to "b" and one outgoing edge to "c", thus we should get the "a b" vertex pair. One way to get this is with:
gremlin> g.V().as('out').out().as('in').
......1> select('out','in').
......2> groupCount().
......3> unfold().
......4> filter(select(values).is(gt(1))).
......5> select(keys)
==>[out:v[a],in:v[b]]
The above traversal uses groupCount() to count the number of times the "out" and "in" labelled vertices show up (i.e. the number of edges between them). It uses unfold() to iterate through the Map of <Vertex Pairs,Count> (or more literally <List<Vertex>,Long>) and filter out those that have a count greater than 1 (i.e. multiple edges). The final select(keys) drops the "count" as it is not needed anymore (i.e. we just need the keys which hold the vertex pairs for the result).
Perhaps another way to go is with this method:
gremlin> g.V().filter(outE()).
......1> project('out','in').
......2> by().
......3> by(out().
......4> groupCount().
......5> unfold().
......6> filter(select(values).is(gt(1))).
......7> select(keys)).
......8> select(values)
==>[v[a],v[b]]
This approach with project() forgoes the heavier memory requirements for a big groupCount() over the whole graph in favor of building a smaller Map over an individual Vertex that becomes eligible for garbage collection at the end of the by() (or essentially per initial vertex processed).
My suggestion is similar to Stephen's, but also includes the edges or rather the whole path (I guess the Cypher query returned the edges too).
g.V().as("dest").outE().inV().as("cust").
group().by(select("dest","cust")).by(path().fold()).
unfold().filter(select(values).count(local).is(gt(1))).
select(values).unfold()

Directed Acyclic Graph Traversal in Java Web Application

So I am building a web application where you can build a directed graph where a node will represent some operation and the edge will represent data flow between those operations. So for an edge {u,v} , u must run before v does. Click this link to see a sample graph
START node represents the initial value and the other nodes except the output does the operation as specified. Output node will output the value it receives as input.
Which algorithm approach should i use to process a graph like that ?
This is a perfect example of a topological sort. The most common algorithm for creating a set following the order requirements via traversing is Kahn's Algorithm. The pseudocode can be seen below and the information in the Wikipedia link should be enough to get you started.
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
Note the "starting node" will be enforced by properly representing the incoming edges in the graph. It will be the only node in S to start. Please let me know in the comments if you would like any other information.

Graph representation in Java using linked structure

I need to implement a graph in Java. I cannot use the collection classes. I can readily create a graph using an Adjacency Matrix or an Adjacency List, but I need to create a directed graph using a linked structure.
I might be given the adjacency matrix form:
4 // square matrix size -- all would be square -- square only
0 1 1 0
1 1 1 1
1 0 0 0
1 1 0 1
NOTE: This is supposed to a a 4x4 matrix, but I cannot format correctly for StackOverflow. I do not know how to enter hard return.
Continuing my question, I found a similar question on StackOverflow:
Questions regarding Implementation of graph in c++
Where in the best accepted answer, pmr wrote:
1.) Yes, you can also implement it explicitly using pointers to other nodes.
This answer by pmr is the form of solution I need. This is where I am having trouble. I simply cannot picture this solution in my head. One of the problems is how do I allow for a number of child nodes that is read from a file so that I can refer to them for a search ? One adjacency list implementation would allow me to create an array of linked lists, but I cannot use that.
Is this a multi-graph ? Is this a problem that multi-graphs solve ?
Alternate: I have also envisioned a tree structure with multiple child nodes per parent.
How would I go about implementing this in Java ?
One possible solution:
Create a class representing each graph node.
class GraphNode {
LinkedList<GraphNode> children;
}
Each line of data you read in will represent a GraphNode. Each '1' in that line represents a link to the GraphNode at the same index as that '1'.
Create a GraphNode for each line and add it to a list/array of GraphNodes (maybe call it 'allNodes'). For each 1 or 0 on a line, increment a counter. If it is a 1, add the Node in allNodes at that index (use counter as the index) to the children of the current node.
Now you will have a list of GraphNodes, each with its own list of the GraphNodes it is connected to.
Hope that makes sense.

How to merge multiple trees by retaining only those nodes which are common to all the trees

so I have two to three Trees that I generate during execution of some code. Each node of this tree has this property that it will have a minimum of 0 children to a maximum of 8 children. So I guess that you are able to get the picture of a complete tree of this sort, that has at level 0 a single root node. At level 1 8 nodes at level 2 64 nodes and so on. Each of the children nodes of a parent are numbered from 0 to 7 which I store on them as a byte integer type in java, btw I am making this in java.
Now I need to merge these two to three tree's that I have generated and I do it level wise completely disregarding the children of a given node. If at a level for the three tree's say at level 1, if tree 1 has 4,5,6 and tree 2 has 5,6,7 and tree 3 has 2,5,6 my resultant tree should have 5,6 as it is common to all the three tree's. I am not concerned if the tree 1's 5th node has 4 children and the same 5th node at that level in tree 2 has 3 children. if a node is labeled 5 at a level it is suppose to be identical to the node labeled 5 at the same level in a different tree irrespective of the number of children it has or if the children are same or not.
So to state what I mentioned in second paragraph visually so as to ensure no ambiguity I am also enclosing these three diagrams. The first two are merged to give the third tree.
And please I have all the time in the world to make this, and this is for a personal purpose and I am trying to learn something here, so please don't suggest any libraries.
The solution am thinking of involves a single queue for each tree and doing a level order traversal on these three trees and adding nodes that I encounter to the queue. and right before I start adding children of a given node I see what is common to these three queues ! and I set the common part nodes in my resultant tree. But I was wondering if there is a solution more efficient than this.
Assuming the trees and children are in the same relative order in both cases, you can merge the trees together using a simple recursive algorithm:
Merging together an empty tree and any tree yields the empty tree.
Merging together trees with different roots yields the empty tree.
Merging together two trees with the same root r and children c1, ..., cn and d1, ..., dn is done by producing a new tree with root r whose children are the merged ci / dj pairs for ci and di that have the same root value.
Hope this helps!

Categories

Resources