Content-Length: 515135 | pFad | http://github.com/algorithm-visualizer/tracers.js/commit/1f06da72de6563557c8fc44481c8b5b3a8fd665d

12 Fix graph randomizer · algorithm-visualizer/tracers.js@1f06da7 · GitHub
Skip to content

Commit 1f06da7

Browse files
committed
Fix graph randomizer
1 parent 134632c commit 1f06da7

File tree

6 files changed

+28
-32
lines changed

6 files changed

+28
-32
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [2.0.0]
810
### Added
911
- Tracers for Java and C++.
1012

languages/cpp/src/Randomize.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,13 @@ namespace Randomize {
185185
}
186186
Double ratioRandomizer;
187187
for (int i = 0; i < _N; i++) {
188-
G[i][i] = 0;
189-
T value = ratioRandomizer.create() < _ratio ? _weighted ? (*_randomizer).create() : 1 : 0;
190-
if (_directed) {
191-
for (int j = 0; j < _N; j++) {
192-
if (i == j) continue;
193-
G[i][j] = value;
194-
}
195-
} else {
196-
for (int j = 0; j < i; j++) {
197-
G[i][j] = G[j][i] = value;
188+
for (int j = 0; j < _N; j++) {
189+
if (i == j) {
190+
G[i][j] = 0;
191+
} else if (_directed || i < j) {
192+
G[i][j] = ratioRandomizer.create() < _ratio ? _weighted ? (*_randomizer).create() : 1 : 0;
193+
} else {
194+
G[i][j] = G[j][i];
198195
}
199196
}
200197
}

languages/cpp/test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using namespace Randomize;
88

99
int main() {
1010
int N = 5;
11-
int **graph = Graph<int>(N, 1, *(new Integer(1, 5))).weighted(true).create();
11+
int **graph = Graph<int>(N, .5, *(new Integer(1, 5))).weighted(true).directed(false).create();
1212
for (int i = 0; i < N; i++) {
1313
for (int j = 0; j < N; j++) {
1414
cout << graph[i][j] << " ";

languages/java/src/main/java/org/algorithm_visualizer/tracers/Randomize.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,13 @@ public Graph weighted() {
252252
public Object[][] create() {
253253
Object[][] G = (Object[][]) Array.newInstance(_randomizer.getType(), _N, _N);
254254
for (int i = 0; i < _N; i++) {
255-
G[i][i] = 0;
256-
Object value = random.nextDouble() < _ratio ? _weighted ? _randomizer.create() : 1 : 0;
257-
if (_directed) {
258-
for (int j = 0; j < _N; j++) {
259-
if (i == j) continue;
260-
G[i][j] = value;
261-
}
262-
} else {
263-
for (int j = 0; j < i; j++) {
264-
G[i][j] = G[j][i] = value;
255+
for (int j = 0; j < _N; j++) {
256+
if (i == j) {
257+
G[i][j] = 0;
258+
} else if (_directed || i < j) {
259+
G[i][j] = random.nextDouble() < _ratio ? _weighted ? _randomizer.create() : 1 : 0;
260+
} else {
261+
G[i][j] = G[j][i];
265262
}
266263
}
267264
}

languages/js/src/Randomize.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,17 @@ class Graph extends Randomizer {
104104

105105
create() {
106106
const G = new Array(this._N);
107-
for (let i = 0; i < this._N; i++) G[i] = new Array(this._N);
108107
for (let i = 0; i < this._N; i++) {
109-
G[i][i] = 0;
110-
const value = Math.random() < this._ratio ? this._weighted ? this._randomizer.create() : 1 : 0;
111-
if (this._directed) {
112-
for (let j = 0; j < this._N; j++) {
113-
if (i === j) continue;
114-
G[i][j] = value;
115-
}
116-
} else {
117-
for (let j = 0; j < i; j++) {
118-
G[i][j] = G[j][i] = value;
108+
G[i] = new Array(this._N);
109+
}
110+
for (let i = 0; i < this._N; i++) {
111+
for (let j = 0; j < this._N; j++) {
112+
if (i === j) {
113+
G[i][j] = 0;
114+
} else if (this._directed || i < j) {
115+
G[i][j] = Math.random() < this._ratio ? this._weighted ? this._randomizer.create() : 1 : 0;
116+
} else {
117+
G[i][j] = G[j][i];
119118
}
120119
}
121120
}

specs/randomizers/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { default as Integer } from './Integer';
2+
export { default as Double } from './Double';
23
export { default as String } from './String';
34
export { default as Array2D } from './Array2D';
45
export { default as Array1D } from './Array1D';

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/algorithm-visualizer/tracers.js/commit/1f06da72de6563557c8fc44481c8b5b3a8fd665d

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy