How to remove nodes with java API for graphstream? - java

I currently use the Graphstream API for Java for my project.
I wan't to delete or add Nodes on command.
With JFrame & co. I initialized a console so I can just insert
"addNode()" or "removeNode(id)" in order to get the result.
A Interface shows the nodes with a number next to them(the ID).
When I delete one node, I want all nodes with higher ID to change their ID,
but I did not figure out a way jet to change the ID of one node.
F.e. I have:
graph.addNode(0);
graph.addNode(1);
graph.addNode(2);
When deleting a Node:
graph.removeNode(0);
I want 1,2 to be changed to 0,1 without reinitializing the complete graph.
Is there a way to achieve this behaviour? I thought about something like:
graph.getNode(1).setID(0);
Unfortunately I have only access to .getID() and can't manipulate it this way.
Thanks

Nodes ids are strings and they are immutable (no renaming, no setId()).
Now what you are doing in your example is different. You are using the index-based access to the nodes. Indices are integers and correspond to arbitrary nodes in the graph, they are not associated to the ids.
When you do graph.addNode(0), the integer is converted to the string "0". Then when you do graph.removeNode(0), you are removing a node that was indexed as the first of the list of nodes. But it does have to be the node this id "0".
You can remove nodes with index (integer) 0 as long as there are nodes in the graph (graph.removeNode(0)) but you can only remove the one node with id "0" once (graph.removeNode("0")).

Related

Why does my Gremlin traversal add only one edge?

As described in another question, I am attempting to add several "identity" vertices into a "group" vertex. Based on the recipe recommendation, I'm trying to write the traversal steps in such a way that the traversers iterate the identity vertices instead of appending extra steps in a loop. Here's what I have:
gts.V(group)
.addE('includes')
.to(V(identityIds as Object[]))
.count().next()
This always returns a value of 1, no matter how many IDs I pass in identityIds, and only a single edge is created. The profile indicates that only a single traverser is created for the __.V even though I'm passing multiple values:
Traversal Metrics
Step Count Traversers Time (ms) % Dur
=============================================================================================================
TinkerGraphStep(vertex,[849e1878-86ad-491e-b9f9... 1 1 0.633 40.89
AddEdgeStep({label=[Includes], ~to=[[TinkerGrap... 1 1 0.915 59.11
TinkerGraphStep(vertex,[50d9bb4f-ed0d-493d-bf... 1 1 0.135
>TOTAL - - 1.548 -
Why is only a single edge added to the first vertex?
The to() syntax you are using is not quite right. A modulator like to() expects the traversal you provide it to produce a single Vertex not a list. So, given V(identityIds) only the first vertex returned from that list of ids will be used to construct the edge. Step modulators like to(), by(), etc. tend to work like that.
You would want to reverse your approach to:
gts.V(identityIds)
.addE('includes')
.from(V(group))
.count().next()
But perhaps that leads back to your other question.

Data Structure choices based on requirements

I'm completely new to programming and to java in particular and I am trying to determine which data structure to use for a specific situation. Since I'm not familiar with Data Structures in general, I have no idea what structure does what and what the limitations are with each.
So I have a CSV file with a bunch of items on it, lets say Characters and matching Numbers. So my list looks like this:
A,1,B,2,B,3,C,4,D,5,E,6,E,7,E,8,E,9,F,10......etc.
I need to be able to read this in, and then:
1)display just the letters or just the numbers sorted alphabetically or numerically
2)search to see if an element is contained in either list.
3)search to see if an element pair (for example A - 1 or B-10) is contained in the matching list.
Think of it as an excel spreadsheet with two columns. I need to be able to sort by either column while maintaining the relationship and I need to be able to do an IF column A = some variable AND the corresponding column B contains some other variable, then do such and such.
I need to also be able to insert a pair into the original list at any location. So insert A into list 1 and insert 10 into list 2 but make sure they retain the relationship A-10.
I hope this makes sense and thank you for any help! I am working on purchasing a Data Structures in Java book to work through and trying to sign up for the class at our local college but its only offered every spring...
You could use two sorted Maps such as TreeMap.
One would map Characters to numbers (Map<Character,Number> or something similar). The other would perform the reverse mapping (Map<Number, Character>)
Let's look at your requirements:
1)display just the letters or just the numbers sorted alphabetically
or numerically
Just iterate over one of the maps. The iteration will be ordered.
2)search to see if an element is contained in either list.
Just check the corresponding map. Looking for a number? Check the Map whose keys are numbers.
3)search to see if an element pair (for example A - 1 or B-10) is
contained in the matching list.
Just get() the value for A from the Character map, and check whether that value is 10. If so, then A-10 exists. If there's no value, or the value is not 10, then A-10 doesn't exist.
When adding or removing elements you'd need to take care to modify both maps to keep them in sync.

How do I determine list type (ul/li vs span) using WebDriver

I am working with a user-created table and list, where my program has to read in a list of entries for processing. I have the processors functioning, and I can navigate to the location in the table without any problems. The issue is that I am trying to allow for some flexibility in creating the list (inside the table) by allowing for the creator to input the list by either using un-ordered lists (/ul/li) and carriage-returns (/p).
Right now, I am determining whether the un-ordered list is used via driver.findElements(By.xpath("foo/ul/li")).size() being greater than 0. The issue is that this can take forever to "fail over." Is there a way that I am missing for making verifying element type (/ul/li vs /p vs /ol/li) faster?
I am using Java and Webdriver.
I guess you want to check if the concerned list is ordered or unordered by getting the size() and checking if it is greater than "0".
My suggestion is to get the parent tag of first "li" element which will then return "ul" or "li".
You can try the below code for that (Assuming list is present under some 'div' tag):
String tag = driver.findElement(By.xpath("//div//li[1]/..")).getTagName();//Returns the parent tag of the first element in the list
if(tag.contains("ul"))
System.out.println("List is unordered");
else if(tag.contains("li"))
System.out.println("List is ordered.");

Algorithm to remove childs from array

I have to write an algorithm for my project. Following is the problem
A Tree like Structure for which only one functionality is exposed i.e to **getAllChildNodes** which returns all the children of a particular node.
Now I am given an array of Nodes , I have to only retain the topmost parent node among them.
Example : Lets say there are 3 trees
Tree 1 : P1 has two children C11 & C12
Tree 2 : P2 has 1 children C21, and C21 has 2 child C22, C23
Tree 3 : P3 has 2 Children C31 and C32
Now if given an array say { C11, C21, C22 , P1, P3, C32}
The expected result is { C21, P1 , P3 }
Let me know if more information is required from my side.
More info :
I have done it by first getting all the child of first element of array and then compare with rest of the elements of array ,and similarly with each element. but this has more complexity.. i.e n*n*getAllChildNodes. I am here for a better suggestion
Pick ith element of array and add all its children to a hashmap(using the given function). Do this for i 1 to n(complete pass)
Loop i for 1 to n and for each iteration, check if the element is present in hashmap.
If it is present, delete it from the array,
else continue
Note the order of checking if element belong to hashmap is O(1) and also order of addition is O(1), avearage. So the algorithm is O(n*getAllChlidNodes) average
You can do this easily.
first create a method isPresent(node) to check if the node you are entering is present in the array. Then enter each node given to find its parent.
if(isPresent(x->parent)) push(x->parent);
Continue this for the entire list.
Now recursively check to see if the current list has any parents present. Once you start doing you'll get it. If there are any parents just pop() those elements.
Hope this helps :)
If you could get a function first like getParentNode(childNode) or create map/multimap of child as key and parent as value as pre processing step then Problem would become very easy.
If you have getParentNode(childNode) then just walk through the array and push the parents into set.
If you have map/multimap of child as key and parent as value as pre processing step then apply the algo as mentioned by adi

rich:tree - programmatically set the selected node

I have a tree (a parent could have any number of child ) and an inputText for searching nodes by name. Assume the following tree:
A
--A1
----AA1
--A2
Where A has two children A1 and A2, A1 has one child AA1.
When i type A1 in input text , i want to set nodes (A1,AA1) programmatically to select and if it is necessary expands nodes.
(i have a list<T> for building my tree)
I'm not sure what exactly you're asking for but I'll assume you want to set the value of node A1 and all it's children when you type "A1".
First you need to check that the user input is formatted correctly (doesn't look like A?1 when only letters and numbers are used to identify nodes).
Second, you need to locate the indicated node.
Third, you need to set the value and check for children.
Finally, once children are discovered, go back to the third step for each child.
Recursion could make your method more sleek but would not be necessary if you don't like recursion.
Sorry for such a vague answer but without a more detailed question it's hard to give a more detailed solution.
You can use TreeNode of richfaces instead of list, so you can easily achieve your requirements.
because using it you can easily get parent-child relationship using key value

Categories