最短路径BFS算法matlab,BFS 算法求解最短路径

是根据  << Introduction to Algorithms>>里的相关算法实现的。

这个 BFS算法是这样的:

0818b9ca8b590ca3270a3433284dd417.png

代码实现如下:

1) queue.h

#ifndef QUEUE_H

#define QUEUE_H

#define MAX_QUEUE_SIZE 100

#ifndef bool

typedef int bool;

#endif

#ifndef true

#define true 1

#endif

#ifndef false

#define false 0

#endif

struct queue;

//typedef void (*queue_init)(struct queue *queue);

typedef bool (*queue_isEmpty)(struct queue *queue);

typedef bool (*queue_isFull)(struct queue *queue);

typedef bool (*queue_enqueue)(struct queue *queue, int data);

typedef bool (*queue_dequeue)(struct queue *queue, int *data);

typedef int (*queue_size)(struct queue *queue);

struct queue

{

int head;

int tail;

int size;

int data[MAX_QUEUE_SIZE];

//queue_init init;

queue_isEmpty isEmpty;

queue_isFull isFull;

queue_enqueue enqueue;

queue_dequeue dequeue;

};

void InitQueue(struct queue *queue);

#endif

queue.c:

#include "queue.h"

static bool isFull(struct queue *queue);

static bool isEmpty(struct queue *queue);

static bool enqueue(struct queue * queue, int data);

static bool dequeue(struct queue *queue, int *data);

void InitQueue(struct queue *queue)

{

queue->head = -1;

queue->tail = -1;

queue->size = 0;

//queue->init = init;

queue->enqueue = enqueue;

queue->dequeue = dequeue;

queue->isEmpty = isEmpty;

queue->isFull = isFull;

}

static bool enqueue(struct queue * queue, int data)

{

if(isFull(queue))

{

return false;

}

queue->tail = (queue->tail + 1) % MAX_QUEUE_SIZE;

queue->data[queue->tail] = data;

queue->size += 1;

return true;

}

static bool dequeue(struct queue *queue, int *data)

{

if(isEmpty(queue) == true)

{

return false;

}

queue->head = (queue->head + 1) % MAX_QUEUE_SIZE;

*data = queue->data[queue->head];

queue->size -= 1;

return true;

}

static bool isFull(struct queue *queue)

{

return queue->size == MAX_QUEUE_SIZE;

}

static bool isEmpty(struct queue *queue )

{

return queue->size == 0;

}

static int size(struct queue *queue)

{

return queue->size;

}

2) BFS代码:

#include "queue.h"

#include

#ifndef bool

typedef int bool;

#endif

#ifndef true

#define true 1

#endif

#ifndef false

#define false 0

#endif

enum

{

WHITE,

GRAY,

BLACK,

};

struct vertex

{

int id;

int color;

unsigned int dist;

int pi;

};

#define INFINITE ~0

#define MAX_VERTEX 100

#define NIL -1

int matrix[MAX_VERTEX][MAX_VERTEX];

struct vertex vertex[MAX_VERTEX];

void BFS(int s, int size, struct vertex * vertex, int matrx[][MAX_VERTEX])

{

vertex[s].color = GRAY;

vertex[s].dist = 0;

vertex[s].pi = NIL;

struct queue que;

InitQueue(&que);

que.enqueue(&que, s);

while(que.isEmpty(&que) == false)

{

int d;

que.dequeue(&que, &d);

int i,j;

for(i = 1; i <= size; i++)

{

if(matrix[d][i] == 1)

{

if(vertex[i].color == WHITE)

{

vertex[i].color = GRAY;

vertex[i].dist = vertex[d].dist + 1;

vertex[i].pi = i;

que.enqueue(&que, i);

}

}

}

vertex[d].color = BLACK;

}

int i;

for(i = 1; i <= size;i++)

{

printf(" %d to %d shortest dist: %d\n", i, s, vertex[i].dist);

}

}

int main(void)

{

int N;

scanf("%d", &N);

int i, j;

for(i = 1; i <= N; i++)

{

vertex[i].color = WHITE;

vertex[i].dist = INFINITE;

vertex[i].pi = NIL;

vertex[i].id = i;

for(j = 1; j <= N; j++)

{

int value;

scanf("%d", &value);

matrix[i][j] = value;

//vertex[i][j].color = WHITE;

//vertex[i][j].dist = INFINITE;

//vertex[i][j].pi = NIL;

}

}

BFS(4, 6, vertex, matrix);

return 0;

}

测试的用例如下:

0818b9ca8b590ca3270a3433284dd417.png

输出如下:

$ ./bfs < data.txt

1 to 4 shortest dist: 2

2 to 4 shortest dist: 2

3 to 4 shortest dist: 1

4 to 4 shortest dist: 0

5 to 4 shortest dist: 1

6 to 4 shortest dist: 1

data.txt为输入数据:

$ cat data.txt

6

0 1 1 0 0 0

1 0 1 0 0 0

1 1 0 1 0 0

0 0 1 0 1 1

0 0 0 1 0 1

0 0 0 1 1 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值