算法分析与设计
来源:互联网 发布:js根据标签name 编辑:程序博客网 时间:2024/04/29 11:28
/* 背包问题的贪心法算法*/
#include<stdio.h>
#include<stdlib.h>
/* 线性表p和w中,按p[i]/w[i]的降序分别存放物体的价格(单位为元)和重量(单位为公斤);*/
/* m是背包能放的物体总重量,n是物体件数。x存放解向量*/
double knapSack(double* p, double* w, double* x ,double m, int n) {
int i = 0;
double s = 0;
for (i = 0; i < n; i++)
x[i] = 0;
i = 0;
while (i < n && w[i] < m) {
m -= w[i];
s += p[i];
x[i] = 1;
i++;
}
if (i < n && m > 0) {
s += p[i]*m/w[i];
x[i] = m/w[i];
i++;
}
return s;
}
int main() {
double m = 0, s = 0, temp = 0;
double *p, *w, *x;
int n = 0, i = 0, flag = 1;
printf("please input the maximum weight of the bag:/nm = ");
scanf("%f", &m);
printf("please input the number of objects:/nn = ");
scanf("%d", &n);
p = (double*)malloc(n*sizeof(double));
printf("please input the prices of all the objects:/n");
for (i = 0; i < n; i++)
scanf("%f", p+i);
w = (double*)malloc(n*sizeof(double));
printf("please input the weight of all the objects:/n");
for (i = 0; i < n; i++)
scanf("%f", w+i);
/* 线性表p和w中,按p[i]/w[i]的降序分别存放物体的价格(单位为元)和重量(单位为公斤);*/
while (flag != 0) {
flag = 0;
for (i = 0; i < n-1; i++) {
if (p[i]/w[i] < p[i+1]/w[i+1]) {
temp = p[i];
p[i] = p[i+1];
p[i+1] = temp;
temp = w[i];
w[i] = w[i+1];
w[i+1] = temp;
flag = 1;
}
}
}
x = (double*)malloc(n*sizeof(double));
s = knapSack(p,w,x,m,n);
printf("the max value is %f/n",s);/*输出*/
for(i = 0; i < n; i++) {
if(x[i] > 0) {
printf("the x: %f",x[i]);
printf(" the p: %f",p[i]);
printf(" the w: %f/n",w[i]);
}
}
return 0;
}
/* 用动态规划法求组和数的算法*/
#include<stdio.h>
int combinat(int m, int n) {
int i, j;
int mat[100][100];
if (n == 0 || m == n)
return 1;
else {
for (j = 0; j < n; j++) {
mat[0][j] = 1;
for (i = 1; i <= m-n; i++)
if (j == 0)
mat[i][j] = i+1;
else
mat[i][j] = mat[i-1][j] + mat[i][j-1];
}
return (mat[m-n][n-1]); /* 返回计算得到的结果 */
}
}
int main() {
printf("m=%d ,n=%d ,combinat=%d/n", 10, 2, combinat(10, 2));
printf("m=%d ,n=%d ,combinat=%d/n", 5, 3, combinat(5, 3));
printf("m=%d ,n=%d ,combinat=%d/n", 6, 1, combinat(6, 1));
printf("m=%d ,n=%d ,combinat=%d/n", 4, 2, combinat(4, 2));
return 0;
}
/* 用回溯法解决骑士周游问题的算法*/
#include<stdio.h>
#include<stdlib.h>
#define NUM 6 /*方阵为n*n的*/
#define MAXNUM NUM * NUM /* 栈中最大元素个数 */
struct Node {
int x, y, d;
};
typedef struct Node DataType;
struct SeqStack { /* 顺序栈类型定义 */
int t; /* 指示栈顶位置 */
DataType s[MAXNUM];
};
typedef struct SeqStack *PSeqStack; /* 顺序栈类型的指针类型 */
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack==NULL)
printf("Out of space!! /n");
else
pastack->t = -1;
return pastack;
}
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}
/* 在栈中压入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Overflow! /n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 删除栈顶元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!/n" );
else
pastack->t--;
}
/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
DataType top_seq( PSeqStack pastack ) {
return (pastack->s[pastack->t]);
}
int area[NUM][NUM]; /*n*n方阵*/
/*相对于位置(i,j)八个方向的增量*/
int direction[8][2] = {-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1,2,1,1,2};
int inarea(int x, int y) { /*坐标(x,y)在区域内*/
return x >= 0 && x < NUM && y >= 0 && y < NUM;
}
void init_area(int n, int a[][n]) {
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = 0;/*初始化,0表示未游历过*/
}
void path(int x0, int y0) { /*初始位置为(x0,y0)*/
int i, j, k;
int g, h;
int step;/*所游历的区域数*/
DataType element;
PSeqStack st = createEmptyStack_seq( );
init_area(NUM, area);
area[x0][y0] = 1; /*1表示此位置游历过*/
element.x = x0;
element.y = y0;
element.d = -1;
push_seq(st, element); /* 初始点进栈 */
step = 1;
do {
element = top_seq(st);
pop_seq(st);
i = element.x;
j = element.y;
area[i][j] = 1;
for (k = element.d+1; k < 8 && step < MAXNUM; k++) {/* 依次试探每个方向 */
g = i + direction[k][0];
h = j + direction[k][1];
if (inarea(g,h) == 1 && area[g][h] == 0) {
area[g][h] = 1;
step++;
element.x = i;
element.y = j;
element.d = k;
push_seq(st, element); /* 进栈 */
i = g; /* 下一点转换成当前点 */
j = h;
k = -1;
}
}
if (step < MAXNUM) {
area[i][j] = 0;
step--;
}
} while(!isEmptyStack_seq(st) && step < MAXNUM);
if (step < MAXNUM)
printf("cannot find the path !/n");
else {
printf("the reverse path is :/n");
printf(" %d , %d/n", g, h);
while(!isEmptyStack_seq(st)) {
element = top_seq(st);
pop_seq(st);
printf(" %d , %d/n", element.x, element.y);
}
}
}
int main() {
int x0, y0;
printf("please input the position (x0,y0) /nx0 = ");
scanf("%d", &x0);
printf("y0 = ");
scanf("%d", &y0);
path(x0, y0);
getchar(); getchar();
return 0;
}
//-----
/* 0/1背包问题的回溯法算法*/
#include<stdio.h>
#define NUM 4
/* m 为总容量,w 为各种物品的数量,p 为各种物品的价值,n 为物品种类数。
结果存放在 x。i 为递归深度 */
void solve(double m, int n, double p[], double w[], int x[], int i){
static double max = 0;
static double totalweight = 0;
static int x1[NUM];
if (i == n){
int i;
double sum;
for (sum = 0, i = 0; i < n; i++)
sum += x1[i] * p[i];
if (sum > max) {
max = sum;
for (i = 0; i < n; i++) x[i] = x1[i];
}
return;
}
x1[i] = 0;/* 将x1[i]置为0 */
solve(m, n, p, w, x, i + 1);
if (totalweight + w[i] <= m){
x1[i] = 1;/* 将x1[i]置为1 */
totalweight += w[i];
solve(m, n, p, w, x, i + 1);
totalweight -= w[i];
}
}
#define NUM 4
double m = 15;
double w[] = {2, 4, 6, 9};
double p[] = {10, 10, 12, 18};
int x[NUM];
int main(){
int i;
solve(m, NUM, p, w, x, 0);
for (i = 0; i < NUM; i++)
printf("x%d = %d ", i+1, x[i]);
getchar();
return 0;
}
//-----
/* 0/1背包问题的回溯法算法*/
#include<stdio.h>
#define NUM 4
/* m 为总容量,w 为各种物品的数量,p 为各种物品的价值,n 为物品种类数。
结果存放在 x。i 为递归深度 */
void solve(double m, int n, double p[], double w[], int x[], int i){
static double max = 0;
static double totalweight = 0;
static int x1[NUM];
if (i == n){
int i;
double sum;
for (sum = 0, i = 0; i < n; i++)
sum += x1[i] * p[i];
if (sum > max) {
max = sum;
for (i = 0; i < n; i++) x[i] = x1[i];
}
return;
}
x1[i] = 0;/* 将x1[i]置为0 */
solve(m, n, p, w, x, i + 1);
if (totalweight + w[i] <= m){
x1[i] = 1;/* 将x1[i]置为1 */
totalweight += w[i];
solve(m, n, p, w, x, i + 1);
totalweight -= w[i];
}
}
#define NUM 4
double m = 15;
double w[] = {2, 4, 6, 9};
double p[] = {10, 10, 12, 18};
int x[NUM];
int main(){
int i;
solve(m, NUM, p, w, x, 0);
for (i = 0; i < NUM; i++)
printf("x%d = %d ", i+1, x[i]);
getchar();
return 0;
}
/* 0/1背包问题的分支定界法算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXNUM 100
struct node{
int step;
double price;
double weight;
double max, min;
unsigned long po;
};
typedef struct node DataType;
struct SeqQueue { /* 顺序队列类型定义 */
int f, r;
DataType q[MAXNUM];
};
typedef struct SeqQueue *PSeqQueue;
PSeqQueue createEmptyQueue_seq( void ) {
PSeqQueue paqu;
paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
if (paqu == NULL)
printf("Out of space!! /n");
else
paqu->f = paqu->r = 0;
return paqu;
}
int isEmptyQueue_seq( PSeqQueue paqu ) {
return paqu->f == paqu->r;
}
/* 在队列中插入一元素x */
void enQueue_seq( PSeqQueue paqu, DataType x ) {
if( (paqu->r + 1) % MAXNUM == paqu->f )
printf( "Full queue./n" );
else {
paqu->q[paqu->r] = x;
paqu->r = (paqu->r + 1) % MAXNUM;
}
}
/* 删除队列头元素 */
void deQueue_seq( PSeqQueue paqu ) {
if( paqu->f == paqu->r )
printf( "Empty Queue./n" );
else
paqu->f = (paqu->f + 1) % MAXNUM;
}
/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq( PSeqQueue paqu ) {
return (paqu->q[paqu->f]);
}
/* 物品按性价比从新排序*/
void sort(int n, double p[], double w[]){
int i, j;
for (i = 0; i < n-1; i++)
for (j = i; j < n-1; j++) {
double a = p[j]/w[j];
double b = p[j+1]/w[j+1];
if (a < b) {
double temp = p[j];
p[j] = p[j+1];
p[j+1] = temp;
temp = w[j];
w[j] = w[j+1];
w[j+1] = temp;
}
}
}
/* 求最大可能值*/
double up(int k, double m, int n, double p[], double w[]){
int i = k;
double s = 0;
while (i < n && w[i] < m) {
m -= w[i];
s += p[i];
i++;
}
if (i < n && m > 0) {
s += p[i] * m / w[i];
i++;
}
return s;
}
/* 求最小可能值*/
double down(int k, double m, int n, double p[], double w[]){
int i = k;
double s = 0;
while (i < n && w[i] <= m) {
m -= w[i];
s += p[i];
i++;
}
return s;
}
/* 用队列实现分支定界算法*/
double solve(double m, int n, double p[], double w[], unsigned long* po){
double min;
PSeqQueue q = createEmptyQueue_seq();
DataType x = {0,0,0,0,0,0};
sort(n, p, w);
x.max = up(0, m, n, p, w);
x.min = min = down(0, m, n, p, w);
if (min == 0) return -1;
enQueue_seq(q, x);
while (!isEmptyQueue_seq(q)){
int step;
DataType y;
x = frontQueue_seq(q);
deQueue_seq(q);
if (x.max < min) continue;
step = x.step + 1;
if (step == n+1) continue;
y.max = x.price + up(step, m - x.weight, n, p, w);
if (y.max >= min) {
y.min = x.price + down(step, m-x.weight, n, p, w);
y.price = x.price;
y.weight = x.weight;
y.step = step;
y.po = x.po << 1;
if (y.min >= min) {
min = y.min;
if (step == n) *po = y.po;
}
enQueue_seq(q, y);
}
if (x.weight + w[step-1] <= m) {
y.max = x.price + p[step-1] +
up(step, m-x.weight-w[step-1], n, p, w);
if (y.max >= min) {
y.min = x.price + p[step-1] +
down(step, m-x.weight-w[step-1], n, p, w);
y.price = x.price + p[step-1];
y.weight = x.weight + w[step-1];
y.step = step;
y.po = (x.po << 1) + 1;
if (y.min >= min) {
min = y.min;
if (step == n) *po = y.po;
}
enQueue_seq(q, y);
}
}
}
return min;
}
#define n 4
double m = 15;
double p[n] = {10, 10, 12, 18};
double w[n] = {2, 4, 6, 9};
int main() {
int i;
double d;
unsigned long po;
d = solve(m, n, p, w, &po);
if (d == -1)
printf("No solution!/n");
else {
for (i = 0; i < n; i++)
printf("x%d is %d/n", i + 1, ((po & (1<<(n-i-1))) != 0));
printf("The max weight is %f/n", d);
}
getchar();
return 0;
}
第八章 图
图的表示:邻接矩阵, 邻接表
/* 用图邻接矩阵表示实现的一些基本运算*/
#define MAXVEX 20
#define NON -1
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
VexType vexs[MAXVEX]; /* 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
int firstVertex(GraphMatrix* pgraph) {
return pgraph->n == 0 ? NON : 0;
}
int nextVertex(GraphMatrix* pgraph,int n) {
return n == pgraph->n-1 ? NON : n+1;
}
int firstAdjacent(GraphMatrix* pgraph, int i) {
int k;
for (k = 0; k < pgraph->n; k++)
if(pgraph->arcs[i][k] != 0) return k;
return NON;
}
int nextAdjacent(GraphMatrix* pgraph, int i, int j) {
int k;
for (k = j+1; k < pgraph->n; k++)
if (pgraph->arcs[i][k] != 0) return k;
return NON;
}
int main() {
return 0; /* 用图邻接表表示实现的一些基本运算*/
#include<stdio.h>
#define MAXVEX 20
#define NON -1
typedef char VexType;
typedef float AdjType;
typedef int Vertex;
struct EdgeNode;
typedef struct EdgeNode * PEdgeNode;
typedef struct EdgeNode * EdgeList;
struct EdgeNode {
int endvex; /* 相邻顶点字段 */
AdjType weight; /* 边的权,非带权图可以省略 */
PEdgeNode nextedge; /* 链字段 */
}; /* 边表中的结点 */
typedef struct {
VexType vertex; /* 顶点信息 */
EdgeList edgelist; /* 边表头指针 */
} VexNode; /* 顶点表中的结点 */
typedef struct {
int n; /* 图的顶点个数 */
VexNode vexs[MAXVEX];
} GraphList;
int firstVertex(GraphList* pgraph) {
return pgraph->n == 0 ? NON : 0;
}
int nextVertex(GraphList* pgraph,int n) {
return n == pgraph->n-1 ? NON : n+1;
}
int firstAdjacent(GraphList* pgraph, int i) {
if (pgraph->vexs[i].edgelist != NULL)
return pgraph->vexs[i].edgelist->endvex;
else return NON;
}
int nextAdjacent(GraphList* pgraph, int i, int j) {
PEdgeNode p;
for (p = pgraph->vexs[i].edgelist; p != NULL; p = p->nextedge)
if (p->endvex == j) {
if (p->nextedge != NULL)
return p->nextedge->endvex;
else
return NON;
}
return NON;
}
int main() {
return 0;
}
/* 用邻接矩阵表示的图的广度优先周游算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 6
#define MAX 0
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX]; 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
#define MAXNUM 8/* 队列中最大元素个数 */
typedef int DataType;
struct SeqQueue { /* 顺序队列类型定义 */
int f, r;
DataType q[MAXNUM];
};
typedef struct SeqQueue *PSeqQueue; /* 顺序队列类型的指针类型 */
PSeqQueue createEmptyQueue_seq( void ) {
PSeqQueue paqu;
paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
if (paqu == NULL)
printf("Out of space!! /n");
else
paqu->f = paqu->r = 0;
return paqu;
}
int isEmptyQueue_seq( PSeqQueue paqu ) {
return paqu->f == paqu->r;
}
/* 在队列中插入一元素x */
void enQueue_seq( PSeqQueue paqu, DataType x ) {
if( (paqu->r + 1) % MAXNUM == paqu->f )
printf( "Full queue./n" );
else {
paqu->q[paqu->r] = x;
paqu->r = (paqu->r + 1) % MAXNUM;
}
}
/* 删除队列头部元素 */
void deQueue_seq( PSeqQueue paqu ) {
if( paqu->f == paqu->r )
printf( "Empty Queue./n" );
else
paqu->f = (paqu->f + 1) % MAXNUM;
}
/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq( PSeqQueue paqu ) {
return (paqu->q[paqu->f]);
}
#define NON -1
int firstVertex(GraphMatrix* pgraph) {
if (pgraph->n == 0)
return NON;
else return 0;
}
int nextVertex(GraphMatrix* pgraph,int n) {
if (n == pgraph->n-1)
return NON;
else return n + 1;
}
int firstAdjacent(GraphMatrix* pgraph, int i) {
int k;
for (k = 0; k < pgraph->n; k++)
if(pgraph->arcs[i][k] != 0) return k;
return NON;
}
int nextAdjacent(GraphMatrix* pgraph, int i, int j) {
int k;
for (k = j+1; k < pgraph->n; k++)
if (pgraph->arcs[i][k] != 0) return k;
return NON;
}
typedef int Vertex;
#define TRUE 1
#define FALSE 0
int visited[MAXVEX];
void bfs ( GraphMatrix* g , Vertex v );
void bft ( GraphMatrix* g ) {
Vertex v ;
for ( v =firstVertex ( g ) ; v != NON ; v = nextVertex ( g , v ) )
if ( visited[v] == FALSE ) bfs ( g , v ) ;
}
void bfs ( GraphMatrix* g , Vertex v ) {
Vertex v1 , v2;
PSeqQueue q = createEmptyQueue_seq(); /* 队列元素的类型为Vertex* */
enQueue_seq ( q, v ) ;
printf("%d ", v);
visited[v] = TRUE ;
while ( !isEmptyQueue_seq(q) ) {
v1 = frontQueue_seq ( q ) ;
deQueue_seq ( q );
v2 = firstAdjacent ( g, v1 );
while ( v2!= NON ) {
if ( visited[v2] == FALSE ) {
enQueue_seq ( q, v2 );
visited[v2] = TRUE ;
printf("%d ", v2);
}
v2 = nextAdjacent ( g, v1 , v2 ) ;
}
}
}
GraphMatrix graph = {
6,
{{0,10,MAX,MAX,19,21},
{10,0,5,6,MAX,11},
{MAX,5,0,6,MAX,MAX},
{MAX,6,6,0,18,14},
{19,MAX,MAX,18,0,33},
{21,11,MAX,14,33,0}
}
};
int main(){
bft(&graph);
return 0;
}
/* 用邻接表表示的图的广度优先周游算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 20
typedef struct EdgeNode EdgeNode;
typedef struct EdgeNode * PEdgeNode;
typedef struct EdgeNode * EdgeList;
struct EdgeNode {
int endvex; /* 相邻顶点字段 */
PEdgeNode nextedge; /* 链字段 */
}; /* 边表中的结点 */
typedef struct {
/*VexType vertex;*/ /* 顶点信息 */
EdgeList edgelist; /* 边表头指针 */
} VexNode; /* 顶点表中的结点 */
typedef struct {
int n; /* 图的顶点个数 */
VexNode vexs[MAXVEX];
} GraphList;
/* 边的插入算法*/
void insert(GraphList* p,int a,int b) {
EdgeList pp;
PEdgeNode temp;
temp = (PEdgeNode)malloc(sizeof(EdgeNode));
temp->endvex = b;
temp->nextedge = NULL;
pp = p->vexs[a].edgelist;
if (pp == NULL)
p->vexs[a].edgelist = temp;
else {
while(pp->nextedge != NULL)
pp = pp->nextedge;
pp->nextedge = temp;
}
}
/* 实例邻接表的构造 */
GraphList* makeList() {
GraphList* p;
int i;
p = (GraphList*)malloc(sizeof(GraphList));
p->n = 8;
for (i = 0; i < p->n; i++)
p->vexs[i].edgelist = NULL;
insert(p,0,1);
insert(p,0,2);
insert(p,1,3);
insert(p,1,4);
insert(p,2,5);
insert(p,2,6);
insert(p,3,7);
insert(p,4,7);
insert(p,5,6);
return p;
}
#define MAXNUM 20/* 队列中最大元素个数 */
typedef int DataType;
struct SeqQueue { /* 顺序队列类型定义 */
int f, r;
DataType q[MAXNUM];
};
typedef struct SeqQueue *PSeqQueue; /* 顺序队列类型的指针类型 */
PSeqQueue createEmptyQueue_seq( void ) {
PSeqQueue paqu;
paqu = (PSeqQueue)malloc(sizeof(struct SeqQueue));
if (paqu == NULL)
printf("Out of space!! /n");
else
paqu->f = paqu->r = 0;
return paqu;
}
int isEmptyQueue_seq( PSeqQueue paqu ) {
return paqu->f == paqu->r;
}
/* 在队列中插入一元素x */
void enQueue_seq( PSeqQueue paqu, DataType x ) {
if( (paqu->r + 1) % MAXNUM == paqu->f )
printf( "Full queue./n" );
else {
paqu->q[paqu->r] = x;
paqu->r = (paqu->r + 1) % MAXNUM;
}
}
/* 删除队列头部元素 */
void deQueue_seq( PSeqQueue paqu ) {
if( paqu->f == paqu->r )
printf( "Empty Queue./n" );
else
paqu->f = (paqu->f + 1) % MAXNUM;
}
/* 对非空队列,求队列头部元素 */
DataType frontQueue_seq( PSeqQueue paqu ) {
return (paqu->q[paqu->f]);
}
#define NON -1
int firstVertex(GraphList* pgraph) {
if (pgraph->n == 0)
return NON;
else return 0;
}
int nextVertex(GraphList* pgraph,int n) {
if (n == pgraph->n - 1)
return NON;
else return n+1;
}
int firstAdjacent(GraphList* pgraph, int i) {
if (pgraph->vexs[i].edgelist != NULL)
return pgraph->vexs[i].edgelist->endvex;
else return NON;
}
int nextAdjacent(GraphList* pgraph, int i, int j) {
PEdgeNode p;
for (p = pgraph->vexs[i].edgelist; p != NULL; p = p->nextedge)
if (p->endvex == j) {
if (p->nextedge != NULL)
return p->nextedge->endvex;
else
return NON;
}
return NON;
}
typedef int Vertex;
#define TRUE 1
#define FALSE 0
int visited[MAXVEX];
void bfs ( GraphList* g , Vertex v );
void bft ( GraphList* g ) {
Vertex v ;
for ( v = firstVertex ( g ) ; v != NON ; v = nextVertex ( g , v ) )
if ( visited[v] == FALSE ) bfs ( g , v ) ;
}
void bfs ( GraphList* g , Vertex v ) {
Vertex v1 , v2;
PSeqQueue q = createEmptyQueue_seq ( ) ; /* 队列元素的类型为Vertex* */
enQueue_seq ( q ,v ) ;
printf("%d ",v);
visited[v] = TRUE ;
while ( !isEmptyQueue_seq(q) ) {
v1 = frontQueue_seq ( q ) ;
deQueue_seq ( q );
v2 = firstAdjacent ( g ,v1 );
while ( v2 != NON ) {
if ( visited[v2] == FALSE ) {
enQueue_seq ( q, v2 );
visited[v2] = TRUE ;
printf("%d ",v2);
}
v2 = nextAdjacent ( g , v1 , v2 ) ;
}
}
}
int main(){
GraphList* p = makeList();
bft(p);
return 0;
}
/* 用邻接矩阵表示的图的深度优先周游的递归算法*/
#include<stdio.h>
#define MAXVEX 6
#define MAX 0
#define NON -1
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX]; 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
int firstVertex(GraphMatrix* pgraph) {
if (pgraph->n == 0)
return NON;
else return 0;
}
int nextVertex(GraphMatrix* pgraph,int n) {
if (n == pgraph->n-1)
return NON;
else return n+1;
}
int firstAdjacent(GraphMatrix* pgraph, int i) {
int k;
for (k = 0; k < pgraph->n; k++)
if (pgraph->arcs[i][k] != 0) return k;
return NON;
}
int nextAdjacent(GraphMatrix* pgraph, int i, int j) {
int k;
for(k=j+1; k<pgraph->n; k++)
if(pgraph->arcs[i][k]!=0) return k;
return NON;
}
typedef int Vertex;
#define TRUE 1
#define FALSE 0
int visited[MAXVEX];
void dfs ( GraphMatrix* g , Vertex v );
void dft ( GraphMatrix* g ) {
Vertex v ;
for ( v =firstVertex ( g ) ; v != NON ; v = nextVertex ( g , v ) )
if ( visited[v] == FALSE ) dfs ( g , v ) ;
}
void dfs ( GraphMatrix* g , Vertex v ) {
Vertex v1;
visited[v] = TRUE ;
printf("%d ",v);
for ( v1 = firstAdjacent(g , v); v1 != NON ; v1=nextAdjacent(g ,v, v1) )
if(visited[v1]==FALSE) dfs ( g ,v1 );
}
GraphMatrix graph = {
6,
{{0,10,MAX,MAX,19,21},
{10,0,5,6,MAX,11},
{MAX,5,0,6,MAX,MAX},
{MAX,6,6,0,18,14},
{19,MAX,MAX,18,0,33},
{21,11,MAX,14,33,0}
}
};
int main(){
dft(&graph);
return 0;
}
/* 用邻接矩阵表示的图的深度优先周游的非递归算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 6
#define MAX 0
#define NON -1
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX]; 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
int firstVertex(GraphMatrix* pgraph) {
if(pgraph->n == 0)
return NON;
else return 0;
}
int nextVertex(GraphMatrix* pgraph,int n) {
if(n==pgraph->n-1)
return NON;
else return n+1;
}
int firstAdjacent(GraphMatrix* pgraph, int i) {
int k;
for(k=0;k<pgraph->n;k++)
if(pgraph->arcs[i][k]!=0) return k;
return NON;
}
int nextAdjacent(GraphMatrix* pgraph, int i, int j) {
int k;
for(k=j+1; k<pgraph->n; k++)
if(pgraph->arcs[i][k]!=0) return k;
return NON;
}
typedef int Vertex;
#define TRUE 1
#define FALSE 0
typedef struct {
Vertex v;
Vertex k;
} DataType;
#define MAXNUM 20 /* 栈中最大元素个数 */
struct SeqStack { /* 顺序栈类型定义 */
int t; /* 指示栈顶位置 */
DataType s[MAXNUM];
};
typedef struct SeqStack *PSeqStack; /* 顺序栈类型的指针类型 */
/*创建一个空栈;为栈结构申请空间,并将栈顶变量赋值为-1*/
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack == NULL)
printf("Out of space!! /n");
else
pastack->t = -1;
return (pastack);
}
/*判断pastack所指的栈是否为空栈,当pastack所指的栈为空栈时,则返回1,否则返回0*/
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}
/* 在栈中压入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Overflow! /n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 删除栈顶元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!/n" );
else
pastack->t--;
}
/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
DataType top_seq( PSeqStack pastack ) {
return pastack->s[pastack->t];
}
int visited[MAXVEX];
void dfs ( GraphMatrix* g , Vertex v );
void dft ( GraphMatrix* g ) {
Vertex v ;
for ( v = firstVertex ( g ) ; v != NON ; v = nextVertex ( g , v ) )
if ( visited[v] == FALSE ) dfs ( g , v ) ;
}
void dfs ( GraphMatrix* g , Vertex v ) {
DataType element;
Vertex v1,v2;
PSeqStack s ;
s = createEmptyStack_seq ( ) ;
element.v = v;
element.k = firstAdjacent(g, v);
push_seq ( s, element) ;
printf("%d ", v);
visited[v] = TRUE ;
while ( !isEmptyStack_seq(s) ) {
element = top_seq ( s ) ;
pop_seq ( s );
v1 = element.v;
v2 = element.k;
while (v2 != NON ) {
if ( visited[v2] == FALSE ) {
element.v = v1;
element.k = v2;
push_seq ( s, element);
visited[v2] = TRUE ;
printf("%d ", v2);
v1 = v2;
v2 = firstAdjacent(g, v1);
}
else v2 = nextAdjacent(g , v1 , v2) ;
}
}
}
GraphMatrix graph = {
6,
{{0,10,MAX,MAX,19,21},
{10,0,5,6,MAX,11},
{MAX,5,0,6,MAX,MAX},
{MAX,6,6,0,18,14},
{19,MAX,MAX,18,0,33},
{21,11,MAX,14,33,0}
}
};
int main(){
dft(&graph);
return 0;
}
/* 用邻接表表示的图的深度优先周游的非递归算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 20
typedef struct EdgeNode EdgeNode;
typedef struct EdgeNode * PEdgeNode;
typedef struct EdgeNode * EdgeList;
struct EdgeNode {
int endvex; /* 相邻顶点字段 */
PEdgeNode nextedge; /* 链字段 */
}; /* 边表中的结点 */
typedef struct {
/*VexType vertex;*/ /* 顶点信息 */
EdgeList edgelist; /* 边表头指针 */
} VexNode; /* 顶点表中的结点 */
typedef struct {
int n; /* 图的顶点个数 */
VexNode vexs[MAXVEX];
} GraphList;
/* 边的插入算法*/
void insert(GraphList* p,int a,int b) {
EdgeList pp;
PEdgeNode temp;
temp = (PEdgeNode)malloc(sizeof(EdgeNode));
temp->endvex = b;
temp->nextedge = NULL;
pp = p->vexs[a].edgelist;
if (pp == NULL)
p->vexs[a].edgelist = temp;
else {
while (pp->nextedge != NULL)
pp = pp->nextedge;
pp->nextedge = temp;
}
}
/* 实例邻接表的构造 */
GraphList* makeList() {
GraphList* p;
int i;
p = (GraphList*)malloc(sizeof(GraphList));
p->n = 8;
for (i = 0; i < p->n; i++)
p->vexs[i].edgelist = NULL;
insert(p,0,1);
insert(p,0,2);
insert(p,1,3);
insert(p,1,4);
insert(p,2,5);
insert(p,2,6);
insert(p,3,7);
insert(p,4,7);
insert(p,5,6);
return p;
}
#define NON -1
int firstVertex(GraphList* pgraph) {
if(pgraph->n == 0)
return NON;
else return 0;
}
int nextVertex(GraphList* pgraph,int n) {
if (n == pgraph->n - 1)
return NON;
else return n+1;
}
int firstAdjacent(GraphList* pgraph, int i) {
if(pgraph->vexs[i].edgelist != NULL)
return pgraph->vexs[i].edgelist->endvex;
else return NON;
}
int nextAdjacent(GraphList* pgraph, int i, int j) {
PEdgeNode p;
for(p = pgraph->vexs[i].edgelist; p != NULL; p = p->nextedge)
if(p->endvex==j) {
if(p->nextedge!=NULL)
return p->nextedge->endvex;
else
return NON;
}
return NON;
}
typedef int Vertex;
#define TRUE 1
#define FALSE 0
typedef struct {
Vertex v;
Vertex k;
} DataType;
#define MAXNUM 20 /* 栈中最大元素个数 */
struct SeqStack { /* 顺序栈类型定义 */
int t; /* 指示栈顶位置 */
DataType s[MAXNUM];
};
typedef struct SeqStack *PSeqStack; /* 顺序栈类型的指针类型 */
/*创建一个空栈;为栈结构申请空间,并将栈顶变量赋值为-1*/
PSeqStack createEmptyStack_seq( void ) {
PSeqStack pastack;
pastack = (PSeqStack)malloc(sizeof(struct SeqStack));
if (pastack == NULL)
printf("Out of space!/n");
else
pastack->t = -1;
return (pastack);
}
/*判断pastack所指的栈是否为空栈,当pastack所指的栈为空栈时,则返回1,否则返回0*/
int isEmptyStack_seq( PSeqStack pastack ) {
return pastack->t == -1;
}
/* 在栈中压入一元素x */
void push_seq( PSeqStack pastack, DataType x ) {
if( pastack->t >= MAXNUM - 1 )
printf( "Overflow! /n" );
else {
pastack->t++;
pastack->s[pastack->t] = x;
}
}
/* 删除栈顶元素 */
void pop_seq( PSeqStack pastack ) {
if (pastack->t == -1 )
printf( "Underflow!/n" );
else
pastack->t--;
}
/* 当pastack所指的栈不为空栈时,求栈顶元素的值 */
DataType top_seq( PSeqStack pastack ) {
return (pastack->s[pastack->t]);
}
int visited[MAXVEX];
void dfs ( GraphList* g , Vertex v );
void dft ( GraphList* g ) {
Vertex v ;
for ( v = firstVertex ( g ) ; v != NON ; v = nextVertex ( g , v ) )
if ( visited[v] == FALSE ) dfs ( g , v ) ;
}
void dfs ( GraphList* g , Vertex v ) {
DataType element;
Vertex v1, v2;
PSeqStack s ;
s = createEmptyStack_seq ( ) ;
element.v = v;
element.k = firstAdjacent(g, v);
push_seq(s ,element) ;
printf("%d ", v);
visited[v] = TRUE ;
while ( !isEmptyStack_seq(s) ) {
element = top_seq ( s ) ;
pop_seq ( s );
v1 = element.v;
v2 = element.k;
while (v2 != NON ) {
if ( visited[v2] == FALSE ) {
element.v = v1;
element.k = v2;
push_seq (s, element);
visited[v2] = TRUE ;
printf("%d ", v2);
v1 = v2;
v2 = firstAdjacent(g, v1);
}
else
v2 = nextAdjacent(g , v1 , v2) ;
}
}
}
int main(){
GraphList* p = makeList();
dft(p);
return 0;
}
最小生成树:Prim算法, Kruskal算法
/* 用邻接矩阵表示的图的Kruskal算法的源程序*/
#include<stdio.h>
#define MAXVEX 6
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX]; 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
typedef struct{
int start_vex, stop_vex; /* 边的起点和终点 */
AdjType weight; /* 边的权 */
} Edge;
Edge mst[5];
#define MAX 1e+8
void prim(GraphMatrix * pgraph, Edge mst[]) {
int i, j, min, vx, vy;
float weight, minweight; Edge edge;
for (i = 0; i < pgraph->n-1; i++) {
mst[i].start_vex = 0;
mst[i].stop_vex = i+1;
mst[i].weight = pgraph->arcs[0][i+1];
}
for (i = 0; i < pgraph->n-1; i++) { /* 共n-1条边 */
minweight = MAX; min = i;
for (j = i; j < pgraph->n-1; j++)/* 从所有边(vx,vy)(vx∈U,vy∈V-U)中选出最短的边 */
if(mst[j].weight < minweight) {
minweight = mst[j].weight;
min = j;
}
/* mst[min]是最短的边(vx,vy)(vx∈U, vy∈V-U),将mst[min]加入最小生成树 */
edge = mst[min];
mst[min] = mst[i];
mst[i] = edge;
vx = mst[i].stop_vex; /* vx为刚加入最小生成树的顶点的下标 */
for(j = i+1; j < pgraph->n-1; j++) { /* 调整mst[i+1]到mst[n-1] */
vy=mst[j].stop_vex; weight = pgraph->arcs[vx][vy];
if (weight < mst[j].weight) {
mst[j].weight = weight;
mst[j].start_vex = vx;
}
}
}
}
GraphMatrix graph = {
6,
{{0,10,MAX,MAX,19,21},
{10,0,5,6,MAX,11},
{MAX,5,0,6,MAX,MAX},
{MAX,6,6,0,18,14},
{19,MAX,MAX,18,0,33},
{21,11,MAX,14,33,0}
}
};
int main(){
int i;
prim(&graph,mst);
for (i = 0; i < graph.n-1; i++)
printf("(%d %d %.0f)/n", mst[i].start_vex,
mst[i].stop_vex, mst[i].weight);
return 0;
}
/* 用邻接矩阵表示的图的prim算法的源程序*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 6
enum{FALSE,TRUE};
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX]; 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
typedef struct {
int start_vex, stop_vex; /* 边的起点和终点 */
AdjType weight; /* 边的权 */
} Edge;
Edge mst[5];
#define MAX 1e+8
int kruskal(GraphMatrix graph, Edge mst[]) {
int i, j, num = 0, start, stop;
float minweight;
int* status = (int *)malloc(sizeof(int)*graph.n);
for (i = 0; i < graph.n; i++)
status[i] = i;
while (num < graph.n - 1){
minweight = MAX;
for (i = 0; i < graph.n-1; i++)
for (j = i+1; j < graph.n; j++)
if (graph.arcs[i][j] < minweight){
start = i; stop = j;
minweight = graph.arcs[i][j];
}
if (minweight == MAX) return FALSE;/* 不能得到最小生成树*/
/* 加入start和stop组成的边不产生回路*/
if (status[start] != status[stop]){
mst[num].start_vex = start;
mst[num].stop_vex = stop;
mst[num].weight = graph.arcs[start][stop];
num++;
j = status[stop];
for (i = 0; i < graph.n; i++)
if(status[i] == j)
status[i] = status[start];
}
/* 删除start和stop组成的边*/
graph.arcs[start][stop] = MAX;
}
return TRUE;/* 能得到最小生成树*/
}
GraphMatrix graph = {
6,
{{0,10,MAX,MAX,19,21},
{10,0,5,6,MAX,11},
{MAX,5,0,6,MAX,MAX},
{MAX,6,6,0,18,14},
{19,MAX,MAX,18,0,33},
{21,11,MAX,14,33,0}
}
};
int main(){
int i;
if (kruskal(graph,mst) == TRUE)
for (i = 0; i < graph.n-1; i++)
printf("(%d %d %.0f)/n", mst[i].start_vex,
mst[i].stop_vex, mst[i].weight);
return 0;
}
/* 用邻接矩阵表示的图的prim算法的源程序*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 6
enum{FALSE,TRUE};
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX]; 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
typedef struct {
int start_vex, stop_vex; /* 边的起点和终点 */
AdjType weight; /* 边的权 */
} Edge;
Edge mst[5];
#define MAX 1e+8
int kruskal(GraphMatrix graph, Edge mst[]) {
int i, j, num = 0, start, stop;
float minweight;
int* status = (int *)malloc(sizeof(int)*graph.n);
for (i = 0; i < graph.n; i++)
status[i] = i;
while (num < graph.n - 1){
minweight = MAX;
for (i = 0; i < graph.n-1; i++)
for (j = i+1; j < graph.n; j++)
if (graph.arcs[i][j] < minweight){
start = i; stop = j;
minweight = graph.arcs[i][j];
}
if (minweight == MAX) return FALSE;/* 不能得到最小生成树*/
/* 加入start和stop组成的边不产生回路*/
if (status[start] != status[stop]){
mst[num].start_vex = start;
mst[num].stop_vex = stop;
mst[num].weight = graph.arcs[start][stop];
num++;
j = status[stop];
for (i = 0; i < graph.n; i++)
if(status[i] == j)
status[i] = status[start];
}
/* 删除start和stop组成的边*/
graph.arcs[start][stop] = MAX;
}
return TRUE;/* 能得到最小生成树*/
}
GraphMatrix graph = {
6,
{{0,10,MAX,MAX,19,21},
{10,0,5,6,MAX,11},
{MAX,5,0,6,MAX,MAX},
{MAX,6,6,0,18,14},
{19,MAX,MAX,18,0,33},
{21,11,MAX,14,33,0}
}
};
int main(){
int i;
if (kruskal(graph,mst) == TRUE)
for (i = 0; i < graph.n-1; i++)
printf("(%d %d %.0f)/n", mst[i].start_vex,
mst[i].stop_vex, mst[i].weight);
return 0;
}
/* 用邻接矩阵表示的图的Dijkstra算法的源程序*/
#include<stdio.h>
#define MAXVEX 100
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
VexType vexs[MAXVEX]; /* 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
typedef struct {
VexType vertex; /* 顶点信息 */
AdjType length; /* 最短路径长度 */
int prevex; /* 从v0到达vi(i=1,2,…n-1)的最短路径上vi的前趋顶点 */
} Path;
Path dist[6]; /* n为图中顶点个数*/
#define MAX 1e+8
void init(GraphMatrix* pgraph, Path dist[]) {
int i;
dist[0].length = 0;
dist[0].prevex = 0;
dist[0].vertex = pgraph->vexs[0];
pgraph->arcs[0][0] = 1; /* 表示顶点v0在集合U中 */
for(i = 1; i < pgraph->n; i++) { /* 初始化集合V-U中顶点的距离值 */
dist[i].length=pgraph->arcs[0][i];
dist[i].vertex=pgraph->vexs[i];
if (dist[i].length != MAX)
dist[i].prevex=0;
else dist[i].prevex= -1;
}
}
void dijkstra(GraphMatrix graph, Path dist[]) {
int i,j,minvex;
AdjType min;
init(&graph,dist); /* 初始化,此时集合U中只有顶点v0*/
for(i = 1; i < graph.n; i++) {
min=MAX; minvex=0;
for (j = 1; j < graph.n; j++) /*在V-U中选出距离值最小顶点*/
if( graph.arcs[j][j] == 0 && dist[j].length < min ) {
min=dist[j].length; minvex=j;
}
if(minvex == 0) break; /* 从v0没有路径可以通往集合V-U中的顶点 */
graph.arcs[minvex][minvex] = 1; /* 集合V-U中路径最小的顶点为minvex */
for (j = 1; j < graph.n; j++) { /* 调整集合V-U中的顶点的最短路径 */
if(graph.arcs[j][j] == 1) continue;
if(dist[j].length > dist[minvex].length + graph.arcs[minvex][j]) {
dist[j].length = dist[minvex].length + graph.arcs[minvex][j];
dist[j].prevex = minvex;
}
}
}
}
GraphMatrix graph;
void initgraph(){
int i,j;
graph.n=6;
for (i = 0; i < graph.n; i++)
for (j = 0; j < graph.n; j++)
graph.arcs[i][j] = (i == j ? 0 : MAX);
graph.arcs[0][1] = 50;
graph.arcs[0][2] = 10;
graph.arcs[1][2] = 15;
graph.arcs[1][4] = 5;
graph.arcs[2][0] = 20;
graph.arcs[2][3] = 15;
graph.arcs[3][1] = 20;
graph.arcs[3][4] = 35;
graph.arcs[4][3] = 30;
graph.arcs[5][3] = 3;
graph.arcs[0][4] = 45;
}
int main(){
int i;
initgraph();
dijkstra(graph, dist);
for (i = 0; i < graph.n; i++)
printf("(%.0f %d)", dist[i].length,dist[i].prevex);
return 0;
}
/* 用邻接矩阵表示的图的Floyd算法的源程序*/
#include<stdio.h>
#define MAXVEX 100
#define MAX 1e+8
typedef char VexType;
typedef float AdjType;
typedef struct {
int n; /* 图的顶点个数 */
VexType vexs[MAXVEX]; /* 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
typedef struct {
AdjType a[MAXVEX][MAXVEX];/* 关系矩阵A,存放每对顶点间最短路径长度 */
int nextvex[MAXVEX][MAXVEX];
/* nextvex[i][j]存放vi到vj最短路径上vi的后继顶点的下标值 */
} ShortPath;
void floyd(GraphMatrix * pgraph, ShortPath * ppath) {
int i, j, k;
for (i = 0; i < pgraph->n; i++)
for (j = 0; j < pgraph->n; j++) {
if (pgraph->arcs[i][j] != MAX)
ppath->nextvex[i][j] = j;
else ppath->nextvex[i][j] = -1;
ppath->a[i][j] = pgraph->arcs[i][j];
}
for (k = 0; k < pgraph->n; k++)
for (i = 0; i < pgraph->n; i++)
for (j = 0; j < pgraph->n; j++) {
if ( ppath->a[i][k] >= MAX || ppath->a[k][j] >= MAX )
continue;
if ( ppath->a[i][j] > ppath->a[i][k]+ ppath->a[k][j] ) {
ppath->a[i][j] = ppath->a[i][k] + ppath->a[k][j];
ppath->nextvex[i][j] = ppath->nextvex[i][k];
}
}
}
GraphMatrix graph;
ShortPath path;
void init(){
int i,j;
graph.n = 6;
for(i = 0; i < graph.n; i++)
for(j = 0; j < graph.n; j++)
graph.arcs[i][j] = (i == j ? 0 : MAX);
graph.arcs[0][1] = 50;
graph.arcs[0][2] = 10;
graph.arcs[1][2] = 15;
graph.arcs[1][4] = 5;
graph.arcs[2][0] = 20;
graph.arcs[2][3] = 15;
graph.arcs[3][1] = 20;
graph.arcs[3][4] = 35;
graph.arcs[4][3] = 30;
graph.arcs[5][3] = 3;
graph.arcs[0][4] = 45;
}
int main(){
int i,j;
init();
floyd(&graph, &path);
for (i = 0; i < graph.n; i++){
for (j = 0; j < graph.n; j++)
printf("%d ", path.nextvex[i][j]);
putchar('/n');
}
return 0;
}
/* 用邻接表表示图的拓扑排序算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 100
#define TRUE 1
#define FALSE 0
typedef struct EdgeNode EdgeNode;
typedef struct EdgeNode * PEdgeNode;
typedef struct EdgeNode * EdgeList;
struct EdgeNode {
int endvex; /* 相邻顶点字段 */
PEdgeNode nextedge; /* 链字段 */
}; /* 边表中的结点 */
typedef struct {
/*VexType vertex;*/ /* 顶点信息 */
EdgeList edgelist; /* 边表头指针 */
} VexNode; /* 顶点表中的结点 */
typedef struct{
int n; /* 图的顶点个数 */
VexNode vexs[MAXVEX];
} GraphList;
typedef struct {
int vexsno[MAXVEX]; /* 顶点在顶点表中的下标值 */
/*VexType vexs[MAXVEX];*/ /* 顶点信息 */
} Topo;
/* 求出图中所有顶点的入度 */
/* 方法是搜索整个邻接表 */
void findInDegree(GraphList* g, int *inDegree){
int i; PEdgeNode p;
for (i = 0; i < g->n; i++)
inDegree[i] = 0;
for (i = 0; i < g->n; i++){
p = g->vexs[i].edgelist;
while (p) {
++inDegree[p->endvex];
p = p->nextedge;
}
}
}
void makeNewAOV(EdgeList p, int* indegree, int* top) {
int k;
while (p) { /* 删除以该顶点为起点的边 */
k = p->endvex;
indegree[k]--;
if (indegree[k] == 0) { /* 将新的入度为零的边入栈 */
indegree[k] = *top;
*top = k;
}
p = p->nextedge;
}
}
int topoSort(GraphList * paov, Topo * ptopo) {
EdgeList p;
int i, j, nodeno = 0, top = -1;
int indegree[MAXVEX];
findInDegree(paov, indegree); /* 求出图中所有顶点的入度 */
for (i = 0; i < paov->n; i++)
if (indegree[i] == 0) { /* 将入度为零的顶点入栈 */
indegree[i] = top; top = i;
}
while (top != -1) { /* 栈不为空 */
j = top;
top = indegree[top]; /* 取出当前栈顶元素 */
/*ptopo->vexs[nodeno]=paov->vexs[j].vertex;*/ /* 将该元素输出到拓扑序列中 */
ptopo->vexsno[nodeno++] = j;
p = paov->vexs[j].edgelist; /* 取该元素边表中的第一个边结点 */
/*删除该结点,构造新的AOV网*/
/*对indegree数组进行修改*/
makeNewAOV(p, indegree, &top);
}
if (nodeno < paov->n) { /* AOV网中存在回路 */
printf("The aov network has a cycle/n");
return FALSE;
}
return TRUE;
}
/* 边的插入算法*/
void insert(GraphList* p,int a,int b){
EdgeList pp;
PEdgeNode temp;
temp = (PEdgeNode)malloc(sizeof(EdgeNode));
temp->endvex = b;
temp->nextedge = NULL;
pp = p->vexs[a].edgelist;
if (pp == NULL) p->vexs[a].edgelist = temp;
else {
while (pp->nextedge != NULL)
pp = pp->nextedge;
pp->nextedge = temp;
}
}
/* 实例邻接表的构造 */
GraphList* makeList(){
GraphList* p;
int i;
p = (GraphList*)malloc(sizeof(GraphList));
p->n = 9;
for (i = 0; i < p->n; i++)
p->vexs[i].edgelist = NULL;
insert(p, 0, 2);
insert(p, 0, 7);
insert(p, 1, 2);
insert(p, 1, 3);
insert(p, 1, 4);
insert(p, 2, 3);
insert(p, 3, 5);
insert(p, 3, 6);
insert(p, 4, 5);
insert(p, 7, 8);
insert(p, 8, 6);
return p;
}
Topo topo;
int main(){
GraphList* p;
int i;
p = makeList();
if (topoSort(p, &topo) == TRUE)
for (i = 0; i < p->n; i++)
printf("%d ", topo.vexsno[i]);
return 0;
}
/* 用邻接矩阵表示图的拓扑排序算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 100
#define TRUE 1
#define FALSE 0
typedef char VexType;
typedef int AdjType;
typedef struct {
int n; /* 图的顶点个数 */
/*VexType vexs[MAXVEX];*/ /* 顶点信息 */
AdjType arcs[MAXVEX][MAXVEX]; /* 边信息 */
} GraphMatrix;
typedef struct {
/*VexType vexs[MAXVEX];*/ /* 顶点信息 */
int vexsno[MAXVEX]; /* 顶点在顶点表中的下标值 */
} Topo;
/* 求出图中所有顶点的入度 */
/* 方法是搜索整个邻接表 */
void findInDegree(GraphMatrix* g, int *inDegree) {
int i, j;
for (i = 0; i < g->n; i++) {
inDegree[i] = 0;
for (j = 0; j < g->n; j++)
if (g->arcs[j][i]) ++inDegree[i];
}
}
void makeNewAOV(GraphMatrix* g, int p, int* indegree, int* top) {
int k;
for (k = 0; k < g->n; k++)/* 删除以该顶点为起点的边 */
if (g->arcs[p][k]) {
indegree[k]--;
if (indegree[k] == 0) { /* 将新的入度为零的边入栈 */
indegree[k] = *top;
*top = k;
}
}
}
int topoSort(GraphMatrix * paov, Topo * ptopo) {
int i, j, nodeno = 0, top = -1;
int indegree[MAXVEX];
findInDegree(paov, indegree); /* 求出图中所有顶点的入度 */
for (i = 0; i < paov->n; i++)
if (indegree[i] == 0) { /* 将入度为零的顶点入栈 */
indegree[i] = top; top = i;
}
while (top != -1) { /* 栈不为空 */
j = top;
top = indegree[top]; /* 取出当前栈顶元素 */
/*ptopo->vexs[nodeno] = paov->vexs[j];*/ /* 将该元素输出到拓扑序列中 */
ptopo->vexsno[nodeno++] = j;
/* 取该元素边表中的第一个边结点 */
/* 删除该结点,构造新的AOV网 */
/* 对indegree数组进行修改 */
makeNewAOV(paov, j, indegree, &top);
}
if (nodeno < paov->n) { /* AOV网中存在回路 */
printf("The aov network has a cycle/n");
return FALSE;
}
return TRUE;
}
/* 构造邻接矩阵*/
GraphMatrix* makeMatrix(){
GraphMatrix* p;
int i,j;
p = (GraphMatrix*)malloc(sizeof(GraphMatrix));
p->n = 9;
for (i = 0; i < p->n; i++)
for (j = 0; j < p->n; j++)
p->arcs[i][j] = 0;
p->arcs[0][2] = 1;
p->arcs[0][7] = 1;
p->arcs[1][2] = 1;
p->arcs[1][3] = 1;
p->arcs[1][4] = 1;
p->arcs[2][3] = 1;
p->arcs[3][5] = 1;
p->arcs[3][6] = 1;
p->arcs[4][5] = 1;
p->arcs[7][8] = 1;
p->arcs[8][6] = 1;
return p;
}
Topo topo;
int main(){
int i;
GraphMatrix* p;
p = makeMatrix();
if (topoSort(p, &topo) == TRUE)
for (i = 0; i < p->n; i++)
printf("%d ", topo.vexsno[i]);
return 0;
}
/* 图的关键路径问题的算法*/
#include<stdio.h>
#include<stdlib.h>
#define MAXVEX 100
#define TRUE 1
#define FALSE 0
typedef struct EdgeNode EdgeNode;
typedef struct EdgeNode * PEdgeNode;
typedef struct EdgeNode * EdgeList;
typedef float AdjType;
struct EdgeNode {
int endvex; /* 相邻顶点字段 */
AdjType weight;
PEdgeNode nextedge; /* 链字段 */
}; /* 边表中的结点 */
typedef struct {
/*VexType vertex;*/ /* 顶点信息 */
EdgeList edgelist; /* 边表头指针 */
} VexNode; /* 顶点表中的结点 */
typedef struct {
int n; /* 图的顶点个数 */
VexNode vexs[MAXVEX];
} GraphList;
typedef struct {
/*VexType vexs[MAXVEX];*/ /* 顶点信息 */
int vexsno[MAXVEX]; /* 顶点在顶点表中的下标值 */
} Topo;
/* 求出图中所有顶点的入度 */
/* 方法是搜索整个邻接表 */
void findInDegree(GraphList* g, int *inDegree) {
int i;
PEdgeNode p;
for (i = 0; i < g->n; i++)
inDegree[i] = 0;
for (i = 0; i < g->n; i++) {
p = g->vexs[i].edgelist;
while(p) {
inDegree[p->endvex]++;
p = p->nextedge;
}
}
}
void makeNewAOV(EdgeList p, int* indegree, int* top) {
int k;
while (p) { /* 删除以该顶点为起点的边 */
k = p->endvex;
indegree[k]--;
if (indegree[k] == 0) { /* 将新的入度为零的边入栈 */
indegree[k] = *top;
*top = k;
}
p = p->nextedge;
}
}
/* 拓扑排序算法*/
int topoSort(GraphList * paov, Topo * ptopo) {
EdgeList p;
int i, j, nodeno = 0, top = -1;
int indegree[MAXVEX];
findInDegree(paov, indegree); /* 求出图中所有顶点的入度 */
for (i = 0; i < paov->n; i++)
if (indegree[i] == 0) { /* 将入度为零的顶点入栈 */
indegree[i] = top; top = i;
}
while (top != -1) { /* 栈不为空 */
j = top;
top = indegree[top]; /* 取出当前栈顶元素 */
/*ptopo->vexs[nodeno]=paov->vexs[j].vertex;*/ /* 将该元素输出到拓扑序列中 */
ptopo->vexsno[nodeno++] = j;
p = paov->vexs[j].edgelist; /* 取该元素边表中的第一个边结点 */
/*删除该结点,构造新的AOV网*/
/*对indegree数组进行修改*/
makeNewAOV(p, indegree, &top);
}
if (nodeno < paov->n) { /* AOV网中存在回路 */
printf("The aov network has a cycle/n");
return FALSE;
}
return TRUE;
}
void insert(GraphList* p,int a,int b,float weight){
EdgeList pp;
PEdgeNode temp;
temp = (PEdgeNode)malloc(sizeof(EdgeNode));
temp->endvex = b;
temp->nextedge = NULL;
temp->weight = weight;
pp = p->vexs[a].edgelist;
if (pp == NULL)
p->vexs[a].edgelist = temp;
else {
while (pp->nextedge != NULL)
pp = pp->nextedge;
pp->nextedge = temp;
}
}
/* 邻接表的构造*/
GraphList* makeList(){
GraphList* p;
int i;
p = (GraphList*)malloc(sizeof(GraphList));
p->n = 9;
for (i = 0; i < p->n; i++)
p->vexs[i].edgelist = NULL;
insert(p,0,1,6);
insert(p,0,2,4);
insert(p,0,3,5);
insert(p,1,4,1);
insert(p,2,4,1);
insert(p,3,5,2);
insert(p,4,6,9);
insert(p,4,7,7);
insert(p,5,7,4);
insert(p,6,8,2);
insert(p,7,8,4);
return p;
}
int CriticalPath(GraphList * paoe);
/* 主程序*/
int main(){
GraphList* p;
/*int i;*/
p = makeList();
/*if(topoSort(p,&topo)==TRUE)
for(i=0;i<p->n;i++)
printf("%d ",topo.vexsno[i]);*/
if (CriticalPath(p) == FALSE)
printf("There is no critical path!/n");
return 0;
}
/*******************************************/
#define MAXEDGE 100
/* 计算数组ee*/
void countee(GraphList* paoe,Topo* ptopo, AdjType* ee) {
int i, j, k;
EdgeList p;
for (i = 0; i < paoe->n; i++) ee[i] = 0;
for (k = 0; k < paoe->n; k++) { /* 求事件vj可能的最早发生时间ee(j) */
i = ptopo->vexsno[k];
p = paoe->vexs[i].edgelist;
while (p != NULL) {
j = p->endvex;
if (ee[i] + p->weight > ee[j])
ee[j] = ee[i] + p->weight;
p = p->nextedge;
}
}
}
/* 计算数组le*/
void countle(GraphList * paoe,Topo* ptopo, AdjType* ee, AdjType* le) {
int i, j, k;
EdgeList p;
for (i = 0; i < paoe->n; i++) /* 求事件vi允许的最迟发生时间le(i) */
le[i] = ee[paoe->n - 1];
for (k = paoe->n - 2; k >= 0; k--) {
i = ptopo->vexsno[k];
p = paoe->vexs[i].edgelist;
while (p != NULL) {
j = p->endvex;
if( le[j] - p->weight < le[i] )
le[i] = le[j] - p->weight;
p = p->nextedge;
}
}
}
/* 计算数组e,l并输出结果*/
void counte_l(GraphList * paoe,Topo* ptopo, AdjType* ee,
AdjType* le, AdjType* e, AdjType* l) {
int i, j, k = 0;
EdgeList p;
/* 求活动ak的最早开始时间e(k)及最晚开始时间l(k) */
for (i = 0; i < paoe->n; i++) {
p = paoe->vexs[i].edgelist;
while (p != NULL) {
j = p->endvex;
e[k] = ee[i];
l[k] = le[j] - p->weight;
if (e[k] == l[k])
printf("<v%2d, v%2d>, ", i, j);
k++;
p = p->nextedge;
}
}
}
/* 关键路径算法*/
int CriticalPath(GraphList * paoe) {
AdjType ee[MAXVEX], le[MAXVEX], l[MAXEDGE], e[MAXEDGE];
Topo topo;
if (topoSort(paoe, &topo) == FALSE) /* 求AOE网的一个拓扑序列 */
return FALSE;
countee(paoe, &topo, ee); /*计算数组ee*/
countle(paoe, &topo, ee, le);/*计算数组le*/
counte_l(paoe, &topo, ee, le, e, l);/*计算数组e,l并输出结果*/
printf("/n");
return TRUE;
}
第7章,排序
/* 直接插入排序的算法源程序*/
#include<stdio.h>
#define MAXNUM 100
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void insertSort(SortObject * pvector) { /* 按递增序进行直接插入排序 */
int i, j;
RecordNode temp;
RecordNode *data = pvector->record;
for( i = 1; i < pvector->n; i++ ) { /* 依次插入记录R1, R2…Rn-1 */
temp = data[i];
for ( j = i-1; temp.key < data[j].key && j >= 0; j-- )
/* 由后向前找插入位置 将排序码大于ki的记录后移 */
data[j+1] = data[j];
if( j != i-1 ) data[j+1] = temp;
}
}
SortObject vector = {10,
49, 38, 65, 97, 76, 13, 27, 49, 50, 101};
int main(){
int i;
insertSort(&vector);
for(i = 0; i < vector.n; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
/* 二分法插入排序的算法源程序*/
#include<stdio.h>
#define MAXNUM 100
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void binSort(SortObject * pvector) { /* 按递增序进行二分法插入排序 */
int i, j, left, mid, right;
RecordNode temp;
RecordNode *data = pvector->record;
for( i = 1; i < pvector->n; i++ ) {
temp = data[i];
left = 0; right = i-1; /* 置已排序区间的下、上界初值 */
while (left <= right) {
mid = (left + right)/2; /* mid指向已排序区间的中间位置 */
if (temp.key < data[mid].key)
right = mid-1; /* 插入元素应在左子区间 */
else left = mid+1; /* 插入元素应在右子区间 */
}
for (j = i-1; j >= left; j--)
data[j+1] = data[j]; /* 将排序码大于ki的记录后移 */
if (left != i) data[left] = temp;
}
}
SortObject vector={10, 49,38,65,97,76,13,27,49,50,101};
int main(){
int i;
binSort(&vector);
for(i = 0; i < vector.n; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
/* 表插入排序的算法源程序*/
#include<stdio.h>
struct Node; /* 单链表结点类型 */
typedef int KeyType;
typedef int DataType;
typedef struct Node ListNode;
struct Node {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
ListNode *next; /* 记录的指针字段 */
};
typedef ListNode * LinkList;
/* 对链表按递增序进行表插入排序,链表中第一个结点为表头结点。 */
void listSort(LinkList * plist) {
ListNode *now, *pre, *p, *q, *head; head=*plist;
if (head->next == NULL || head->next->next == NULL)
return; /* 为空链表或链表中只有一个结点 */
pre = head->next; now = pre->next;
while (now != NULL) {
q = head; p = head->next;
while(p != now && p->key <= now->key) {
q = p; p = p->next;
} /* 本循环结束时,已经找到了now的插入位置 */
if (p == now) { /* now应放在原位置 */
pre = pre->next; now = pre->next; continue;
}
/* 使now记录脱链,将now记录插入链表中 */
pre->next = now->next; q->next = now;
now->next = p; now = pre->next;
}
}
ListNode element[9]={
0, &element[1],
49, &element[2],
38, &element[3],
65, &element[4],
97, &element[5],
76, &element[6],
13, &element[7],
27, &element[8],
49, NULL
};
int main(){
LinkList p = element;
listSort(&p);
p = p->next;
while (p != NULL){
printf("%d ",p->key);
p = p->next;
}
getchar();
return 0;
}
/* shell排序的算法源程序 */
#include<stdio.h>
#define MAXNUM 100
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void shellSort(SortObject * pvector, int d) { /* 按递增序进行Shell排序 */
int i, j, inc;
RecordNode temp, *data = pvector->record;
for (inc = d; inc > 0; inc /= 2) {
/* inc 为本趟shell排序增量 */
for (i = inc; i < pvector->n; i++) {
temp = data[i]; /* 保存待插入记录Ri*/
for (j = i-inc; j >= 0 && temp.key < data[j].key; j -= inc)
data[j+inc] = data[j]; /* 查找插入位置,记录后移 */
data[j+inc] = temp; /* 插入记录Ri */
}
}
}
SortObject vector={8,49,38,65,97,76,13,27,49};
int main(){
int i;
shellSort(&vector,4);
for(i=0;i<8;i++)
printf("%d ",vector.record[i]);
getchar();
return 0;
}
/* 直接选择排序的算法源程序*/
#include<stdio.h>
#define MAXNUM 100
#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void selectSort(SortObject * pvector) { /* 按递增序进行直接选择排序 */
int i, j, k;
RecordNode temp, *data = pvector->record;
for( i = 0; i < pvector->n-1; i++ ) { /* 做n-1趟选择排序 */
k = i;
for (j = i+1; j < pvector->n; j++) /* 在无序区内找出排序码最小的记录Rk*/
if (data[j].key < data[k].key) k = j;
if (k != i) { /* 记录Rk与Ri互换 */
temp = data[i];
data[i] = data[k];
data[k] = temp;
}
}
}
SortObject vector={8, 49,38,65,97,76,13,27,49};
int main(){
int i;
selectSort(&vector);
for(i = 0; i < 8; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
/* 堆排序的算法源程序*/
#include<stdio.h>
#define MAXNUM 100
#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
/* 定义宏是为了使程序清晰 */
#define leftChild(i) (2*(i)+1)
void sift(SortObject * pvector, int i, int n) {
int child;
RecordNode temp = pvector->record[i], *data = pvector->record;
child = leftChild(i); /* Rchild是R0的左子女 */
while(child<n) {
if (child < n-1 && data[child].key < data[child+1].key)
child++; /* child 指向Ri的左、右子女中排序码较大的结点 */
if (temp.key < data[child].key) {
data[i] = data[child];
i = child; child = leftChild(i);/* child换到父结点位置,继续调整*/
}
else break; /* 调整结束 */
}
data[i] = temp; /* 将记录Ri放入正确位置 */
}
void heapSort(SortObject * pvector) { /* 对记录R0到Rn-1进行堆排序 */
int i, n = pvector->n;
RecordNode temp, *data = pvector->record;
for (i = n/2-1; i >= 0; i--)
sift(pvector, i, n); /* 建立初始堆 */
for (i = n-1; i > 0; i--) { /* 进行n-1趟堆排序 */
temp = data[0]; /* 当前堆顶记录和最后一个记录互换 */
data[0] = data[i];
data[i] = temp;
sift(pvector, 0, i); /* 从R0到Ri-1重建堆 */
}
}
SortObject vector={8, 49,38,65,97,76,13,27,49};
int main(){
int i;
heapSort(&vector);
for(i = 0; i < 8; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
/* 起泡排序的算法源程序*/
/* 快速排序的算法源程序*/
#include<stdio.h>
#define MAXNUM 100
#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void quickSort(SortObject * pvector, int l, int r) {
int i, j;
RecordNode temp, *data = pvector->record;
if (l >= r) return; /* 只有一个记录或无记录,则无须排序 */
i = l; j = r; temp = data[i];
while (i != j) { /* 寻找Rl的最终位置 */
while( data[j].key >= temp.key && j > i )
j--; /* 从右向左扫描,查找第1个排序码小于temp.key的记录 */
if (i < j) data[i++] = data[j];
while( data[i].key <= temp.key && j > i )
i++; /* 从左向右扫描,查找第1个排序码大于temp.key的记录 */
if (i < j) data[j--] = data[i];
}
data[i] = temp; /* 找到Rl的最终位置 */
quickSort(pvector, l, i-1); /* 递归处理左区间 */
quickSort(pvector, i+1, r); /* 递归处理右区间 */
}
SortObject vector = {8, 49,38,65,97,76,13,27,49};
int main(){
int i;
quickSort(&vector, 0, 7);
for(i = 0; i < 8; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
/* 基数排序的算法源程序*/
#include<stdio.h>
#define D 3 /* D为排序码的最大位数 */
#define R 10 /* R为基数 */
typedef int KeyType;
typedef int DataType;
struct Node; /* 单链表结点类型 */
typedef struct Node RadixNode;
struct Node {
KeyType key[D];
/* DataType info;*/
RadixNode *next;
};
typedef RadixNode * RadixList;
typedef struct QueueNode {
RadixNode *f; /* 队列的头指针 */
RadixNode *e; /* 队列的尾指针 */
} Queue;
Queue queue[R];
void radixSort(RadixList * plist, int d, int r) {
int i,j,k;
RadixNode *p, *head = (*plist)->next;
for(j = d-1; j >= 0; j--) { /* 进行d次分配和收集*/
p = head;
for(i = 0; i < r; i++) {
queue[i].f = NULL; queue[i].e = NULL; /* 清队列 */
}
while (p != NULL) {
k = p->key[j]; /* 按排序码的第j个分量进行分配*/
if (queue[k].f == NULL)
queue[k].f = p; /* 若第k个队列为空,则当前记录为队头*/
else (queue[k].e)->next = p;/* 否则当前记录链接到第k队的队尾*/
queue[k].e = p;
p = p->next;
}
for(i = 0; queue[i].f == NULL; i++) /* 找出第一个非空队列*/
;
p = queue[i].e; head = queue[i].f; /* head为收集链表的头指针*/
for(i++; i < r; i++)
if(queue[i].f != NULL) { /* 收集非空队列 */
p->next = queue[i].f;
p = queue[i].e;
}
p->next = NULL;
}
(*plist)->next = head;
}
struct Node element[11]={
0,0,0,NULL,/*表头*/
0,3,6,NULL,/*36*/
0,0,5,NULL,/*5*/
0,1,6,NULL,/*16*/
0,9,8,NULL,/*98*/
0,9,5,NULL,/*95*/
0,4,7,NULL,/*47*/
0,3,2,NULL,/*32*/
0,3,6,NULL,/*36*/
0,4,8,NULL,/*48*/
0,1,0,NULL /*10*/
};
int main(){
int i;
RadixList p = element;
for (i = 0; i < 10; i++)
element[i].next = &element[i+1];
element[10].next = NULL;
radixSort(&p, 3, 10);
p = p->next;
while (p != NULL){
printf("%d ", p->key[1]*10+p->key[2]);
p = p->next;
}
getchar();
return 0;
}
#include<stdio.h>
#define MAXNUM 100
#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void bubbleSort(SortObject * pvector) {
int i, j, noswap;
RecordNode temp, *data = pvector->record;
for(i = 0; i < pvector->n-1; i++) { /* 做n-1次起泡 */
noswap = TRUE; /* 置交换标志 */
for (j = 0; j < pvector->n-i-1; j++) /* 从前向后扫描 */
if (data[j+1].key < data[j].key) { /* 交换记录 */
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
noswap = FALSE;
}
if ( noswap ) break; /* 本趟起泡未发生记录交换,算法结束 */
}
}
SortObject vector={8, 49,38,65,97,76,13,27,49};
int main(){
int i;
bubbleSort(&vector);
for(i = 0; i < 8; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
/* 二路归并排序算法的源程序*/
#include<stdio.h>
#define MAXNUM 100
#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef int DataType;
typedef struct {
KeyType key; /* 排序码字段 */
/*DataType info; 记录的其它字段 */
} RecordNode;
typedef struct {
int n; /* n为文件中的记录个数,n<MAXNUM */
RecordNode record[MAXNUM];
} SortObject;
void merge(RecordNode r[], RecordNode r1[], int low, int m, int high) {
/* r[low]到r[m]和r[m+1]到r[right]是两个有序段 */
int i = low, j = m + 1, k = low;
while ( i <= m && j <= high ) { /* 反复复制两个段的第一个记录中较小的 */
if (r[i].key <= r[j].key)
r1[k++] = r[i++];
else r1[k++] = r[j++];
}
while (i <= m) r1[k++] = r[i++]; /* 复制第一个段的剩余记录 */
while (j <= high) r1[k++] = r[j++];/* 复制第二个段的剩余记录 */
}
/* 对 r 做一趟归并,结果放到 r1 中 */
void mergePass(RecordNode r[], RecordNode r1[], int n, int length) {
int i = 0, j; /* length为本趟归并的有序子段的长度 */
while(i + 2*length - 1 < n) {
merge(r, r1, i, i+length-1, i + 2*length - 1);/* 归并长length的两个子段*/
i += 2*length;
}
if(i + length - 1 < n - 1) /* 剩下两段,后一段长度小于 length */
merge(r, r1, i, i+length-1, n-1);
else /* 将剩下的一段复制到数组r1 */
for(j = i; j < n; j++) r1[j] = r[j];
}
void mergeSort(SortObject * pvector) {
RecordNode record[MAXNUM];
int length = 1;
while (length < pvector->n) {
/* 一趟归并,结果存放在数组record中*/
mergePass(pvector->record, record, pvector->n, length);
length *= 2;
/* 一趟归并,结果存回 */
mergePass(record, pvector->record, pvector->n, length);
length *= 2;
}
}
SortObject vector = {8, 49,38,65,97,76,13,27,49};
int main(){
int i;
mergeSort(&vector);
for(i = 0; i < 8; i++)
printf("%d ", vector.record[i]);
getchar();
return 0;
}
0
- 算法分析与设计
- 算法设计与分析
- 算法分析与设计
- 算法设计与分析
- 算法设计与分析
- 算法分析与设计
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法设计与分析
- 算法分析与设计
- 书名: 算法分析与设计
- 常用算法分析与设计
- Java中的语句、分支和路径覆盖测试
- 换个口味,试试Linux
- eclipse快捷键大全
- [转]关于windows 的窗口消息和WM_PAINT
- mediawiki中,SkinTemplate->skinname的用处
- 算法分析与设计
- 薪酬迅速翻倍的13条跳槽原则
- 网站建设合同
- 信息分类能给网站设计带来巨大好处
- 2007/0827-2007/0831工作周记
- 网站运营公司提高产品设计意识
- Java程序员应该搞懂的六个问题
- mplayer中文字幕乱码解决
- 2007/0903-2007/0907工作周记