C data structures and algorithms - the basis of consolidation - Figure 02: The two most common ways to create a graph

主要讲解图的最常用的两种创建方式,邻接矩阵和邻接表,代码实现

0x01. Adjacency matrix

Meaning: two arrays diagram showing information stored in a one-dimensional array of vertices, a two-dimensional array (the adjacency matrix) storing information or arc edge.

For normal view (refer to FIG no weights):

If you (V_ {i}, V_ {j}) \ and Eor  <V_{i},V)_{j}>\in Ethen  arc\left [ i \right ]\left [ j \right ]=1.

On the contrary, arc\left [ i \right ]\left [ j \right ]=0.

Seeking a degree vertex , in fact, this vertex  V_ {i}in the matrix of the  ielements of the line and.

For undirected graphs, such as:

For the FIG., Such as:

FIG net, weighted values ​​are diagrams.

If you  (V_ {i}, V_ {j}) \ and Eor  <V_{i},V)_{j}>\in Ethen  arc\left [ i \right ]\left [ j \right ]=W_{ij}. (W refers to weight)

If  i=j, then  arc\left [ i \right ]\left [ j \right ]=0.

If the edge points does not exist, it was  \ proptogenerally considered impossible to achieve by conventional values, expressed as 32727,65535. Such as:

structure:

#define MAXSIZE 100
#define INTMAX 32767//通常认为无法到达的int值,short型的边界值
typedef struct
{
	char ver[MAXSIZE];//顶点信息域
	int edge[MAXSIZE][MAXSIZE];
	int numv;//顶点数
	int nume;//边数
}Graph;

set up:

void CreateGraph(Graph *G)
{
	int i, j, k, w;
	printf("请输入图的顶点数和边数:\n");
	scanf("%d %d", &G->numv, &G->nume);
	while (getchar() != '\n');
	printf("请输入节点信息:");
	for (i = 0; i < G->numv; i++)//读入顶点信息
	{
		scanf("%c", &G->ver[i]);
	}
	for (i = 0; i < G->numv; i++)//初始化邻接矩阵
	{
		for (j = 0; j < G->numv; j++)
		{
			G->edge[i][j] = INTMAX;
		}
	}
	for (k = 0; k < G->nume; k++)//读入边信息,最好加上i-i权值为0,不然其它都会是INTMAX
	{
		printf("请输入第  %d  条边(Vi,Vj)的下标i,j,和权值w:\n", k + 1);
		while (getchar() != '\n');//必须清空缓冲区
		scanf("%d %d %d",&i, &j, &w);
		G->edge[i][j] = w;
		G->edge[j][i] = G->edge[i][j];//无向图则矩阵对称,有向图不需要
	}
}

Time complexity: n e vertices edges create O(n+n^{2}+e), initialize O (n ^ {2})

0x02. Adjacency list

Meaning: For a small number of edges in the graph adjacency matrix is wasteful, then consider using a chain memory structure stored side information, one-dimensional array to store the vertices, the array also stores a pointer pointing to the first edge, the edge list single chain stores, such vertices and edges will connect up.

Defect: adjacency table is stored is a default of the vertex, i.e., the vertex points, it is difficult to determine the level information.

Inverse adjacency table: solving the adjacency list of the defect information can not be determined, but in order to establish a first arc vertex adjacent table, but can not determine the degree information.

Icon:

 

structure:

#define MAXSIZE 100


//边表结构
typedef struct EdgeNode
{
	int adjvex;//存储该顶点对应的数组下标
	int weight;//存储权值
	struct EdgeNode* next;//指针域,指向下一个邻接点
}EdgeNode;

//顶点结构
typedef struct VertexNode
{
	char data;//存储顶点的信息
	EdgeNode* firstedge;//代表边表的头指针
}VertexNode,AdjList[MAXSIZE];//同时创建了一个大小为MAXSIZE的顶点数组

//整个图结构
typedef struct
{
	AdjList adjList;//此时的AdjList代表顶点表的指针类型,adjList就是创建的顶点表数组
	int numv;//顶点数
	int nume;//边数
}GraphAdjList;

set up:

void CreateALGraph(GraphAdjList* G)
{
	int i, j,k,w;
	EdgeNode* e;//建立边表的头结点
	printf("请输入顶点数目和边数目,以空格隔开\n");
	scanf("%d %d", &G->numv, &G->nume);
        while (getchar() != '\n');//必须清空缓冲区
	for (i = 0; i < G->numv; i++)//开始建立顶点表
	{
		scanf("%c", &G->adjList[i].data);//读取数据域
		G->adjList[i].firstedge = NULL;//初始化边表头指针
	}
	for (k = 0; k < G->nume; k++)//开始建立边表
	{
		printf("请输入边左端下标 i ,右端下标 j,和权值 :\n");
                while (getchar() != '\n');//必须清空缓冲区
		scanf("%d %d %d", &i,&j,&w);
		e = (EdgeNode*)malloc(sizeof(EdgeNode));//边表头结点申请内存
		e->adjvex = j;//存储 i 指向的顶点的下标
		e->weight = w;//存储该边的权值
		e->next = G->adjList[i].firstedge;//将 e 指向顶点表中的边表头结点
		G->adjList[i].firstedge = e;//将当前顶点表的头节点指向e,单链表的头插法
		//以下代码对于无向图而言,对称的
		e = (EdgeNode*)malloc(sizeof(EdgeNode));//e重新申请一块内存
		e->adjvex = i;
		e->weight = w;
		e->next = G->adjList[j].firstedge;
		G->adjList[j].firstedge = e;
	}
}

Adjacency list of common traversal:

void PrintfGraphAdjList(GraphAdjList G)//只传结构,不传指针,以防遍历时改变原来结构
{
	for (int i = 0; i < G.numv; i++)
	{
		EdgeNode* p = G.adjList[i].firstedge;
		printf("顶点 %c  的边有:\n", G.adjList[i].data);
		while (p)//遍历链表
		{
			printf("顶点  %c  到顶点  %c  ,权值为  %d  \n", G.adjList[i].data, G.adjList[p->adjvex].data, p->weight);
			p = p->next;
		}
		printf("\n");
	}
}

Time complexity: n vertices edges e O (n + e)

The main function test:

int main()
{
	GraphAdjList G;
	CreateALGraph(&G);
	PrintfGraphAdjList(G);
	return 0;
}

 

 

 

This chapter ends.

 

Published 19 original articles · won praise 7 · views 409

Guess you like

Origin blog.csdn.net/ATFWUS/article/details/104324417