Guava Graph library ElementOrder on Edges instead of Nodes - guava

I have this straight-forward Graph structure using the Guava Graph library and I'd like to understand better if that is possible to sort the adjacents/edges (not the node order). For the sake of clarification:
import com.google.common.graph.ElementOrder;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.MutableGraph;
public class MyNodeTest {
public static void main(String[] args) {
MutableGraph<String> graph = GraphBuilder.undirected().nodeOrder(ElementOrder.insertion()).build();
graph.addNode("A");
graph.addNode("C");
graph.addNode("D");
graph.addNode("B");
graph.addNode("E");
graph.putEdge("A", "B");
graph.putEdge("A", "C");
graph.putEdge("A", "D");
graph.putEdge("A", "E");
System.out.println("My default Insertion.order Nodes: " + graph.nodes());
System.out.println("Adj. Order that I couldn't understand: " + graph.adjacentNodes("A"));
System.out.println("Successor. Order that I couldn't understand: " + graph.successors("A"));
System.out.println("Pred. Order that I couldn't understand: " + graph.predecessors("A"));
}
}
My outcome is:
My default Insertion.order Nodes: [A, C, D, B, E]
Adj. Order that I couldn't understand: [D, E, B, C]
Successor. Order that I couldn't understand: [D, E, B, C]
Pred. Order that I couldn't understand: [D, E, B, C]
Without further ado, what I mean is:
Using .nodeOrder(ElementOrder.insertion()) it is possible to sort the nodes themselves. Nonetheless, I'm more interested in sorting the edges associated with a given node in a way that if I used the putEdge respectively from A with B, C, D, E the outcome is precisely this instead of the above shown.
Any insight?
Thanks in advance.

In case someone faces the same question, here is how I solved it (disclaimer: not the optimal, but a working solution).
MutableNetwork<String, UUID> graph = NetworkBuilder.undirected().edgeOrder(ElementOrder.insertion()).build();
graph.addNode("A");
graph.addNode("C");
graph.addNode("D");
graph.addNode("B");
graph.addNode("E");
graph.addEdge("A", "B", UUID.randomUUID());
graph.addEdge("A", "C", UUID.randomUUID());
graph.addEdge("A", "D", UUID.randomUUID());
graph.addEdge("A", "E", UUID.randomUUID());
System.out.println("My default Insertion.order Nodes: " + graph.nodes());
System.out.println("Adj. Order that I couldn't understand: " + graph.adjacentNodes("A"));
System.out.println("Successor. Order that I couldn't understand: " + graph.successors("A"));
System.out.println("Pred. Order that I couldn't understand: " + graph.predecessors("A"));
And the results:
My default Insertion.order Nodes: [A, C, D, B, E]
Adj. Order that I couldn't understand: [B, C, D, E]
Successor. Order that I couldn't understand: [B, C, D, E]
Pred. Order that I couldn't understand: [B, C, D, E]
The MutableNetwork has the .edgeOrder(ElementOrder.insertion()) that does the trick. The cons here are associated with the K,V needed to create this data structure.
Regards

Related

How to save an org.apache.spark.rdd.RDD[org.apache.spark.sql.Row] of 46 tuples to cassandra

I have a data with 46 field and I converted it into data frame in spark. After it coveted into RDD of usersTable: org.apache.spark.rdd.RDD[org.apache.spark.sql.Row]. Now I want to save this file to Cassandra but getting below error:
error: object Tuple46 is not a member of package scala
I have tried:
val usersTable = sqlContext.sql("select a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, aa, ab, ac, ad, ae, af, ag, ah, ai, aj, ak, al, am, an, ao, ap, aq, ar, as, at from abcdef").rdd
usersTable: org.apache.spark.rdd.RDD[org.apache.spark.sql.Row] = MapPartitionsRDD[39] at rdd at <console>:67
val finalUsers = usersTable.flatMap(e => (e(0).toString, e(1).toString, e(2).toString, e(3).toString, e(4).toString, e(5).toString, e(6).toString, e(7).toString, e(8).toString, e(9).toString, e(10).toString, e(11).toString, e(12).toString, e(13).toString, e(14).toString, e(15).toString, e(16).toString, e(17).toString, e(18).toString, e(19).toString, e(20).toString, e(21).toString, e(22).toString, e(23).toString, e(24).toString, e(25).toString, e(26).toString, e(27).toString, e(28).toString, e(29).toString, e(30).toString, e(31).toString, e(32).toString, e(33).toString, e(34).toString, e(35).toString, e(36).toString, e(37).toString, e(38).toString, e(39).toString, e(40).toString, e(41).toString, e(42).toString, e(43).toString, e(44).toString, e(45).toString))
error: object Tuple46 is not a member of package scala
what is the possible way to save large column table in Cassandra?

The right way to normalize database into 3NF

I have this database:
R(A, B, C, D, E)
Keys: A
F = {A -> B, D -> E, C -> D}
I normalize it into 3NF like this:
R(A, B, C, D, E)
Keys: AD
F = {AD -> B, AD -> E, C -> D}
What I do is when I check D -> E, D is not a superkey and E is not a key attribute, so I treat D and A as a superkey {AD}. When I check C -> D, C is not a key but D is a key attribute so it's OK.
Is my normalization correctly?
There is a problem in your input data. If the relation R has the dependencies F = {A -> B, D -> E, C -> D}, then A cannot be a key. In fact, a key is a set of attributes whose closure determines all the attributes of the relation, which is not the case here, since:
A+ = AB
From F, the (only) possible key is AC, in fact
AC+ = ABCD
Normalizing means to reduce the redundancy by decomposing a relation in other relations in which the functional dependencies do not violate the normal form, and such that joining the decomposed relations, one can obtain the original one.
In you solution, you do not decompose the relation, but only change the set of dependencies with other dependencies not implied by the first set.
A correct decomposition would be instead the following:
R1 < (A B) ,
{ A → B } >
R2 < (C D) ,
{ C → D } >
R3 < (D E) ,
{ D → E } >
R4 < (A C) ,
{ } >
The algorithm to decompose a relation into 3NF can be found on any good book on databases.

Titan GraphDB query within a set

I have edges that has a property called type that stores the different type of relationships between two vertices. The type property is a set. I was wondering how can I query to get the edges that contain that type?
For example:
Types = [A, B, C]
Edge1.type = [A, B]
Edge2.type = [B, C]
Edge3.type = [A, C]
If I query for edges with type A, I would get Edge1 and Edge3.
Not efficient, but the only way I can think of:
In TP2:
g.E().filter { it.getProperty("type").contains("A") }
In TP3:
g.E().filter { it.get().value("type").contains("A") }

New collection or nested array?

Supposed we have a db called A. The structure of A can be:
1) A( a, b, c, d).
a, b, c, d are collections.
And the element in each collection is like { _id:id, data : data }
2) A(k).
k(a, b, c, d)
k is a colletion. and a, b, c, d are elements inside k.
a, b, c, d are like
{
type : 'a / b / c / d',
data : [
{_id : id1, data : data1 },
{_id : id2, data : data2},
...
]
}
the daily operations are { get, inserting element into, empty element of } a, b, c and d.
Which one is better in terms of efficiency?
#Markus-W-Mahlberg is right about your actual-use-case.
As you are using mongodb and it uses documents not tabular data structure (such as ms-sql), your both approaches work fine and if you define right index, u get same performance.
But in my opinion if your types (a, b, c and d ) have different structures (different properties, different queries, different update scenarios, aggregation plans and ...) Use way1, other wise use Way2 with right index.

Building a list of "incremental sums"

What is an efficient, functional way of building a list of "incremental sums"
For example, given
val (a,b,c,d) = (2,3,5,6)
val list1 = List(a, b, c, d)
How would you implement f such as:
list1.map(f)
would result in
List(a, a+b, a+b+c, a+b+c+d)
Can you do
list1.scanLeft(0)(_ + _).tail
?