prim算法
- #include<iostream>
- #include<stdlib.h>
- using namespace std;
- #define OK 1
- #define OVERFLOW -2
- #define MAX 1000
- typedef int Status;
- typedef char VertexType;
- typedef int* Inpoint;
- typedef int VRType;
- typedef enum{DG,DN,UDG,UDM} GraphKind;
- typedef struct {
- VertexType *vexs;
- int **arcs;
- int vexnum,arcnum;
- GraphKind kind;
- }MGraph;
- int LocateVex(MGraph G,VertexType v){
- for(int i=0;i<G.vexnum;i++)
- if(v==G.vexs[i]) return i;
- return G.vexnum;}
- Status CreateUDM(MGraph &G){
- G.kind=UDM;
- cin>>G.vexnum>>G.arcnum;
- G.vexs=new VertexType[G.vexnum];
- G.arcs=new Inpoint[G.vexnum];
- if(!G.vexs||!G.arcs) exit(OVERFLOW);
- int i,j,k;
- for(i=0;i<G.vexnum;i++){
- if(!(G.arcs[i]=new int[G.vexnum])) exit(OVERFLOW);
- for(j=0;j<G.vexnum;j++)
- if(i!=j) G.arcs[i][j]=MAX;
- else G.arcs[i][j]=0;}
- VertexType v1,v2;
- for(i=0;i<G.vexnum;i++)
- cin>>G.vexs[i];
- for(k=0;k<G.arcnum;k++){
- do{
- cin>>v1>>v2;
- i=LocateVex(G,v1);
- j=LocateVex(G,v2);
- cin>>G.arcs[i][j];
- G.arcs[j][i]=G.arcs[i][j];
- }while(i==G.vexnum||j==G.vexnum);
- }
- return OK;
- }
- void MiniSpanTree(MGraph G){
- typedef struct{
- VertexType adjvex;
- VRType lowcost;}Node,*cclosedge;
- cclosedge closedge;
- if(!(closedge=new Node[G.vexnum])) exit(OVERFLOW);
- int i,j,k,min;
- for(i=0;i<G.vexnum;i++) {
- closedge[i].lowcost=G.arcs[0][i];
- closedge[i].adjvex=G.vexs[0];}
- for(i=1;i<G.vexnum;i++){
- min=MAX;
- j=1;
- while(j<G.vexnum) {
- if(closedge[j].lowcost&&closedge[j].lowcost<min){
- min=closedge[j].lowcost;
- k=j;}
- j++;}
- cout<<closedge[k].adjvex<<"/t"<<G.vexs[k]<<"/t"<<min<<endl;;
- closedge[k].lowcost=0;
- for(j=1;j<G.vexnum;j++)
- if(G.arcs[k][j]<closedge[j].lowcost) {
- closedge[j].lowcost=G.arcs[k][j];
- closedge[j].adjvex=G.vexs[k];}
- }
- }
- int main(){
- MGraph G;
- CreateUDM(G);
- MiniSpanTree(G);
- return 0;}
- 输入:
- 6 10
- a b c d e f
- a b 6
- a d 5
- a c 1
- b c 5
- c d 5
- b e 3
- c e 6
- c f 4
- d f 2
- e f 6
- 输出:
- a c 1
- c f 4
- f d 2
- c b 5
- b e 3
- 请按任意键继续. . .
kruskal算法
- #include<iostream>
- #include<stdlib.h>
- using namespace std;
- #define MAX 1000
- #define OVERFLOW -2
- typedef char VertexType;
- typedef struct{
- VertexType v1,v2;
- int cost;}EdgeType,*PEdge;
- typedef struct{
- VertexType *vexs; //顶点数组
- PEdge edges; //边数组
- int vexnum,arcnum;}Graph;
- int Cmp(const void *a,const void *b){
- return (*(PEdge)a).cost-(*(PEdge)b).cost;}
- void Create(Graph &G){
- cin>>G.vexnum>>G.arcnum;
- if(!(G.vexs = new VertexType[G.vexnum])||!(G.edges = new EdgeType[G.arcnum]))
- exit(OVERFLOW);
- int i,j,k;
- for(i=0;i<G.vexnum;i++)
- cin>>G.vexs[i];
- for(i=0;i<G.arcnum;i++)
- cin>>G.edges[i].v1>>G.edges[i].v2>>G.edges[i].cost;
- qsort(G.edges,G.arcnum,sizeof(EdgeType),Cmp);
- //对边数组按权值从小到大排序
- }
- int LocateVex(Graph G,VertexType v){
- for(int i=0;i<G.vexnum;i++)
- if(v==G.vexs[i]) return i;
- return G.vexnum;}
- int Find(Graph G,int *tree,VertexType v){//返回V的根节点
- int t=LocateVex(G,v);
- while(tree[t]>=0)
- t=tree[t];
- return t;}
- void Kruskal(Graph &G){
- int *tree;
- if(!(tree=new int[G.vexnum])) exit(OVERFLOW);
- int i,j=1,V1,V2;
- for(i=0;i<G.vexnum;i++) tree[i]=-1;
- i=0;
- while(j<G.vexnum){
- V1=Find(G,tree,G.edges[i].v1);
- V2=Find(G,tree,G.edges[i].v2);
- if(V1!=V2){
- tree[V2]=V1; //V1作为V2的根节点
- j++;
- cout<<G.edges[i].v1<<"/t"<<G.edges[i].v2<<"/t"<<G.edges[i].cost<<endl;}
- i++;}
- }
- int main(){
- Graph G;
- Create(G);
- Kruskal(G);
- return 0;}
- 输入:
- 6 10
- a b c d e f
- a b 6
- a d 5
- a c 1
- b c 5
- c d 5
- b e 3
- c e 6
- c f 4
- d f 2
- e f 6
- 输出:
- a c 1
- d f 2
- b e 3
- c f 4
- b c 5
- 请按任意键继续. . .