I've been trying to learn about minimum span trees and the algorithms associated with it, namely Prim's, Kruskal's and Dijkstra's algorithms.
I understand how these algorithms work and have seen them in action but there is only one thing I don't understand about Prim's algorithm, which is an array that I don't understand what it is it's intention and how does it work.
So here is the situation:
I have to do an exercise wherein I am given an adjacency table and I have to run Prim's algorithm to create a minimum span tree.
The table looks like this:
0 |1|2| 3| 4| 5|
0| 0 73 4 64 40 74
1| 73 0 46 26 30 70
2| 4 46 0 77 86 14
3| 64 26 77 0 20 85
4| 40 30 86 20 0 22
5| 74 70 14 85 22 0
The numbers separated by the "|" are the vertices and the numbers in the table are the edges. Simple, I run the algorithm ( in this website for example: http://www.jakebakermaths.org.uk/maths/primsalgorithmsolverv10.html ) or just jot it down on paper and and draw the minimum span tree and I get the tree with the minimal cost of 86 and the edges that have been used are 4, 26, 20, 22 and 14.
Now here comes the problem, apparently just solving it wasn't enough. I need to find the values of an array called closest[0,...,5]. I know it is used in the algorithm but I don't know it's purpose and what I should do with it or how to get it's values.
I have searched the internet for it and found this link about Prim's algorithm:
http://lcm.csa.iisc.ernet.in/dsa/node183.html
Which defines the array "closest" as "For i in V - U, closest[i] gives the vertex in U that is closest to i".
I still don't understand what it is, what it is used for and what the values inside of them are.
All I know the answer to my exercise is
closest[1] = 3
closest[2] = 0
closest[3] = 4
closest[4] = 5
closest[5] = 2
Thank you in advance.
When doing a MST with Prim's algorithm, it is important to keep track of four things: the vertex, has it been visited, minimal distance to vertex, and what precedes this vertex (this is what you are looking for).
You start at vertex 0, and you see that the closest vertex to 0 is 2. At the same time, you could have visited all other nodes, but with bigger distances. Nevertheless, the closest node to 0 is 2, so thus 2 becomes visited and its parent is set to vertex 0. All the other nodes are not visited yet, but its parent as of now is set to 0, with its respective distance. You now need to set the smallest distance vertex to visited, and now consider this node as the node to be considered.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 73 | 0
2 | T | 4 | 0
3 | F | 64 | 0
4 | F | 40 | 0
5 | F | 74 | 0
We then check all the distances of nodes from 2. We compare the new distances from 2 to the other nodes to the distance from the other nodes from its previous distance, and if it needs to be updated, it gets updated. We now see that the distance from 2 to 5 is shorter than 0 to 5, and vertex 5 now becomes becomes visited, with its parent now equal to vertex 2.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | F | 64 | 0
4 | F | 40 | 0
5 | T | 14 | 2
Now we visit 5. One thing to note is that if a node is visited, we do not consider it in our distance calculations. I have simulated the rest, and hopefully you can see how you get the answer you're looking for.
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | F | 64 | 0
4 | T | 22 | 5
5 | T | 14 | 2
Now visit 4
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | F | 46 | 2
2 | T | 4 | 0
3 | T | 20 | 4
4 | T | 22 | 5
5 | T | 14 | 2
And now visit 3
Vertex | Visited | Distance | Parent
0 | T | - | -
1 | T | 26 | 3
2 | T | 4 | 0
3 | T | 20 | 4
4 | T | 22 | 5
5 | T | 14 | 2
Related
For each row in a table, I want to find the minimum value across a couple of numeric columns, then take the name of that column (which holds the desired value) and populate a new column with the name (or custom string).
A few rules first in my specific scenario: the value to be found across the columns must also be > 0. Also, if no value in the column is > 0, then a custom string should be placed (ie. 'none').
For example, take this table below with columns alpha to delta storing the values:
id | alpha | bravo | charlie | delta
------+--------+--------+---------+--------
1 | 5 | 2.3 | -1 | -5
2 | 9 | 8 | 3 | 1
3 | -1 | -4 | -7 | -9
4 | 6.1 | 4 | 3.9 | 0
for each row, I want to find out which column holds the lowest positive value. My expected output is something like this:
id | alpha | bravo | charlie | delta | lowest_postive
------+--------+--------+---------+--------+---------------
1 | 5 | 2.3 | -1 | -5 | 'col: bravo'
2 | 9 | 8 | 3 | 1 | 'col: delta'
3 | -1 | -4 | -7 | -9 | 'col: none'
4 | 6.1 | 4 | 3.9 | 0 | 'col: charlie'
Should I use a CASE ... WHEN ... THEN ...? Should I be converting the row into an array first, then assinging each position in the array?
You can do:
select *,
case when mp = alpha then 'col: alpha'
when mp = bravo then 'col: bravo'
when mp = charlie then 'col: charlie'
when mp = delta then 'col: delta'
end as lower_positive
from (
select *,
least(
case when alpha > 0 then alpha end,
case when bravo > 0 then bravo end,
case when charlie > 0 then charlie end,
case when delta > 0 then delta end
) as mp
from t
) x
However, this solution doesn't account for multiple minimums; the first one (from left ro right) wins.
How to print numbers from 1 to 10 using a loop in Brainfuck? Is it even possible?
I am looking for a solution to this issue.
+++++++++++++++++++++++++++++++++++++++++++++++++ Cell 0 to '1'
>++++++++++ cell 1 to '\n'
>+++++++++ cell 2 to 9 as counter
[ Print numbers 1 to 9
<< Data pointer to cell 0
.+ Print and increment cell 0
>. Data pointer to cell 1 and print the newline
>- Data pointer to cell 2 and decrement counter
] Loop till counter is 0
+++++++++ Set cell 2 to 9
[ Set cell 0 to '1'
<<- Data pointer to cell 0 and decrement
>>- Data pointer to cell 2 and decrement counter
] Loop till counter is 0
<<. Data pointer to cell 0 and print '1'
-. Decrement cell 0 and print '0'
>. Data pointer to cell 1 and print newline
Readable version:
+++++++++++++++++++++++++++++++++++++++++++++++++>
++++++++++>
+++++++++[<<.+>.>-]
+++++++++[<<->>-]
<<.-.>.
Output:
1
2
3
4
5
6
7
8
9
10
Live demo:
Brainf**k print 1 to 10
Brainf**k Visualizer
TL;DR
-[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]>>---------.-.
Try it online!
END TL;DR
In order to program in BrainF**k, pretend like every program (even simple ones) will need to start out with a layout.
The pseudo-code for this would be something like:
Generate the character '0'
Move left and generate '\n'
Move left and generate the counter (10 numbers in this case)
Loop: Get back to the character '0', print it, increment it to '1', go to the newline, print it, go to the counter, and decrement it. End it when the counter is 0
Generate '1' and print it
Generate '0' and print it
However, the last two steps could be simplified to just:
Go back to the digit '9'
Decrement it until '1' and print
Decrement it until '0' and print
This saves a lot of time and bytes characters.
To generate the character '0', you generate the integer 48 (because that's it's ASCII value). To do this you can go to Esolangs' BF Constants. Looking up the number 48, we find -[>+<-----]>---
Our program so far is -[>+<-----]>--- to generate 0
Next, move left and generate \n (newline). We can use <++++++++++. Notice how it is completely plus signs. This is because there is not much room to reduce the character count at the number 10.
Our program so far is -[>+<-----]>---<++++++++++
Then, move left and generate the counter. We want the counter to be 10 to print numbers from 0 to 9. <++++++++++.
Our program so far is -[>+<-----]>---<++++++++++<++++++++++
After that, start the loop [. Go to the '0' >>, print it ., increment it +, go to the newline and print <., Go to the counter and decrement it, and end the loop when it is zero <-]. [>>.+<.<-]
Our program so far is -[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]
Finally, go to the '9' >>, decrement it until it is 1 and print ---------., and decrement it until it is 0 and print -.. ---------.-.
The program is finished.
++++++++++++++++++++++++++++++++++++++++++++++++ Let address 0 be the digit we want to print, starting with '0'
>++++++++++ Let address 1 be our newline character
>+++++++++ Let address 2 be our counter, starting at 9
[
- Decrement the counter
<<+. Increment the digit we want to print and print it
>. Print the newline
> Make sure we're at the counter again before we loop back
]
<< Move back to address 0
--------. Make address 0 '1'
-. Make address 0 '0'
Live demo
This is possible. Here's the code: ++++++++++>++++++++++[>+++++<-]>-.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>--------.-. It could be shorter, however, it still accomplishes the same task. Brainf*** can theoretically perform any computation, as it is Turing complete. This is just one of those computations.
I suggest the following:
+++++++++>>++++++++[-<++++++>][-]>++++[-<++++++++>]<<<[->+.>.<<]>[-]+++++++[-<+++++++>]<.-.
It outputs 1 2 3 4 5 6 7 8 9 10.
We have this (each line is after the next cell-affecting instruction, which are + - .):
| C1 | C2 | C3 | C4 | Output |
+----+----+----+----+-----------------------+
| 0 | 0 | 0 | 0 | |
| 1 | 0 | 0 | 0 | |
| 2 | 0 | 0 | 0 | |
...
| 8 | 0 | 0 | 0 | |
| 9 | 0 | 0 | 0 | |
... (first loop)
| 9 | 48 | 0 | 0 | |
| 9 | 48 | 0 | 1 | |
| 9 | 48 | 0 | 2 | |
| 9 | 48 | 0 | 3 | |
| 9 | 48 | 0 | 4 | |
| 9 | 48 | 0 | 3 | | (enter second loop)
| 9 | 48 | 1 | 3 | |
| 9 | 48 | 2 | 3 | |
...
| 9 | 48 | 8 | 3 | |
| 9 | 48 | 8 | 2 | |
| 9 | 48 | 9 | 2 | |
...
| 9 | 48 | 16 | 2 | |
| 9 | 48 | 16 | 1 | |
| 9 | 48 | 17 | 1 | |
...
| 9 | 48 | 24 | 1 | |
| 9 | 48 | 24 | 0 | |
| 9 | 48 | 25 | 0 | |
...
| 0 | 48 | 32 | 0 | | (exit second loop)
| 8 | 48 | 32 | 0 | | (enter third loop)
| 8 | 49 | 32 | 0 | |
| 8 | 49 | 32 | 0 | 1 |
| 8 | 49 | 32 | 0 | 1_ | (underscores are spaces)
| 7 | 49 | 32 | 0 | 1_ |
| 7 | 50 | 32 | 0 | 1_ |
| 7 | 50 | 32 | 0 | 1_2 |
| 7 | 50 | 32 | 0 | 1_2_ |
| 6 | 50 | 32 | 0 | 1_2_ |
| 6 | 51 | 32 | 0 | 1_2_ |
| 6 | 51 | 32 | 0 | 1_2_3 |
| 6 | 51 | 32 | 0 | 1_2_3_ |
| 5 | 51 | 32 | 0 | 1_2_3_ |
| 5 | 52 | 32 | 0 | 1_2_3_ |
| 5 | 52 | 32 | 0 | 1_2_3_4 |
| 5 | 52 | 32 | 0 | 1_2_3_4_ |
| 4 | 52 | 32 | 0 | 1_2_3_4_ |
| 4 | 53 | 32 | 0 | 1_2_3_4_ |
| 4 | 53 | 32 | 0 | 1_2_3_4_5 |
| 4 | 53 | 32 | 0 | 1_2_3_4_5_ |
| 3 | 53 | 32 | 0 | 1_2_3_4_5_ |
| 3 | 54 | 32 | 0 | 1_2_3_4_5_ |
| 3 | 54 | 32 | 0 | 1_2_3_4_5_6 |
| 3 | 54 | 32 | 0 | 1_2_3_4_5_6_ |
| 2 | 54 | 32 | 0 | 1_2_3_4_5_6_ |
| 2 | 55 | 32 | 0 | 1_2_3_4_5_6_ |
| 2 | 55 | 32 | 0 | 1_2_3_4_5_6_7 |
| 2 | 55 | 32 | 0 | 1_2_3_4_5_6_7_ |
| 1 | 55 | 32 | 0 | 1_2_3_4_5_6_7_ |
| 1 | 56 | 32 | 0 | 1_2_3_4_5_6_7_ |
| 1 | 56 | 32 | 0 | 1_2_3_4_5_6_7_8 |
| 1 | 56 | 32 | 0 | 1_2_3_4_5_6_7_8_ |
| 0 | 56 | 32 | 0 | 1_2_3_4_5_6_7_8_ |
| 0 | 57 | 32 | 0 | 1_2_3_4_5_6_7_8_ |
| 0 | 57 | 32 | 0 | 1_2_3_4_5_6_7_8_9 |
| 0 | 57 | 32 | 0 | 1_2_3_4_5_6_7_8_9_ | (exit third loop)
+----+----+----+----+-----------------------+
Then cell C2 is set to 0 and used to set cell C1 to 49 (charater 1).
It prints C1, then removes one and prints it again, giving an output of:
1_2_3_4_5_6_7_8_9_10 (underscores are spaces)
It's for me the simplest program to print the integers from 1 to 10.
If you want each number to be on its own line, replace the >>>++++[-<++++++++>]<<< by ++++++++++.
Issue [SOLVED]: instead of printing 10 at the end, it prints :...
I need to generate long (pseudo)random arrays (1000-25 000 000 integers) where no element is repeated. How do I do it since rand() function does not generate numbers long enough?
I tried to use this idea: array[i] = (rand() << 14) | rand() % length; however I suppose there is much better way that I don't know.
Thank you for your help.
You can use the Fisher-Yates shuffle for this.
Create an array of n elements and populate each element sequentially.
-------------------------
| 1 | 2 | 3 | 4 | 5 | 6 |
-------------------------
In this example n is 6. Now select a random index from 0 to n-1 (i.e. rand() % n) and swap the number at that index with the number at the top of the array. Let's say the random index is 2. So we swap the value at index 2 (3) and the one at n-1 (6). Now we have:
v
-------------------------
| 1 | 2 | 6 | 4 | 5 | 3 |
-------------------------
Now we do the same, this time with the upper bound of the index being n-2. Then we swap the value at that index with the value at index n-2. Let's say time we randomly get 0. So we swap index 0 (1) with index n-2 (5):
v
-------------------------
| 5 | 2 | 6 | 4 | 1 | 3 |
-------------------------
Then repeat. Let's say the next random index is 3. This happens to be our upper limit, so no change:
v
-------------------------
| 5 | 2 | 6 | 4 | 1 | 3 |
-------------------------
Next we get 0:
v
-------------------------
| 6 | 2 | 5 | 4 | 1 | 3 |
-------------------------
And finally 1:
v
-------------------------
| 6 | 2 | 5 | 4 | 1 | 3 |
-------------------------
How can i visualize 2D array with surface(mesh, surf) for incomplete dataset?
'Incomplete' means (v - known values, 0 - unknown):
1 | 2 | 3 | 4 | 5
1 | v | 0 | v | 0 | v
2 | 0 | 0 | 0 | 0 | 0
3 | v | 0 | v | 0 | v
4 | v | 0 | v | 0 | v
5 | 0 | 0 | 0 | 0 | 0
Such data indexing is handy for analyzing non-linear relation between variables.
The thing i want is working somehow with plot function. Lets say, x = [1,2,4,5]. plot will show continuous figure.
Is it possible to do so for 2D arrays without manual interpolation? Don't care about smoothness. Linear connection of known points is alright.
So you have non-linear sampling (x = [1 3 5], y = [1 3 4]), and you don't want to interpolate? I don't think surf etc will handle it. Sounds like a job for plot3.
This is mildly ugly (see result) but I'm presuming you just want to visualise it to get a feel for the data. First make up your x and y with repmat if you don't already have them like this:
x =
1 3 5
1 3 5
1 3 5
y =
1 1 1
3 3 3
4 4 4
Then you'll need your values without all the zeros in to match:
z =
6 8 10
6 5 4
4 2 1
This can be plotted with markers (might be the simplest if you have lots of points). Or you can use this trick to make a "mesh" out of two sets of lines:
plot3(x,y,z)
hold on
plot3(x',y',z')
xlabel('x');
ylabel('y');
Exactly as with your plot example, this simply linearly connects between the existing points.
You could replace the 0 values for NaN values.
Both, surf and mesh work with NaN.
I have a table in Access database as below;
Name | Range | X | Y | Z
------------------------------
A | 100-200 | 1 | 2 | 3
A | 200-300 | 4 | 5 | 6
B | 100-200 | 10 | 11 | 12
B | 200-300 | 13 | 14 | 15
C | 200-300 | 16 | 17 | 18
C | 300-400 | 19 | 20 | 21
I have trying write a query that convert this into the following format.
Name | X_100_200 | Y_100_200 | Z_100_200 | X_200_300 | Y_200_300 | Z_200_300 | X_300_400 | Y_300_400 | Z_300_400
A | 1 | 2 | 3 | 4 | 5 | 6 | | |
B | 10 | 11 | 12 | 13 | 14 | 15 | | |
C | | | | 16 | 17 | 18 | 19 | 20 | 21
After trying for a while the best method I could come-up with is to write bunch of short queries that selects the data for each Range and then put them together again using a Union query. The problem is that for this example I have shown 3 columns (X, Y and Z), but I actually have much more. Access is starting to strain with the amount of SQL I have come up with.
Is there a better way to achieve this?
The answer was simple. Just use Access Pivotview. Finding it hard to export the results to Excel though.