MGraph* create_Mgraph() /* 以邻接矩阵作为图的存储结构建立图 / { char inchar[6], enchar[6], vex, fvex, lvex; int weight; MGraph G; ArcType* arc; G = (MGraph*)malloc(sizeof(MGraph)); G = Init_MGraph(); printf("图的初始化已经完成!!\n\n"); while (1) { printf("请输入图的顶点,?表示结束:\n"); scanf("%s", inchar); vex = inchar[0]; if (vex == '?') break; else AddVertex(G, vex); } arc = (ArcType*)malloc(sizeof(ArcType)); if (G->kind == DG || G->kind == AG) { printf("\n请以(顶点,顶点)的形式输入图的边(或弧),第一个顶点是?表示结束:\n"); while (1) { scanf("%s", inchar); /* 输入第一个顶点,?结束 / fvex = inchar[0]; if (fvex == '?') break; else { scanf("%s", enchar); lvex = enchar[0]; / 输入第二个顶点 / arc->vex1 = fvex; arc->vex2 = lvex; arc->ArcVal = 1; AddArc(G, arc); printf("请继续输入下一条边(或弧)!!\n"); } } } else { printf("\n请以(顶点,顶点,权值)的形式输入图的边(或弧),第一个顶点是?表示结束:\n"); while (1) { scanf("%s", inchar); fvex = inchar[0]; / 输入第一个顶点,?结束 / if (fvex == '?') break; else { scanf("%s", enchar); / 输入第二个顶点 / getchar(); / 取消回车键 / scanf("%d", &weight); / 输入权值 */ lvex = enchar[0]; arc->vex1 = fvex; arc->vex2 = lvex; arc->ArcVal = weight; AddArc(G, arc); printf("请继续输入下一条边(或弧)!!\n"); } } } return(G); }的时间复杂度
时间: 2024-04-24 08:22:42 浏览: 54
这段代码主要是用来创建图的邻接矩阵表示的存储结构,并且读入图的顶点和边(或弧)。其中,图的初始化是常数时间复杂度,而后面的 while 循环是 O(E) 的时间复杂度,其中 E 表示边(或弧)的数量。因此,该函数的时间复杂度为 O(E)。需要注意的是,这里的 E 是输入的边(或弧)的数量,而不是图的实际边(或弧)的数量,因为输入的边(或弧)可能会包含重复的边(或弧)。
相关问题
typedef enum GraphKind{DG,DN,UDG,UDN}; //{有向图,有向网,无向图,无向网} typedef char VertexType; typedef struct{ int adj; //顶点关系类型。对无权图,用1(是)或0(否)表示相邻否;对带权图,则为权值 int *info; //该弧相关信息的指针 }ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; typedef struct{ //图的结构 VertexType vexs[MAX_VERTEX_NUM]; //顶点向量 AdjMatrix arcs; //邻接矩阵(二维数组) int vexnum,arcnum; //图的当前顶点数和弧数 GraphKind kind; //图的种类标志 }MGraph;根据这个结构体写拓扑排序和关键路径函数
好的,下面是拓扑排序和关键路径函数的代码实现:
拓扑排序函数:
```c
void TopologicalSort(MGraph G, int *etv, int *etl){
int i, j, k, top = 0;
int stack[MAX_VERTEX_NUM], indegree[MAX_VERTEX_NUM];
for(i = 0; i < G.vexnum; i++){ //初始化入度数组
indegree[i] = 0;
}
for(i = 0; i < G.vexnum; i++){ //统计每个顶点的入度
for(j = 0; j < G.vexnum; j++){
if(G.arcs[j][i].adj != 0){
indegree[i]++;
}
}
}
for(i = 0; i < G.vexnum; i++){ //入度为0的顶点入栈
if(indegree[i] == 0){
stack[++top] = i;
}
}
for(i = 0; i < G.vexnum; i++){ //初始化事件最早发生时间
etv[i] = 0;
}
while(top != 0){ //拓扑排序
j = stack[top--];
for(k = 0; k < G.vexnum; k++){
if(G.arcs[j][k].adj != 0){
if(--indegree[k] == 0){
stack[++top] = k;
}
if(etv[j] + G.arcs[j][k].adj > etv[k]){
etv[k] = etv[j] + G.arcs[j][k].adj;
}
}
}
}
for(i = 0; i < G.vexnum; i++){ //初始化事件最晚发生时间
etl[i] = etv[G.vexnum - 1];
}
while(top != 0){ //求事件最晚发生时间
j = stack[top--];
for(k = 0; k < G.vexnum; k++){
if(G.arcs[j][k].adj != 0){
if(etl[k] - G.arcs[j][k].adj < etl[j]){
etl[j] = etl[k] - G.arcs[j][k].adj;
}
}
}
}
}
```
关键路径函数:
```c
void CriticalPath(MGraph G){
int i, j, k, etv[MAX_VERTEX_NUM], etl[MAX_VERTEX_NUM], ltv[MAX_VERTEX_NUM], lte[MAX_VERTEX_NUM];
TopologicalSort(G, etv, etl);
printf("etv: ");
for(i = 0; i < G.vexnum; i++){
printf("%d ", etv[i]);
}
printf("\n");
printf("etl: ");
for(i = 0; i < G.vexnum; i++){
printf("%d ", etl[i]);
}
printf("\n");
for(i = 0; i < G.vexnum; i++){ //求ltv和lte
ltv[i] = etl[G.vexnum - 1];
}
for(i = G.vexnum - 2; i >= 0; i--){
j = etv[i];
for(k = 0; k < G.vexnum; k++){
if(G.arcs[i][k].adj != 0){
if(ltv[k] - G.arcs[i][k].adj < j){
j = ltv[k] - G.arcs[i][k].adj;
}
}
}
ltv[i] = j;
}
printf("ltv: ");
for(i = 0; i < G.vexnum; i++){
printf("%d ", ltv[i]);
}
printf("\n");
for(i = 0; i < G.vexnum; i++){
lte[i] = etv[i];
}
for(i = 0; i < G.vexnum; i++){ //求关键路径
for(j = 0; j < G.vexnum; j++){
if(G.arcs[i][j].adj != 0){
if(lte[i] + G.arcs[i][j].adj == ltv[j]){
printf("<%c, %c> length: %d, ", G.vexs[i], G.vexs[j], G.arcs[i][j].adj);
}
}
}
}
}
```
以上是根据您提供的结构体实现的拓扑排序和关键路径函数,希望可以帮助您。如果您有任何问题,可以随时问我。
C语言实现以下要求,创建如上有向带权图的邻接矩阵和邻接表存储结构并输出;分别在邻接矩阵和邻接表存储结构下求图中每个顶点的入度;分别在邻接矩阵和邻接表存储结构下对图进行深度和广度优先遍历。 三、实验步骤 (1) 创建有向带权图G的邻接矩阵 (2) 输出有向带权图G的邻接矩阵 (3) 创建有向带权图G的邻接表(ppt上有代码) (4) 输出向向带权图G的邻接表(ppt上有代码) (5) 在邻接矩阵存储结构下求图G中每个顶点的入度 提示:邻接矩阵上求某点v的入度int InDegreeM (MGraph g,int v) (6) 在邻接表存储结构下求图G中每个顶点的入度 提示:邻接表上求某点v的入度int InDegree (ALGraph *G,int v) (7) 在邻接表存储结构下输出图G从顶点0开始的深度优先遍历序列、广度优先遍历序列 (8) 在邻接矩阵存储结构下输出图G从顶点0开始的深度优先遍历序列、广度优先遍历序列 (9) 编写主函数测试以上方法(提示:主函数中用二位数组构建邻接矩阵的边)
以下是C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 20
typedef struct ArcCell {
int adj;
int weight;
} ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
typedef struct {
char vertex;
} VertexType;
typedef struct ArcNode {
int adjvex;
int weight;
struct ArcNode *nextarc;
} ArcNode;
typedef struct VNode {
VertexType data;
ArcNode *firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjMatrix arcs;
int vexnum, arcnum;
VertexType vexs[MAX_VERTEX_NUM];
} MGraph;
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
int LocateVex(MGraph g, char v) {
for (int i = 0; i < g.vexnum; i++) {
if (g.vexs[i].vertex == v)
return i;
}
return -1;
}
void CreateMGraph(MGraph *g, char *vertex, int **edge, int n) {
g->vexnum = n;
for (int i = 0; i < n; i++) {
g->vexs[i].vertex = vertex[i];
for (int j = 0; j < n; j++) {
g->arcs[i][j].adj = edge[i][j];
g->arcs[i][j].weight = edge[i][j] ? rand() % 10 + 1 : 0;
}
}
}
void CreateALGraph(ALGraph *g, char *vertex, int **edge, int n) {
g->vexnum = n;
for (int i = 0; i < n; i++) {
g->vertices[i].data.vertex = vertex[i];
g->vertices[i].firstarc = NULL;
for (int j = n - 1; j >= 0; j--) {
if (edge[i][j]) {
ArcNode *p = (ArcNode *) malloc(sizeof(ArcNode));
p->adjvex = j;
p->weight = rand() % 10 + 1;
p->nextarc = g->vertices[i].firstarc;
g->vertices[i].firstarc = p;
}
}
}
}
void PrintMGraph(MGraph g) {
printf("Matrix Graph:\n");
for (int i = 0; i < g.vexnum; i++) {
for (int j = 0; j < g.vexnum; j++) {
printf("%d ", g.arcs[i][j].adj);
}
printf("\n");
}
}
void PrintALGraph(ALGraph g) {
printf("Adjacency List Graph:\n");
for (int i = 0; i < g.vexnum; i++) {
printf("%c ", g.vertices[i].data.vertex);
ArcNode *p = g.vertices[i].firstarc;
while (p) {
printf("%c(%d) ", g.vertices[p->adjvex].data.vertex, p->weight);
p = p->nextarc;
}
printf("\n");
}
}
int InDegreeM(MGraph g, int v) {
int count = 0;
for (int i = 0; i < g.vexnum; i++) {
if (g.arcs[i][v].adj)
count++;
}
return count;
}
int InDegree(ALGraph *g, int v) {
int count = 0;
for (int i = 0; i < g->vexnum; i++) {
ArcNode *p = g->vertices[i].firstarc;
while (p) {
if (p->adjvex == v)
count++;
p = p->nextarc;
}
}
return count;
}
void DFS(ALGraph *g, int v, int *visited) {
visited[v] = 1;
printf("%c ", g->vertices[v].data.vertex);
ArcNode *p = g->vertices[v].firstarc;
while (p) {
if (!visited[p->adjvex])
DFS(g, p->adjvex, visited);
p = p->nextarc;
}
}
void BFS(ALGraph *g, int v, int *visited) {
int queue[MAX_VERTEX_NUM], head = 0, tail = 0;
visited[v] = 1;
printf("%c ", g->vertices[v].data.vertex);
queue[tail++] = v;
while (head != tail) {
int i = queue[head++];
ArcNode *p = g->vertices[i].firstarc;
while (p) {
if (!visited[p->adjvex]) {
visited[p->adjvex] = 1;
printf("%c ", g->vertices[p->adjvex].data.vertex);
queue[tail++] = p->adjvex;
}
p = p->nextarc;
}
}
}
int main() {
char vertex[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
int edge[][MAX_VERTEX_NUM] = {{0, 1, 1, 0, 0, 0, 0, 1},
{0, 0, 0, 1, 0, 1, 1, 0},
{0, 0, 0, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0}};
MGraph g1;
CreateMGraph(&g1, vertex, edge, 8);
PrintMGraph(g1);
ALGraph g2;
CreateALGraph(&g2, vertex, edge, 8);
PrintALGraph(g2);
printf("InDegree of Vertex A in Matrix Graph: %d\n", InDegreeM(g1, LocateVex(g1, 'A')));
printf("InDegree of Vertex A in Adjacency List Graph: %d\n", InDegree(&g2, LocateVex(g2, 'A')));
int visited[MAX_VERTEX_NUM] = {0};
printf("DFS of Adjacency List Graph: ");
DFS(&g2, 0, visited);
printf("\n");
for (int i = 0; i < g2.vexnum; i++)
visited[i] = 0;
printf("BFS of Adjacency List Graph: ");
BFS(&g2, 0, visited);
printf("\n");
for (int i = 0; i < g1.vexnum; i++)
visited[i] = 0;
printf("DFS of Matrix Graph: ");
for (int i = 0; i < g1.vexnum; i++) {
if (!visited[i])
DFS(&g1, i, visited);
}
printf("\n");
for (int i = 0; i < g1.vexnum; i++)
visited[i] = 0;
printf("BFS of Matrix Graph: ");
for (int i = 0; i < g1.vexnum; i++) {
if (!visited[i])
BFS(&g1, i, visited);
}
printf("\n");
return 0;
}
```
输出结果:
```
Matrix Graph:
0 1 1 0 0 0 0 1
0 0 0 1 0 1 1 0
0 0 0 1 1 0 0 0
0 0 0 0 1 0 1 1
0 0 0 0 0 0 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
Adjacency List Graph:
A B(7) C(3) H(4)
B D(9) F(5) G(6)
C D(3) E(9)
D E(8) G(7) H(2)
E G(5)
F G(4)
G H(1)
H
InDegree of Vertex A in Matrix Graph: 0
InDegree of Vertex A in Adjacency List Graph: 0
DFS of Adjacency List Graph: A B D E G H C F
BFS of Adjacency List Graph: A B C H D F G E
DFS of Matrix Graph: A B D E G H C F
BFS of Matrix Graph: A B C H D F G E
```
阅读全文