图的存储结构
邻接矩阵
存储结构
typedef char VertexType;
typedef int EdgeType;
#define MAXSIZE 100
#define INFINTY 65535
typedef struct
{
VertexType vexs[MAXSIZE];
EdgeType arc[MAXSIZE][MAXSIZE];
int numVertexes;
int numEdges;
}MGraph;
邻接矩阵的建立
void CreateMGraph(MGraph * G)
{
printf("请输入顶点数和边数: \n");
scanf("%d %d", &G->numVertexes, &G->numEdges);
for (int i = 0; i < G->numVertexes; i++)
{
for (int j = 0; j < G->numVertexes; j++)
{
G->arc[i][j] = INFINTY;
}
}
for (int k = 0; k < G->numEdges; k++)
{
int i = 0;
int j = 0;
int w = 0;
printf("输入(vi,vj)边上的下标i,j,权值w: \n");
scanf("%d %d %d", &i, &j, &w);
G->arc[i][j] = w;
G->arc[j][i] = G->arc[i][j];
}
printf("读入顶点信息: \n");
for (int i = 0; i < G->numVertexes; i++)
{
scanf("%c", &G->vexs[i]);
}
}
邻接表
存储结构
typedef char VertexType;
typedef int EdgeType;
#define MAXSIZE 100
#define INFINTY 65535
typedef struct EdgeNode
{
int adj;
EdgeType weight;
struct EdgeNode * next;
}EdgeNode;
typedef struct VertexNode
{
VertexType data;
EdgeNode * firstEdge;
}VertexNode, AdjList[MAXSIZE];
typedef struct
{
AdjList adjList;
int numVertexes;
int numEdges;
}GraphAdjList;
邻接表的建立
void CreateALGraph(GraphAdjList * G)
{
printf("请输入顶点数和边数: \n");
scanf("%d %d", &G->numVertexes, &G->numEdges);
printf("请输入顶点信息: \n");
for (int i = 0; i < G->numVertexes; i++)
{
scanf("%c", &G->adjList[i].data);
G->adjList[i].firstEdge = NULL;
}
int i = 0, j = 0, w = 0;
for (int k = 0; k < G->numEdges; k++)
{
printf("请输入边(vi,vj)上的编号i,j,w\n");
scanf("%d %d %d", &i, &j, &w);
EdgeNode *e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adj = j;
e->weight = w;
e->next = G->adjList[i].firstEdge;
G->adjList[i].firstEdge = e;
e = (EdgeNode *)malloc(sizeof(EdgeNode));
e->adj = i;
e->weight = w;
e->next = G->adjList[j].firstEdge;
G->adjList[j].firstEdge = e;
}
}
十字链表
存储结构
typedef struct OEdgeNode
{
EdgeType tailVex;
EdgeType headVex;
int weight;
struct OEdgeNode * tailLink;
struct OEdgeNode * headLink;
}OEdgeNode;
typedef struct OVertexNode
{
VertexType data;
OEdgeNode * firstIn;
OEdgeNode * firstOut;
}OVertexNode, OAdjList[MAXSIZE];
typedef struct
{
OAdjList adjList;
int numVertexes;
int numEdges;
}OGraphAdjList;
十字链表的建立
void CreateOALGraph(OGraphAdjList * OG)
{
printf("请输入顶点数和边数: \n");
scanf("%d %d", &OG->numVertexes, &OG->numEdges);
printf("请输入顶点信息: \n");
for (int i = 0; i < OG->numVertexes; i++)
{
scanf("%c", &OG->adjList[i].data);
OG->adjList[i].firstIn = NULL;
OG->adjList[i].firstOut = NULL;
}
int i = 0, j = 0, w = 0;
for (int k = 0; k < OG->numEdges; k++)
{
printf("请输入边(vi,vj)上的编号i,j,w\n");
scanf("%d %d %d", &i, &j, &w);
OEdgeNode *e = (OEdgeNode *)malloc(sizeof(OEdgeNode));
e->tailVex = i;
e->headVex = j;
e->weight = w;
e->tailLink = OG->adjList[i].firstOut;
OG->adjList[i].firstOut = e;
e->headLink = OG->adjList[j].firstIn;
OG->adjList[j].firstIn = e;
}
}
图的遍历
深度优先遍历
邻接矩阵
bool visited[MAXSIZE];
void dfs(MGraph G, int i)
{
visited[i] = true;
printf("%c ", G.vexs[i]);
for (int j = 0; j < G.numVertexes; j++)
{
if (G.arc[i][j] == 1 && !visited[j])
{
dfs(G, j);
}
}
}
void dfsTraverse(MGraph G)
{
for (int i = 0; i < G.numVertexes; i++)
{
visited[i] = false;
}
for (int i = 0; i < G.numVertexes; i++)
{
if (!visited[i])
{
dfs(G, i);
}
}
}
邻接表
void dfsList(GraphAdjList G, int i)
{
visited[i] = true;
printf("%c ", G.adjList[i].data);
EdgeNode * p = G.adjList[i].firstEdge;
while (p)
{
if (!visited[p->adj])
{
dfsList(G, p->adj);
}
p = p->next;
}
}
void dfsTraverseList(GraphAdjList G)
{
for (int i = 0; i < G.numVertexes; i++)
{
if (!visited[i])
{
dfsList(G, i);
}
}
}
广度优先遍历
邻接矩阵
void BfsTraverse(MGraph G)
{
int k = 0;
for (int i = 0; i < G.numVertexes; i++)
visited[i] = false;
SqQueue q;
InitQueue(&q);
for (int i = 0; i < G.numVertexes; i++)
{
if (!visited[i])
{
visited[i] = true;
printf("%c ", G.vexs[i]);
EnQueue(&q, i);
while (!QueueEmpty(q))
{
DeQueue(&q, &k);
for (int j = 0; j < G.numVertexes; i++)
{
if (G.arc[k][j] == 1 && !visited[j])
{
visited[j] = true;
printf("%c ", G.vexs[j]);
EnQueue(&q, j);
}
}
}
}
}
}
邻接表
```c
void BfsAdfTraverse(GraphAdjList G)
{
int k = 0;
EdgeNode * p;
for (int i = 0; i < G.numVertexes; i++)
visited[i] = false;
SqQueue q;
InitQueue(&q);
for (int i = 0; i < G.numVertexes; i++)
{
if (!visited[i])
{
visited[i] = true;
printf("%c ", G.adjList[i].data);
EnQueue(&q, i);
while (!QueueEmpty(q))
{
DeQueue(&q, &k);
p = G.adjList[k].firstEdge;
while (p)
{
if (!visited[p->adj])
{
visited[p->adj] = true;
printf("%c ", G.adjList[p->adj].data);
EnQueue(&q, p->adj);
}
p = p->next;
}
}
}
}
}
未完,待结 ~
|