您的位置:首页 > 其它

【无浪】无向图论_最小生成树三种算法

2014-12-23 00:32 274 查看
Prim算法,Kruskal算法,Sollin算法

#include<iostream>
#include<queue>
using namespace std;

#define Max 9999
#define Max_Number 10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define Status int
#define ElemType int

class Closedge{
public:
int adjvex;
int lowcost;
};

class SL{//边类
public:
int Start;
int End;
int Weight;
bool isSame(SL temp){//判断两条边是否相同
if(Start == temp.End && End == temp.Start)return true;
if(Start == temp.Start && End == temp.End)return true;
else return false;
}//isSame
};

bool judgethesame(SL *s1,SL temp,int length){  //判断边与边集的关系
for(int k=-1;++k<length;){
if(s1[k].isSame(temp))return true;
}//for
return false;
}//judgethesame

void sort(SL *s1,int length){//给边集排序
int temp;SL temp1;
for(int i=-1;++i<length;){
temp=i;
for(int j=i;++j<length;){ //注意j=i
if(s1[temp].Weight>s1[j].Weight){
temp=j;
}//if
}//for
if(temp!=i){
temp1=s1[i];
s1[i]=s1[temp];
s1[temp]=temp1;
}//if
}//for
}//sort

class Set{//集合类
private:
void renew(){
int *temp=new int[Max_size];
for(int i=-1;++i<size;){
temp[i]=s[i];
}//for
delete[]s;
s=temp;
}//renew
public:
int *s;  //元素数组
int size; //元素的个数
int Max_size;  //数组的最大长度
Set(){size=0;Max_size=10;s=new int[Max_size];}
bool judge(const int num){//判断是否有重复的数据
for(int i=-1;++i<size;){
if(s[i]==num)return true;
}//for
return false;
}//judge

Status InsertElem(const int num){
if(judge(num))return FALSE;//如果有重复的就不插入
if(size==Max_size){Max_size+=10;renew();}
s[size++]=num;
sort();
return TRUE;
}//InsertElem

Status sort(){        //集合内的元素排序
int temp,choose;
for(int i=-1;++i<size;){
choose=i;
for(int j=i;++j<size;){
if(s[choose]>s[j]){
choose=j;
}//if
}//for
if(choose!=i){
temp=s[i];
s[i]=s[choose];
s[choose]=temp;
}//if
}//for
return TRUE;
}//sort

void operator +(const Set set){//两个集合合并
int Size=set.size;
for(int i=-1;++i<Size;){
InsertElem(set.s[i]);
}//for
sort();
}//operator+

void display(){
for(int i=-1;++i<size-1;){
cout<<s[i]<<" ";
}//for
cout<<s[i]<<endl;
}//display
};

void delete_SL(Set *set,int index,int &size){
int i=index-1;
for(;++i<size;){
set[i]=set[i+1];
}
size--;
}//delete_SL
class Array{
private:
int *ArrayInt;
int size;
int Max_size;
void renew(int size){
int *temp=new int[size];
for(int i=-1;++i<this->size;){
temp[i]=ArrayInt[i];
}//for
delete[]ArrayInt;
ArrayInt=temp;
}//renew
public:
Array(){ArrayInt=NULL;size=0;}
Status setArrayInt(int size)
{
ArrayInt=new int[size];
Max_size=size;
this->size=0;
return OK;
}//setArrayInt
Status InsertInt(int Int)
{
if(size>=Max_size){
Max_size+=10;
renew(Max_size);
}//if
ArrayInt[size++]=Int;
return OK;
}//InsertInt
int getsize()
{
return size;
}//getsize
int getMax_size()
{
return Max_size;
}//getMax_size
int indexOf(int i)
{
return ArrayInt[i];
}//indexOf
};

class Vertex{
public:
int *D;
int *W;
int DSize;
int WSize;
int data;
Vertex():DSize(0),WSize(0){data=NULL;}
};

class Graph{
public:
Vertex *V;
int size;
int Max_number;
Graph():size(0)
{
V=new Vertex[Max_Number];
Max_number=Max_Number;
}//Graph
};

class Graph_Sq{
private:
void renew(Graph* &G){
G->Max_number+=Max_Number;
Vertex *temp=new Vertex[G->Max_number];
for(int i=-1;++i<G->size;){
temp[i]=G->V[i];
}//for
delete[]G->V;
G->V=temp;
}//renew
Status MakeVertex(Vertex* &v,ElemType data,Array D,Array W,bool _New){//创造顶点
if(_New)v=new Vertex();
v->data=data;
int DSize=D.getsize();
int WSize=W.getsize();
v->D=new int[DSize];
v->W=new int[WSize];
for(int i=-1;++i<DSize;){
v->D[i]=D.indexOf(i);
}//for
for(i=-1;++i<WSize;){
v->W[i]=W.indexOf(i);
}//for
v->DSize=DSize;
v->WSize=WSize;
return OK;
}//MakeVertex
void DFS(Graph G,int index,bool *Visit)//深度优先搜索
{
int i,temp;
Visit[index]=true;
IndexOf(G,index+1);
for(i=-1;++i<G.V[index].DSize;)
{
temp=G.V[index].D[i]-1;
if(!Visit[temp]){
DFS(G,temp,Visit);
}//if
}//for
}//DFS
void BFS(Graph G,int index,bool *Visit)//广度优先搜索
{
int i,temp;
queue<int>q;
Visit[index]=true;
IndexOf(G,index+1);
for(i=-1;++i<G.V[index].DSize;)
{
q.push(G.V[index].D[i]-1);
}//for
while(!q.empty())
{
index=q.front();
q.pop();
if(!Visit[index]){
Visit[index]=true;
IndexOf(G,index+1);
for(i=-1;++i<G.V[index].DSize;){
temp=G.V[index].D[i]-1;
if(!Visit[temp])	q.push(temp);
}//for
}//if
}//while
}//DFS
public:
Status InsertVertex(Graph* &G,Vertex* &v){ //将创建好的顶点插入到顶点数组中
if(G->size==G->Max_number-1){
renew(G);
}
G->V[G->size++]=*v;
return	OK;
}//InsertVertex
Status CreateAndInsertVertex(Graph* &G,Vertex* &v,ElemType data,Array D,Array W,bool _New){//一个函数完成创造和插入结点
MakeVertex(v,data,D,W,_New);
InsertVertex(G,v);
return OK;
}//CreateAndInsertVertex
Status ChangeTheVertex(Graph* &G,int index,ElemType data,Array D,Array W)//改变结点数据
{
if(index<=0 || index>G->size)return ERROR;
Vertex *v=&G->V[index-1];
MakeVertex(v,data,D,W,false);
return OK;
}//ChangeTheVertex
Status CreateGraph(Graph&G,Vertex v[],int size){//使用顶点数组创造图
G.Max_number=size;
G.size=size;
G.V=new Vertex[size];

for(int i=-1;++i<size;){
G.V[i]=v[i];
}//for
return OK;
}//CreateGraph
void IndexOf(Graph G,int index)
{
if(index<=0||index>G.size)return;
cout<<index-1<<" V"<<index<<" DATA "<<G.V[index-1].data<<" ";
cout<<"D ";
for(int j=-1;++j<G.V[index-1].DSize;){
cout<<G.V[index-1].D[j]<<" ";
}//for
cout<<"DW ";
for(j=-1;++j<G.V[index-1].WSize-1;){
cout<<G.V[index-1].W[j]<<" ";
}cout<<G.V[index-1].W[j]<<endl;
}//IndexOf
Status DFS(Graph G,int index)
{
if(index>G.size || index<=0)return ERROR;
int num=G.size,i;
bool *Visit=new bool[num];
for(i=-1;++i<num;)Visit[i]=false;//用于判断结点是否已被访问
DFS(G,index-1,Visit);
return OK;
}//DFS
Status BFS(Graph G,int index)
{
if(index>G.size || index<=0)return ERROR;
int num=G.size,i;
bool *Visit=new bool[num];
for(i=-1;++i<num;)Visit[i]=false;//用于判断结点是否已被访问
BFS(G,index-1,Visit);
return OK;
}//BFS
int **Dijkstra(Graph G,int index){//迪杰斯特拉算法求最短路径
//index,入度值和出度值都指的是顶点下标
int Size=G.size;   //获得顶点个数
if(index<=0||index>Size)return ERROR;   //如果起点在顶点以外则返回ERROR
int DSize=G.V[index-1].DSize;         //获得顶点的出度
int temp,sum,i,j;                       //temp用于临时存储出度的顶点  sum用于存储路径和
queue<int>q;                            //队列,用于遍历
int **path=new int*[Size];              //path用于获得最短路径所经过的顶点
bool *final=new bool[Size];             //判断顶点是否已访问
int *dist=new int[Size];                //获得最小权值,无穷大用9999表示
int *len=new int[Size];                 //获得每个最短路径经过的顶点数
//初始化
for(i=-1;++i<Size;){
path[i]=new int[Size];
for(j=-1;++j<Size;){
path[i][j]=-1;
}//for
path[i][0]=index;                    //第一列全部标记为起点下标
len[i]=1;                            //已经标记了一个顶点
final[i]=false;                      //全部标记为未访问
dist[i]=Max;                         //全部设为9999
}//for
final[index-1]=true;                     //起点设为已访问
for(i=-1;++i<DSize;){
temp=G.V[index-1].D[i];             //temp临时存储起点的出度顶点
path[temp-1][1]=temp;                //第二列存储起点的出度顶点
len[temp-1]++;                       //因为该行增加了一个顶点,所以该行的大小增加1
dist[temp-1]=G.V[index-1].W[i];//可以标记一个权值了
q.push(temp);                        //把起点所有的出度顶点入队用于遍历
}//for
while(!q.empty()){
index=q.front();                     //获取队首元素
q.pop();                             //队首元素出队
if(!final[index-1]){                 //如果队首元素被访问过
DSize=G.V[index-1].DSize;      //获取对应顶点的出度
final[index-1]=true;             //标记为已访问
//开始访问它的所有出度顶点
for(i=-1;++i<DSize;){
temp=G.V[index-1].D[i];     //按顺序获取出度顶点
if(!final[temp-1]){          //如果该顶点没有被访问过
q.push(temp);            //把该顶点入队
sum=dist[index-1]+G.V[index-1].W[i];      //起始顶点到这一顶点的权值 加上 这一顶点到其出度顶点的权值
if(sum<dist[temp-1]){                            //把 sum 与 起始顶点到出度顶点的权值 比较 如果小于则执行以下语句
dist[temp-1]=sum;                            //起始顶点 到 这一出度顶点的权值 换成sum
//Path数组中把 这一顶点那一行 覆盖 出度顶点 的那一行
for(j=-1;++j<len[index-1];){
path[temp-1][j]=path[index-1][j];
}path[temp-1][j]=temp;
//出度顶点那一行的长度+1
len[temp-1]=len[index-1]+1;
}//if
}//if
}//for
}//if
}//while
cout<<"输出dist数组..."<<endl;
for(i=-1;++i<Size;)
cout<<dist[i]<<" ";
cout<<endl;
return path;
}//Dijkstra
void display(Graph G)//输出该图的矩阵存储结构
{
int i;
cout<<G.size<<"/"<<G.Max_number<<endl;
for(i=-1;++i<G.size;){
cout<<i<<" V"<<i+1<<" DATA "<<G.V[i].data<<" ";

cout<<"D ";
for(int j=-1;++j<G.V[i].DSize;){
cout<<G.V[i].D[j]<<" ";
}//for

cout<<"W ";
for(j=-1;++j<G.V[i].WSize-1;){
cout<<G.V[i].W[j]<<" ";
}cout<<G.V[i].W[j]<<endl;
}//for
}//display
int minmum(Closedge* closedge,int size){
int temp=closedge[0].lowcost,k=0,i=0;
while(!temp && i<size){
temp=closedge[i+1].lowcost;
i++;
k=i;
}//if
for(;++i<size;){
if(closedge[i].lowcost && temp>closedge[i].lowcost){
temp=closedge[i].lowcost;
k=i;
}//if
}//for
return k;
}//minmum
void display_CloseVertex(Graph G,int i){//输出该顶点指向的邻接顶点
int j=-1;
queue<int> q;
if(i<=0||i>G.size)return;
i--;
for(;++j<G.V[i].DSize;)
{
q.push(G.V[i].D[j]);
}//for
while(!q.empty()){
j=q.front();
q.pop();
IndexOf(G,j);
}//while
}//display_CloseVertex
void Prim(Graph G,int index){
int Size=G.size;
if(index<=0||index>Size)return;
cout<<"Prim算法"<<endl;
//将整个图压缩成一个二维矩阵
int **Matrix=new int*[Size];
for(int i=-1;++i<Size;){
Matrix[i]=new int[Size];
for(int j=-1;++j<Size;){
Matrix[i][j]=Max;
for(int k=-1;++k<G.V[i].DSize;){
Matrix[i][G.V[i].D[k]-1]=G.V[i].W[k];
}//for
//cout<<Matrix[i][j]<<" ";
}//for
//cout<<endl;
}//for

//用k来表示当前访问的结点
int k=index;
//Closedge{adjvex,lowcost}  adjvex表示当前为adjvex这个编号的结点指向以下标+1为编号的结点所需要的权值比其它边都要小
//lowcost记录这个权值最小的边
Closedge *closedge=new Closedge[Size];
//初始化closedge数组,把index(即k)这个编号的结点指向其它Size-1个结点的权值储存进lowcost来 adjvex全部赋值为index
//如果index与某个结点不相通的话直接赋值为0,在以后的判断中会把0排除掉
for(i=-1;++i<Size;){
closedge[i].lowcost=0;
closedge[i].adjvex=0;
if(i!=k-1){
closedge[i].adjvex=k;
closedge[i].lowcost=Matrix[k-1][i];
}//if
}//for
//起点已经访问过了,直接赋值为0
closedge[k-1].lowcost=0;
//开始遍历结点之旅
for(i=0;++i<Size;){
k=minmum(closedge,Size);  //minmum函数是获取closedge数组中lowcost最小的那个结点下标
//		cout<<"k="<<k<<" ";
cout<<closedge[k].adjvex-1<<"-"<<k<<endl;   //输出最小的那条边
index=k+1;                     //访问的结点改成查找到的结点
closedge[k].lowcost=0;         //设置已找到的结点为已访问
//对找到的结点进行对其它未访问的结点的遍历,由于已访问的结点lowcost已被标为0,所以if判断语句无法通过
for(int j=-1;++j<G.size;)
{
if(Matrix[k][j]<closedge[j].lowcost){  //如果这个结点访问某个结点的权值比上一个结点访问同一个结点的权值要小
//更改成更小的权值
closedge[j].adjvex=k+1;
closedge[j].lowcost=Matrix[k][j];
}//if
//	cout<<closedge[j].adjvex<<" "<<closedge[j].lowcost<<endl;
}//for
}//for
for(i=-1;++i<Size;){
delete[]Matrix[i];
}//for
delete[]Matrix;
delete[]closedge;
}//Prim
void Kruskal(Graph G){
int Size=G.size,length=0;
//	if(index<=0||index>Size)return;
cout<<"Kruskal算法"<<endl;
//第一步. 将整个图压缩成一个二维矩阵
int **Matrix=new int*[Size];
for(int i=-1;++i<Size;){
Matrix[i]=new int[Size];
for(int j=-1;++j<Size;){
Matrix[i][j]=Max;
for(int k=-1;++k<G.V[i].DSize;){
Matrix[i][G.V[i].D[k]-1]=G.V[i].W[k];
}//for
//	cout<<Matrix[i][j]<<" ";
}//for
//	cout<<endl;
}//for
//第二步. 把不重复的边筛选出来存在边集里面
SL *sl=new SL[(Size-1)*Size],temp_SL;//一个图拥有n(n-1)条边
int temp_Weight;
for(i=-1;++i<Size;){
temp_SL.Start=i;
for(int j=i;++j<Size;){
temp_Weight=Matrix[i][j];
if(temp_Weight!=Max){
temp_SL.End=j;
temp_SL.Weight=temp_Weight;
sl[length++]=temp_SL;
}//if
}//for
}//for
sort(sl,length);//第三步. 把边按照权值排序
/*cout<<length<<endl;
for(i=-1;++i<length;){
cout<<i<<" "<<sl[i].Start<<" "<<sl[i].End<<" "<<sl[i].Weight<<endl;
}//for*/
//第四步. 克鲁斯卡尔算法从现在开始
Set *SS=new Set[100];//建立一个集合数组{}
int size=1,First,Second,Start,End,temp;
//第一条边直接插入
SS[0].InsertElem(sl[0].Start);
SS[0].InsertElem(sl[0].End);
cout<<sl[0].Start<<"-"<<sl[0].End<<endl;
//集合数组为{{0,2}}
for(i=0;++i<length;){//遍历其余边
First=-1;
Second=-1;
Start=sl[i].Start;
End=sl[i].End;
for(int j=-1;++j<size;){
if(SS[j].judge(Start)){
First=j;
}//if
if(SS[j].judge(End)){
Second=j;
}//if
/*			if(First!=-1 && Second!=-1){
break;
}//if*/
}//for
//第一种情况,边的两个元素在边集中没有重复的,入队,集
if(First==-1 && Second ==-1){
SS[size].InsertElem(Start);
SS[size++].InsertElem(End);
cout<<Start<<"-"<<End<<endl;
}//if
//第二种情况,边的其中一个元素在边集中有重复的,入队,边的另一个元素插进该集合
else if(First==-1){
SS[Second].InsertElem(Start);
cout<<Start<<"-"<<End<<endl;
}//else if
else if(Second==-1){
SS[First].InsertElem(End);
cout<<Start<<"-"<<End<<endl;
}//else if
//第三种情况,边的一个元素在一个集合,边的另一个元素在另一个集合,入队,将两个集合合并
else if(First!=Second){
if(First>Second){
temp=First;
First=Second;
Second=temp;
}//if
SS[First]+SS[Second];
delete_SL(SS,Second,size);
cout<<Start<<"-"<<End<<endl;
}//else if
//第四种情况,边的两个元素是其中一个集合的子集,不考虑
/*for(int k=-1;++k<size;){
SS[k].display();
}//for
cout<<endl;*/
}//for

delete[]sl;
for(i=-1;++i<Size;){
delete[]Matrix[i];
}//for
delete[]Matrix;
}//Kruskal

void Sollin(Graph G){
int Size=G.size,length=0;
//	if(index<=0||index>Size)return;
cout<<"Sollin算法"<<endl;
//第一步. 将整个图压缩成一个二维矩阵
int **Matrix=new int*[Size];
for(int i=-1;++i<Size;){
Matrix[i]=new int[Size];
for(int j=-1;++j<Size;){
Matrix[i][j]=Max;
for(int k=-1;++k<G.V[i].DSize;){
Matrix[i][G.V[i].D[k]-1]=G.V[i].W[k];
}//for
//	cout<<Matrix[i][j]<<" ";
}//for
//	cout<<endl;
}//for
//第二步,遍历每个结点,获得每个结点与其它结点权值最小的那条边,加入边集当中
Set *SS=new Set[100];
int size=0,First,Second,Start,End,temp;
for(i=-1;++i<Size;){
temp=0;
Start=i;
End=0;
First=-1;
Second=-1;
for(int j=0;++j<Size;){
if(Matrix[i][temp]>Matrix[i][j]){
temp=j;
}//if
}//for
End=temp;
for(j=-1;++j<size;){
if(SS[j].judge(Start)){
First=j;
}//if
if(SS[j].judge(End)){
Second=j;
}//if
/*			if(First!=-1 && Second!=-1){
break;
}//if*/
}//for
//第一种情况,边的两个元素在边集中没有重复的,入队,集
if(First==-1 && Second ==-1){
SS[size].InsertElem(Start);
SS[size++].InsertElem(End);
cout<<Start<<"-"<<End<<endl;
}//if
//第二种情况,边的其中一个元素在边集中有重复的,入队,边的另一个元素插进该集合
else if(First==-1){
SS[Second].InsertElem(Start);
cout<<Start<<"-"<<End<<endl;
}//else if
else if(Second==-1){
SS[First].InsertElem(End);
cout<<Start<<"-"<<End<<endl;
}//else if
//第三种情况,边的一个元素在一个集合,边的另一个元素在另一个集合,入队,将两个集合合并
else if(First!=Second){
if(First>Second){
temp=First;
First=Second;
Second=temp;
}//if
SS[First]+SS[Second];
delete_SL(SS,Second,size);
cout<<Start<<"-"<<End<<endl;
}//else if
}//for
int temp_Start,temp_End,temp_Weight;
//第三步,在边集中,树与树之间选择一条权值最小的边合在一起,最终只剩下一个集合。
for(i=-1;++i<size-1;){
Start=SS[i].s[0];
for(int a=i;++a<size;){
End=SS[a].s[0];
temp=Matrix[Start][End];
for(int j=-1;++j<SS[i].size;){
for(int k=-1;++k<SS[a].size;){
temp_Start=SS[i].s[j];
temp_End=SS[a].s[k];
temp_Weight=Matrix[temp_Start][temp_End];
if(temp>temp_Weight){
temp=temp_Weight;
Start=temp_Start;
End=temp_End;
}//if
}//for
}//for
}//for
cout<<Start<<"-"<<End<<endl;
}//for
}//Sollin
//造成中间那个图的二叉树构建不同的原因是因为两棵树的连接边都为5,克鲁斯卡尔选择了下面的,Sollin选择了上面的
};

int main(){
Graph *G=new Graph();
Graph_Sq GS;
Vertex *v=new Vertex();
Array D;
Array W;
/*//建立V1顶点,度为2,  2  6, 权 28 10
D.setArrayInt(2);
W.setArrayInt(2);
D.InsertInt(2);D.InsertInt(6);
W.InsertInt(28);W.InsertInt(10);
GS.CreateAndInsertVertex(G,v,'a',D,W,true);
//建立V2顶点,度为3,D 1 3 7 , 权 28 16 14
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(1);
D.InsertInt(3);
D.InsertInt(7);
W.InsertInt(28);
W.InsertInt(16);
W.InsertInt(14);
GS.CreateAndInsertVertex(G,v,'b',D,W,true);
//建立V3顶点,度为2,D 2 4 , 权 16 12
D.setArrayInt(2);
W.setArrayInt(2);
D.InsertInt(2);
D.InsertInt(4);
W.InsertInt(16);
W.InsertInt(12);
GS.CreateAndInsertVertex(G,v,'c',D,W,true);
//建立V4顶点,度为3,D 3 5 7, 权 12 22 18
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(3);
D.InsertInt(5);
D.InsertInt(7);
W.InsertInt(12);
W.InsertInt(22);
W.InsertInt(18);
GS.CreateAndInsertVertex(G,v,'d',D,W,true);
//建立V5顶点,度为3,D 4 6 7, 权 22 25 24
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(4);
D.InsertInt(6);
D.InsertInt(7);
W.InsertInt(22);
W.InsertInt(25);
W.InsertInt(24);
GS.CreateAndInsertVertex(G,v,'e',D,W,true);
//建立V6顶点,度为2,D 1 5 , 权 10 25
D.setArrayInt(2);
W.setArrayInt(2);
D.InsertInt(1);
D.InsertInt(5);
W.InsertInt(10);
W.InsertInt(25);
GS.CreateAndInsertVertex(G,v,'f',D,W,true);
//建立V7顶点,度为3,D 2 4 5,W 14 18 24
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(2);
D.InsertInt(4);
D.InsertInt(5);
W.InsertInt(14);
W.InsertInt(18);
W.InsertInt(24);
GS.CreateAndInsertVertex(G,v,'g',D,W,true);*/

/*	cout<<"按顺序输出一遍..."<<endl;
GS.display(*G);
cout<<endl;
cout<<"输出V6的邻接顶点..."<<endl;
GS.display_CloseVertex(*G,6);
cout<<endl;
cout<<"从V2开始深度优先搜索..."<<endl;
GS.DFS(*G,2);
cout<<"从V2开始广度优先搜索..."<<endl;
GS.BFS(*G,2);
//	cout<<"输出V6顶点并改变其值..."<<endl;
//	GS.IndexOf(*G,6);
//	GS.ChangeTheVertex(G,6,'z',D,W);
//	GS.IndexOf(*G,6);
int **path=GS.Dijkstra(*G,1);
cout<<"输出Path数组..."<<endl;
for(int i=-1;++i<7;)
{
for(int j=-1;path[i][++j]!=-1;)
{
cout<<path[i][j]<<" ";
}
cout<<endl;
}*/
/*	//建立顶点V1,度为3:  2 3 4。 权  6 1 5
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(2);D.InsertInt(3);D.InsertInt(4);
W.InsertInt(6);W.InsertInt(1);W.InsertInt(5);
GS.CreateAndInsertVertex(G,v,'a',D,W,true);
//建立顶点V2,度为3:  1 3 5,   权   6 5 3
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(1);D.InsertInt(3);D.InsertInt(5);
W.InsertInt(6);W.InsertInt(5);W.InsertInt(3);
GS.CreateAndInsertVertex(G,v,'b',D,W,true);
//建立顶点V3,度为4:  2,4,5,6,权 5 6 5 4
D.setArrayInt(4);
W.setArrayInt(4);
D.InsertInt(2);D.InsertInt(4);D.InsertInt(5);D.InsertInt(6);
W.InsertInt(5);W.InsertInt(5);W.InsertInt(6);W.InsertInt(4);
GS.CreateAndInsertVertex(G,v,'c',D,W,true);
//建立顶点V4,度为3:  1 3 6,  权 5 5 2
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(1);D.InsertInt(3);D.InsertInt(6);
W.InsertInt(5);W.InsertInt(5);W.InsertInt(2);
GS.CreateAndInsertVertex(G,v,'d',D,W,true);
//建立顶点V5,度为3:  2 3 6,  权 3 6 6
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(2);D.InsertInt(3);D.InsertInt(6);
W.InsertInt(3);W.InsertInt(6);W.InsertInt(6);
GS.CreateAndInsertVertex(G,v,'e',D,W,true);
//建立顶点V6,度为3:   3 4 5,  权 4 2 6
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(3);D.InsertInt(4);D.InsertInt(5);
W.InsertInt(4);W.InsertInt(2);W.InsertInt(6);
GS.CreateAndInsertVertex(G,v,'f',D,W,true);*/
//建立顶点V1,度为4:  2 3 4 6,  权 5 8 7 3
D.setArrayInt(3);
W.setArrayInt(3);
D.InsertInt(2);D.InsertInt(3);D.InsertInt(4);D.InsertInt(6);
W.InsertInt(5);W.InsertInt(8);W.InsertInt(7);W.InsertInt(3);
GS.CreateAndInsertVertex(G,v,'a',D,W,true);
//建立顶点V2,度为2:  1 3,    权 5 4
D.setArrayInt(2);
W.setArrayInt(2);
D.InsertInt(1);D.InsertInt(3);
W.InsertInt(5);W.InsertInt(4);
GS.CreateAndInsertVertex(G,v,'b',D,W,true);
//建立顶点V3,度为4:  1 2 4 6,权 8 4 5 9
D.setArrayInt(4);
W.setArrayInt(4);
D.InsertInt(1);D.InsertInt(2);D.InsertInt(4);D.InsertInt(6);
W.InsertInt(8);W.InsertInt(4);W.InsertInt(5);W.InsertInt(9);
GS.CreateAndInsertVertex(G,v,'c',D,W,true);
//建立顶点V4,度为4:  1 3 5 6,权 7 5 5 6
D.setArrayInt(4);
W.setArrayInt(4);
D.InsertInt(1);D.InsertInt(3);D.InsertInt(5);D.InsertInt(6);
W.InsertInt(7);W.InsertInt(5);W.InsertInt(5);W.InsertInt(6);
GS.CreateAndInsertVertex(G,v,'d',D,W,true);
//建立顶点V5,度为2:  4 6,    权 5 1
D.setArrayInt(2);
W.setArrayInt(2);
D.InsertInt(4);D.InsertInt(6);
W.InsertInt(5);W.InsertInt(1);
GS.CreateAndInsertVertex(G,v,'e',D,W,true);
//建立顶点V6,度为4:  1 3 4 5,权 3 9 6 1
D.setArrayInt(4);
W.setArrayInt(4);
D.InsertInt(1);D.InsertInt(3);D.InsertInt(4);D.InsertInt(5);
W.InsertInt(3);W.InsertInt(9);W.InsertInt(6);W.InsertInt(1);
GS.CreateAndInsertVertex(G,v,'f',D,W,true);
//	GS.Prim(*G,4);
//	GS.Kruskal(*G);
GS.Sollin(*G);
return 0;
}//main
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: