Skip to content

Commit

Permalink
190503 - G1 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JazonJiao committed May 8, 2019
1 parent f57159b commit 98291e5
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 34 deletions.
166 changes: 132 additions & 34 deletions Graph Algorithms/1_bfsdfs.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,98 @@
let E =
[[0, 1],
[0, 2],
[0, 3],
[0, 4],
[0, 6],
[1, 2],
[1, 3],
[1, 4],
[1, 7],
[2, 3],
[2, 5],
[2, 6],
[2, 7],
[2, 9],
[3, 4],
[3, 5],
[3, 6],
[3, 7],
[3, 8],
[3, 10],
[4, 6],
[4, 7],
[4, 8],
[4, 11],
[5, 6],
[5, 9],
[5, 10],
[5, 12],
[6, 7],
[6, 9],
[6, 10],
[6, 13],
[7, 8],
[7, 9],
[7, 10],
[7, 11],
[7, 14],
[8, 10],
[8, 11],
[8, 15],
[9, 10],
[9, 12],
[9, 13],
[9, 14],
[9, 16],
[10, 11],
[10, 12],
[10, 13],
[10, 14],
[10, 15],
[10, 17],
[11, 13],
[11, 14],
[11, 15],
[11, 18],
[12, 13],
[12, 16],
[12, 17],
[13, 14],
[13, 16],
[13, 17],
[13, 18],
[14, 15],
[14, 16],
[14, 17],
[14, 18],
[15, 17],
[15, 18],
[16, 17],
[17, 18]
];

let G = {
V: [[300, 100],
[200, 200],
[300, 300],
[400, 200],
V: [[240, 77],
[360, 77],
[180, 177],
[300, 177],
[420, 177],
[120, 277],
[240, 277],
[360, 277],
[480, 277],
[180, 377],
[300, 377],
[420, 377],
[120, 477],
[240, 477],
[360, 477],
[480, 477],
[180, 577],
[300, 577],
[420, 577],
],
E: [[0, 1],
[0, 2],
[2, 3],
]
E: randomizeEdges(E, 0.47)
};


Expand Down Expand Up @@ -47,7 +132,7 @@ const Graph00 = function(s) {



class Graph_BFS extends Graph_U {
class Graph_BFS extends Graph_U { // 2019-05-07
constructor(ctx, args) {
super(ctx, args);

Expand All @@ -63,7 +148,7 @@ class Graph_BFS extends Graph_U {

this.z = new Tracer(this.s, {
str: "Breadth-first search",
x: 537, y: 57, start: args.time, begin: args.begin,
x: 617, y: 57, start: args.time, begin: args.begin,
});
this.z.add("Create a queue, and enqueue start vertex", 0, 35, 45);
this.z.add("While queue is not empty, do:", -1, 35, 90);
Expand All @@ -72,17 +157,14 @@ class Graph_BFS extends Graph_U {
this.z.add("End", 3, 35, 225);

this.state = 0;
this.f = 47;
this.fc = 0; // frame count for this class
}

stepBFS() {

}

resetNode(v) {
resetNode(v, to) { // to stands for time offset
v.txt = new TextFade(this.s, {
x: v.x, y: v.y + this.yOffset,
size: 42, start: this.s.frameCount + 1, mode: 1, str: "" + this.top,
size: 42, start: this.s.frameCount + to, mode: 1, str: "" + this.top,
});
}

Expand All @@ -94,45 +176,61 @@ class Graph_BFS extends Graph_U {
if (!this.finished && this.fc % this.f === 0 && this.fc > this.begin) {
if (this.state === 0) { // initial state
this.z.reset(0);
this.resetNode(this.nodes[0]);
this.resetNode(this.nodes[0], 1);
this.nodes[0].reColor(Yellow);

this.state = 1;
} else if (this.state === 1) { // dequeue a vertex (highlight it)
this.z.reset(1);
this.bottom++;
let node = this.queue[this.bottom - 1];
let node = this.queue[this.bottom];

if (this.bottom === this.n) {
this.state = 3;
}
let flag = false; // highlight the node for 1 or 2 seconds
for (let i = 0; i < this.n; i++) {
if (this.A[node][i] && !this.visited[i]) {
flag = true;
this.state = 2; // if there is a unvisited node nearby, go to state 2
}
}
this.nodes[node].highlight(Orange, this.f / fr * (flag ? 2 : 1), 14);
this.nodes[node].highlight(Orange, this.f / fr * 2, 14);

this.state = 2;
} else if (this.state === 2) { // add unvisited neighbors to queue
this.z.reset(2);
let node = this.queue[this.bottom - 1];
this.nodes[node].reColor(Green);
let node = this.queue[this.bottom];
this.bottom++;
this.nodes[node].reColor(Green, false, [77, 147, 117, 248]);

let to = 1;
for (let i = 0; i < this.n; i++)
if (this.A[node][i] && !this.visited[i]) {
this.top++;
this.queue[this.top] = i;
this.visited[i] = true;
this.edges[node][i].highlight(Orange, this.f / fr);
this.resetNode(this.nodes[i]);
this.resetNode(this.nodes[i], to);
to += 4;
this.nodes[i].reColor(Yellow);
}

if (this.bottom > this.top) { // queue is empty now
let flag = false; // whether there are unvisited vertices left
for (let i = 0; i < this.n; i++)
if (!this.visited[i])
flag = true;
this.state = flag ? 4 : 3; // flag is true, then not finished
return;
}
this.state = 1;
} else {
} else if (this.state === 3) { // finished
this.z.reset(3);
this.nodes[this.queue[this.bottom - 1]].reColor(Green);

this.finished = true;
} else if (this.state === 4) { // start BFS at a new vertex; intermediate step
this.z.reset(0);
for (let i = 0; i < this.n; i++)
if (!this.visited[i]) {
this.top++;
this.queue[this.top] = i;
this.visited[i] = true;
this.resetNode(this.nodes[i], 1);
this.nodes[i].reColor(Yellow);
break;
}
this.state = 1;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions Graph Algorithms/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,22 @@ class Tracer extends PointBase {
}

// 2019-05-06
// change the weights to be a random int value between 0 and max
function randomizeWeights(arr, max) { // this will change the array
for (let i = 0; i < arr.length; i++) {
arr[i][3] = Math.floor(Math.random() * max); // fixme: random often gives duplicate nums
}
}

// 2019-05-06
// select edges from a given list of valid edges, each choice with a certain probability
function randomizeEdges(arr, prob) {
let r = [];
let l = 0;
for (let i = 0; i < arr.length; i++)
if (Math.random() < prob) {
r[l] = arr[i];
l++;
}
return r;
}

0 comments on commit 98291e5

Please sign in to comment.