图的DFS、BFS及最短路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_41668995/article/details/85220793

输入: 
6 11
0 1 50
0 2 10
0 4 45
1 2 15
1 4 10
2 0 20
2 3 15
3 1 20
3 4 35
4 3 30
5 3 3

输出: 
0:1 2 4
1:2 4
2:0 3
3:1 4
4:3
5:3

The sequence of vertex names getting from Depth-First Search (from 'V1'):
V1 V2 V0 V4 V3 V5

The sequence of vertex names getting from Breadth-First Search (from 'V1'):
V1 V2 V4 V0 V3 V5

Shortest paths from v0 to each vertex are
V0 to V1   45
V0 to V2   10
V0 to V3   25
V0 to V4   45
V0 to V5   1000

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#define MaxSize 100
int v, e, e2, n;
int visit[MaxSize], num[MaxSize][MaxSize], trans[MaxSize], cost[MaxSize][MaxSize], distance[MaxSize];
typedef struct Node *ListPointer; 
struct Node{
    int data;
    int length;
    ListPointer next;  
};  
//邻接表 
struct Node List[MaxSize];
ListPointer head[MaxSize];
ListPointer p[MaxSize];

//队列
int front, rear;
void CreateQue(){
	memset(visit, 0, MaxSize);
	front=rear=0;
}

int IsFull(){
	if(rear==MaxSize-1) return 1;
	return 0;
}

int IsEmpty(){
	if(rear==front) return 1;
	return 0;
}

void Enqueue(int x){
	if(!IsFull()) trans[rear++]=x;
}

int Dequeue(){
	if(!IsEmpty())
	{
		return trans[front++];
	}
}


void create(){
	ListPointer s;
	int i, j, len;
	while(e2--)
	{	
		scanf("%d %d %d", &i, &j, &len);
		cost[i][j]=len;
    	s=(Node *)malloc(sizeof(Node));
    	s->data=j;
		s->length=len;
		p[i]->next=s;
		p[i]=s;
	}
	for(int k=0; k<v; k++)
    {
		p[k]->next=NULL;
	}	
}

void print(ListPointer head, int n){
	ListPointer q=head->next;
	int m=0;
	while(q!=NULL){
		printf("%d ", q->data);
		num[n][m++]=q->data;
		q=q->next;
	}
	num[n][m++]=-1;
	printf("\n");
}

void dfs(int i){
	visit[i]=1;
	printf("V%d ", i);
	for(int j=0; ; j++)
	{
		if(num[i][j]==-1) break;
		if(!visit[num[i][j]]){
			dfs(num[i][j]);
		}
	}
}

void bfs(int i){
	visit[i]=1;
	int x, flag=1;
	printf("V%d ", i);
	Enqueue(i);
	while(front!=rear){
		x=Dequeue();
		for(int j=0; ; j++)
		{
		    if(num[x][j]==-1) break;
		    if(!visit[num[x][j]]){
		    	printf("V%d ", num[x][j]);
		    	Enqueue(num[x][j]);
			    visit[num[x][j]]=1;
			}
		}
	}
}

int choose(int distance[], int n, int visit[]){
	int i, min, minpos;
	min=MaxSize;
	minpos=-1;
	for(i=0; i<n; i++){
		if(distance[i]<min && !visit[i]){
			min=distance[i];
			minpos=i;
		}
	}
	return minpos;
}

void shortestPath(int s, int cost[][MaxSize], int distance[], int n, int visit[]){
	int i, u, w;
	for(i=0; i<n; i++){
		visit[i]=0;
		distance[i]=cost[s][i];	
	}
	visit[s]=1;
	distance[s]=0;
	for(int i=0; i<n-2; i++){
		u=choose(distance, n, visit);
		visit[u]=1;
		for(w=0; w<n; w++)
		{
			if(!visit[w])
				if(distance[u]+cost[u][w]<distance[w])
					distance[w]=distance[u]+cost[u][w];
		}
	}
}

int main(){
	scanf("%d %d", &v, &e);
	for(int i=0; i<v; i++){
		for(int j=0; j<v; j++){
			cost[i][j]=1000;
		}
	}
	e2=e;
	for(int k=0; k<v; k++){
		head[k]=(Node *)malloc(sizeof(Node));
		head[k]->next=NULL;
		p[k]=head[k];
	}
	create();
	printf("\n"); 
	for(int i=0; i<v; i++){
		printf("%d:", i);
		print(head[i], i);
	}

	//dfs
	printf("\nThe sequence of vertex names getting from Depth-First Search (from 'V1'):\n");
	dfs(1);
	for(int i=0; i<v; i++)
	{
		if(visit[i]==0)
		{
			dfs(i);
		}
	}
	printf("\n");
	//bfs
	printf("\nThe sequence of vertex names getting from Breadth-First Search (from 'V1'):\n");
	CreateQue();
	bfs(1);
	for(int i=0; i<v; i++)
	{
		if(visit[i]==0)
		{
			bfs(i);
		}
	}
	printf("\n");

	//shortest path
	printf("\nShortest paths from v0 to each vertex are\n");
	memset(visit, 0, MaxSize);
	shortestPath(0, cost, distance, v, visit);
	for(int i=1; i<v; i++){
		printf("V0 to V%d   %d\n", i, distance[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41668995/article/details/85220793