Number based on Adjacent nodes - math

Let's assume I have a directed graph with 5 nodes and 6 edges:
1,2
2,3
4,3
2,4
4,1
5,4
Is there a way to generate a unique number to each of the edges(between 1 and |E|) combining the numbers of its adjacent nodes?
For example, for edge <4,3> if we can assign this edge with the absolute difference 2. But this way the numbering won't be unique.
Any suggestions?
EDIT: I found what is known as a pairing function which generates a unique number.But it doesn't ensure its between 1 and |E|

If |E| = 6 and the unique id must be between 1 and |E|, that means there can only be 6 unique ids.
It wouldn't be possible to have a unique id for every edge because there is way more than |E| combinations of edge verticie pairs in a directed graph. So you wouldnt be able to use integers.
One approach would be to take the fraction of the nodes. The fractions would range from 1/5 to 5. add 1 to your fraction and now you are between 6/5 and 6. Im not sure if the id need to be integers.
You could also have one number as a the ones place and the other as the tenths place. TLDR: if your using floats or doubles you have lots of options for unique ids otherwise you have none.

Related

Formula for number of unorder pairs resulting from unique elements in k groups, excluding pairs of elements from same group k

I do not know how to solve the following problem, because combinatorics is not a strength of mine: I have a number of groups k, each of which has at least one element. All elements are different from one another. I want to know the total number of unordered pairs (i.e. of size 2) of all elements from all groups. BUT I do not want to include in that number those pairs which result from combining elements which belong to the same group k. I am looking for the correct terminology to describe and the right formula to solve these kinds of problems. Two examples below illustrate the problem and desired outcome. Your help is appeciated!
For example, group 1 consists of element a and b, group 2 of element c, group 3 of element d. The desired unordered pairs are: (a,c), (a,d), (b,c), (b,d), (c,d). Ergo, there are 5 pairs. The pair that is excluded is the pair (a,b), because both elements belong to the same group.
Another example: group 1 includes a,b; group 2 includes c,d. The desired pairs are: (a,c), (a,d), (b,c), (b,d). The total number of pairs is 4. The pairs (a,b) and (c,d) are excluded because the respective elements belong to the same group.
Thank you!
You have groups G(1) .. G(K) where G(k) has N(k) elements
So you have
M = N(1) + .. + N(K)
elements in total, and can form
(M*(M-1))/2
2 element subsets. But this includes the ones you don't want, that have elements from the same group. There are
(N(k)*(N(k)-1))/2
such subsets from group k. So the number of subsets you want is
(M*(M-1))/2 - Sum{ k | (N(k)*(N(k)-1))/2}

How to check if a path exists between two nodes of length exactly x in an undirected graph?

An undirected graph is given (as an adjacency list or incidence matrix). For multiple queries, check if a path of length exactly x exists between two nodes. Same nodes can be visited more than once.
I know that for single queries it's easy to check for this, simply by raising the incidence matrix to the power x (number of steps) and checking if the value at [first node][second node] is greater that 0. This takes too long, and for bigger matrices it takes too much memory. Even more so for multiple queries.
How can I solve this problem using as little space and time as possible?
Example:
Graph
Queries:
Is it possible to reach 3 from 2 in 1 step? yes
Is it possible to reach 4 from 1 in 1 step? no
Is it possible to reach 5 from 5 in 8 steps? yes
Is it possible to reach 8 from 1 in 10 steps? no
Thank you in advance.

Transform two numbers until they equal each other

Given two different positive integers, a and b, can they ever equal each other given the following rules for transforming them (written in python-esque pseudocode):
if a < b:
a = 2a
b = b-a
elif a > b:
b = 2b
a = a-b
else:
return True
Example of a pair that will equal:
11,21
22,10
12,20
24,8
16,16
And an example of a pair that will never equal each other:
1,4
2,3
4,1
3,2
1,4
repeating...
So my question is, can you tell that two numbers will equal without running through a function
similar to the one above and looking for equality or an infinite loop? Is there any way to tell just by looking at the two numbers?
It kind of reminds me of the Collatz conjecture but with two numbers instead of one.
Having graphed all combinations of two numbers (a on the y-axis and b on the x-axis) we get a repeating pattern. The yellow indicating pairs that will equal each other. The lower triangle is missing because it's a mirror of the upper triangle. This was computed with a modification of the above function. But it would be great not to have to do this because it quickly becomes intractable.

Assign integral value to list of relative values

I have an assortment of syrups, each of which has a value - the amount of sugar per volume. As people blend these syrups, I track which ones are used, and created a table to get a Relative Weight of each blend. I understand > Data > Sort > Options > Custom Sort Order.
However, I really don't wish to sort each table, and am looking for a way to parse a column of this list as entered, and return a column with results in an Integral Relative Value of each row, as compared to the weights of syrups in the other rows of the table.
Unique Name weight Not Unique Relative Value
blueberry .250 2
raspberry .333 3
orange .425 4
tangerine .333 3
blackberry .225 1
I am attempting to find the "Relative Sort", a nested function which can assign an integral value of the Unique Name which compares the weights of the syrups. A "Lookup" only works if there is an absolute equality, right?
What if someone doesn't use "blackberry syrup", then "blueberry" is the lightest, and should be labeled as 1.
Is this too complicated for LibreOffice Calc?
It's a recursive greater than/less than/equal to comparison?
IF the problem is calculating the right hand column below from entries that may be sorted ascending by value as on the left:
then an answer is, in C2 and copied down to suit (provided C1 is blank or 0):
=IF(B1<>B2,C1+1,C1)
Without sorting the RANK function might be simpler and adequate (though in the example returning 5 rather than 4).

How to obtain the maximum sum of the array with the following condition?

Suppose the problem posed is as follows:
On Mars there lives a colony of worms. Each worm is represented as elements in an 1D array. Worms decide to eat each other but any worm can eat only its nearest neighbour. Each worm has a preset amount of energy(i.e the value of the element). On Mars, the laws dictate that when a worm i with energy x eats another worm with energy y, the i-th worm’s final energy becomes x-y. A worm is allowed to have negative energy levels.
Find the maximum value of energy of the last standing worm.
Sample data:
0,-1,-1,-1,-1 has answer 4.
2,1,2,1 has answer 4.
What will be the suitable logic to address this problem?
This problem has a surprisingly simple O(N) solution.
If any two members in the array have different signs, the answer is then sum of absolute values of all elements.
To see why, imagine a single positive value in the array, all other elements are negative (Example 1). Now the best strategy would be keeping this value positive and gradually eating all neighbors away to increase this positive value. The position of the positive value doesn't matter. The strategy is same in case of a single negative element.
In more general case, if an array of size N have values of different signs, we can always find an array of size N-1 with different signs, because there must be a pair of neighbors with different sign, which we can combine to form a number of any sign we prefer.
For example with this array : [1,2,-5,4,-10]
we can combine either (2,-5) or (4,-10). Lets combine (4,-10) to get [1,2,-5,-14]
We can only take (2,-5) now. So our array now is : [1,-7,-14]
Again only (1,-7) possible. But this time we have to keep combined value positive. So we are left with: [8,-14]
Final combining gives us 22, sum of all absolute values.
In case of all values with same sign, our first move would be to produce an opposite sign combining a neighbor pair with as little "cost" as possible. Intuitively, we don't want to waste two big numbers on this conversion. If we take x,y neighbor pair, when combined the new value (of opposite sign) will be abs(x-y). Since result is simply sum of absolute values, we can interpret it as - "loosing" abs(x) and abs(y) from maximum possible output and "gaining" abs(x-y) instead. So the "cost" for using this pair for sign conversion is abs(x)+abs(y)-abs(x-y). Since we need to minimise this cost, we choose from initial array neighbor pair that have lowest such value.
So if we take the above array but now all values are positive [1,2,5,4,10]:
"cost" of converting (1,2) to -1 is 1+2-abs(-1)=2.
"cost" of converting (2,5) to -3 is 2+5-abs(-3)=4.
"cost" of converting (5,4) to -1 is 5+4-abs(-1)=8.
"cost" of converting (4,10) to -6 is 4+10-abs(-6)=8.
So, we take and convert pair (1,2) to -1. Then just sum absolute values of resultant array to get 20. Notice that this value is exactly 2 less than our previous example.

Resources