Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
21
Добавлен:
02.05.2014
Размер:
3.97 Кб
Скачать
#include <stdio.h>
#include <conio.h>
#include <alloc.h>

#define MAX 30000

struct List{
	int v;
   int w;
   struct List *next;
};
struct Graph{
   int h;
   int p;
   struct List *first;
   struct List *last;
}*G;

int N;
int V;

void Ford();
void BelmanKalab();
void PathFord(int );
void Menu();
void ListAdj();
void FreeList();

void main()
{
	Menu();
}

void BelmanKalab()
{
   int i,j,k;
   struct List *c;
	int **M=(int **)malloc(N*sizeof(int *));
   int *VK=(int *)malloc(N*sizeof(int ));
   int *VK_1=(int *)malloc(N*sizeof(int ));
   int *t,f=1;
   int *P=(int *)malloc(N*sizeof(int ));
   for(i=0;i<N;i++)
   	M[i]=(int *)malloc(N*sizeof(int));
   for(i=0;i<N;i++)
   	for(j=0;j<N;j++)
      	M[i][j]=(i==j)?0:MAX;
   for(i=0;i<N;i++){
   	c=G[i].first;
      while(c!=G[i].last){
      	M[i][c->v]=c->w;
         c=c->next;
      }
   }
   printf("Enter final vertex: ");
   scanf("%d",&V);
   V--;
   for(i=0;i<N;i++){
		VK_1[i]=M[i][V];
      P[i]=-1;
   }
   while(f){
   	for(i=0;i<N;i++)
      	VK[i]=MAX;
   	for(i=0;i<N;i++)
   		for(j=0;j<N;j++)
      		if(i!=j && VK[i]>VK_1[j]+M[i][j]){
         		VK[i]=VK_1[j]+M[i][j];
               P[i]=j;
            }
      VK[V]=0;
      for(i=0;i<N && VK[i]==VK_1[i];i++)
      	;
      f=(i==N)?0:1;
   	t=VK_1;
   	VK_1=VK;
   	VK=t;
   }
   for(i=0;i<N;i++){
   	printf("\nPath from %d to %d is ",i+1,V+1);
      if(VK_1[i]==MAX)
      	printf("not exist.");
      else {
      	for(k=i,j=0;j<N && P[k]!=-1 && k!=V;j++){
         	printf("->%d",k+1);
            k=P[k];
         }
         printf("->%d",V+1);
         printf(". It has length %d.",VK_1[i]);
      }
   }
   for(i=0;i<N;i++)
   	free(M[i]);
   free(P);
   free(M);
   free(VK);
   free(VK_1);
}

void Ford()
{
   int i,f=1;
   struct List *c;
   if(G==NULL)
   	return;
	for(i=0;i<N;i++){
		G[i].p=-1;
      G[i].h=MAX;
   }
   printf("Enter start vertex: ");
   scanf("%d",&V);
   G[--V].h=0;
   while(f){
   	f=0;
      for(i=0;i<N;i++){
      	c=G[i].first;
         while(c!=G[i].last){
         	if(G[c->v].h>G[i].h+c->w){
            	G[c->v].h=G[i].h+c->w;
               G[c->v].p=i;
               f=1;
            }
            c=c->next;
         }
      }
   }
   for(i=0;i<N;i++){
   	printf("\nPath from %d to %d is ",V+1,i+1);
      if(G[i].h==MAX)
      	printf("not exist.");
      else{
      	PathFord(i);
         printf(". It has length %d.",G[i].h);
      }
   }
}

void PathFord(int v)
{
	if(v!=V)
   	PathFord(G[v].p);
	printf("->%d",v+1);
}

void Menu()
{
   int i;
	char ch;
	clrscr();
	printf("1.To enter the graph\n");
	printf("2.Minimum path by Ford's algorithm\n");
	printf("3.Minimum path by Belman-Kalab's algorithm\n");
	printf("4.Exit\n");
	do
		ch=getch();
	while(ch<'0' || ch>'5');
	clrscr();
	switch (ch)
	{
		case '1': ListAdj();break;
		case '2': Ford();getch();break;
		case '3': BelmanKalab();getch();break;
		case '4': FreeList();return;
	}
	Menu();
}

void ListAdj()
{
	int i,v,w;
	struct List *c;
   if(G)
		FreeList();
   printf("Enter number of vertexes of graph: ");
   scanf("%d",&N);
   G=(struct Graph *)malloc(N*sizeof(struct Graph));
	printf("\n");
	for(i=0;i<N;i++){
		printf("%d->",i+1);
      G[i].first=(struct List*)malloc(sizeof(struct List));
      G[i].last=G[i].first;
      G[i].last->next=NULL;
      G[i].last->v=-1;
      scanf("%d",&v);
		while(v){
			scanf("%d",&w);
         G[i].last->v=v-1;
         G[i].last->w=w;
         G[i].last->next=(struct List*)malloc(sizeof(struct List));
         G[i].last=G[i].last->next;
         G[i].last->next=NULL;
      	G[i].last->v=-1;
	 		scanf("%d",&v);
		}
	}
}

void FreeList()
{
	struct List *c,*t;
	while(N--){
		c=G[N].first;
		while(c!=G[N].last){
			t=c->next;
			free(c);
			c=t;
		}
	}
   free(G);
}
Соседние файлы в папке Лабораторная работа №5