写一程序实现图的广度优先搜索算法(BFS与DFS),并对输入的有向网或无向网输出遍历结果。
输入描述
文本文件“input.txt”中保存了一个有向网的输入,文件以-1结束。第一行表示该网的顶点数n(输入示例中为6)和弧数m(输入示例中为11);接下去m行是用(i,j,e)表示的弧,它表示从顶点i出发到顶点j的权值为e的弧。最后几行分别表示从这些顶点出发进行BFS,输出它们的结果。
输出描述
输出结果保存在文本文件“output.txt”中。对每个开始搜索的顶点,都有一行,该行表示从该顶点出发进行BFS的结果。
输入示例(输入图1中的有向网)
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
1
3
-1
输出示例
BFS From V1: V1 V4 V2 V3 V0
BFS From V3: V3 V4 V1 V2 V0
#include<stdio.h>
#include<stdlib.h>
#include<memory.h>
#include"Public.h"
struct node;
typedef struct node* nodePointer;
typedef struct node {
int vertex;
nodePointer next;
int weight;
};
void CreateGraph(FILE* fin, nodePointer graph[], int& n);
void PrintGraph(nodePointer graph[], int n);
void dfs(int v, nodePointer graph[], int n, short int visited[], FILE* fout);
void bfs(int v, nodePointer graph[], int n, short int visited[], FILE* fout);
typedef int element;
int IsEmptyQ_Sq(int front, int rear);
int IsFullQ_Sq(int front, int rear);
int AddQ_Sq(element item, element* queue, int front, int& rear);//进队列
int DeleteQ_Sq(element* queue, int& front, int rear, element& item);//出队列
void main() {
nodePointer graph[MAX_VERTICES];
int n = 0;
short int visited[MAX_VERTICES];
/*for (int i = 0; i < MAX_VERTICES; i++)visited[i] = FALSE;*/
memset(visited, 0, sizeof(short int) * MAX_VERTICES);
FILE* fin, * fout;
int v0;
fin = fopen("input.txt", "r");
fout = fopen("output.txt", "w");
CreateGraph(fin, graph, n);
while ((fscanf(fin, "%d", &v0), v0) != -1) {
bfs(v0, graph, n, visited, fout);
fprintf(fout, "\n");
memset(visited, 0, sizeof(short int) * MAX_VERTICES);
}
fclose(fout);
fclose(fin);
system("pause");
}
void CreateGraph(FILE* fin, nodePointer graph[], int& n) {
int numNodes, numEdges;
int p, q, w;
fscanf(fin, "%d %d", &numNodes, &numEdges);
for (int i = 0; i < numNodes; i++) {
MALLOC(graph[i], sizeof(struct node), nodePointer);
graph[i]->next = NULL;
}
n = numNodes;
for (int j = 0; j < numEdges; j++) {
fscanf(fin, "%d%d%d", &p, &q, &w);
nodePointer rear, temp;
rear = graph[p];
while (rear->next) {
rear = rear->next;
}
MALLOC(temp, sizeof(struct node), nodePointer);
temp->vertex = q;
temp->weight = w;
temp->next = NULL;
rear->next = temp;
}
for (int i = 0; i < numNodes; i++) {
graph[i] = graph[i]->next;
}
}
int flag;
void dfs(int v, nodePointer graph[], int n, short int visited[], FILE* fout) {
nodePointer w;
visited[v] = TRUE;
if (flag == 0) {
fprintf(fout, "DFS From V%d:V%d", v, v);
flag = 1;
}
else {
fprintf(fout, " V%d", v);
}
for (w = graph[v]; w; w = w->next)
if (!visited[w->vertex]) {
dfs(w->vertex, graph, n, visited, fout);
flag = 0;
}
}
void bfs(int v, nodePointer graph[], int n, short int visited[], FILE* fout) {
nodePointer w;
int Q[10];
int front = 0, rear = 0;
fprintf(fout, "BFS From V%d:V%d", v, v);
visited[v] = TRUE;
AddQ_Sq(v, Q, front, rear);
while (!IsEmptyQ_Sq(front, rear)) {
v = DeleteQ_Sq(Q, front, rear, v);
for (w = graph[v]; w; w = w->next)
if (!visited[w->vertex]) {
fprintf(fout, " V%d", w->vertex);
AddQ_Sq(w->vertex, Q, front, rear);
visited[w->vertex] = TRUE;
}
}
}
//四个有关队列操作的函数
int IsEmptyQ_Sq(int front, int rear) {
return (front == rear);
}
int IsFullQ_Sq(int front, int rear) {
return (front == (rear + 1) % MAX_QUEUE_SIZE);
}
int AddQ_Sq(element item, element* queue, int front, int& rear) {
rear = (rear + 1) % MAX_QUEUE_SIZE;
if (rear == front)return FALSE;
queue[rear] = item;
return TRUE;
}
int DeleteQ_Sq(element* queue, int& front, int rear, element& item) {
if (rear == front)return FALSE;
front = (front + 1) % MAX_QUEUE_SIZE;
item = queue[front];
return item;
}
Public.h
#ifndef _PUBLIC_H_
#define _PUBLIC_H_
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
/*以下是严蔚敏版《数据结构》书中的有关宏定义*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
/*以下是《Fundamentals of Data Structures in C (2nd Edition) 》书中的有关宏定义*/
#define MAX_SIZE 100 //Chapter 1
#define SWAP(x,y,t) ((t) = (x),(x) = (y),(y) = (t)) //Chapter 1
#define MAX_TERMS 101 //Chapter 2
#define MAX_POLYS 15 //Chapter 2
#define MAX_STACK_SIZE 100 //Chapter 3
#define MAX_QUEUE_SIZE 100 //Chapter 3
#define MEMORY_SIZE 100 //Chapter 3
#define MAX_STACKS 10 //Chapter 3
#define MAX_QUEUES 10 //Chapter 3
#define IS_EMPTY(first) (!(first)) //Chapter 4
#define IS_FULL(temp) (!(temp)) //Chapter 4
#define MAX_ELEMENTS 200 //Chapter 5
#define HEAP_FULL(n) (n == MAX_ELEMENTS - 1) //Chapter 5
#define HEAP_EMPTY(n) (!n) //Chapter 5
#define MAX_VERTICES 50 //Chapter 6
#define MALLOC(p,s,type)\
if (!((p) = (type)malloc(s)))\
{\
fprintf(stderr,"Insufficient memory");\
exit(EXIT_FAILURE);\
}
#define REALLOC(p,s)\
if (!(realloc((void*)p,s)))\
{\
fprintf(stderr,"Insufficient memory");\
exit(EXIT_FAILURE);\
}
#define CALLOC(p,n,s,type)\
if (!((p) = (type)calloc(n,s)))\
{\
fprintf(stderr,"Insufficient memory");\
exit(EXIT_FAILURE);\
}
#define FREE(p)\
if (!(p))\
{ \
free(p);p = NULL;\
}
#endif