¡¶µÚ7Õ ͼ½á¹¹¡·Ï°Ìâ½â´ð

µÚ7Õ ͼ½á¹¹

int v; cout<<\½¨Á¢Ò»¸öÓÐÏòÍøµÄÁÚ½Ó¾ØÕóG:\\n\ CreateGraph_MG(G,gk); cout<<\ÓÐÏòÍøGµÄÁÚ½Ó¾ØÕóΪ:\\n\ DisplyMG(G); cout<<\ÊäÈëÔ´µãϱê:\ ShortestPath_DIJ(dist,G,v); cout<<\Ô´µãΪ:v=\µ½¸÷µãµÄ×î¶Ì·¾¶Îª:\\n\ Disply_Dist(dist); }³ÌÐòÔËÐÐÑÝʾ½á¹ûΪ£º

½¨Á¢Ò»¸öÓÐÏòÍøµÄÁÚ½Ó¾ØÕóG:

ÊäÈ붥µãÊývexnumºÍ»¡Êýarcnum:7 12¨L °´Ä³ÖÖ˳ÐòÊäÈë7¸ö¶¥µãµÄÖµ: 1 2 3 4 5 6 7¨L ÊäÈëͼÖÐ12Ìõ±ß(»¡)ºÍȨµÄÐÅÏ¢u v w:

1 2 2 1 4 1 2 4 3 2 5 10 3 1 4 3 6 5 4 3 2 4 5 2 4 6 8 4 7 4 5 7 6 7 6 1¨L ÓÐÏòÍøGµÄÁÚ½Ó¾ØÕóΪ: 0 2 0 1 0 0 0 0 0 0 3 10 0 0 4 0 0 0 0 5 0 0 0 2 0 2 8 4

0 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 1 0

ÊäÈëÔ´µãϱê:0¨L

Ô´µãΪ:v=0 µ½¸÷µãµÄ×î¶Ì·¾¶Îª: 0: min=0,path=0 0 1: min=2,path=0 1 2: min=3,path=0 3 2 3: min=1,path=0 3 4: min=3,path=0 3 4 5: min=6,path=0 3 6 5 6: min=5,path=0 3 6

³ÌÐòÔËÐÐÖÐËù½¨Á¢µÄÊÇͼ7.26(a)ËùʾµÄÓÐÏòÍøGµÄÁÚ½Ó¾ØÕó£¬ÆäÊä³ö½á¹ûÊÇͼ7.26(b)ËùʾµÄ£¬´ÓÔ´µãv0³ö·¢µ½¸÷¸ö¶¥µãµÄ×î¶Ì·¾¶³¤¶ÈÒÔ¼°Â·¾¶Éϸ÷¸ö¶¥µãµÄϱꡣ¸ÃËã·¨µÄʱ¼ä¸´ÔÓ¶ÈΪO(n2)¡£

7.5.2ÿһ¶Ô¶¥µãÖ®¼äµÄ×î¶Ì·¾¶

ÔÚÓÐÏòÍøÖУ¬Í¨¹ýµÏ½Ü˹ÌØÀ­(Dijkstra)Ëã·¨¿ÉÒÔÇó³ö´ÓÔ´µãµ½ÆäËü¸÷¶¥µãµÄ×î¶Ì·¾¶¡£¶ÔÓÚ¾ßÓÐn¸ö¶¥µãµÄÓÐÏòÍøG£¬¿ÉÒÔͨ¹ýn´Îµ÷ÓõϽÜ˹ÌØÀ­Ëã·¨ÇóµÃÿһ¶Ô¶¥µãÖ®¼äµÄ×î¶Ì·¾¶£¬Æä×ܵÄÖ´ÐÐʱ¼äΪO(n3)¡£´ËÍ⻹ÓÐÁíÒ»ÖÖÇó½â·½·¨£¬Õâ¾ÍÊǸ¥ÂåÒÁµÂ(Floyd)Ëã·¨¡£¸ÃËã·¨µÄÖ´ÐÐʱ¼äÒ²ÊÇO(n3)£¬µ«ÊÇÒª±ÈµÏ½Ü˹ÌØÀ­(Dijkstra)Ëã·¨¼ò½à¡¢ÇåÎú¡£

1£®¸¥ÂåÒÁµÂ(Floyd)Ë㷨˼Ïë

¸¥ÂåÒÁµÂ(Floyd)Ëã·¨µÄ»ù±¾Ë¼ÏëÊÇ£ºÈç¹û´Óviµ½vjÓл¡ars[i][j]£¬Ôò´Óviµ½vjÓÐÒ»Ìõ³¤¶ÈΪdist[i][j]=ars[i][j]µÄ·¾¶path[i][j]=¡£ÏÂÃæÑéÖ¤¸Ã·¾¶ÊÇ·ñΪ×î¶Ì·¾¶£º

Ê×ÏÈ¿¼ÂÇÊÇ·ñ´æÔÚ£¨¼´ºÍÊÇ·ñ´æÔÚ£©¡£Èç¹û´æÔÚÔò±È½Ïars[i][1]+ars[1][j]Óëars[i][j]£¬È¡ÆäÖнÏСÕß×÷Ϊ´Óviµ½vjµÄÖмäµãÐòºÅ²»´óÓÚ1µÄ×î¶Ì·¾¶³¤¶Èdist[i][j]²¢ÐÞ¸ÄÏàÓ¦µÄ·¾¶path[i][j]¡£

È»ºóÔÚ·¾¶path[i][j]ÖÐÔÙÔö¼ÓÒ»¸ö¶¥µãv2£¬Èç¹ûpath[i][2]Óëpath[2][j]¾ù´æÔÚÔò±È½Ïdist[i][2]+dist[2][j]Óëdist[i][j]£¬È¡ÆäÖнÏСÕß×÷Ϊ´Óviµ½vjµÄÖмäµãÐòºÅ²»´óÓÚ2µÄ×î¶Ì·¾¶³¤¶Èdist[i][j]²¢ÐÞ¸ÄÏàÓ¦µÄ·¾¶path[i][j]¡£

Ö®ºóÔÚpath[i][j]ÖÐÔÙÔö¼ÓÒ»¸ö¶¥µãv3Öظ´ÒÔÉϹý³Ì¡£Ò»°ãµØ½²£¬Èç¹ûpath[i][k]Óëpath[k][j]·Ö±ðÊÇ´Óviµ½vkºÍvkµ½vjµÄÖм䶥µãÐòºÅ²»´óÓÚk-1µÄ×î¶Ì·¾¶£¬Ôò±È½Ïdist[i][k]+dist[k][j]

-.207.-

µÚ7Õ ͼ½á¹¹

Óëdist[i][j]£¬È¡½ÏСÕß×÷Ϊ´Óviµ½vjµÄÖмäµãÐòºÅ²»´óÓÚkµÄ×î¶Ì·¾¶³¤¶Èdist[i][j]²¢ÐÞ¸ÄÏàÓ¦µÄ·¾¶path[i][j]¡£

¸¥ÂåÒÁµÂ(Floyd)Ëã·¨µÝÍƵزúÉúÒ»¸ö¾ØÕóÐòÁУº

(dist(0)[i][j],path(0)[i][j])£¬(dist(1)[i][j],path(1)[i][j])£¬¡­£¬(dist(n)[i][j],path(n)[i][j])¡£

ÆäÖУ¬(dist(k)[i][j],path(k)[i][j])·Ö±ð±íʾ´Óviµ½vjµÄ£¬Öм䶥µãÐòºÅ²»´óÓÚkµÄ£¬×î¶Ì·¾¶³¤¶ÈºÍ·¾¶¡£ËùÒÔ£¬FloydËã·¨¾ÍÊÇÖð²½ÔÊÐíÔ½À´Ô½¶àµÄ¶¥µã×÷Ϊ·¾¶µÄÖм䶥µã£¬Ö±ÖÁËùÓпÉÄܵĶ¥µã¾ù×÷ΪÖмäµãΪֹ¡£

È¡¾ØÕóÐòÁÐdist(k)[i][j]µÄ³õֵΪdist(0)[i][j]=arcs[i][j]£¬ÔòÒÑÖªdist(k-1)[i][j]Çó½âdist(k)[i][j]µÄ¹ý³Ì·ÖÒÔÏÂÁ½ÖÖÇé¿ö£º

(1) Èç¹û´Óviµ½vjµÄ·¾¶²»¾­¹ývk£¬Ôòdist(k)[i][j]=dist(k-1)[i][j]£¬path(k)[i][j]=path(k-1)[i][j]¡£ (2) Èç¹û´Óviµ½vjµÄ·¾¶¾­¹ývk£¬Ôò±È½Ïdist(k-1)[i][k]+dist(k-1)[k][j]Óëdist(k-1)[i][j]£¬È¡½ÏСÕß×÷Ϊdist(k)[i][j]£¬²¢ÐÞ¸ÄÏàÓ¦µÄ·¾¶path(k)[i][j]Ϊ£ºpath(k-1)[i][j]»òpath(k-1)[i][k]+path(k-1)path[k][j]¡£

ÀýÈ磬¶Ôͼ7.28ËùʾµÄÓÐÏòÍøG£¬Í¨¹ý¸¥ÂåÒÁµÂ(Floyd)Ëã·¨£¬ÇóÆäÿһ¶Ô¶¥µãÖ®¼äµÄ×î¶Ì·¾¶³¤¶Èdist[i][j]ºÍ×î¶Ì·¾¶path[i][j]¡£Çó½â¹ý³ÌÖÐdist¡¢pathµÄ±ä»¯Çé¿öÈçͼ7.29Ëùʾ¡£

?0411??0411??046??046??Dist=?602?Dist=?602?Dist=?502?

Dist0=?6021?23????????????300???370???370???370??

ÔÚͼ7.29ÖУ¬Dist0Óëpath0·Ö±ð±íʾ³õʼ״̬µÄvi¡¢vjµÄ×î¶Ì¾àÀ뼰·¾¶£»Dist1Óëpath1

·Ö±ð±íʾ¼ÓÈ붥µãv0(¼´¶¥µãa)ÒԺ󶥵ãvi¡¢vjµÄ×î¶Ì¾àÀ뼰·¾¶£»Dist2Óëpath2·Ö±ð±íʾ¼ÓÈ붥µãv1(¼´¶¥µãb)ÒԺ󶥵ãvi¡¢vjµÄ×î¶Ì¾àÀ뼰·¾¶£»Dist3Óëpath3·Ö±ð±íʾ¼ÓÈ붥µãv2(¼´¶¥µãc)ÒԺ󶥵ãvi¡¢vjµÄ×î¶Ì¾àÀ뼰·¾¶¡£

2£®¸¥ÂåÒÁµÂ(Folyd)Ëã·¨µÄʵÏÖ

(1) ¶¨Ò帨ÖúÊý×édist[]Óëpath[]µÄ½á¹¹ÀàÐÍ

struct Stack{int t,*s;}; //¶¨Òå´æ´¢×î¶Ì·¾¶µÄÕ»ÀàÐÍ

-.208.-

µÚ7Õ ͼ½á¹¹

struct Min

{ int num; //ÓÐÏòÍøµÄ½áµã¸öÊý int *dist; //×î¶Ì·¾¶³¤¶ÈµÄÊý×éÖ¸Õë Stack *path; //´æ´¢Â·¾¶µÄÊý×éÖ¸Õë };

(2) Êý×éµÄ³õʼ»¯²Ù×÷

º¯Êývoid InitMin(Min &min,MGraph G)µÄ¹¦ÄÜÊÇ£¬Í¨¹ýGµÄÁÚ½Ó¾ØÕó¶Ô¾ØÕóÀ´³õʼ»¯Â·¾¶Êý×émin¡£

void InitMin(Min &min,MGraph G) { int i,j,w; int num=G.vexnum; min.num=num; min.dist=new int[num*num]; //Ϊ×î¶Ì¾àÀë¾ØÕódist·ÖÅä¿Õ¼ä min.path=new Stack[num*num]; //Ϊ×î¶Ì·¾¶¾ØÕópath·ÖÅä¿Õ¼ä for(i=0;i { min.path[i*num+j].s[0]=i; min.path[i*num+j].s[1]=j; min.path[i*num+j].t+=2; } } } (3) ·¾¶²Ù×÷ÓëÏÔʾÊä³ö

º¯Êývoid Addpath(Stack &path,Stack path1,Stack path2)µÄ¹¦ÄÜÊÇ£¬ÊµÏÖ¶Ô·¾¶path[i][j]µÄÁ¬½Ó²Ù×÷£ºpath[i][j]=path[i][k]+path[k][j]µÄÔËËã¡£

void Addpath(Stack &path,Stack path1,Stack path2) { int t=0,i=0,j=1; while(i

º¯Êývoid DisplyMin(Min min)µÄ¹¦ÄÜÊÇ£¬ÏÔʾÊä³öminÖеÄ×î¶Ì·¾¶³¤¶Èmin.distºÍ×î¶Ì·¾¶min.path¡£

void DisplyMin(Min min) { int i,j,k,t,n=min.num;

-.209.-

µÚ7Õ ͼ½á¹¹

}

cout<<\×î¶Ì¾àÀë¾ØÕódistΪ:\\n\for(i=0;i

{ for(j=0;j

cout<<\×î¶Ì·¾¶ÉϵĽáµãΪ:\\n\for(i=0;i

(4) ¸¥ÂåÒÁµÂË㷨ʵÏÖ

º¯Êývoid Floyd(Min &min,MGraph G)µÄ¹¦ÄÜÊÇ£¬Í¨¹ý¸¥ÂåÒÁµÂËã·¨ÇóÓÐÏòÍøGÖÐÿһ¶Ô

¶¥µãÖ®¼äµÄ×î¶Ì·¾¶¼°Æ䳤¶Èµ½minÖС£

void Floyd(Min &min,MGraph G) { int i,j,k,n=G.vexnum; InitMin(min,G); for(k=0;k

ÔÚÖ÷º¯ÊýÖУ¬Ê×ÏÈͨ¹ýº¯Êýµ÷Óá°CreateGraph_MG(G,gk);¡±´´½¨Ò»¸öÓÐÏòÍøG²¢ÏÔʾÊä³öGµÄÁÚ½Ó¾ØÕó£¬ÔÙͨ¹ýº¯Êýµ÷Óá°Floyd(min,G);¡±Çó³öGÖÐÈÎÒâÁ½µã¼äµÄ×î¶Ì·¾¶µ½ÊäminÖУ¬×îºóÏÔʾÊä³öÑÝʾ½á¹û¡£

void main() {

-.210.-

ÁªÏµ¿Í·þ£º779662525#qq.com(#Ì滻Ϊ@)