采用贪心算法的思想;总是做除对当前来说最优的算法;层层叠加;得到最终的结果;需要维护一个前驱数组来记录前驱;
头文件(Header file);
# ifndef _ALGRAPH_
# define _ALGRAPH_
# include <iostream>
using namespace std;
# define MaxVertexNum 256
typedef int InfoType;
typedef int VertexType;
typedef struct EdgeNode
{
int adjvertex;//对数组节点增加一个记录入度的节点;
InfoType info;
struct EdgeNode * next;
}EdgeNode;
typedef struct VertexNode
{
int indegree;
VertexType vertex;
EdgeNode * firstedge;
}VertexNode;
typedef struct
{
VertexNode adjlist[MaxVertexNum];
int vertexNum;
int edgeNum;
}AlGraph, * PALGraph;
PALGraph CreateGraph(void);
void DFS(PALGraph g, int i);
void DFSTraversal(PALGraph g);
void BFSTraversal(PALGraph g, int i);
void CalculateIndegree(PALGraph g);
void TopologicalSort(PALGraph g);
# endif
实现文件(Implement file);
# include "ALGraph.h"
# include "SeqQueue.h"
bool visited[MaxVertexNum] = { 0 };
PALGraph CreateGraph(void)
{
PALGraph g = (PALGraph)malloc(sizeof(AlGraph));
if (NULL != g)
{
memset(g, 0, sizeof(AlGraph));
g->vertexNum = 0;
g->edgeNum = 0;
cout << "Please input vertecNum and edgeNum: " << endl;
cin >> g->vertexNum >> g->edgeNum;
for (int i = 0; i < g->vertexNum; i++)
{
cout << "Please input the element value: " << endl;
cin >> g->adjlist[i].vertex;
g->adjlist[i].firstedge = NULL;
}
int i = 0;
int j = 0;
EdgeNode * p = NULL;
for (int k = 0; k < g->edgeNum; k++)
{
cout << "please input two index: " << endl;
cin >> i >> j;
p = (EdgeNode *)malloc(sizeof(EdgeNode));
if (NULL == p)
{
cout << "Memory allocate is error! " << endl;
system("pause");
exit(0);
}
p->adjvertex = j;
p->info = 0;
p->next = g->adjlist[i].firstedge;
g->adjlist[i].firstedge = p;
}
return g;
}
else
{
cout << "Memory allocate is error! " << endl;
system("pause");
return 0;
}
}
void DFS(PALGraph g, int i)
{
EdgeNode * p = NULL;
int j = 0;
cout << g->adjlist[i].vertex << endl;
visited[i] = true;
for (p = g->adjlist[i].firstedge; p != NULL; p = p->next)
{
j = p->adjvertex;
if (!visited[j])
{
DFS(g, j);
}
}
}
void DFSTraversal(PALGraph g)
{
for (int i = 0; i < g->vertexNum; i++)
{
visited[i] = false;
}
for (int i = 0; i < g->vertexNum; i++)
{
if (!visited[i])
{
DFS(g, i);
}
}
}
void BFSTraversal(PALGraph g, int i)
{
EdgeNode * p = NULL;
int j = 0;
PSeqQueue q = InitSeqQueue();
cout << g->adjlist[i].vertex << endl;
visited[i] = true;
InSeqQueue(q, i);
while (!EmptySeqQueue(q))
{
OutSeqQueue(q, &i);
for (p = g->adjlist[i].firstedge; p != NULL; p = p->next)
{
j = p->adjvertex;
if (!visited[j])
{
cout << g->adjlist[j].vertex << endl;
visited[j] = true;
InSeqQueue(q, j);
}
}
}
}
void CalculateIndegree(PALGraph g)
{
int i = 0;
EdgeNode * p = NULL;
for (i = 0; i < g->vertexNum; i++)
{
g->adjlist[i].indegree = 0;
}
for (i = 0; i < g->vertexNum; i++)
{
for (p = g->adjlist[i].firstedge; p != NULL; p = p->next)
{
g->adjlist[p->adjvertex].indegree++;
}
}
return;
}
void TopologicalSort(PALGraph g)
{
int i = 0;
int j = 0;
int cnt = 0;
int top = -1;
EdgeNode * p = NULL;
CalculateIndegree(g);
for (i = 0; i < g->vertexNum; i++)
{
if (g->adjlist[i].indegree == 0)
{
g->adjlist[i].indegree = top;
top = i;
}
}
while (top != -1)
{
j = top;
top = g->adjlist[top].indegree;
cout << g->adjlist[j].vertex << endl;
cnt++;
for (p = g->adjlist[j].firstedge; p != NULL; p = p->next)
{
g->adjlist[p->adjvertex].indegree--;
if (g->adjlist[p->adjvertex].indegree == 0)
{
g->adjlist[p->adjvertex].indegree = top;
top = p->adjvertex;
}
}
}
if (cnt < g->vertexNum)
{
cout << "The network have a cycle!" << endl;
}
}
主函数(Main function);
# include "ALGraph.h"
int main(int argc, char ** argv)
{
//PALGraph g = CreateGraph();
//EdgeNode * p = NULL;
//for (int i = 0; i < g->vertexNum; i++)
//{
// p = g->adjlist[i].firstedge;
// while (NULL != p)
// {
// cout << g->adjlist[p->adjvertex].vertex << endl;
// p = p->next;
// }
// cout << endl;
//}
//cout << "--------------------------------------------" << endl;
//
//BFSTraversal(g, 0);
//----------------------------------拓扑排序算法-----------------------------
AlGraph g;
g.vertexNum = 6;
g.edgeNum = 8;
for (int i = 0; i < g.vertexNum; i++)
{
g.adjlist[i].vertex = i;
g.adjlist[i].firstedge = NULL;
}
EdgeNode * p = new EdgeNode;
p->next = NULL;
p->adjvertex = 3;
p->next = g.adjlist[0].firstedge;
g.adjlist[0].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 2;
p->next = g.adjlist[0].firstedge;
g.adjlist[0].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 1;
p->next = g.adjlist[0].firstedge;
g.adjlist[0].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 4;
p->next = g.adjlist[2].firstedge;
g.adjlist[2].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 1;
p->next = g.adjlist[2].firstedge;
g.adjlist[2].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 5;
p->next = g.adjlist[3].firstedge;
g.adjlist[3].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 4;
p->next = g.adjlist[3].firstedge;
g.adjlist[3].firstedge = p;
p = new EdgeNode;
p->next = NULL;
p->adjvertex = 4;
p->next = g.adjlist[5].firstedge;
g.adjlist[5].firstedge = p;
TopologicalSort(&g);
system("pause");
return 0;
}