Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 30 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
30
Dung lượng
491,29 KB
Nội dung
/*Code by: Hondacodon*/ 1 Thuật toán LC-BFS(Least Cost -Breadth First Search=tìm kiếm chi phí ít nhất theo chiều Rộng) trước tiên bạn phải tạo 1 file input.txt trong ổ đĩa C:/ #include "stdafx.h" #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 100 typedef struct Queue//khai bao' hang doi { int *QArray; int QMax; int QNumItems; int QFront; int QRear; }QUEUE; int InitQueue(QUEUE &q,int MaxItems)//khoi tao Hang` doi Rong { q.QArray=new int [MaxItems]; if(q.QArray==NULL) return 0; //het bo nho de cap phat' /*Code by: Hondacodon*/ 2 q.QMax=MaxItems; q.QNumItems=0; q.QFront=q.QRear= -1; //-1 tuc' la` chua co phan tu nao trong Quenue return 1; //khoi tao thanh cong } int IsQueueEmpty(const QUEUE &q)//kiem tra hang doi co rong hay ko { if(q.QNumItems== -1) { return 1; // Queue Rong } return 0; //Queue Ko Rong } //Kiem tra Quenue da^y` int IsQueueFull(const QUEUE &q)//kiem tra hang` doi co' day` hay chua { if(q.QNumItems==q.QMax) { return 1; //Queue day } return 0; //Queue chua day } int DeQueue(QUEUE &q , int newitem)//lay 1 pt o dau Queue,co the lam Queue thay doi /*Code by: Hondacodon*/ 3 { if(IsQueueFull(q)) { return 0; //Quenue day ko them dc } q.QRear++; if(q.QRear==q.QMax) { q.QRear=0; } q.QArray[q.QRear]=newitem; if(IsQueueEmpty(q)) q.QFront=0; q.QNumItems++; return 1;// them thanh cong } int EnQueue(QUEUE &q , int &outitem)//them 1 pt o cuoi Queue,co the lam Queue thay doi if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QFront]; q.QFront++; q.QNumItems ; if(q.QFront==q.QMax) q.QFront=0; if(IsQueueEmpty(q)) q.QFront=q.QRear=-1; /*Code by: Hondacodon*/ 4 return 1; } //kiem tra 1 phan tu o dau Quenue, ko lam thay doi Queue int QueueFront(const QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; //Queue rong ,ko lay ra dc outitem=q.QArray[q.QFront];//lay pt dau ra return 1; } //kiem tra 1 phan tu o CuoiQuenue, ko lam thay doi Queue int QueueRear(const QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QRear];//lay pt o cuoi ra return 1; } struct MYGRAPH { int N; int A[MAX][MAX]; int Previous[MAX]; }; /*Code by: Hondacodon*/ 5 void InputGraph(MYGRAPH &g,char *file,int &start ,int &end) { int b=0; FILE*f= fopen(file,"r"); fscanf(f,"%d",&g.N); fscanf(f,"%d",&start); fscanf(f,"%d",&end); int i=0,j=0; while(!feof(f)) { if(b%g.N==0 && b!=0) { i++; j=0; } fscanf(f,"%d",&g.A[i][j]); j++; b++; } for(i=0;i<g.N;i++) { g.Previous[i]=-1; } fclose(f); } int BFS(MYGRAPH &g,int start,int end) /*Code by: Hondacodon*/ 6 { int k[MAX]; QUEUE q; InitQueue(q,100); int flag=0; DeQueue(q,start); k[start]=0; int s; g.Previous[start] = -2; while(IsQueueEmpty(q)==0) { PushQueue(q,s); for(int i=0;i<g.N;i++) { if((g.A[s][i]!=0 && g.Previous[i]==-1) || (g.A[s][i]!=0 && k[s] +g.A[s][i]<k[i])) { PushQueue(q,i); g.Previous[i]=s; k[i]=k[s]+g.A [s][i]; } } } if (g.Previous [end]==-1) return 0; else return 1; /*Code by: Hondacodon*/ 7 } void PrintPath(MYGRAPH g,int end) { int i=end; printf("\nDuong di ngan nhat cua thuat toan BFS la`:\n"); printf("%d",end); i=g.Previous[i]; while(i!=-2) { printf("< %d",i); i=g.Previous[i]; } } void main() { MYGRAPH g; int start,end; InputGraph(g,"C:/input.txt",start ,end); printf("tim` duong di ngan' nhat Tu %d > %d\n",start,end); if(BFS(g,start,end)) { /*Code by: Hondacodon*/ 8 PrintPath(g,end); } printf("\n"); getch(); } Thuật Toán BFS LC BFS là thuật toán nâng cấp cua BFS tạo 1 file input.txt nhu thaut toan LCBFS // BFS.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include<stdio.h> #include<conio.h> #include<string.h> #define MAX 100 typedef struct Queue { int *QArray; int QMax; int QNumItems; int QFront; int QRear; }QUEUE; /*Code by: Hondacodon*/ 9 int InitQueue(QUEUE &q,int MaxItems) { q.QArray=new int [MaxItems]; if(q.QArray==NULL) return 0; q.QMax=MaxItems; q.QNumItems=0; q.QFront=q.QRear=-1; return 1; } int IsQueueEmpty(const QUEUE &q) { if(q.QNumItems==0) { return 1; } return 0; } int IsQueueFull(const QUEUE &q) { if(q.QNumItems==q.QMax) { return 1; } return 0; /*Code by: Hondacodon*/ 10 } int EnQueue(QUEUE &q , int newitem) { if(IsQueueFull(q)) { return 0; } q.QRear++; if(q.QRear==q.QMax) { q.QRear=0; } q.QArray[q.QRear]=newitem; if(IsQueueEmpty(q)) q.QFront=0; q.QNumItems++; return 1; } int DeQueue(QUEUE &q , int &outitem) { if(IsQueueEmpty(q)) return 0; outitem=q.QArray[q.QFront]; q.QFront++; q.QNumItems ; [...]... Mo File !! "); } fclose(f); } // -// //ham khoi tao void Khoi_Tao(DoThi &d) { int i; for (i=0;i . void PrintPath(MYGRAPH g,int end) { int i=end; printf("
Duong di ngan nhat cua thuat toan BFS la`:
"); printf("%d",end); i=g.Previous[i]; while(i!=-2). (i=0;i<d.n;i++) { d.ChiPhi[i] = 0; d.DaXet[i] = 0; d.LuuVet[i] = 0; } } // // //ham thuat toan UCS void ThanhDong_UCS(DoThi &d,int start,int end) { int u,i; int min; . d.a[u][i]; d.LuuVet[i] = u; } } } } } // // //ham tim tim duong dj tren thuat toan UCS int Path_ThanhDong_UCS(DoThi d,char*fn,int start,int end) { int i,khong=0; FILE