How to avoid duplicate vertex node on AgensGraph? - agens-graph

I want to create edge on two vertexes.
agens=# create (:v1{id:1}), (:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 0)
agens=# create (:v1{id:1})-[:e1{id:3}]->(:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 1)
agens=# match (n:v1) return n;
n
------------------
v1[3.1]{"id": 1}
v1[3.2]{"id": 2}
v1[3.3]{"id": 1}
v1[3.4]{"id": 2}
(4 rows)
But, There is duplicated vertex on it.
How to avoid duplicate vertex node on AgensGraph?

First, find the vertexes using match
After, add edge using found vertexes.
agens=# create (:v1{id:1}), (:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 0)
agens=# match (n1:v1{id:1}), (n2:v1{id:2}) create (n1)-[:e1{id:3}]->(n2);
GRAPH WRITE (INSERT VERTEX 0, INSERT EDGE 1)
agens=# match (n:v1) return n;
n
------------------
v1[3.1]{"id": 1}
v1[3.2]{"id": 2}
(2 rows)

Related

How to remove "duplicate" edges in 2-D array in Numpy?

I'm working with a (15000, 2) array in Numpy from which I plan to build an adjacency matrix. Each row represents a vertex connection from node to i to j, or in other words, the first element of each row has an edge with the second element. For example, [24, 79] represents an edge between node 24 and 79.
If there exists a row that was [79, 24], I would like to remove it altogether because [24, 79] already exists.
Is there a way to remove these "duplicate" connections so that the overall array only consists of uni-directional vertices? I'm doing this step before I make symmetrize the matrix, where I add the matrix to its transpose.
You can do that by sorting the items in each row so to easily track duplicate edges (ie. duplicate rows). The later can be done using np.unique. Here is an example:
v = np.random.randint(0, 1_000, size=(15000, 2)) # len: 15000
result = np.unique(np.sort(v, axis=1), axis=0) # len: 14790
result contains the set of unique non-directed edges where, for each row, the smallest ID is the first item. The computation is done efficiently in O(n log n) time.
Note the parameter return_index and return_inverse of np.unique can be used to track the unsorted row source index. Also note that using a (2, 15_000) array is likely to be faster due to the operation being more SIMD-friendly and cache-friendly.

How to create star shaped graph on AgensGraph?

I want to create star shaped graph using single "CREATE" statement of CYPHER.
How to create star shaped graph on AgensGraph?
Use variable on first cypher pattern, and reuse variable on following cypher patterns.
agens=# create (n:v{id:1})-[:e]->(:v{id:2}),
(n)-[:e]->(:v{id:3}),
(n)-[:e]->(:v{id:4});
GRAPH WRITE (INSERT VERTEX 4, INSERT EDGE 3)
agens=# match p = (n:v{id:1})-[:e]->() return p;
p
-----------------------------------------------------
[v[3.1]{"id": 1},e[4.1][3.1,3.2]{},v[3.2]{"id": 2}]
[v[3.1]{"id": 1},e[4.2][3.1,3.3]{},v[3.3]{"id": 3}]
[v[3.1]{"id": 1},e[4.3][3.1,3.4]{},v[3.4]{"id": 4}]
(3 rows)

Create a regular graph through one vertex deletion

The problem: Given an undirected graph, implemented using adjacency list. I'm looking for an algorithm to transform it to a regular graph (each vertex has same degree) through one vertex deletion.
For example:
Iterate all vertex, partition them by their degrees.
If all have same degree, its only possible if there is a vertex that has degree n - 1.
If you can partition them into 2 different degrees set: Let´s call X the set with the lower degree and Y the one with higher. Lets call dg(X) and dg(Y) the degree of those vertex
If one of the partitions has only 1 vertex and its degree is either 0 or the amount of vertex in the other set, remove it
If dg(Y) - dg(X) > 1, its not possible
If dg(Y) - dg(X) = 1 and |Y| = dg(X), check if a vertex from X is connected to all vertex from Y and remove it.
If dg(Y) - dg(X) = 1 and |X| = dg(Y), check if a vertex from Y is connected to all vertex from X and remove it.
Any other case is not possible with 2 partitions
If you can partition into 3 sets:
One of them must have only 1 vertex and that vertex has to be connected to all vertex from the other highest degree set, and to none of the remaining set. The degree difference between the other highest degree set and the remaining set must also be 1
Any other case, its not possible

Deletion in B Plus Tree Korth Pseucode

The pseudo code given in the Database Systems Concept by Korth is the following.
Pseucode
Consider that we have the following tree with order = 5
[7.0]
[3.0, 5.0] [9.0, 11.0, 13.0]
<Lead Nodes Here> <Lead Nodes Here>
Now image that we are at level 2 and in the function delete_entry([3.0, 5.0], 3.0, Pointer to Child). Now as per the algorithm we delete 3.0, so now we are left with [5.0] and [9.0, 11.0, 13.0]. Looking at the first marked if condition we see that these nodes can be merged as (1+3) <= (order - 1) therefore 4 keys can fit in one node. But since they are not a leaf node therefore the second marked line in the code will execute and apart from merging these two nodes it will also insert 7 in them. Therefore giving us a node with number of keys = 5, which violates the condition that number of keys in non leaf node should be <= order -1.
Am I doing something wrong here?

read a graph by vertices not as an edge list in R

To explain: I have an undirected graph stored in a text file as edges where each line consist of two values represent an edge, like:
5 10
1000 2
212 420
.
.
.
Normally when reading a graph in R from a file (using igraph), it will be read as edges so to call the edges of the graph "g" we write E(g) and to call the vertices of "g" we write V(g) and to call both vertices of a certain edge (i.e to call a certain edge (edge i)) we write E(g)[i].
My question: Is there a similar way to call one vertex only inside an edge not to call both of them.
For example, if I need the second vertex in the third edge then what I need to type?
Also from the beginning, is there something on igraph to read the graph as vertices and not as edges? like to read the graph as a table with two columns such that each edge to be read as X[i][1], X[i][2].
I need this because I want to do a loop among all vertices and to choose them separately from the edge and I think it is possible if each vertex was labeled like an element in a table.
Many thanks in advance for any help
If you have a two column table with vertices, you could use graph_from_data_frame to convert it into graph. To get nodes on particular edge, you can use ends.
#DATA
set.seed(2)
m = cbind(FROM = sample(LETTERS[1:5], 10, TRUE), TO = sample(LETTERS[6:10], 10, TRUE))
#Convert to graph
g = graph_from_data_frame(m, directed = FALSE)
#plot(g)
#Second vertex on third edge
ends(graph = g, es = 3)[2]
#[1] "I"

Resources