# 前情提要:
这里的算法只是记录个大致思路,并不能运行(未经过运行测试)
可能存在的部分错误(因为懒加健忘):
1、忘了在每个结构体的前面加 struct
# BFS
| |
| |
| |
| #define MAXSIZE 50; |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| struct ArcNode *nextArc; |
| int info; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| struct ArcNode *firstArc; |
| }VNode; |
| |
| typedef struct |
| { |
| struct VNode *adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| |
| |
| void Visit(VNode *v){ |
| printf("当前的顶点为%s\n",v); |
| } |
| |
| |
| |
| |
| |
| void BFS(AGraph G,int v,int visit[MAXSIZE]){ |
| |
| int queue[MAXSIZE]; |
| struct ArcNode *p; |
| int front = rear = 0; |
| int j; |
| rear = (rear+1)%MAXSIZE; |
| queue[rear] = v; |
| Visti(v); |
| visit[v] = 1; |
| while(front!=rear){ |
| front = (front+1)%MAXSIZE; |
| j = queue[front]; |
| p = G->adjlist[j].firstArc; |
| while(p!=NULL){ |
| if(visit[p]->adjvex){ |
| Visit(p->adjvex); |
| visit[p->adjvex] = 1; |
| rear = (rear+1)%MAXSIZE; |
| queue[rear] = p->adjvex; |
| } |
| p->nextArc; |
| } |
| } |
| } |
| |
| |
| void gotobfs(AGraph *G){ |
| int i = 0; |
| for(i = 0;i<G->n;i++){ |
| if(visti[i]==0) |
| { |
| bfs(G,visit[i],visit); |
| } |
| } |
| } |
# DFS
| |
| #define MAXSIZE 50; |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| struct ArcNode *nextArc; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| struct ArcNode *firstArc; |
| }VNode; |
| |
| typedef struct |
| { |
| struct VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| void Visit(VNode v){ |
| |
| } |
| |
| int visit[MAXSIZE]; |
| void DFS(AGraph G,int v){ |
| |
| Visit(v); |
| visit[v] = 1; |
| ArcNode *p; |
| p = G->adjlist[v]->firstArc; |
| while(p!=NULL){ |
| if(visit[p->adjvex] == 0){ |
| DFS(G,p->adjvex); |
| } |
| p = p->nextArc; |
| } |
| } |
| |
| void gotoDfs(AGraph G,int v){ |
| for(int i=0;i<G->n;i++){ |
| if(visit[v] == 0){ |
| DFS(G,v); |
| } |
| } |
| } |
# 图的应用
| |
| |
| |
| |
| #define MAXSIZE 50 |
| typedef struct BiNode |
| { |
| int data; |
| int deepth; |
| BiNode *lchild; |
| BiNode *rchild; |
| }BiNode,*BiTree; |
| |
| void findDeepth(BiTree bt){ |
| int d = 0; |
| int p = 0; |
| BiNode *s = bt; |
| if(s!=NULL){ |
| d = findDeepth(s->lchild); |
| if(d>p){ |
| p = d; |
| } |
| d = findDeepth(s->rchild); |
| if(d>p){ |
| p = d; |
| } |
| } |
| p = p+1; |
| } |
| |
| |
| |
| #define MAXSIZE 50 |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| ArcNode *nextArc; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| ArcNode *firstArc; |
| }VNode; |
| |
| typedef struct |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| int findTheLongestVex(AGraph G,int v,int visit[MAXSIZE]){ |
| int j; |
| int queue[MAXSIZE],front = rear = 0; |
| rear = (rear+1)%MAXSIZE; |
| visit[v] = 1; |
| queue[rear] = v; |
| while(front!=rear){ |
| front = (front+1)%MAXSIZE; |
| j = queue[front]; |
| p = G->adjlist[j]->firstArc; |
| while(p!=NULL){ |
| if(visit[p->adjvex] == 0){ |
| visit[p->adjvex] = 1; |
| rear = (rear+1)%MAXSIZE; |
| queue[rear] = p->adjvex; |
| findTheLongestVex(G,p->adjvex,visit); |
| } |
| } |
| p = p->nextArc; |
| |
| } |
| return j; |
| } |
# 邻接表、邻接矩阵的定义
| |
| |
| #define MAXSIZE 50; |
| |
| |
| |
| |
| |
| |
| typedef struct VertexType |
| { |
| int no; |
| char info; |
| }VertexType; |
| |
| |
| typedef struct Mgraph |
| { |
| |
| |
| int edges[MAXSIZE][MAXSIZE]; |
| int n; |
| int e; |
| VertexType vex[MAXSIZE]; |
| }Mgraph; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| ArcNode *nextarc; |
| char info; |
| }ArcNode; |
| |
| |
| typedef struct |
| { |
| ArcNode *firstnode; |
| char data; |
| }VNode; |
| |
| |
| typedef struct AGraph |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
# 邻接表和邻接矩阵的应用
| |
| |
| |
| |
| |
| #define MAXSIZE 50 |
| typedef struct ArcNode |
| { |
| int adjvex; |
| int info; |
| ArcNode *nextArc; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| ArcNode *firstArc; |
| }VNode; |
| |
| typedef struct |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| int visit[MAXSIZE]; |
| |
| bool DFS(AGraph G,int i,int j){ |
| ArcNode *p; |
| for(int k = 0;k<G->n;k++){ |
| visit[k] = 0; |
| } |
| visit[i] = 1; |
| if(visit[j] == 1){ |
| return true; |
| }else{ |
| p = G->adjlist[i]->firstArc; |
| while(p!=NULL){ |
| if(visit[p->adjvex] == 0){ |
| DFS(G,p->adjvex); |
| } |
| p = p->nextArc; |
| } |
| |
| } |
| return false; |
| } |
| |
| #define MAXSIZE 50 |
| typedef struct ArcNode |
| { |
| int adjvex; |
| ArcNode *nextArc; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| ArcNode *firstArc; |
| int info; |
| }VNode; |
| |
| typedef struct |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGragh; |
| |
| int count; |
| bool judgeWhetherIsRoot(AGragh G,int v){ |
| ArcNode *p; |
| count = count+1; |
| visit[v] = 1; |
| p = G->adjlist[v].firstArc; |
| while(p!=NULL){ |
| if(visit[p->adjvex] == 0){ |
| judgeWhetherIsRoot(G,p->adjvex); |
| } |
| p = p->nextArc; |
| } |
| } |
| |
| int main(AGragh G) |
| { |
| bool flag = false; |
| int visit[MAXSIZE]; |
| |
| for(int j = 0;j<G->n;j++){ |
| count = 0; |
| for(int k = 0;k<G->n;k++){ |
| visit[k] = 0; |
| } |
| DFS(G,j); |
| if(count == G->n){ |
| return 1; |
| }else{ |
| return 0; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| #define MAXSIZE 50; |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| ArcNode *nextArc; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| ArcNode *firstArc; |
| }VNode; |
| |
| typedef struct |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| |
| int visit[MAXSIZE]; |
| void judgeWhetherTree(AGraph G,int v,int i,int j){ |
| |
| i++; |
| visit[v] = 1; |
| ArcNode *p; |
| p = G->adjlist[v]->firstArc; |
| while(p!=NULL){ |
| j++; |
| if(visit[p->adjvex] == 0){ |
| judgeWhetherTree(G,p->adjvex,i); |
| } |
| p = p->nextArc; |
| } |
| judge(G,i,j) |
| } |
| |
| bool judge(AGraph G,int i,int j){ |
| if(G->n != (G->e)+1){ |
| return false; |
| } |
| |
| |
| |
| if(G->n!=i||G->n-1)!=j/2){ |
| return false |
| }else{ |
| return true; |
| } |
| } |
# 图的高级算法 -- 极有可能考
恶心人有一手啊北科~~!
# Kruskal
| |
| |
| |
| |
| |
| |
| 1. 任选一个顶点作为起始节点,而后选择与其相邻边中权值最小的那个进行连接 |
| 2. 逐次递归完成连接即可 |
| |
| 算法思路 |
| 1. 定义一个 Road 路径类(阐明每一条边的起点和终点对应的顶点编号,及该边的权值) |
| 2. 定义一个并查集数组 vex [MAXSIZE], 用来判断是否形成回路 |
| 3. 针对二中的如何判断是否形成回路这一问题,采用的解决方法是 -- 通过查找每条边中所对应的顶点中二者各自的根节点 |
| 若二者根节点相同,说明父节点一致形成了回路,反之未形成回路,即可将该边作为最小生成树的备选边 */ |
| |
| |
| #define MAXSIZE 50 |
| |
| typedef struct |
| { |
| int a; |
| int b; |
| int weight; |
| }Road; |
| |
| typedef struct |
| { |
| int no; |
| char info; |
| }vertexType; |
| |
| typedef struct |
| { |
| vertexType vex = [MAXSIZE]; |
| int edges[MAXSIZE][MAXSIZE] |
| int n; |
| int e; |
| }MGraph; |
| |
| |
| Road road[MAXSIZE]; |
| |
| int vex[MAXSIZE]; |
| |
| |
| |
| int getRoot(int a){ |
| |
| |
| |
| while(a!=v[a]){ |
| a = v[a]; |
| } |
| return a; |
| } |
| |
| |
| void kruskal(MGraph G,int &sum,Road road){ |
| int a; |
| int b; |
| for(int i = 0;i<G->n;i++){ |
| v[i] = i; |
| } |
| sort(G,G->e); |
| |
| for(int j = 0;j<G->e;j++){ |
| a = getRoot(road[j].a); |
| b = getRoot(road[j].b); |
| } |
| if(a!=b){ |
| v[a] = b; |
| sum = sum+road[j].weight; |
| } |
| } |
# Prim
| |
| |
| |
| |
| |
| |
| |
| |
| #define MAXSIZE 50 |
| #define INF 100 |
| typedef struct |
| { |
| int no; |
| char info; |
| }vertexType; |
| |
| typedef struct |
| { |
| int edges[MAXSIZE][MAXSIZE]; |
| vertexType vex[MAXSIZE]; |
| int n; |
| int e |
| }MGraph; |
| |
| void prim(MGraph G,int sum,int lowcost[MAXSIZE],int v){ |
| |
| for(int i = 0;i<G.n;i++){ |
| lowcost[i] = G.edges[v][i]; |
| vex[i] = 0; |
| } |
| |
| vex[v] = 1; |
| |
| int min; |
| int temp; |
| for(int j = 0;i<G.n-1;j++){ |
| min = INF; |
| for(int k = 0;k<G.n;k++){ |
| if(vex[k] == 0&&G.edges[v][k]<min){ |
| min = G.edges[v][k]; |
| temp = k; |
| } |
| } |
| } |
| v[k] = 1; |
| v = k; |
| for(int x = 0;x<G.n;x++){ |
| if(G.edges[v][x]<lowcost[x]){ |
| lowcost[x] = G.edges[v][x]; |
| } |
| } |
| } |
# Floyd
| |
| |
| |
| |
| #define MAXSIZE 50 |
| #define INF 1000 |
| typedef struct |
| { |
| int no; |
| char info; |
| }vertexType; |
| |
| typedef struct |
| { |
| int edges[MAXSIZE][MAXSIZE]; |
| vertexType vex[MAXSIZE]; |
| int n; |
| int e |
| }MGraph; |
| |
| void floyd(MGraph G,int path[][MAXSIZE],int cost[][MAXSIZE]){ |
| |
| |
| for(int i = 0;i<G.n;i++){ |
| for(int j = 0;j<G.n;j++){ |
| cost[i][j] = G.edges[i][j]; |
| path[i][j] = -1; |
| } |
| } |
| for(int x = 0;x<G.n;x++){ |
| for(int y = 0;y<G.n;y++){ |
| for(int z = 0;z<G.n;z++){ |
| if(cost[y][z]>cost[y][x]+cost[x][z]){ |
| cost[y][z] = cost[y][x]+cost[x][z] |
| path[y][z] = x; |
| } |
| } |
| } |
| } |
| } |
| |
| |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| struct ArcNode *nextarc; |
| struct ArcNode *priorarc; |
| char info; |
| }ArcNode; |
| |
| |
| typedef struct |
| { |
| struct ArcNode *firstnode; |
| char data; |
| }VNode; |
| |
| |
| typedef struct AGraph |
| { |
| struct VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| |
| int getOuter(AGraph *G){ |
| int outer = 0; |
| struct ArcNode *p; |
| if(i=0;i<MAXSIZE;i++){ |
| p = G->adjlist[v]->firstArc; |
| while(p!=NULL){ |
| outer++; |
| p = p->nextarc; |
| } |
| } |
| return outer; |
| } |
| |
| |
| int getInner(AGraph *G){ |
| int inner = 0; |
| struct ArcNode *p; |
| if(i=0;i<MAXSIZE;i++){ |
| p = G->adjlist[v]->firstArc; |
| while(p!=NULL){ |
| inner++; |
| p = p->priorarc; |
| } |
| } |
| return inner; |
| } |
| |
| sum = outer+inner; |
| |
| |
| |
| |
| |
| |
| #define MAXSIZE 50; |
| |
| typedef struct ArcNode |
| { |
| int adjvex; |
| ArcNode *nextArc; |
| }ArcNode; |
| |
| typedef struct |
| { |
| char data; |
| ArcNode *firstArc; |
| }VNode; |
| |
| typedef struct |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| void Visit(VNode v){ |
| |
| } |
| |
| int visit[MAXSIZE]; |
| void DFS(AGraph G,int v){ |
| |
| visit[v] = 1; |
| ArcNode *p; |
| p = G->adjlist[v]->firstArc; |
| while(p!=NULL){ |
| if(visit[p->adjvex] == 0){ |
| DFS(G,p->adjvex); |
| } |
| p = p->nextArc; |
| } |
| } |
| |
| |
| |
| #define MAXSIZE 50; |
| typedef struct ArcNode |
| { |
| int adjvex; |
| struct ArcNode *nextarc; |
| char info; |
| }ArcNode; |
| |
| |
| typedef struct |
| { |
| ArcNode *firstnode; |
| char data; |
| }VNode; |
| |
| |
| typedef struct |
| { |
| VNode adjlist[MAXSIZE]; |
| int n; |
| int e; |
| }AGraph; |
| |
| |
| typedef struct |
| { |
| int no; |
| char info; |
| }VertexType; |
| |
| |
| typedef struct |
| { |
| |
| |
| int edges[MAXSIZE][MAXSIZE]; |
| int n; |
| int e; |
| VertexType vex[MAXSIZE]; |
| }Mgraph; |
| |
| void change(AGraph &aG){ |
| Mgraph mG; |
| ArcNode *p; |
| for(int i=0;i<MAXSIZE;i++){ |
| p = (ag.adjlist[i])->firstnode; |
| |
| |
| while(p!=NULL){ |
| mg.edges[i][p->adjvex] = 1; |
| p = p->nextarc; |
| } |
| } |
| } |
# 简答题
1. 图的拓扑排序可以用于解决什么问题,说明一种应用场景