Python nodes- How to create this? - arrays

I have imported an array into Python. How do I:
Create a list of unique nodes in 2D array of edges, order it by node ID
Have number of edges and the number of unique nodes in the graph
Print the nodes in group 1 and group 2 separately?
enter image description here
What can I do? I have no idea and can't find useful info

Related

Excel function UNIQUE is not working on MAKEARRAY and on RANDARRAY (multi-dimension array)

I have a question about UNIQUE function for random number generation in multi-dimension array.
As you can see, I try to generate an array with random and unique numbers of 1->100 inside 5X5 array.
I try both MAKEARRAY and RANDARRAY with the UNIQUE in hopes that it can generate random number, but it still produces duplicate numbers (highlighted in red).
Here are the formulas used:
=UNIQUE(MAKEARRAY(5,5,LAMBDA(row,col,RANDBETWEEN(1,100))),,TRUE)
=MAKEARRAY(5,5,LAMBDA(row,col,UNIQUE(RANDBETWEEN(1,100),,TRUE)))
=UNIQUE(RANDARRAY(5,5,1,50,TRUE))
Is there any workaround for this (or any unknown hidden function in Excel)?
The issue with UNIQUE, is that it looks at either the full row or the full column. and using it on an existing 5x5 matrix will not find doubles by cell. so you need to create a single column or row of unique values then morph that into a 5x5 grid.
There are many approaches one can take.
First creating a unique vertical list of a large number of random numbers between 1 and 100:
=WRAPROWS(TAKE(UNIQUE(RANDARRAY(100,,1,100,1)),25),5)
The RANDARRAY creates a list of 100 numbers between 1 and 100, odds are that you will find at least 25 unique numbers in that list.
The second is to create a list of numbers between 1 and 100 and randomly sort them:
=WRAPROWS(TAKE(SORTBY(SEQUENCE(100),RANDARRAY(100)),25),5)
Both create a list of 25 unique number between 1 and 100 then morph them into a 5 x 5 grid.

Rank elements based on their frequency in logarithmic time complexity

Cheers, I am trying to find an algorithm/data structure I can use to rank elements by their frequency.
For example, let's say I am given 5 names and I want to rank them based on their frequency. I am given the names consecutively, and every insertion and query I perform MUST be in O(log(n)) time, where n is the number of given names.
For example let's say I am given:
"foo"
"bar"
"bar"
"pop"
"foo"
"bar"
Then, by ranking the 1st should be "bar" (3 times), 2nd => "foo" and 3rd "pop". Keep in mind that when two or more elements have the same frequency (and the same ranking), which ever I return is correct.
I have tried using a Map (Hash), to keep the frequency in which the strings are given, for example if given "foo" I can return 3 (NOT the rank however), or even thought of using a Set (using an AVL tree) in order to arrange them by their frequency, but again I can't turn that into a Ranking data structure in logarithmic time. Any ideas ?
Return rating by name.
You can do insert and query in constant time O(1). For this, you need to employ two structures hash-map and something that I call doubly-linked-list.
Hash-map contains pairs - a name and pointer to a list item/bucket with this name statistics.
Doubly-linked-list bucket stores two numbers: an integer for the number of names pointing to the lower buckets (Rating) and a number of repetitions for the names in it (RepCount).
Initialization:
Create the first bucket, put all names into the hash-map and initialize pointers with the address of the first bucket. Create another bucket with RepCount = INFINITY and Rating = #names.
OPERATIONS:
Insert name. Find the address of the corresponding bucket Target, check if the bucket OneMore with OneMore.RepCount == Target.RepCount + 1 true exists. If it exists then --OneMore.Rating, if not then create one with RepCount = Target.RepCount + 1 and Rating = NextToTarget.Rating - 1. Observe that NextToTarget always exists due to initialization. Repoint hash-map entry to OneMore.
Query rating. Extract appropriate pointer from the hash-map and read Target.Rating.
Return name by rating (and rating by names)
You need two hash-maps and doubly-linked-list. In hash-map names store name => name-in-list*, in hash-map ratings store rating and a pointer to the first and the last name with this rating in the list rating => (first, last). In the list store pairs (name, rating) in the order described below.
Initialization:
Insert all names into the list. Insert a single entry into the hash-map (0, (list.head, list.tail)).
OPERATIONS:
Inset name. Recover name list node using names. Using ratings find out there node.rating finishes and move node next to it increasing its rating by one. Compare new rating with the next node's rating and see if you need to update an existing rating or create a new one in ratings. Remove ratings entry in case the old rating is empty now or update it if node was first or last.
Query name. Use ratings[..].first or return null if not exists.
Query rating. Return names[..].rating.

Is it possible to construct an adjacency matrix for a graph without storing indexes of nodes

I have managed to implement the solution with storing an index of each node, but is it possible to it without the indexes? My assignment is to implement a bloxrolz game, sort of. In the example I'm about to show you, I am out of ideas how to write code for checking if two nodes are adjacent to one another.
This is just a random field for the game. When reading from a file, I store every charachter here in a linked list, with a x,y coordinate and ID(if the char is not '-'). Only those that are not "-" are to be stored in a adjacency matrix since they represent the playground and "-" are nodes that could be changed to "-".
I have successfully implemented a solution in a way that I'm not sure if its legitimate or not. Can you help me figure out how to do this without the indexes.
An adjacency matrix is by definition a square matrix where each node is represented by a row and a column. If the entry at row i and column j is 1 (or some value you pick), then those two nodes are connected in the graph. Therefore, you need some way to map rows and columns to nodes and vice versa. Putting the nodes in a list and using each node's index in the list is one way to do that, but it's not the only way. You could give each node an ID number and, as long as each node's ID is unique and within the bounds of the matrix, you can use that. You could store a pointer to the corresponding node at the beginning or end of each row and column. In short, any method that lets you find the entries in the matrix for a given pair of nodes is sufficient.
That said, because you're talking about ordered lists of rows and columns, any method you pick will be similar to putting the nodes in a list and using the index of a node in the list. Think hard about what problem you're actually trying to solve.

how to generate a symmetric sequence (same amount of elements of each part) between 0 and 1 in c?

I have a double-chained list with descriptor of which the information in each node is a soccer team (also a structured type, with name, origin and an identification number). I have to separate the total teams in two groups randomly, but keeping an equal amount in each (except if the amount is odd).
Select one team at random and assign it to group 1. Then pick another team at random and assign to group 2.
Repeat until all teams have been assigned to a group.
you can iterate over your list, compute a hash value for each item (which should almost be random), and sort the item against the hash value modulo the number of category you like to have (here is 2). It seems to me that could make the job.

Import sparse matrix from .csv directly into graph using neo4j and cypher

I've got a small directed graph stored in a .csv as a sparse matrix. The file comprises 2 columns in the following format:
1,2
2,3
1,3
1,4
2,5
3,4
3,5
4,5
Every row is basically an adjacency relation between two nodes: 1->2, 2->3, 1->3, etc. I want to import this data into neo4j and create a graph (as a first step it can be an undirected graph).
I tried the following Cypher code:
LOAD CSV FROM 'file:///dummy.csv' AS line
CREATE((:node {`name`:line[1]})-[:`connects`]->(:node {`name`:line[2]}))
This is the furthest I've gone so far, but the results are not what I want. I'm reading the file into neo4j but I'm only getting multiple small graphs composed of two nodes with many node duplicates. My aim is to read every line as an adjacency relation and add connections to existing nodes without producing node duplications. Also, I would ideally like to display the name property of each node on top of itself (instead of node id) during graph visualisation. Your help is appreciated.
You should use MERGE to avoid creating duplicate nodes and relationships:
LOAD CSV FROM 'file:///dummy.csv' AS line
MERGE (a:node {name:line[0]})
MERGE (b:node {name:line[1]})
MERGE (a)-[:connects]->(b);
Also, see the documentation for how to style the browser visualization.

Resources