Skip to content

Commit

Permalink
graphs have their own random number generators
Browse files Browse the repository at this point in the history
  • Loading branch information
MHenderson committed Nov 24, 2020
1 parent 3927d71 commit 6a06fc8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
40 changes: 20 additions & 20 deletions Room/algorithm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

public class algorithm {

static void h1(graph g, java.util.Random rng) {
static void h1(graph g) {
int u, v, c, w;
int n = g.n();
int r = g.m();
do {
u = g.randomVertex(rng);
u = g.randomVertex();
} while (!g.liveVertex(u));
do {
c = g.randomColour(rng);
c = g.randomColour();
} while (!(g.liveColour(c) & !g.colouredWith(c, u)));
do {
v = g.randomVertex(rng);
v = g.randomVertex();
} while (!(u != v & !g.edgeColoured(u, v)));
if (!g.colouredWith(c, v)) {
g.colourEdge(u, v, c);
Expand All @@ -25,16 +25,16 @@ static void h1(graph g, java.util.Random rng) {
}
}

static void h2(graph g, java.util.Random rng) {
static void h2(graph g) {
int u, v, c, d;
int n = g.n();
int r = g.m();
do {
c = g.randomColour(rng);
c = g.randomColour();
} while (!g.liveColour(c));
do {
u = g.randomVertex(rng);
v = g.randomVertex(rng);
u = g.randomVertex();
v = g.randomVertex();
} while (!(u != v & !g.colouredWith(c, u) & !g.colouredWith(c, v)));
if (!g.edgeColoured(u, v)) {
g.colourEdge(u, v, c);
Expand All @@ -46,18 +46,18 @@ static void h2(graph g, java.util.Random rng) {
}
}

static void oh1(graph g1, graph g2, roomSquare R, java.util.Random rng) {
static void oh1(graph g1, graph g2, roomSquare R) {
int u, v, w, c1j, c1k, c2;
int n = g1.n();
int r = g1.m();
do {
u = g2.randomVertex(rng);
u = g2.randomVertex();
} while (!g2.liveVertex(u));
do {
c2 = g2.randomColour(rng);
c2 = g2.randomColour();
} while (!(g2.liveColour(c2) & !g2.colouredWith(c2, u)));
do {
v = g2.randomVertex(rng);
v = g2.randomVertex();
} while (! (u != v & !g2.edgeColoured(u, v)));

c1j = g1.colourOf(u, v);
Expand All @@ -78,16 +78,16 @@ static void oh1(graph g1, graph g2, roomSquare R, java.util.Random rng) {
}
}

static void oh2(graph g1, graph g2, roomSquare R, java.util.Random rng) {
static void oh2(graph g1, graph g2, roomSquare R) {
int u, v, c1j, c2i, c2k;
int n = g1.n();
int r = g1.m();
do {
c2i = g2.randomColour(rng);
c2i = g2.randomColour();
} while (!g2.liveColour(c2i));
do {
u = g2.randomVertex(rng);
v = g2.randomVertex(rng);
u = g2.randomVertex();
v = g2.randomVertex();
}
while (!(u != v & !g2.colouredWith(c2i, u) & !g2.colouredWith(c2i, v)));

Expand Down Expand Up @@ -115,9 +115,9 @@ public static int oneFactorisation(graph g, java.util.Random rng) {
do {
int rr = rng.nextInt(2);
if (rr == 0)
h1(g, rng);
h1(g);
else
h2(g, rng);
h2(g);
count++;
} while (!g.isFull());
return count;
Expand All @@ -127,9 +127,9 @@ public static void hillClimbing(graph g, graph h, roomSquare R, int iterations,
for (int j = 0; j < iterations; j++) {
int rr = rng.nextInt(2);
if (rr == 0) {
oh1(g, h, R, rng);
oh1(g, h, R);
} else {
oh2(g, h, R, rng);
oh2(g, h, R);
}
if (h.isFull()) {
System.err.println();
Expand Down
10 changes: 7 additions & 3 deletions Room/graph.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package Room;

import java.util.Random;

public class graph {

int[][] g;
Random rng;

public graph(int n, int m) {
public graph(int n, int m, Random randnum) {
g = new int[n][m];
rng = randnum;
for(int i = 1; i < n; i++) {
for(int j = 0; j < m; j++) {
g[i][j] = -1;
Expand Down Expand Up @@ -136,11 +140,11 @@ public boolean isFull() {
return result;
}

int randomColour(java.util.Random rng) {
int randomColour() {
return rng.nextInt(n() - 1) + 1;
}

int randomVertex(java.util.Random rng) {
int randomVertex() {
return rng.nextInt(m());
}

Expand Down
4 changes: 2 additions & 2 deletions Room/search.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public static void main (String[] args) {
Random randnum = new Random(seed);

for (int i = 0; i < tries; i++) {
graph g = new graph(n, n);
graph g = new graph(n, n, randnum);
algorithm.oneFactorisation(g, randnum);
graph h = new graph(n, n);
graph h = new graph(n, n, randnum);
roomSquare R = new roomSquare(n);
algorithm.hillClimbing(g, h, R, count, randnum);
}
Expand Down

0 comments on commit 6a06fc8

Please sign in to comment.