Link to graph nodes using integer identifier

As a bit of a tutorial project, I am working on replacing a somewhat slow program in perl using a Chapel implementation. I have algorithms, but I try my best to refer to the data in the Chapel. I can do a direct translation, but it seems that I am missing a better way.

Details of the existing program:

  • I have a graph with ~ 32000 nodes and ~ 2.1M edges. The state is saved to data files, but it starts as a daemon that stores data in memory.
  • Each node has a numeric identifier (assigned by a different system) and has a variety of other attributes defined by string, integer and boolean.
  • The edges are directional and have a pair of logical values ​​attributed to them.
  • I have an external system that interacts with this daemon, which I cannot change. It executes queries such as “Add node (int) with these attributes,” “find the shortest path from node (int) to node (int)”, or “add edges from node (int) to node (s) (int, int , int) "

In Perl, a program uses hashes with common integer identifiers for node and edge attributes. I can of course reproduce this in the Chapel by associative arrays.

Is there a better way to tie it all together? I am trying to wrap my head to have opaque node and edges with each specific element, but struggling with how easy it is to refer to them with integer identifiers.

If someone can provide the perfect way to do the following, it will give me the boost I need.

  • xx, .
  • xx
  • " xx node (int)"

.

+4
1

, , , , , , , . , , / .

Chapel: - , , , node . () . , -.

, Chapel. node:

record node {
  var id: int;

  var str: string,
      i: int,
      flag: bool;

  var edges: [1..0] edge;
}

, id , ( str, i flag), , , ) , . , node ... , , node, , , , , , , .

: , , , : , node, , , node . 1D , (1..0 - 1 > 0). , . node . , .

:

record edge {
  var from, to: int,
      flag1, flag2: bool;
}

(from to) , . node ID , , from /, . , .

node :

var NodeIDs: domain(int),
    Nodes: [NodeIDs] node;

NodeIDs - () , . Nodes - , node (, ).

, :

xx, .

node n1 / , Chapel , :

var n1 = new node(id=1, "node 1", 42, flag=true);

:

Nodes[n1.id] = n1;

n1.id NodeIDs n1 Nodes. , node :

Nodes[2] = new node(id=2, "node 2", i=133);

, , node (, , node?). ( ) , (, , ).

, ( , forall for, ):

writeln("Printing all node IDs (in an arbitrary order):");
forall nid in NodeIDs do
  writeln("I have a node with ID ", nid);

writeln("Printing all nodes (in an arbitrary order):");
forall n in Nodes do
  writeln(n);

, , : (1) ; (2) .

xx

, node, :

proc node.addEdge(to: int, flag1: bool, flag2: bool) {
  edges.push_back(new edge(id, to, flag1, flag2));
}

node , , ( node from) push_back() , .

, node 2 ( , ):

Nodes[2].addEdge(n1.id, true, false);
Nodes[2].addEdge(n1.id, false, true);
Nodes[2].addEdge(2, false, false);

node :

writeln("Printing all edges for node 2: (in an arbitrary order):");
forall e in Nodes[2].edges do
  writeln(e);

. for, , - 1D .

" xx node (int)"

, , node Nodes. , :

...Nodes[2].str...

node 2. , , ( ) ):

proc showAttributes(id: int) {
  if (!NodeIDs.member(id)) {
    writeln("No such node ID: ", id);
    return;
  }

  writeln("Printing the complete attributes for node ", id);
  writeln(Nodes[id]);

  writeln("Printing its string field only:");
  writeln(Nodes[id].str);
}

:

showAttributes(n1.id);
showAttributes(2);
showAttributes(3);

perl Chapel

, , , , --fast, .

+4

Source: https://habr.com/ru/post/1685682/


All Articles