数据结构:图
#define INFINITY INT_MAX
#define MAX_VERTEX_NUM 20
typedef int VRType;
typedef char InfoType;
typedef char VertexType;
typedef struct
{
VRType adj;
InfoType *info;
}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct
{
VertexType vexs[MAX_VERTEX_NUM];
AdjMatrix arcs;
int vexnum, arcnum;
}
#define MAX_NAME 3
typedef char VertexType[MAX_NAME];
typedef struct ArcNode
{
int adjvex;
struct ArcNode *nextarc;
InfoType *info;
}ArcNode;
typedef struct VNode
{
VertexType data;
ArcNode *firstarc;
}VNode, AdjList[MAX_VERTEX_NUM];
typedef struct
{
AdjList vertices;
int vexnum, arcnum;
}ALGraph;
typedef struct ArcBox
{
int tailvex, headvex;
struct ArcBox *tlink, *hlink;
InfoType *info;
}ArcBox;
typedef struct VexNode
{
VertexType data;
ArcBox *firstin *firstout
}VexNode;
typedef struct{
VexNode xlist[MAX_VERTEX_NUM];
int vexnum, arcnum;
}OLGraph;
enum VisitIf{unvisited, visited};
typedef struct EBox
{
VisitIf mark;
int ivex, jvex;
struct EBox *ilink, *jlink;
InfoType *info;
}EBox;
typedef struct VexBox
{
VertexType data;
EBox *firstedge;
}VexBox;
typedef struct{
VexBox adjmulist[MARTEX_VERTEX_NUM];
int vexnum, edgenum;
}AMLGraph;
Boolean visited[MAX_VERTEX_NUM];
void DFS(ALGraph G, int v, void(*Visit)(char*))
{
ArcNode *p;
visited[v] = true;
Visit(G.vertices[v].data);
for(p = G.vertices[v].firstarc; p; p=p->nextarc)
if(!visited[p->data.adjvex])
DFS(G, p->data.adjvex, Visit);
}
int LocateVex(ALGraph G, VertexType u)
{
int i;
for(i = 0; i < G.vexnum; i++)
if(strcmp(u, G.vertices[i].data))
return i;
return -1;
}
void DFSTraverse(ALGraph G, void(*Visit)(char *))
{
int v;
for(v = 0; v < G.vexnum; v++)
visited[v] = false;
for(v = 0; v < G.vexnum; v++)
if(!visited[v])
DFS(G, v, Visit);
}
void BFSTraverse(ALGraph G, void(*Visit)(char *))
{
int v, u;
ArcNode *p;
LinkQueue Q;
for(v = 0; v<G.vexnum; v++)
visited[v] = false;
InitQueue(Q);
for(v = 0; v < G.vexnum; v++)
if(!visited[v])
{
visited[v] = true;
Visit(G.vertices[v].data);
EnQueue(Q, v);
while(!QueueEmpty(Q))
{
DeQueue(Q, u);
for(p = G.vertices[u].firstarc; p; p = p->nextarc)
if(!visited[p->data.adjvex])
{
visited[p->data.adjvex] = true;
Visit(G.vertices[p->data.adjvex].data);
EnQueue(Q, p->data.adjvex);
}
}
}
}
typedef struct
{
VertexType adjvex;
VRType lowcost;
}minside[MAX_VERTEX_NUM]
int minimum(minside SZ, MGraph)
{
int i = 0, k, j, min;
while(!(SZ[i].lowcost))
i++;
min = SZ[i].lowcost;
k = i;
for(j = i + 1; j<G.vexnum; j++)
if(SZ[j].lowcost > 0 && min > SZ[j].lowcost)
{
min = SZ[j].lowcost;
k = j;
}
return k;
}
void MiniSpanTree_PRIM(MGraph G, VertexType u)
{
int i, j, k;
minside closedge;
k = LocateVex(G,u);
for(j = 0; j < G.vexnum; j++)
{
strcpy(closedge[j].adjvex, u);
closedge[j].lowcost = G.arcs[k][j].adj;
}
closedge[k].lowcost = 0;
printf("最小代价生成树的各条边为\n");
for(i = 1; i < G.vexnum; ++i)
{
k = minimum(closedge, G);
printf("(%s-%s)\n", closedge[k].adjvex, G.vexs[k]);
closedge[k].lowcost = 0;
for(j = 0; j < G.vexnum; j++)
if(G.arcs[k][j].adj < closedge[j].lowcost)
{
strcpy(closedge[j].adjvex, G.vexs[k]);
closedge[j].lowcost = G.arcs[k][j].adj
}
}
}
void Kruskal(MGraph G)
{
int set[MAX_VERTEX_NUM], i, j;
int k = 0, a = 0, b = 0, min = G.arcs[a][b].adj;
for(i = 0; i < G.vexnum; i++)
set[i] = 0;
printf("最小生成树的各条边为\n");
while(k < G.vexnum - 1)
{
for(i = 0; i < G.vexnum; ++i)
for(j = i +1; j < G.vexnum; ++j)
if(G.[i][j].adj < min)
{
min = G.arcs[i][j].adj;
a = i;
b = j;
}
min = G.arcs[i][j].adj = INFINITY;
if(set[a] != set[b])
{
printf("%s-%s\n", G.vexs[a], G.vexs[b]);
k++;
for(i = 0; i < G.vexnum; i++)
if(set[i] = set[b])
set[i] = set [a];
}
}
}
void FindInDegree(ALGraph G, int indegree[])
{
int i;
ArcNode *p;
for(i = 0; i < G.vexnum; i++)
indegree[i] = 0;
for(i = 0; i < G.vexnum; i++)
{
p = G.vertices[i].firstarc;
while(p)
{
indegree[p->data.adjvex]++;
p = p->nextarc;
}
}
}
Status TopologicalSort(ALGraph G)
{
int i, k, count;
int indegree[MAX_VARTEX_NUM];
Sqstack S;
ArcNode *p;
FindInDegree(G, indegree);
InitStack(S);
for(i = 0; i < G,vexnum; ++i)
if(!indegree[i])
push(S, i);
while(!StackEmpty(S))
{
pop(S, i);
printf("%s", G.vertices[i].data);
++count;
for(p = G.vertices[i].firstarc; p; p = p->nextarc)
{
k = p->data.adjvex;
if(!(--indegree[k]))
push(S, k);
}
}
}
typedef int PathMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef int ShortPathTable[MAX_VERTEX_NUM];
void ShortestPath_DIJ(MGraph G, int v0, PathMatrix P, ShortPathTable D)
{
int v, i, j, w, min;
Status final[MAX_VERTEX_NUM];
for(v = 0; v < G.vexnum; ++v)
{
final[v] = false;
D[v] = G.arcs[v0][v].adj;
for(w = 0; w < G.vexnum; ++w)
P[v0][w] = false;
if(D[v] < INFINITY)
P[v][v0] = true
}
D[v0] = 0;
final[v0] = true;
for(i = 1; i < G.vexnum; ++i)
{
min = INFINITY;
for(w = 0; w < G.vexnum; ++w)
if(!final[w] && D[w] < min)
{
v = w;
min = D[w]
}
final[v] = true;
for(w = 0; w < G.vexnum; ++w)
if(!final[w] && min < INFINITY && G.arcs[v][w].adj < INFINITY && (min +G.arcs[v][w].adj ) < D[w])
{
D[w] = min + G.arcs[v][w].adj;
for(j = 0; j < G.vexnum; ++j)
P[w][j] = P[v][j]
P[v0][w] = true;
}
}
}