How to change VLABEL on AgensGraph? - agens-graph

I want to change vlabel of vertex.
agens=# create (:v1{id:1})-[:e1{id:3}]->(:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 1)
agens=# match (n:v1{id:1}) set n:v2 remove n:v1;
ERROR: syntax error at or near ":"
LINE 1: match (n:v1{id:1}) set n:v2 remove n:v1;
^
But, there is an error on it.
How to change VLABEL on AgensGraph?

There is no way to change label on vertex on AgensGraph.
But, You can try add new vertex with same properties and remove old vertex.
agens=# create (:v1{id:1})-[:e1{id:3}]->(:v1{id:2});
GRAPH WRITE (INSERT VERTEX 2, INSERT EDGE 1)
agens=# match p = ( (n1)-[r1]->(n2) ) return p;
p
---------------------------------------------------------------
[v1[3.1]{"id": 1},e1[4.1][3.1,3.2]{"id": 3},v1[3.2]{"id": 2}]
(1 row)
agens=# match (n1:v1{id:1})-[r1:e1]->(n2) create (n3:v2)-[r2:e1]->(n2) set n3 = properties(n1), r2 = properties(r1) delete r1, n1;
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 1, DELETE VERTEX 1, DELETE EDGE 1, UPDATE PROPERTY 2)
agens=# match p = ( (n1)-[r1]->(n2) ) return p;
p
---------------------------------------------------------------
[v2[5.1]{"id": 1},e1[4.2][5.1,3.2]{"id": 3},v1[3.2]{"id": 2}]
(1 row)

Related

minimum operations to make array left part equal to right part

Given an even length array, [a1, a2,....,an], a beautiful array is an array where a[i] == a[i + n / 2] for 0<= i < n / 2. define an operation as change all array elements equal to value x to value y. what's the minimum operations required to make a given array beautiful? all elements are in range [1, 100000]. If simply return unmatch array pairs (ignore order) in left and right part of array, it will return wrong results in some cases such as [1, 1, 2, 5, 2, 5, 5, 2], unmatched pairs are (1, 2), (1, 5), (2, 5), but when change 2 -> 5, than (1, 2) and (1, 5) become the same. so what's the correct method to solve this problem?
It is a graph question.
For every pair(a[i], a[i+n/2]) where a[i]!=a[i+n/2], add an undirected edge between the two nodes.
Note that you shouldn't add multiple edges between 2 numbers.
Now you essentially need to remove all the edges in the graph by performing some operations. The final answer is the number of operations.
In each operation, you remove an edge. After removing an edge between two vertices, combine the vertices and rearrange their edges.

How to amend object in an array with duplicate attributes? (XY coordinates)

I have an array of objects (nodes) with XY coordinates. Wherever I find a node overlaying another node (with the exact same XY coords) I would like to edit the Y attribute "up" by 3.
id, x, y are all attributes of the objects I am looking at. And collectively held in an Array of nodes.
I am looking to go through the array and whenever a duplicate XY is present, edit the first non-unique instance by adding 3 to the Y attribute. If another duplicate is found, I would like this object's Y attribute to be altered by 6 and so on.
E.g.
NODE X Y
node1 267555 666777
node2 267555 666777
node3 245698 656400
node4 267555 666777
I would like node 2 and node 4 to become:
NODE X Y
node1 267555 666777
node2 267555 666780
node3 245698 656400
node4 267555 666783
Essentially adding 3 to 'Y' for every instance of a duplicate XY, and doing so for every instance where there are overlying nodes in the array (the real array is much larger).
I have managed to identify duplicates using, but unsure of how to proceed:
duplicates = nodes.group_by{|i| [i.x, i.y] }.select{|k,v| v.length > 1}.values
However I don't want a new array, I wish to amend the original "nodes" array attributes.
Thanks
Let's start by creating a class of nodes.
class Nodes
attr_accessor :name, :x, :y
def initialize(name, x, y)
#name = name
#x = x
#y = y
end
end
Next, create the instances shown in the problem.
nodes = [
Nodes.new("node1", 267555, 666777),
Nodes.new("node2", 267555, 666777),
Nodes.new("node3", 245698, 656400),
Nodes.new("node4", 267555, 666777)
]
#=> [#<Nodes:0x00005c7949ee8e40 #name="node1", #x=267555, #y=666777>,
# #<Nodes:0x00005c7949f57c50 #name="node2", #x=267555, #y=666777>,
# #<Nodes:0x00005c7949f57958 #name="node3", #x=245698, #y=656400>,
# #<Nodes:0x00005c7949f577a0 #name="node4", #x=267555, #y=666777>]
Now modify the y values as desired. For this we make use of the form of the method Hash::new that takes an argument called the default value. If a hash h has been created this way, h[k] will return the default value if it does not have a key k. This is sometimes called a counting hash.
nodes.each_with_object(Hash.new(0)) do |inst,h|
y = inst.y
inst.y += 3 * h[y] if h.key?(y)
h[y] += 1
end
#=> {666777=>3, 656400=>1}
Let's see what nodes looks like now.
nodes
#=> [#<Nodes:0x00005c7949ee8e40 #name="node1", #x=267555, #y=666777>,
# #<Nodes:0x00005c7949f57c50 #name="node2", #x=267555, #y=666780>,
# #<Nodes:0x00005c7949f57958 #name="node3", #x=245698, #y=656400>,
# #<Nodes:0x00005c7949f577a0 #name="node4", #x=267555, #y=666783>]
nodes.map { |inst| [inst.name, inst.x, inst.y] }
#=> [["node1", 267555, 666777],
# ["node2", 267555, 666780],
# ["node3", 245698, 656400],
# ["node4", 267555, 666783]]

How to remove property on AgensGraph?

I create some vertex on AgensGraph.
And, I want to remove some properties of specific vertex.
agens=# create (:v1{v1:1,v2:2,v3:3});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
agens=# match (n:v1) return n;
n
------------------------------------
v1[3.1]{"v1": 1, "v2": 2, "v3": 3}
(1 row)
How to remove property on AgensGraph?
You can use REMOVE or SET clause for removing property.
First, use REMOVE clause with property name.
agens=# match (n:v1) remove n.v3 return n;
n
---------------------------
v1[3.1]{"v1": 1, "v2": 2}
(1 row)
Second option, set null to property name.
agens=# match (n:v1) set n.v2 = null return n;
n
------------------
v1[3.1]{"v1": 1}
(1 row)
Thank you.

Determine provided values and copied values in SQL Server instead of update trigger

Let's say I have a table like this:
CREATE TABLE [A]
(
[X] INT,
[Y] INT,
[Z] INT
)
..that has an instead of update trigger. If I insert a row into this table:
INSERT INTO [A]
SELECT 1, 1, 1
Then I hit the update trigger with code like this:
UPDATE [A]
SET [X] = 2
WHERE [X] = 1
I'll end up with an [deleted] table like so:
X: 1,
Y: 1,
Z: 1
I'll end up with an [inserted] table like so:
X: 2,
Y: 1,
Z: 1
Is there any way to determine that only the X was actually set? In other words, how do I distinguish between:
UPDATE [A]
SET
[X] = 2,
[Y] = 1,
[Z] = 1
WHERE [X] = 1
...and the statement above?
Note:
SQL Server 2008
In the trigger, you can add a WHERE clause to compare old and new values and ignore non-changing rows.
There is the function UPDATE() you can use but it can still gives true if the old and new values are the same. It isn't very reliable.
However, there is no way to determine the actual columns in the SET clause of the triggering UPDATE call. That is, these look exactly the same to the trigger
UPDATE [A]
SET
[X] = 2,
[Y] = 1,
[Z] = 1
WHERE [X] = 1
UPDATE [A]
SET [X] = 2
WHERE [X] = 1

How do I organize this data into structures in MATLAB?

Say for n=5, the following code gives me a plot for n randomly generated nodes. These nodes are not structures (just plotted points), but I want to assign every node a message just as I did for sink and source and keep track of the nodes identity and location.
For example, if node 4 has (x,y) coordinates (.3452 , .5463), I want to assign node 4 a msg and temp_value as well. How can I do this?
Code:
n = input('No. of Nodes:');
sink = [0 0];
source = [1 1];
node = rand(n,2)
x = node(:,1);
y = node(:,2);
x1 = sink(:,1);
y1 = sink(:,1);
x2 = source(:,1);
y2 = source(:,1);
plot(x,y,'o')
hold on
plot(x1,y1,'r*')
hold on
plot(x2,y2,'r*')
hold on
sink = struct;
sink.msg = 'temp';
sink.temp_value = '30'
source = struct;
source.msg = 'temp';
source.temp_value = '30'
I would suggest creating an array of structures that stores all of the data associated with each "node". You can create all of the data for your nodes with one call to STRUCT in the following way:
N = 5; %# Number of nodes
coords = num2cell(rand(N,2)); %# Cell array of random x and y coordinates
nodes = struct('x',coords(:,1),... %# Assign x coordinates
'y',coords(:,2),... %# Assign y coordinates
'message','temp',... %# Assign default message
'value',30); %# Assign default value
The variable nodes is an N-by-1 structure array with fields x, y, message, and value. You can access and modify the data using the usual array and field indexing:
>> nodes(1) %# Contents of node 1
ans =
x: 0.4387
y: 0.4898
message: 'temp'
value: 30
>> nodes(1).message %# Message for node 1
ans =
temp
>> nodes(1).message = 'hello world!'; %# Change message for node 1
You can then plot the nodes in the following way:
plot([nodes.x],[nodes.y],'r*'); %# Plot all the nodes in red
index = randi(N,[1 2]); %# Pick two nodes at random
hold on;
plot([nodes(index).x],[nodes(index).y],'b*'); %# Plot 2 random nodes in blue

Resources