graph

Posted kirosola

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了graph相关的知识,希望对你有一定的参考价值。

邻接矩阵类型声明

typedef struct 
	int number;
	InfoType info;
VertexType;
typedef struct 
	int edges[MAXV][MAXV];
	int n, e;
	VertexType vexs[MAXV]; //存放顶点信息
MatGraph;

  邻接表类型声明

typedef struct ANode
	int adjvex;
	struct ANode* nextarc;
	int weight;
ArcNode;
typedef struct 
	InfoType info;
	ArcNode* firstarc;
VNode;
typedef struct 
	VNode adjlist[MAXV];
	int n, e;
AdjGraph;

  创建图

void CreateAdj(AdjGraph*& G, int A[MAXV][MAXV], int n, int e)

	int i, j;
	ArcNode* p;
	G = (AdjGraph*)malloc(sizeof(AdjGraph));
	for (i = 0; i < n; i++)
		G->adjlist[i].firstarc = NULL;
	for (i = 0; i < n; i++)
	
		for (j = n - 1; j >= 0; j--)
		
			if (A[i][j] != 0 && A[i][j] != INF)
			
				p = (ArcNode*)malloc(sizeof(ArcNode));
				p->adjvex = j;
				p->weight = A[i][j];
				p->nextarc = G->adjlist[i].firstarc;
				G->adjlist[i].firstarc = p;

			
		
	
	G->n = n;
	G->e = e;

  输出图

void DispAduj(AdjGraph* G)

	ArcNode* p;
	int i;
	for (i = 0; i < G->n; i++)
	
		//p = (ArcNode*)malloc(sizeof(ArcNode));
		p = G->adjlist[i].firstarc;
		printf("%3d: ", i);
		while (p != NULL)
		
			printf("%3d[%d]->", p->adjvex, p->weight);
			p = p->nextarc;
		
		printf("^\n");
	

  销毁图

void DestroyAdj(AdjGraph*& G)

	int i;
	ArcNode* pre,* p;
	for (i = 0; i < G->n; i++)
	
		//p = (ArcNode*)malloc(sizeof(ArcNode));
		pre = G->adjlist[i].firstarc;
		if(pre != NULL)
		
			p = pre->nextarc;
			while (p != NULL)
			
				free(pre);
				pre = p;
				p = pre->nextarc;
			
			free(pre);
		
	
	free(G);

  邻接矩阵转邻接表

void MatToList(MatGraph g, AdjGraph*& G)

	ArcNode* p;
	int i, j;
	G = (AdjGraph*)malloc(sizeof(AdjGraph));
	for (i = 0; i < g.n; i++)
		G->adjlist[i].firstarc = NULL;
	for (i = 0; i < g.n; i++)
	
		for (j = g.n - 1; j >= 0; j--)
		
			if (g.edges[i][j] != 0 && g.edges[i][j] != INF)
			
				p = (ArcNode*)malloc(sizeof(ArcNode));
				p->adjvex = j;
				p->weight = g.edges[i][j];
				p->nextarc = G->adjlist[i].firstarc;
				G->adjlist[i].firstarc = p;
			
		
	
	G->n = g.n;
	G->e = g.e;

  邻接表转邻接矩阵

void ListToMat(MatGraph& g, AdjGraph* G)

	ArcNode* p;
	int i, j;
	for (int i = 0; i < G->n; i++)
	
		//p = (ArcNode*)malloc(sizeof(ArcNode));
		p = G->adjlist[i].firstarc;
		while (p!=NULL)
		
			g.edges[i][p->adjvex] = p->weight;
			p = p->nextarc;
		
	
	g.n = G->n;
	g.e = G->e;

  深度优先遍历

int visit[MAXV] =  0 ;
void DFS(AdjGraph* G, int v)

	ArcNode* p = G->adjlist[v].firstarc;
	printf("%d ", v);
	visit[v] = 1;
	while (p != NULL)
	
		if (visit[p->adjvex] == 0)
			DFS(G,p->adjvex);
		p = p->nextarc;
	
	printf("\n");

  广度优先遍历

void BFS(AdjGraph* G, int v)

	SqQueue1* qu;
	InitQueue1(qu);
	ArcNode* p;
	int visit[MAXV];
	for (int i = 0; i < G->n; i++)
		visit[i] = 0;
	printf("%d ", v);
	visit[v] = 1;
	enQueue1(qu, v);
	while (!QueueEmpty(qu))
	
		deQueue(qu, v);
		p = G->adjlist[v].firstarc;
		while (p != NULL)
		
			if (visit[p->adjvex] == 0)
			
				printf("%d ", p->adjvex);
				visit[p->adjvex] = 1;
				enQueue1(qu, p->adjvex);
			
			p = p->nextarc;
		
	
	printf("\n");

深度优先遍历非连通无向图

void DFS1(AdjGraph* G)

	for (int i = 0; i < G->n; i++)
	
		if(visit[i]==0)
			DFS(G, i);
	

  广度优先遍历非连通无向图

void BFS1(AdjGraph* G)

	for (int i = 0; i < G->n; i++)
		if (visit[i] == 0)
			BFS(G, i);

判断图的连通性

bool Connect(AdjGraph* G)

	int i;
	bool flag = true;
	int visit[MAXV];
	for (i = 0; i < G->n; i++)
		visit[i] = 0;
	BFS(G, 0);
	for (i = 0; i < G->n; i++)
	
		if (visit[i] == 0)
		
			flag = false;
			break;
		
	
	return flag;

 

判断u到v是否存在简单路径

void ExistPath(AdjGraph* G, int u, int v, bool& has)

	int w;
	ArcNode* p;
	visit[u] = 1;
	if (u == v)
	
		has = true;
		return;
	
	p = G->adjlist[u].firstarc;
	while (p != NULL)
	
		w = p->adjvex;
		if (visit[w] == 0)
			ExistPath(G, w, v, has);
		p = p->nextarc;
	

  输出u到v的一条简单路径

void FindPath(AdjGraph* G, int u, int v, int Path[], int d)

	int w;
	ArcNode* p;
	visit[v] = 1;
	Path[++d] = u;
	if (u == v)
	
		for (int i = 0; i <= d; i++)
			printf("%d ", Path[i]);
		printf("\n");
		return;
	
	p = G->adjlist[v].firstarc;
	while (p != NULL)
	
		w = p->adjvex;
		if (visit[w] == 0)
			FindPath(G, w, v, Path, d);
		p = p->nextarc;
	

  输出u到v的所有简单路径

void FindAllPath(AdjGraph* G, int u, int v, int Path[], int d)

	int w;
	ArcNode* p;
	visit[u] = 1;
	Path[++d] = u;
	if (u == v && d >= 0)
	
		for (int i = 0; i <= d; i++)
		
			printf("%d ", Path[i]);
		
		printf("\n");
	
	p = G->adjlist[u].firstarc;
	while (p != NULL)
	
		w = p->adjvex;
		if (visit[w] == 0)
			FindAllPath(G, w, v, Path, d);
		p = p->nextarc;
	
	visit[u] = 0;

  输出长度为l的简单路径

void PathLenAll(AdjGraph* G, int u, int v, int l, int Path[], int d)

	int w;
	ArcNode* p;
	visit[u] = 1;
	Path[++d] = u;
	if (u == v && d == l)
	
		for (int i = 0; i <= d; i++)
		
			printf("%d ", Path[i]);
		
		printf("\n");
	
	p = G->adjlist[u].firstarc;
	while (p != NULL)
	
		w = p->adjvex;
		if (visit[w] == 0)
			FindAllPath(G, w, v, Path, d);
		p = p->nextarc;
	
	visit[u] = 0;

  输出经过顶点k的简单回路

int visited[MAXV];
void DFSPath(AdjGraph* G, int u, int v, int path[], int d)

	ArcNode* p;
	int w, i;
	visited[u] = 1;
	path[++d] = u;
	p = G->adjlist[u].firstarc;
	while (p != NULL)
	
		w = p->adjvex;
		if (w == v && d > 1)
		
			for (i = 0; i <= d; i++)
				printf("%d ", path[i]);
			printf("%d\n", v);
		
		if (visited[w] == 0)
			DFSPath(G, w, v, path, d);
		p = p->nextarc;
	
	visited[u] = 0;

void FindCyclePath(AdjGraph* G, int k)

	int path[MAXV];
	DFSPath(G, k, k, path, -1);

  输出u到v的最短路径

typedef struct 
	int data;
	int parent;
QUEUE;
void ShortPath(AdjGraph* G, int u, int v)

	ArcNode* p;
	int i, w;
	int front = -1, rear = -1;
	QUEUE qu[MAXV];
	int visited[MAXV];
	for (i = 0; i < G->n; i++)
		visited[i] = 0;
	rear++;
	qu[rear].data = u;
	qu[rear].parent = -1;
	visited[u] = 1;
	while (rear != front)
	
		front++;
		w = qu[front].data;
		if (w == v)
		
			i = front;
			while (qu[i].parent!= -1)
			
				printf("%d", qu[i].data);
				i = qu[i].parent;
			
			printf("%d\n", qu[i]);
			return;
		
		p = G->adjlist[w].firstarc;
		while (p != NULL)
		
			if (visited[p->adjvex] == 0)
			
				rear++;
				qu[rear].data = p->adjvex;
				qu[rear].parent = front;
				visited[p->adjvex] = 1;
			
			p = p->nextarc;
		
	

  输出距顶点v最远的一个顶点

int MaxList(AdjGraph* G, int v)

	ArcNode* p;
	int qu[MAXV];
	int w, i, k;
	int rear = 0, front = 0;
	int visited[MAXV];
	for (i = 0; i < G->n; i++)
		visited[i] = 0;
	rear++;
	qu[rear] = v;
	while (rear != front)
	
		front = (front + 1) % MAXV;
		k = qu[front];
		p = G->adjlist[k].firstarc;
		while (p != NULL)
		
			if (visited[p->adjvex] == 0)
			
				rear = (rear + 1) % MAXV;
				qu[rear] = p->adjvex;
				visited[p->adjvex] = 1;
			
			p = p->nextarc;	
		
	
	return k;

  

 

以上是关于graph的主要内容,如果未能解决你的问题,请参考以下文章

实验三

实验三

实验三:类和对象

图的顺序存储 湖南大学

实验4

《漫画算法2》源码整理-2 图算法