zoukankan      html  css  js  c++  java
  • 图的一系列算法(待续)

    #include<iostream>
    using namespace std;
    
    #define INFINITY INT_MAX
    #define MAX_VERTEX_NUM 20
    typedef enum {DG, DN, UDG, UDN} GraphKind;
    
    //邻接矩阵
    typedef struct ArcCell
    {
    	int key;    //对于无权图,用1或0表示相邻否,对带权图,则为权值类型
    } ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];   //这种定义数组的方式一定要记住
    
    typedef struct {
       int vexs[MAX_VERTEX_NUM];
       AdjMatrix arcs;
       int vexnum,arcnum; //图的当前定点数和弧边数
       GraphKind kind;
    }MGraph;
    
    //邻接表
    typedef struct ArcNode
    {
         int adjvex;
    	 struct ArcNode *nextarc;
    }ArcNode;
    
    typedef struct VNode
    {
    	int key;
    	ArcNode *firstarc;
    }VNode,AdjList[MAX_VERTEX_NUM];
    
    typedef struct {
    	AdjList vertices;
    	int vexnum,arcnum;
    	GraphKind kind;
    } ALGraph;
    
    void CreateUD(MGraph &G)      //用邻接矩阵无向图
    {  
       int i,j;
       cout<<"Input vexnum:"<<endl;
       cin>>G.vexnum;
       cout<<"Input arcnum:"<<endl;
       cin>>G.arcnum;
       for(  i=0; i<G.vexnum; ++i)
       {
    	   for( j=0; j<G.vexnum; ++j)
    	   {
    		   G.arcs[i][j].key = 0;
    	   }
       }
       int v1,v2;
       for( int k=0; k<G.arcnum; ++k)
       {
          cout<<"Input two index:"<<endl;
    	  cin>>v1>>v2;
    	  G.arcs[v1][v2].key=1;
    	  G.arcs[v2][v1].key = G.arcs[v1][v2].key;  //有向图和无向图的区别
       }
       cout<<"Graph:"<<endl;
       for( i=0; i<G.vexnum; ++i)
       {
    	   for( j=0; j<G.vexnum; ++j)
    	   {
    		   cout<<G.arcs[i][j].key<<" ";
    	   }
    	   cout<<endl;
       }
    }
    
    
    void CreateDG(ALGraph &G)      //用邻接表创建有向图
    {
       int i;
       cout<<"Input vexnum:"<<endl;
       cin>>G.vexnum;
       cout<<"Input arcnum:"<<endl;
       cin>>G.arcnum;
       for( i=0; i<G.vexnum; i++)
       {
    	   G.vertices[i].key=i;
    	   G.vertices[i].firstarc=NULL;
       }
       int v1,v2;
       for( int k=0; k<G.arcnum; ++k)
       {
          cout<<"Input two index:"<<endl;
    	  cin>>v1>>v2;
    	  if( v1==v2 )
    	  {
    		  --k;
    		  cout<<"two index are same, renew input:"<<endl;
    		  continue;
    	  }
    	  ArcNode *node = (ArcNode *)malloc(sizeof(ArcNode));
    	  node->adjvex=v2;
    	  node->nextarc=NULL;
          if(G.vertices[v1].firstarc==NULL)
    	  {
             G.vertices[v1].firstarc=node;
    	  }
    	  else
    	  {   
    		  ArcNode *next=G.vertices[v1].firstarc;
    		  while(next->nextarc)
    		  {
    			  next=next->nextarc;
    		  }
    		  next->nextarc=node;
    	  }
    	  
       }
       for( int j=0; j<G.vexnum; j++)
       {
           cout<<G.vertices[j].key<<" :";
    	   ArcNode *next=G.vertices[j].firstarc;
    	   while( next!=NULL)
    	   {
    		   cout<<next->adjvex<<" ";
    		   next=next->nextarc;
    	   }
    	   cout<<endl;
       }
    }


  • 相关阅读:
    DELETE和DELETE FROM有什么区别
    [转]DBA,SYSDBA,SYSOPER三者的区别
    DML语言练习,数据增删改查,复制清空表
    Oracle数据库sys为什么只能以sysdba登录
    Oracle添加数据文件创建表空间,创建用户代码
    ORACLE建表练习
    全局唯一标识符(GUID)
    [转]Java总结篇系列:Java泛型
    Strategy模式
    Android 第三方应用广告拦截实现
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3315417.html
Copyright © 2011-2022 走看看