![]() |
الگوریم دیکسترا
با سلام . اگه کسی راجع به مرتبه اجرایی این الگوریتم راهنماییم کنه ممنون میشم. 1. #include<stdio.h> 2. #include<conio.h> 3. #define INFINITY 100 4. #define MEMBER 1 5. #define NOMEMBER 0 6. #define M 5 7. void shortest(intw[][M],int s,int t,int *pd) 8. int main() 9. { 10. int w[m][m]={{100,7,4,6,1}, 11. {100,100,100,100,100}, 12. {100,2,100,5,100}, 13. {100,3,100,100,100}, 14. {100,100,100,1,100}}; 15. int pd,s=0,t=1; 16. clrscr(); 17. shortest(w,s,t,&pd); 18. printf("Sourse is %d, Destination is %d\n",s,t) 19. printf("length of shortest path is:%d",pd) 20. getch(); 21. return 0; 22. } 23. void shortest(intw[][M],int s,int t,int *pd) 24. { 25. int distance[M],found[M]; 26. int current,i,k,dc; 27. int smallDist,newDist; 28. //initialization 29. for(i=0;i<m;i++) 30. { 31. found[i]=NOMEMBER; 32. distance[i]=INTIFINITY; 33. } 34. found[s]=MEMBER; 35. distance[s]=0; 36. current=s; 37. while(current!=t) 38. { 39. smallDist=INTIFINITY; 40. dc=distance[current]; 41. for(i=0;i<m;i++) 42. if(found[i]==NOMEMBER) 43. { 44. newDist=dc+w[current][i]; 45. if(newDist<distance[i]) 46. distance[i]=newDist; 47. //determin the smallest distance 48. if(distance[i]<smallDist) 49. { 50. smallDist=distance[i]; 51. k=i; 52. } 53. }//end of for,if 54. current=k; 55. found[current]=MEMBER; 56. }//end of while 57. *pd=distance[t]; 58. } |
Farz konid ke graph n node va m edge dare. Dijkstra ye loop ba length n dare va dar har iteration loop ye edge ke sharte greedy Dijkstra ro dare ro entekhab mikone. entekhabe in edge O(m) zaman niaz dare, pas jaman mishe O(mn ) . vali ba estefade az priority queue mishe zamane ro be O(mlogn) kahesh dad.
|
زمان محلي شما با تنظيم GMT +3.5 هم اکنون ۱۰:۱۷ بعد از ظهر ميباشد. |
Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.