| 335 | } |
| 336 | |
| 337 | void ALGraph::Dijkstra(int strtID){ |
| 338 | //Clear previous result |
| 339 | ClearVisit(); |
| 340 | ResetDistance(); |
| 341 | //Set start vex info to all vertexes |
| 342 | for(int i = 0; i < vexList.size(); i++) |
| 343 | vexList[i].info->strtVexInfo = vexList[strtID].info; |
| 344 | //Start dijkstra |
| 345 | vexList[strtID].info->distance = 0; |
| 346 | vexList[strtID].access("strt"); |
| 347 | while(true){ |
| 348 | //Find next |
| 349 | int minVexID = -1; |
| 350 | for(int i = 0; i < vexList.size(); i++){ |
| 351 | if(vexList[i].visited || vexList[i].info->distance == VexInfo::INF) |
| 352 | continue; |
| 353 | if(minVexID == -1) |
| 354 | minVexID = i; |
| 355 | else if(vexList[i].info->distance < vexList[minVexID].info->distance) |
| 356 | minVexID = i; |
| 357 | } |
| 358 | if(minVexID == -1) |
| 359 | break; |
| 360 | //Set visit to edge and vex |
| 361 | ALArc *edge = FindArc(vexList[minVexID].info->preVexID, minVexID); |
| 362 | if(edge){ |
| 363 | if(type == UDG && GetIdOf(edge->gArc->edVex()) != minVexID) |
| 364 | edge->gArc->reverseDirection(); |
| 365 | edge->visit(); |
| 366 | } |
| 367 | vexList[minVexID].visit(); |
| 368 | //Find adjacent |
| 369 | for(ALArc *p = vexList[minVexID].firstArc; p != nullptr; p = p->nextArc){ |
| 370 | if(!vexList[p->eVexID].visited){ |
| 371 | if(GetIdOf(p->gArc->edVex()) != p->eVexID) |
| 372 | p->gArc->reverseDirection(); |
| 373 | p->access(); |
| 374 | if(vexList[p->eVexID].info->distance == VexInfo::INF || |
| 375 | vexList[p->eVexID].info->distance > vexList[minVexID].info->distance + p->weight){ |
| 376 | vexList[p->eVexID].info->preVexID = minVexID; |
| 377 | vexList[p->eVexID].info->distance = vexList[minVexID].info->distance + p->weight; |
| 378 | vexList[p->eVexID].access(QString::asprintf("%d", vexList[p->eVexID].info->distance)); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | AMLGraph* ALGraph::ConvertToAML(){ |
| 386 | AMLGraph *converted = new AMLGraph(type); |
no test coverage detected