The basic operation 03 of FIG.

  1. Adjacency matrix operation
#include<stdio.h>
#include<stdlib.h>
#define VERTEX_NUM 64
typedef struct
{   char  VertexArray[VERTEX_NUM];  //顶点数组
    int   AdjMatrix[VERTEX_NUM][ VERTEX_NUM];  //邻接矩阵
    int  VexNum, EdgeNum;
} AM_Graph;

/*=======================================
函数功能:无向图邻接矩阵的建立
函数输入:邻接矩阵空间地址
函数输出:无
键盘输入:顶点值、边的关联顶点
=========================================*/
void creat_AdjMatrix(AM_Graph *gPtr)
{
	int i,j,k;
	getchar();
	printf("请输入%d个顶点:",gPtr->VexNum);
	for(i=0;i<gPtr->VexNum;i++)
		scanf("%c",&gPtr->VertexArray[i]);
	for(i=0;i<gPtr->VexNum;i++)
		for(j=0;j<gPtr->VexNum;j++)
			gPtr->AdjMatrix[i][j]=0;
		printf("输入邻接的两个顶点下标:\n");
		for(k=0;k<gPtr->EdgeNum;k++)
		{
			scanf("%d%d",&i,&j);
			gPtr->AdjMatrix[i][j]=1;
			gPtr->AdjMatrix[j][i]=1;
		}
}

/*=======================================
函数功能:无向图邻接矩阵的输出
函数输入:邻接矩阵空间地址
函数输出:无
屏幕输出:邻接矩阵
=========================================*/
void print_AdjMatrix(AM_Graph *gPtr) 
{
	int i,j;
	printf("建立好后的无向图的邻接矩阵为:\n");
	for(i=0;i<gPtr->VexNum;i++)
	{
		printf("%c  ",gPtr->VertexArray[i]);
	for(j=0;j<gPtr->VexNum;j++)
	   printf("%d  ",gPtr->AdjMatrix[i][j]);
	printf("\n");
	}
}

int main()
{ 
AM_Graph Graph;

       printf("请输入顶点数和边数:");
	   scanf("%d%d",&Graph.VexNum, &Graph.EdgeNum);
	   creat_AdjMatrix(&Graph);
	   print_AdjMatrix(&Graph);
	  return 0; 
}
  1. Table operating abutting
    adjacency table of FIG establishment, need to know the information including the number of vertices of graph, the number of edges, and each side of the starting and ending number.
    FIG different to the undirected graph processing method: for undirected graphs, the two vertices of an edge v1, v2 Pro mutual contacts. When stored, is inserted into the single linked list header starting v1 side of the junction, i.e. the end v2;. Then the end v2 single linked list head insertion side of the junction, i.e. starting v1 for directed graphs, the starting point v1 single linked list header inserting an edge node, i.e. the end v2.
    Here Insert Picture Description
#include<stdio.h>
#include<stdlib.h>
#define  VERTEX_NUM  6        	//图的顶点数
#define  VERTEX_MAX_NUM 10   	//图的最大顶点数
#define  EDGE_MAX_NUM  20   		//图的最大边数
typedef char VexType;  				//顶点的数据类型
typedef int InfoType;   				//弧(边)的相关信息类型,如权值、边存在或不存在等

typedef struct AdjNode  				//邻接结点结构
{     
   int adjvex;        				//邻接点
   AdjNode *next;   				//邻接点指针
} AL_AdjNode; 

typedef struct  						//邻接表顶点结点结构
{             
   VexType  vertex;   				//顶点
   AdjNode *link;     				//邻接点头指针
} AL_VexNode; 

typedef struct  						//总的邻接表结构
{                 
   AL_VexNode  VexList[VERTEX_MAX_NUM]; //顶点表
   int VexNum, ArcNum;    			//顶点数,弧(边)数
} AL_Graph; 

/*================================
函数功能:建立有向图的邻接表结构
函数输入:无
函数输出:邻接表
键盘输入:图的顶点数、边数、边信息
================================*/
AL_Graph *Create_AdjList()
{         
   int n,e,i,v1,v2; 
   AL_AdjNode * AdjPtr;    			//定义邻接结点指针
   AL_Graph *alPtr;   				//定义邻接表指针
   alPtr=(AL_Graph *)malloc(sizeof(AL_Graph)); 	//申请总的邻接表空间
   printf("请输入图的顶点数:\n"); 
   scanf("%d",&n);    				//输入顶点数
   for(i=0;i<=n;i++)					//初始化邻接表空间
   { 
       alPtr->VexList[i].vertex=(char)i; 	//给头结点赋值
       alPtr->VexList[i].link=NULL;   	//初始化头结点
   } 
    printf("请输入边数:\n"); 
    scanf("%d",&e);   				//输入边的数目 
    printf("请输入弧的信息:\n"); 
    for(i=0;i<e;i++)
	{ 
        printf("请输入弧的两个端点,第一个为弧头,第二个为弧尾\n");
        scanf("%d%d",&v1,&v2);    	//输入边的两个结点
        AdjPtr =(AL_AdjNode *)malloc(sizeof(AL_AdjNode));  //申请新邻接结点
        AdjPtr->adjvex=v2;      		//v2做新邻接点编号
        AdjPtr->next=alPtr->VexList[v1].link;  	//新邻接点链入v1为顶点的邻接链表表头
        alPtr->VexList[v1].link=AdjPtr;  	 //使头结点指向新结点
    } 
    alPtr->VexNum=n;     			//将顶点数n给alPtr->VexNum
    alPtr->ArcNum=e;     			//将边数e给alPtr->ArcNum
    return alPtr;   					//返回该邻接表
} 
/*================================
函数功能:输出邻接表到屏幕
函数输入:邻接表地址
函数输出:无
=================================*/
void Print_AdjList(AL_Graph *algPtr)
{     
    int i; 
    AL_AdjNode *AdjPtr;     
    printf("图的邻接表\n"); 
    for(i=0;i<algPtr->VexNum;i++)		//当在结点个数范围内时
	{     
//输出顶点表中第i个顶点的值       
printf("%d-",algPtr->VexList[i].vertex);          
        							//取第i个邻接链表的首地址
AdjPtr=algPtr->VexList[i].link;   
        while(AdjPtr!=NULL)			//邻接链表非空
		{     
            printf("%d-",AdjPtr->adjvex);	//输出邻接结点
            AdjPtr=AdjPtr->next;    	//取下一个邻接结点地址
 		} 
        printf("--\n"); 
    } 
} 
int main()
{ 
    AL_Graph *gPtr;
    gPtr=Create_AdjList();
    Print_AdjList(gPtr);
    return 0; 
}

Arrangements examination
of Communication Engineering, there are nine courses to mid-term exam, exam courses can not be represented in the set R, in which a pair of parentheses represents the number of the corresponding course examination there is a conflict, how arrangements for conflict-free exam and exam minimum number of days?
Course No. = {1, 2, 3, 4, 5, 6, 7, 8, 9}
conflict course R & lt = {(2, 8), (9, 4), (2, 9), (2, 1) , (2, 5), (6, 2), (5, 9), (5, 6), (5, 4), (7, 5), (7, 6), (3, 7), ( 6, 3)}

Analysis:
it can be observed from the conflict between the course and the course, the course map may correspond to a set of vertices, the program may correspond to the relationship between the set of edges in FIG. Therefore, the problem and its associated information can be represented by FIG. An apex edge at both ends of a conflict can be marked with different colors, such arrangements examinations translates into a graph coloring problem - as little color to each vertex shading in the figure, the adjacent vertex with different colors.
If the descending order according to the degree, then 11 coloring, each point first try painted the first color, if there is a conflict with the already painted a point, then change to another color until the color does not clash so far, this graph coloring algorithm that is Welsh-Powell (Powell Welch *) method.

Algorithm Description
Here Insert Picture Description
Pseudocode describing a
Here Insert Picture Description
pseudo code description of two
Here Insert Picture Description
data structure design

The vertex of the same color in a node in the array, the maximum array length of N, the array has the final position of the vertex rear by a tail pointer record. ColorSet color set color is also the maximum number N, i.e., the color of each vertex is not the same. If each color is used by the used flag, it is not set with 0, 1 as used.
Here Insert Picture Description

struct ColorNode
{
     int used; // 标记颜色是否被用,0代表未用
     int rear; //集合尾指针
     int node[N];//同色顶点集合
} ColorSet[N] //颜色集 

Program realization

#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define N 9 				//顶点数
int AdjMatrix[N][N]= {		//邻接矩阵
	{0,1,0,0,0,0,0,0,0},
	{1,0,0,0,1,1,0,1,1},
	{0,0,0,0,0,1,1,0,0},
	{0,0,0,0,1,0,0,0,1},
	{0,1,0,1,0,1,1,0,1},
	{0,1,1,0,1,0,1,0,0},
	{0,0,1,0,1,1,0,0,0},
	{0,1,0,0,0,0,0,0,0},
	{0,1,0,1,1,0,0,0,0}
};
int degree[N]= {0};  					//记录顶点的degree数目
char *color[N]= {"红","橙","黄","绿","青","蓝","紫","黑","白"};

struct ColorNode {
	int used; 						//标记颜色是否被用,0代表未用
	int rear; 						//顶点集合尾指针
	int node[N];					//同色顶点集合
} ColorSet[N]= {{0,0,0,0}};	//颜色集
/*============================
函数功能:找度最大的结点下标
函数输入:顶点度数组
函数输出:度最大的结点下标
=============================*/
int FindMax(int *a) {
	int i,value,index;
	value=-1;
	index=0;
	for(i=0; i<N; i++) {
		if(value < a[i]) {
			value=a[i];
			index=i;
		}
	}
	a[index]=-1;					//清除当前最大值
	return index;
}

/*=========================================
函数功能:判断k点是否能加入颜色集中第i种颜色顶点集
函数输入:第i种颜色,k结点
函数输出:1——可以加入
          0——不能加入
==========================================*/
int judge(int i,int k) {
	int p,q,m;

	p=0;
	q=ColorSet[i].rear;
	m=ColorSet[i].node[p];			//颜色集中下标为p的结点
	//k、p不是邻接点且p不是颜色集中的最后一个结点
	while (AdjMatrix[k][m]==0 && p!=ColorSet[i].rear) { //条件二判断ColorSet的node不为空且结束在集合中查找
		p++;
		m=ColorSet[i].node[p];			//颜色集中下标为p的结点
	}
	if (p==q)return 1;				//k可以加入颜色集
	return 0;						//k不能加入颜色集
}

/*=========================================
函数功能:Welsh_Powell图结点染色法
函数输入:无
函数输出:无
屏幕输出:同色结点集合
==========================================*/
void Welsh_Powell() {
	int i,k;
	int colorPtr;

	//计算顶点的degree
	for (i=0; i<N; ++i) {
		for (int j=0; j<N; ++j) {
			if (i != j && AdjMatrix[i][j])
				degree[i]++;
		}
	}

	for (int j=0; j<N; ++j) {
		k=FindMax(degree);//找度最大结点k
		colorPtr=0;
		//colorPtr项颜色已经使用过
		if(	ColorSet[colorPtr].used==1) {
			while(!judge(colorPtr,k))//若k不能加入colorPtr项的颜色集
				colorPtr++;
		}
		//将k加入colorPtr项颜色集
		ColorSet[colorPtr].node[ColorSet[colorPtr].rear++]=k;
		if(	ColorSet[colorPtr].used==0) ColorSet[colorPtr].used=1;
	}
	//输出同色结点集合
	for (int j=0; j<N; ++j) {
		if (ColorSet[j].used==1) {
			printf("%s:",color[j]);
			for (i=0; i<ColorSet[j].rear; ++i)
				printf("%d ",ColorSet[j].node[i]+1);
			printf("\n");
		}
	}
}
int main() {
	Welsh_Powell();
	return 0;
}

A missing function in FindMax m = ColorSet [i] .node [p]; // color set index is the junction point p

It has been filled.

Published 26 original articles · won praise 3 · Views 1464

Guess you like

Origin blog.csdn.net/herui7322/article/details/104358860