一些关键词: Edge,Vertex,Graph。 G(V,E) e= ;完全图,路径,路径长度,简单路径,环,度,邻接点,连通,连通图,连通分量,稠密图,稀疏图;
#define MAXV 20 //
typedef struct
{
int no;
char* info; // InfoType info;
} VertexType;
typedef struct
{
int edges[MAXV][MAXV];
int vexnum, arcnum;
VertexType vexs[MAXV];
} MGraph;
邻接表存储方式
typedef struct
{
int adjvex;
InfoType info;
struct ANode *nextarc;
} ArcNode;
typedef struct
{
Vertex data;
ArcNode *firstarc;
}VNode;
typedef VNode AdjList[MAXV];
typedef struct
{
AdjList adjlist;
int n,e;
} ALGraph;
深度优先遍历
void FDS(ALGraph *G, int v)
{
visited[v] = 1; // int visited[MAXV];
ArcNode *p;
p = G->adjlist[v].firstarc;
if (p != NULL)
{
if (visited[p->adjvex] == 0)
{
FDS(G, p->adjvex);
}
p = p->nextarc;
}
}
广度优先遍历
void BFS(ALGraph *G, int v)
{
int w;
ArcNode *p;
int queue[MAXV], front = 0, rear = 0;
int visited[MAXV];
visited[v] = 1;
rear = (rear + 1) % MAXV;
queue[rear] = v;
while (front != rear)
{
front = (front + 1) % MAXV;
w = queue[front];
p = G->adjlist[w].firstarc;
while (p != NULL)
{
if (visited[p->adjvex] == 0)
{
visited[p->adjvex] = 1;
rear = (rear + 1) % MAXV;
queue[rear] = p->adjvex;
}
p = p->nextarc;
}
}
}
普里姆算法
克鲁斯卡尔算法
狄克斯特拉算法
拓扑排序
AOE网 (Activity On Edge)