You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Then a double for loop starts which produces virtually all the edges in pairs of vertices **(i, j)** in lexicographical order. Before the loops, we define the local variable **t** in line 3, with initial value 0. Every time an iterated edge is selected to be added to the graph, it is written in **E[t]** and then the variable **t** increases by 1.
23
+
Then a double for loop starts which produces virtually all the edges in pairs of vertices $(i, j)$ in lexicographical order. Before the loops, we define the local variable $t$ in line 3, with initial value 0. Every time an iterated edge is selected to be added to the graph, it is written in $E_t$ and then the variable $t$ increases by 1.
24
24
25
25
To decide whether or not to include a node in the graph, do the following
26
26
procedure:
27
27
28
-
first call the rand function to generate a random value from **[0, 1)** and store it in **U**. Then check if **U** is less than **k/N** (see image below).
28
+
first call the rand function to generate a random value from $[0, 1)$ and store it in $U$. Then check if $U$ is less than $k/N$ (see image below).
The probability that **U** is in the interval **[0, k/N]** is **k/N**. Then **E[t]** gets the edge **(i, j)**, **k** is reduced by 1 (because the elements that remain to be filled are reduced by 1) and **t** increases by 1 (as explained before).
32
+
The probability that $U$ is in the interval $[0, k/N]$ is $k/N$. Then $E_t$ gets the edge $(i, j)$, $k$ is reduced by 1 (because the elements that remain to be filled are reduced by 1) and $t$ increases by 1 (as explained before).
33
33
34
-
Then **N** decreases by 1, regardless of whether or not the condition was satisfied, which means that there are 1 less edges to select from.
34
+
Then $N$ decreases by 1, regardless of whether or not the condition was satisfied, which means that there are 1 less edges to select from.
35
35
36
-
Once k becomes 0, the algorithm stops (returns E immediately). There is a case,that at some point N becomes equal to k. When this equality is valid, in the if statement of line 7, we will essentially have **U < 1**, and this is always true, which means that the edge will always be selected.
36
+
Once k becomes 0, the algorithm stops (returns E immediately). There is a case,that at some point N becomes equal to k. When this equality is valid, in the if statement of line 7, we will essentially have $U < 1$, and this is always true, which means that the edge will always be selected.
37
37
38
38
In practice this means that if the remaining edges are equal in number to
39
39
the edges that must be contained in E, then all must be chosen necessarily.
@@ -42,7 +42,7 @@ This ensures that the algorithm always provides a valid solution.
42
42
43
43
### The implementation
44
44
45
-
The above algorithm is located in the file **src/randgraph.py**. The **createRandomGraph** function contains the actual algorithm. Notice that **E** is not actually global. This was done to make the code easier to be imported into another **.py** script.
45
+
The above algorithm is located in the file **src/randgraph.py**. The **createRandomGraph** function contains the actual algorithm. Notice that $E$ is not actually global. This was done to make the code easier to be imported into another **.py** script.
46
46
47
47
At the end of the file there is block comment with an example. Feel free to uncomment it and test it yourself. Here is an example output:
0 commit comments