Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
The license selected for the repository is subject to the license used by the main branch of the repository.
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
ConnectedComponent.js 15.42 KB
Copy Edit Raw Blame History
morris authored 2020年01月31日 14:34 +08:00 . init
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
// Copyright 2011 David Galles, University of San Francisco. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of the University of San Francisco
var AUX_ARRAY_WIDTH = 25;
var AUX_ARRAY_HEIGHT = 25;
var AUX_ARRAY_START_Y = 100;
var VISITED_START_X = 475;
var PARENT_START_X = 400;
var D_X_POS_SMALL = [760, 685, 915, 610, 910, 685, 915, 760];
var F_X_POS_SMALL = [760, 685, 915, 610, 910, 685, 915, 760];
var D_Y_POS_SMALL = [18, 118, 118, 218, 218, 318, 318, 418];
var F_Y_POS_SMALL = [32, 132, 132, 232, 232, 332, 332, 432];
var D_X_POS_LARGE = [560, 660, 760, 860,
610, 710, 810,
560, 660, 760, 860,
610, 710, 810,
560, 660, 760, 860];
var F_X_POS_LARGE = [560, 660, 760, 860,
610, 710, 810,
560, 660, 760, 860,
610, 710, 810,
560, 660, 760, 860];
var D_Y_POS_LARGE = [037, 037, 037, 037,
137, 137, 137,
237, 237, 237, 237,
337, 337, 337,
437, 437, 437, 437];
var F_Y_POS_LARGE = [62, 62, 62, 62,
162, 162, 162,
262, 262, 262, 262,
362, 362, 362,
462, 462, 462, 462];
var HIGHLIGHT_CIRCLE_COLOR = "#000000";
var DFS_TREE_COLOR = "#0000FF";
function ConnectedComponent(am, w, h)
{
this.init(am, w, h);
}
ConnectedComponent.prototype = new Graph();
ConnectedComponent.prototype.constructor = ConnectedComponent;
ConnectedComponent.superclass = Graph.prototype;
ConnectedComponent.prototype.addControls = function()
{
this.startButton = addControlToAlgorithmBar("Button", "Run Connected Component");
this.startButton.onclick = this.startCallback.bind(this);
ConnectedComponent.superclass.addControls.call(this, false);
}
ConnectedComponent.prototype.init = function(am, w, h)
{
this.showEdgeCosts = false;
ConnectedComponent.superclass.init.call(this, am, w, h, true, false); // TODO: add no edge label flag to this?
// Setup called in base class init function
}
ConnectedComponent.prototype.setup = function()
{
ConnectedComponent.superclass.setup.call(this);
this.animationManager.setAllLayers([0, this.currentLayer]);
this.messageID = new Array();
this.commands = new Array();
this.animationManager.clearHistory();
this.messageID = new Array();
this.commands = new Array();
this.highlightCircleL = this.nextIndex++;
this.highlightCircleAL = this.nextIndex++;
this.highlightCircleAM= this.nextIndex++
this.initialIndex = this.nextIndex;
this.old_adj_matrix = new Array(this.size);
this.old_adj_list_list = new Array(this.size);
this.old_adj_list_index = new Array(this.size);
this.old_adj_list_edges = new Array(this.size);
for (var i = 0; i < this.size; i++)
{
this.old_adj_matrix[i] = new Array(this.size);
this.old_adj_list_index[i] = this.adj_list_index[i];
this.old_adj_list_list[i] = this.adj_list_list[i];
this.old_adj_list_edges[i] = new Array(this.size);
for (var j = 0; j < this.size; j++)
{
this.old_adj_matrix[i][j] = this.adj_matrix[i][j];
if (this.adj_matrix[i][j] > 0)
{
this.old_adj_list_edges[i][j] = this.adj_list_edges[i][j];
}
}
}
}
ConnectedComponent.prototype.startCallback = function(event)
{
this.implementAction(this.doCC.bind(this),"");
}
ConnectedComponent.prototype.transpose = function()
{
for (var i = 0; i < this.size; i++)
{
for (var j = i+1; j <this.size; j++)
{
var tmp = this.adj_matrix[i][j];
this.adj_matrix[i][j] = this.adj_matrix[j][i];
this.adj_matrix[j][i] = tmp;
}
}
}
ConnectedComponent.prototype.doCC = function(ignored)
{
this.visited = new Array(this.size);
this.commands = new Array();
var i;
if (this.messageID != null)
{
for (i = 0; i < this.messageID.length; i++)
{
this.cmd("Delete", this.messageID[i], 1);
}
}
this.rebuildEdges();
this.messageID = new Array();
this.d_timesID_L = new Array(this.size);
this.f_timesID_L = new Array(this.size);
this.d_timesID_AL = new Array(this.size);
this.f_timesID_AL = new Array(this.size);
this.d_times = new Array(this.size);
this.f_times = new Array(this.size);
this.currentTime = 1
for (i = 0; i < this.size; i++)
{
this.d_timesID_L[i] = this.nextIndex++;
this.f_timesID_L[i] = this.nextIndex++;
this.d_timesID_AL[i] = this.nextIndex++;
this.f_timesID_AL[i] = this.nextIndex++;
}
this.messageY = 30;
var vertex;
for (vertex = 0; vertex < this.size; vertex++)
{
if (!this.visited[vertex])
{
this.cmd("CreateHighlightCircle", this.highlightCircleL, HIGHLIGHT_CIRCLE_COLOR, this.x_pos_logical[vertex], this.y_pos_logical[vertex]);
this.cmd("SetLayer", this.highlightCircleL, 1);
this.cmd("CreateHighlightCircle", this.highlightCircleAL, HIGHLIGHT_CIRCLE_COLOR,this.adj_list_x_start - this.adj_list_width, this.adj_list_y_start + vertex*this.adj_list_height);
this.cmd("SetLayer", this.highlightCircleAL, 2);
this.cmd("CreateHighlightCircle", this.highlightCircleAM, HIGHLIGHT_CIRCLE_COLOR,this.adj_matrix_x_start - this.adj_matrix_width, this.adj_matrix_y_start + vertex*this.adj_matrix_height);
this.cmd("SetLayer", this.highlightCircleAM, 3);
if (vertex > 0)
{
var breakID = this.nextIndex++;
this.messageID.push(breakID);
this.cmd("CreateRectangle", breakID, "", 200, 0, 10, this.messageY,"left","bottom");
this.messageY = this.messageY + 20;
}
this.dfsVisit(vertex, 10, false);
this.cmd("Delete", this.highlightCircleL, 2);
this.cmd("Delete", this.highlightCircleAL, 3);
this.cmd("Delete", this.highlightCircleAM, 4);
}
}
this.clearEdges();
this.removeAdjList();
this.transpose();
this.buildEdges();
this.buildAdjList();
this.currentTime = 1
for (i=0; i < this.size; i++)
{
for (j = 0; j < this.size; j++)
{
if (this.adj_matrix[i][j] >= 0)
{
this.cmd("SetText", this.adj_matrixID[i][j], "1");
}
else
{
this.cmd("SetText", this.adj_matrixID[i][j], "");
}
}
}
for (vertex = 0; vertex < this.size; vertex++)
{
this.visited[vertex] = false;
this.cmd("Delete", this.d_timesID_L[vertex], 5);
this.cmd("Delete", this.f_timesID_L[vertex], 6);
this.cmd("Delete", this.d_timesID_AL[vertex], 7);
this.cmd("Delete", this.f_timesID_AL[vertex], 8);
}
var sortedVertex = new Array(this.size);
var sortedID = new Array(this.size);
for (vertex = 0; vertex < this.size; vertex++)
{
sortedVertex[vertex] = vertex;
sortedID[vertex] = this.nextIndex++;
this.cmd("CreateLabel", sortedID[vertex], "Vertex: " + String(vertex)+ " f = " + String(this.f_times[vertex]), 400, 110 + vertex*20, 0);
}
this.cmd("Step");
for (i = 1; i < this.size; i++)
{
var j = i;
var tmpTime = this.f_times[i];
var tmpIndex = sortedVertex[i];
var tmpID = sortedID[i];
while (j > 0 && this.f_times[j-1] < tmpTime)
{
this.f_times[j] = this.f_times[j-1];
sortedVertex[j] = sortedVertex[j-1];
sortedID[j] = sortedID[j-1];
j--;
}
this.f_times[j] = tmpTime;
sortedVertex[j] = tmpIndex;
sortedID[j] = tmpID;
}
for (vertex = 0; vertex < this.size; vertex++)
{
this.cmd("Move", sortedID[vertex], 400, 110 + vertex*20);
}
for (i = 0; i < this.messageID.length; i++)
{
this.cmd("Delete", this.messageID[i], 9);
}
this.messageID = new Array();
this.messageY = 30;
var ccNum = 1;
for (i = 0; i < this.size; i++)
{
vertex = sortedVertex[i];
if (!this.visited[vertex])
{
var breakID1 = this.nextIndex++;
this.messageID.push(breakID1);
var breakID2 = this.nextIndex++;
this.messageID.push(breakID2);
this.cmd("CreateRectangle", breakID1, "", 200, 0, 50, this.messageY + 8,"left","center");
this.cmd("CreateLabel", breakID2, "CC #" + String(ccNum++), 10, this.messageY, 0);
this.cmd("SetForegroundColor",breakID1 ,"#004B00");
this.cmd("SetForegroundColor",breakID2 ,"#004B00");
this.messageY = this.messageY + 20;
this.cmd("CreateHighlightCircle", this.highlightCircleL, HIGHLIGHT_CIRCLE_COLOR, this.x_pos_logical[vertex], this.y_pos_logical[vertex]);
this.cmd("SetLayer", this.highlightCircleL, 1);
this.cmd("CreateHighlightCircle", this.highlightCircleAL, HIGHLIGHT_CIRCLE_COLOR,this.adj_list_x_start - this.adj_list_width, this.adj_list_y_start + vertex*this.adj_list_height);
this.cmd("SetLayer", this.highlightCircleAL, 2);
this.cmd("CreateHighlightCircle", this.highlightCircleAM, HIGHLIGHT_CIRCLE_COLOR,this.adj_matrix_x_start - this.adj_matrix_width, this.adj_matrix_y_start + vertex*this.adj_matrix_height);
this.cmd("SetLayer", this.highlightCircleAM, 3);
this.dfsVisit(vertex, 75, true);
this.cmd("Delete", this.highlightCircleL, 10);
this.cmd("Delete", this.highlightCircleAL, 11);
this.cmd("Delete", this.highlightCircleAM, 12);
}
this.cmd("Delete", sortedID[i], 13);
}
for (vertex = 0; vertex < this.size; vertex++)
{
this.cmd("Delete", this.d_timesID_L[vertex], 14);
this.cmd("Delete", this.f_timesID_L[vertex], 15);
this.cmd("Delete", this.d_timesID_AL[vertex], 16);
this.cmd("Delete", this.f_timesID_AL[vertex], 17);
}
return this.commands
}
ConnectedComponent.prototype.setup_large = function()
{
this.d_x_pos = D_X_POS_LARGE;
this.d_y_pos = D_Y_POS_LARGE;
this.f_x_pos = F_X_POS_LARGE;
this.f_y_pos = F_Y_POS_LARGE;
ConnectedComponent.superclass.setup_large.call(this);
}
ConnectedComponent.prototype.setup_small = function()
{
this.d_x_pos = D_X_POS_SMALL;
this.d_y_pos = D_Y_POS_SMALL;
this.f_x_pos = F_X_POS_SMALL;
this.f_y_pos = F_Y_POS_SMALL;
ConnectedComponent.superclass.setup_small.call(this);
}
ConnectedComponent.prototype.dfsVisit = function(startVertex, messageX, printCCNum)
{
if (printCCNum)
{
var ccNumberID = this.nextIndex++;
this.messageID.push(ccNumberID);
this.cmd("CreateLabel",ccNumberID, "Vertex " + String(startVertex), 5, this.messageY, 0);
this.cmd("SetForegroundColor", ccNumberID, "#0000FF");
}
var nextMessage = this.nextIndex++;
this.messageID.push(nextMessage);
this.cmd("CreateLabel",nextMessage, "DFS(" + String(startVertex) + ")", messageX, this.messageY, 0);
this.messageY = this.messageY + 20;
if (!this.visited[startVertex])
{
this.d_times[startVertex] = this.currentTime++;
this.cmd("CreateLabel", this.d_timesID_L[startVertex], "d = " + String(this.d_times[startVertex]), this.d_x_pos[startVertex], this.d_y_pos[startVertex]);
this.cmd("CreateLabel", this.d_timesID_AL[startVertex], "d = " + String(this.d_times[startVertex]), this.adj_list_x_start - 2*this.adj_list_width, this.adj_list_y_start + startVertex*this.adj_list_height - 1/4*this.adj_list_height);
this.cmd("SetLayer", this.d_timesID_L[startVertex], 1);
this.cmd("SetLayer", this.d_timesID_AL[startVertex], 2);
this.visited[startVertex] = true;
this.cmd("Step");
for (var neighbor = 0; neighbor < this.size; neighbor++)
{
if (this.adj_matrix[startVertex][neighbor] > 0)
{
this.highlightEdge(startVertex, neighbor, 1);
if (this.visited[neighbor])
{
nextMessage = this.nextIndex;
this.cmd("CreateLabel", nextMessage, "Vertex " + String(neighbor) + " already this.visited.", messageX, this.messageY, 0);
}
this.cmd("Step");
this.highlightEdge(startVertex, neighbor, 0);
if (this.visited[neighbor])
{
this.cmd("Delete", nextMessage, "DNM");
}
if (!this.visited[neighbor])
{
this.cmd("Disconnect", this.circleID[startVertex], this.circleID[neighbor]);
this.cmd("Connect", this.circleID[startVertex], this.circleID[neighbor], DFS_TREE_COLOR, this.curve[startVertex][neighbor], 1, "");
this.cmd("Move", this.highlightCircleL, this.x_pos_logical[neighbor], this.y_pos_logical[neighbor]);
this.cmd("Move", this.highlightCircleAL, this.adj_list_x_start - this.adj_list_width, this.adj_list_y_start + neighbor*this.adj_list_height);
this.cmd("Move", this.highlightCircleAM, this.adj_matrix_x_start - this.adj_matrix_width, this.adj_matrix_y_start + neighbor*this.adj_matrix_height);
this.cmd("Step");
this.dfsVisit(neighbor, messageX + 10, printCCNum);
nextMessage = this.nextIndex;
this.cmd("CreateLabel", nextMessage, "Returning from recursive call: DFS(" + String(neighbor) + ")", messageX + 20, this.messageY, 0);
this.cmd("Move", this.highlightCircleAL, this.adj_list_x_start - this.adj_list_width, this.adj_list_y_start + startVertex*this.adj_list_height);
this.cmd("Move", this.highlightCircleL, this.x_pos_logical[startVertex], this.y_pos_logical[startVertex]);
this.cmd("Move", this.highlightCircleAM, this.adj_matrix_x_start - this.adj_matrix_width, this.adj_matrix_y_start + startVertex*this.adj_matrix_height);
this.cmd("Step");
this.cmd("Delete", nextMessage, 18);
}
this.cmd("Step");
}
}
this.f_times[startVertex] = this.currentTime++;
this.cmd("CreateLabel", this.f_timesID_L[startVertex],"f = " + String(this.f_times[startVertex]), this.f_x_pos[startVertex], this.f_y_pos[startVertex]);
this.cmd("CreateLabel", this.f_timesID_AL[startVertex], "f = " + String(this.f_times[startVertex]), this.adj_list_x_start - 2*this.adj_list_width, this.adj_list_y_start + startVertex*this.adj_list_height + 1/4*this.adj_list_height);
this.cmd("SetLayer", this.f_timesID_L[startVertex], 1);
this.cmd("SetLayer", this.f_timesID_AL[startVertex], 2);
}
}
ConnectedComponent.prototype.reset = function()
{
// TODO: Fix undo messing with setup vars.
this.messageID = new Array();
this.nextIndex = this.initialIndex;
for (var i = 0; i < this.size; i++)
{
this.adj_list_list[i] = this.old_adj_list_list[i];
this.adj_list_index[i] = this.old_adj_list_index[i];
for (var j = 0; j < this.size; j++)
{
this.adj_matrix[i][j] = this.old_adj_matrix[i][j];
if (this.adj_matrix[i][j] > 0)
{
this.adj_list_edges[i][j] = this.old_adj_list_edges[i][j];
}
}
}
}
ConnectedComponent.prototype.enableUI = function(event)
{
this.startButton.disabled = false;
ConnectedComponent.superclass.enableUI.call(this,event);
}
ConnectedComponent.prototype.disableUI = function(event)
{
this.startButton.disabled = true;
ConnectedComponent.superclass.disableUI.call(this, event);
}
var currentAlg;
function init()
{
var animManag = initCanvas();
currentAlg = new ConnectedComponent(animManag, canvas.width, canvas.height);
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/big_mo/data-structure-visualizations.git
git@gitee.com:big_mo/data-structure-visualizations.git
big_mo
data-structure-visualizations
Data-Structure-Visualizations
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /