图的经典算法

2020-12-13 16:54

阅读:253

标签: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.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 

  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 

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

void Floyd(MatGraph g)
{
	int A[MAXV][MAXV];
	int path[MAXV][MAXV];
	int i, j, k;
	for (i = 0; i  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 = 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 n; i++)
		G->adjlist[i].count = 0;
	for (i = 0; i n; i++)
	{
		p = G->adjlist[i].firstarc;
		while (p != NULL)
		{
			G->adjlist[p->adjvex].count++;
			p = p->nextarc;
		}

	}
	for (i = 0; i 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


评论


亲,登录后才可以留言!