码迷,mamicode.com
首页 > 编程语言 > 详细

图的经典算法

时间:2019-10-04 16:36:57      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:next   cost   info   arc   class   经典算法   顶点   node   cos   

Prim算法

void Prim(MatGraph g, int v)
{
	int closet[MAXV];
	int lowcost[MAXV];
	int min;
	int i, j, k;
	for (i = 0; i < g.n; i++)
	{
		lowcost[i] = g.edges[v][i];
		closet[i] = v;
	}
	for (i = 1; i < g.n; i++)
	{
		min = INF;
		for (j = 0; j < g.n; j++)
		{
			if (lowcost[j] != 0 && lowcost[j] < min)
			{
				min = lowcost[j];
				k = j;
			}
		}
		lowcost[k] = 0;
		printf("边(%d,%d)权为%d\n", closet[k], k, min);
		for (j = 0; j < g.n; j++)
		{
			if (lowcost[j] != 0 && lowcost[j] > g.edges[k][j])
			{
				lowcost[j] = g.edges[k][j];
				closet[j] = k;
			}
		}
	}
}

  Kruskal算法

typedef struct {
	int u;
	int v;
	int w;
}Edge;
void Kruskal(MatGraph g)
{
	int i, j, k;
	int u1, v1;
	int sn1, sn2;
	Edge E[MAXV];
	int vset[MAXV];
	k = 0;
	for (i = 0; i < g.n; i++)
	{
		for (j = 0; j <= i; j++)
		{
			if (g.edges[i][j] != 0 && g.edges[i][j] != INF)
			{
				E[k].u = i;
				E[k].v = j;
				E[k].w = g.edges[i][j];
				k++;
			}
		}
	}
	insert_sort(E, g.e);
	for (i = 0; i < g.n; i++)
		vset[i] = i;
	k = 1;
	j = 0;
	while (k < g.n)
	{
		u1 = E[j].u;
		v1 = E[j].v;
		sn1 = vset[u1];
		sn2 = vset[v1];
		if (sn1 != sn2)
		{
			printf("(%d,%d):%d\n", u1, v1, E[j].w);
			k++;
			for (i = 0; i < g.n; i++)
			{
				if (vset[i] == sn2)
					vset[i] = sn1;
			}
		}
		j++;
	}

}

  Dijkstra算法(从一个顶点到其余各顶点的最短路径)

void Dijkstral(MatGraph g, int v)
{
	int i, j, u;
	int mindist;
	int path[MAXV];
	int dist[MAXV];
	int S[MAXV];
	for (i = 0; i < g.n; i++)
	{
		S[i] = 0;
		dist[i] = g.edges[v][i];
		if (g.edges[v][i] < INF)
			path[i] = v;
		else
			path[i] = -1;
	}
	S[v] = 1;
	path[v] = 0;
	for (i = 1; i < g.n; i++)
	{
		mindist = INF;
		for (j = 0; j < g.n; j++)
		{
			if (S[j] == 0 && dist[j] < mindist)
			{
				u = j;
				mindist = dist[j];
			}
		}
		S[u] = 1;
		for (j = 0; j < g.n; j++)
		{
			if (S[j] == 0)
			{
				if (g.edges[u][j] < INF && dist[u] + g.edges[u][j] < dist[j])
				{
					dist[j] = dist[u] + g.edges[u][j];
					path[j] = u;
				}
			}
		}
	}
	DispPath(g, dist, path, S, v);
}
void DispPath(MatGraph g, int dist[], int path[], int S[], int v)

  Floyd算法(每对顶点之间的最短路径)

void Floyd(MatGraph g)
{
	int A[MAXV][MAXV];
	int path[MAXV][MAXV];
	int i, j, k;
	for (i = 0; i < g.n; i++)
	{
		for (j = 0; j < g.n; j++)
		{
			A[i][j] = g.edges[i][j];
			if (i != j && g.edges[i][j] < INF)
				path[i][j] = i;
			else
				path[i][j] = -1;
		}
	}
	for (k = 0; k < g.n; k++)
	{
		for (i = 0; i < g.n; i++)
		{
			for (j = 0; j < g.n; j++)
			{
				if (A[i][j] > A[i][k] + A[k][j])
				{
					A[i][j] = A[i][k] + A[k][j];
					path[i][j] = path[k][j];
				}
			}
		}
	}

}
void Dispath(MatGraph g, int A[][MAXV], int path[][MAXV])
{
	int apath[MAXV], d;
	int i, j, k;
	for (i = 0; i < g.n; i++)
	{
		for (j = 0; j < g.n; j++)
		{
			if (A[i][j] < INF && i != j)
			{
				printf("从%d到%d的路径为:", i, j);
				d = 0;
				apath[d] = j;
				k = path[i][j];
				while (k != -1 && k != i)
				{
					apath[++d];
					k = path[i][k];
				}
				apath[++d] = i;
				printf("%d", apath[d]);
				for (k = d - 1; k >= 0; k--)
					printf(",%d", apath[k]);
				printf("\t路径长度为:%d\n", A[i][j]);
			}
		}
	}
}

  拓扑排序

typedef struct {
	InfoType info;
	int count;//存放入度
	ArcNode* firstarc;
}VNode;
void TopSort(AdjGraph* G)
{
	int i, j;
	int St[MAXV], top = -1;
	ArcNode* p;
	for (i = 0; i < G->n; i++)
		G->adjlist[i].count = 0;
	for (i = 0; i < G->n; i++)
	{
		p = G->adjlist[i].firstarc;
		while (p != NULL)
		{
			G->adjlist[p->adjvex].count++;
			p = p->nextarc;
		}

	}
	for (i = 0; i < G->n; i++)
	{
		if (G->adjlist[i].count == 0)
			St[++top] = i;
	}
	while (top != -1)
	{
		i = St[top--];
		printf("%d ", i);
		p = G->adjlist[i].firstarc;
		while (p != NULL)
		{
			G->adjlist[p->adjvex].count--;
			if (G->adjlist[p->adjvex].count == 0)
				St[++top] = p->adjvex;
			p = p->nextarc;
		}
		
	}
}

  

图的经典算法

标签:next   cost   info   arc   class   经典算法   顶点   node   cos   

原文地址:https://www.cnblogs.com/KIROsola/p/11622302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!