MCPcopy Create free account
hub / github.com/Linloir/GraphBuilder / Dijkstra

Method Dijkstra

graph_implement.cpp:337–383  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

335}
336
337void 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
385AMLGraph* ALGraph::ConvertToAML(){
386 AMLGraph *converted = new AMLGraph(type);

Callers 1

InitMethod · 0.45

Calls 4

edVexMethod · 0.80
reverseDirectionMethod · 0.80
accessMethod · 0.45
visitMethod · 0.45

Tested by

no test coverage detected