Copyright(c) 2017,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:jcy
作者:贾存钰
完成日期:2017年11月23日
版本:vc6.0
问题描述:
输入描述:
a.cpp
测试用图:
#include <stdio.h>#include <malloc.h>#include "a.h"void ArrayToMat(int *Arr, int n, MGraph &g){ int i,j,count=0; g.n=n; for (i=0; i<g.n; i++) for (j=0; j<g.n; j++) { g.edges[i][j]=Arr[i*n+j]; if(g.edges[i][j]!=0 && g.edges[i][j]!=INF) count++; } g.e=count;}void ArrayToList(int *Arr, int n, ALGraph *&G){ int i,j,count=0; ArcNode *p; G=(ALGraph *)malloc(sizeof(ALGraph)); G->n=n; for (i=0; i<n; i++) G->adjlist[i].firstarc=NULL; for (i=0; i<n; i++) for (j=n-1; j>=0; j--) if (Arr[i*n+j]!=0) { p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=j; p->info=Arr[i*n+j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } G->e=count;}void MatToList(MGraph g, ALGraph *&G){ int i,j; ArcNode *p; G=(ALGraph *)malloc(sizeof(ALGraph)); for (i=0; i<g.n; i++) G->adjlist[i].firstarc=NULL; for (i=0; i<g.n; i++) for (j=g.n-1; j>=0; j--) if (g.edges[i][j]!=0) { p=(ArcNode *)malloc(sizeof(ArcNode)); p->adjvex=j; p->info=g.edges[i][j]; p->nextarc=G->adjlist[i].firstarc; G->adjlist[i].firstarc=p; } G->n=g.n; G->e=g.e;}void ListToMat(ALGraph *G,MGraph &g){ int i,j; ArcNode *p; g.n=G->n; g.e=G->e; for (i=0; i<g.n; i++) for (j=0; j<g.n; j++) g.edges[i][j]=0; for (i=0; i<G->n; i++) { p=G->adjlist[i].firstarc; while (p!=NULL) { g.edges[i][p->adjvex]=p->info; p=p->nextarc; } }}void DispMat(MGraph g){ int i,j; for (i=0; i<g.n; i++) { for (j=0; j<g.n; j++) if (g.edges[i][j]==INF) printf("%3s","∞"); else printf("%3d",g.edges[i][j]); printf("\n"); }}void DispAdj(ALGraph *G){ int i; ArcNode *p; for (i=0; i<G->n; i++) { p=G->adjlist[i].firstarc; printf("%3d: ",i); while (p!=NULL) { printf("-->%d/%d ",p->adjvex,p->info); p=p->nextarc; } printf("\n"); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
a.h
#ifndef GRAPH_H_INCLUDED#define GRAPH_H_INCLUDED#define MAXV 100 #define INF 32767 typedef int InfoType;typedef struct{ int no; InfoType info; } VertexType; typedef struct { int edges[MAXV][MAXV]; int n,e; VertexType vexs[MAXV]; } MGraph; typedef struct ANode { int adjvex; struct ANode *nextarc; InfoType info; } ArcNode;typedef int Vertex;typedef struct Vnode { Vertex data; int count; ArcNode *firstarc; } VNode;typedef VNode AdjList[MAXV]; typedef struct{ AdjList adjlist; int n,e; } ALGraph; void ArrayToMat(int *Arr, int n, MGraph &g); void ArrayToList(int *Arr, int n, ALGraph *&); void MatToList(MGraph g,ALGraph *&G);void ListToMat(ALGraph *G,MGraph &g);void DispMat(MGraph g);void DispAdj(ALGraph *G);#endif
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
main.cpp
#include <stdio.h>#include <malloc.h>#include "a.h"#define MaxSize 100typedef struct{ int u; int v; int w; } Edge;void InsertSort(Edge E[],int n) { int i,j; Edge temp; for (i=1; i<n; i++) { temp=E[i]; j=i-1; while (j>=0 && temp.w<E[j].w) { E[j+1]=E[j]; j--; } E[j+1]=temp; }}void Kruskal(MGraph g){ int i,j,u1,v1,sn1,sn2,k; int vset[MAXV]; Edge E[MaxSize]; k=0; for (i=0; i<g.n; i++) for (j=0; j<g.n; j++) if (g.edges[i][j]!=0 && g.edges[i][j]!=INF) { E[k].u=i; E[k].v=j; E[k].w=g.edges[i][j]; k++; } InsertSort(E,g.e); for (i=0; i<g.n; i++) vset[i]=i; k=1; j=0; while (k<g.n) { u1=E[j].u; v1=E[j].v; sn1=vset[u1]; sn2=vset[v1]; if (sn1!=sn2) { printf(" (%d,%d):%d\n",u1,v1,E[j].w); k++; for (i=0; i<g.n; i++) if (vset[i]==sn2) vset[i]=sn1; } j++; }}int main(){ MGraph g; int A[6][6]= { {0,6,1,5,INF,INF}, {6,0,5,INF,3,INF}, {1,5,0,5,6,4}, {5,INF,5,0,INF,2}, {INF,3,6,INF,0,6}, {INF,INF,4,2,6,0} }; ArrayToMat(A[0], 6, g); printf("最小生成树构成:\n"); Kruskal(g); return 0;}