1、 求无向连通图的生成树 精品资料 求无向连通图的生成树 一、实验目的 ⑴掌握图的逻辑结构 ⑵掌握图的邻接矩阵存储结构 ⑶验证图的邻接矩阵存储及其遍历操作的实现 二、实验内容 (1)建立无向图的邻接矩阵存储 (2)对建立的无向图,进行深度优先遍历 (3)对建立的无向图进行广度优先遍历 三、设计与编码 (1)本实验用到的理论知识 (2)算法设计 (3)编码 // 图抽象类型及其实现.cpp : Defines the entry point for the console application. // #include "stdafx.
2、h"
#include "Graph.h"
#include "iostream.h"
int Graph::Find(int key,int &k)
{
int flag=0;
for(int i=0;i 3、 if(vertexnum<1)return(-1);//参数vertexnum非法
int i,front,rear,k;
Enode *q;
//先生成不带边表的顶点表--即顶点为孤立顶点集
A=new Vnode[vertexnum];
if(!A)return(0);//堆耗尽
for(i=0;i 4、rtexLen=vertexnum;
//在生成边表
if(edgenum<0)return(1);//无边的图
for(i=0;i 5、rst=q;
A[rear].data.OutDegree++;
A[front].data.InDegree++;
if(Type>2)
{
q=new Enode;
if(!q)return(0);
q->key=rear;
q->next=A[front].first;
A[front].first=q;
q->Weight=E[i].weight;
};
};
return(1);
};
void Graph::Dfs(int key,int &flag)
{
//static run= 6、1;
Enode *w;
A[key].tag=flag;
if(Type>2)cout<<"连通分量="< 7、gain....\n";
if(!(Find(v0,k))){cout<<"find=="< 8、nentnum);componentnum++;
};
};
return(componentnum-1);
};
int Graph::Bfs()
{
int i,comp=1; //comp=连通分量的标记,、、...
struct queue{int key;queue * next;};
Enode *pe;
queue *f,*r,*q,*p=new queue;
if(!p)return(-1); //堆耗尽
p->next=0;f=r=p; //生成空队列
for(i=0;i 9、)A[i].tag=0;//初始化已访问标志
for(i=0;i 10、p<<'\t';
cout<<"顶点键值="< 11、 if(f==r)f->next=p;
r->next=p;r=p;
};
pe=pe->next;
};//end of (pe)
delete q;
};//end of (f->next)
comp++;
};//enf of if
};
//释放队列
while(f){p=f->next;delete f;f=p;};
return comp-1; //返回连通分量数
};
/*
int Graph::TopoSort(int *que,int &num)
{ //que 12、顺序队列保存了拓扑排序的结果,f和r为que的头尾指示;loop用于判有无环
int i,f=0,r=0,index,loop=1;
Enode *pe;
num=0;
for(i=0;i 13、 for(pe=A[index].first;pe;pe=pe->next)
{
A[pe->key].tag--;
if(A[pe->key].tag==0){que[r++]=pe->key;A[pe->key].tag=-1;};
};
};
num=r;
if(r 14、Edge b[12];
b[0].Head=1;b[0].Tail=0;b[0].weight=1;
b[1].Head=3;b[1].Tail=1;b[1].weight=1;
b[2].Head=0;b[2].Tail=2;b[2].weight=1;
b[3].Head=1;b[3].Tail=4;b[3].weight=1;
b[4].Head=4;b[4].Tail=2;b[4].weight=1;
b[5].Head=4;b[5].Tail=3;b[5].weight=1;
//
b[6].Head=0;b[6].Tail=1;b[6].weight 15、1;
b[7].Head=1;b[7].Tail=3;b[7].weight=1;
b[8].Head=2;b[8].Tail=0;b[8].weight=1;
b[9].Head=4;b[9].Tail=1;b[9].weight=1;
b[10].Head=2;b[10].Tail=4;b[10].weight=1;
b[11].Head=3;b[11].Tail=2;b[11].weight=1;
//b=={{1,0,1},{3,1,1},{0,2,1},(1,4,1},{4,2,1},{2,3,1}};
g1.CreateGraph(num,b,6);
16、
if(g1.GetType()>2)cout<<"连通分量数="<






