文章目录

  • 问题 A: 将邻接矩阵存储的图转换为邻接表存储的图
  • 问题 B: 邻接表存储的图转化为邻接矩阵存储的图
  • 问题 C: 邻接矩阵存储图的DFS
  • 问题 D: 邻接矩阵存储图的DFS-非递归算法
  • 问题 E: 邻接矩阵存储图的BFS
  • 问题 F: 案例6-1.2:邻接表存储图的广度优先遍历
  • 问题 G: 邻接矩阵存储图的DFS完成序求解
  • 问题 H: 算法7-7,7-8:无向图的连通分量和生成树

问题 A: 将邻接矩阵存储的图转换为邻接表存储的图

#include<bits/stdc++.h>
using namespace std;#define MAX_SIZE 100// 邻接矩阵存储的图
struct Graph{int vexNumber;string info[MAX_SIZE];int adjMatrix[MAX_SIZE][MAX_SIZE];Graph(){vexNumber=0;memset(adjMatrix,0,sizeof(adjMatrix));}
};// 弧结点定义
struct ArcNode
{int weight; // 弧上的信息部分int adj;        // 邻接点的序号ArcNode *nextarc;ArcNode(){nextarc=NULL;}
};// 顶点结点定义,也就是头节点定义
struct VexNode
{string Info;        // 顶点上的信息部分ArcNode *firstarc;  // 弧链头指针VexNode(){firstarc=NULL;Info.clear();}
};// 邻接表结构的图的定义
struct linkGraph
{VexNode *vexes;int vexnumber;linkGraph(){vexes=NULL;}
};int InitGraph(linkGraph &G, int vexnumber)
{G.vexes = new VexNode[vexnumber];G.vexnumber = vexnumber;for (int i = 0; i < vexnumber; i++)G.vexes[i].firstarc = NULL;return 0;
}// 将邻接矩阵存储的图转换为邻接表存储的图
void InitGraph(linkGraph &G, const Graph& g){int n=g.vexNumber;InitGraph(G,g.vexNumber);// cout<<G.vexnumber<<endl;for( int i=0;i<n;i++){G.vexes[i].Info=(char)(i+'a');for( int j=0;j<n;j++){if(g.adjMatrix[i][j]){ArcNode * NewNode=new ArcNode;NewNode->nextarc= G.vexes[i].firstarc;G.vexes[i].firstarc=NewNode;NewNode->weight=j;}}}
}int DestroyGraph(linkGraph &G)
{for (int i = 0; i < G.vexnumber; i++){while (G.vexes[i].firstarc != NULL){ArcNode *p = G.vexes[i].firstarc;G.vexes[i].firstarc = p->nextarc;delete p;}}delete[]G.vexes;G.vexes = NULL;G.vexnumber = 0;return 0;
}// 输出邻接表存储的图
void PrintGraph(const linkGraph& G){int n=G.vexnumber;for( int i=0;i<n;i++){cout<<G.vexes[i].Info;ArcNode *p=G.vexes[i].firstarc;while(p!=NULL){printf(" --> %c",p->weight+'a');p=p->nextarc;}cout<<endl;}return ;
}
// please comment the following code when you submit to OJ
int main(){//freopen("/config/workspace/answer/test.in","r",stdin);//freopen("/config/workspace/answer/test.out","w",stdout);Graph g;cin >> g.vexNumber;for(int i=0;i<g.vexNumber;i++){g.info[i] = (char)('a'+i);for(int j=0;j<g.vexNumber;j++){cin >> g.adjMatrix[i][j];}}linkGraph G;InitGraph(G,g);PrintGraph(G);DestroyGraph(G);return 0;
}

问题 B: 邻接表存储的图转化为邻接矩阵存储的图

#include<bits/stdc++.h>
using namespace std;#define MAX_SIZE 100// 邻接矩阵存储的图
struct Graph
{int vexNumber;string vexInfo[MAX_SIZE];int adjMatrix[MAX_SIZE][MAX_SIZE];
};// 弧结点定义
struct ArcNode
{int weight;       // 弧上的信息部分int adj;          // 邻接点的序号ArcNode *nextarc; // 下一条边ArcNode(){nextarc =NULL;}
};// 顶点结点定义
struct VexNode
{string Info;       // 顶点上的信息部分ArcNode *firstarc; // 弧链头指针VexNode(){firstarc=NULL;}
};// 邻接表结构的图的定义
struct linkGraph
{VexNode *vexes; //  每个节点的邻接表int vexnumber;  //  节点数量
};// 邻接表存储的图的初始化
int InitGraph(linkGraph &G, int vexnumber)
{G.vexes = new VexNode[vexnumber];G.vexnumber = vexnumber;for (int i = 0; i < vexnumber; i++)G.vexes[i].firstarc = NULL;return 0;
}// 构造边节点指针
// ArcNode* getArcNode(int adj){//     ArcNode* node = new ArcNode();
//     node->adj = adj;
//     node->nextarc = NULL;
//     return node;
// }// 根据输入构造邻接表存储的图
void InputlinkGraph(linkGraph& LG){int n;cin>>n;InitGraph(LG,n);for( int i=0;i<n;i++){string s;cin>>s;int head=s[0]-'a';LG.vexes[head].Info=s[0];for( int j=s.size()-1;j>=2;j-=2){ArcNode * NewNode=new ArcNode;NewNode->weight=s[j]-'a';NewNode->nextarc=LG.vexes[head].firstarc;LG.vexes[head].firstarc=NewNode;}}return ;
}
// 将邻接表存储的图转化为邻接矩阵存储的图
void linkGraph2Graph(const linkGraph& LG, Graph& G){memset(G.adjMatrix,0,sizeof G.adjMatrix);int n=LG.vexnumber;G.vexNumber=n;for( int i=0;i<n;i++){ArcNode * p=LG.vexes[i].firstarc;while(p!=NULL){int j=p->weight;G.adjMatrix[i][j]=1;p=p->nextarc;}}return ;
}// 输出邻接矩阵
void printGraph(const Graph& G){int n=G.vexNumber;for( int i=0;i<n;i++){for( int j=0;j<n;j++){cout<<G.adjMatrix[i][j]<<" ";}cout<<endl;}
}void PrintlinkGraph(const linkGraph &LG){int n=LG.vexnumber;for( int i=0;i<n;i++){cout<<LG.vexes[i].Info;ArcNode *p=LG.vexes[i].firstarc;while(p!=NULL){printf(" --> %c",p->weight+'a');p=p->nextarc;}cout<<endl;}
}// 邻接表存储的图的销毁
int DestroylinkGraph(linkGraph &G)
{for (int i = 0; i < G.vexnumber; i++){while (G.vexes[i].firstarc != NULL){ArcNode *p = G.vexes[i].firstarc;G.vexes[i].firstarc = p->nextarc;delete p;}}delete[] G.vexes;G.vexes = NULL;G.vexnumber = 0;return 0;
}
// please comment the following code when you submit to OJ
int main(){// freopen("/config/workspace/answer/test.in","r",stdin);linkGraph LG;Graph G;InputlinkGraph(LG);PrintlinkGraph(LG);linkGraph2Graph(LG,G);printGraph(G);DestroylinkGraph(LG);return 0;
}

问题 C: 邻接矩阵存储图的DFS

#include<bits/stdc++.h>
using namespace std;#define MAX_SIZE 100// 邻接矩阵存储的图
struct Graph
{int vexNumber;string vexInfo[MAX_SIZE];int adjMatrix[MAX_SIZE][MAX_SIZE];
};// 查找v0的未被访问的邻接点
// int findAdjVex(const Graph& G, int v0, int visited[]){// }
// 以顶点v0为起点,进行一趟DFS
void DFS(const Graph& G, int v0, int visited[]){// 第一步: 先访问v0,标记已访问visited[v0]=1;cout<<v0<<" ";// 第二步: 再对v0的所有未被访问的邻接点进行DFSfor( int i=0;i<G.vexNumber;i++){if(G.adjMatrix[v0][i]==1&&visited[i]==0){DFS(G,i,visited);}}
}
// 对整个图进行DFS
void DFS(const Graph& G){// 第一步:初始化visited数组int visited[MAX_SIZE]={};// 第二步:以每个未被遍历的顶点为起点,进行DFSfor( int i=0;i<G.vexNumber;i++){if(visited[i]==0) DFS(G,i,visited);}}// please comment the following code when you submit to OJ
int main(){// freopen("/config/workspace/answer/test.in","r",stdin);// freopen("/config/workspace/answer/test.out","w",stdout);Graph G;cin >> G.vexNumber;for(int i=0;i<G.vexNumber;i++){//我的编译器to_string没法用G.vexInfo[i] = (char)(i+'a');for(int j=0;j<G.vexNumber;j++){cin >> G.adjMatrix[i][j];}}DFS(G);return 0;
}

问题 D: 邻接矩阵存储图的DFS-非递归算法

#include <iostream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;#define MAX_SIZE 100// 邻接矩阵存储的图
struct Graph
{int vexNumber;string vexInfo[MAX_SIZE];int adjMatrix[MAX_SIZE][MAX_SIZE];
};// 查找v0的未被访问的邻接点
int findAdjVex(const Graph& G, int v0, int visited[]){for( int i=0;i<G.vexNumber;i++){if(G.adjMatrix[v0][i]==1&&visited[i]==0) return i; }return -1;
}
// 以顶点v0为起点,进行一趟DFS
string DFS(const Graph& G, int v0, int visited[]){string result = "";stack<int>st;st.push(v0);result+=to_string(v0);result+=" ";visited[v0]=1;while(!st.empty()){int now=st.top();int to=findAdjVex(G,now,visited);if(to==-1) {st.pop();continue;}else {st.push(to);visited[to]=1;result+=to_string(to);result+=" ";}}return result;
}
// 对整个图进行DFS
string DFS(const Graph& G){string result = "";// 第一步:初始化visited数组const int n=G.vexNumber;int visited[MAX_SIZE]={};// 第二步:以每个未被遍历的顶点为起点,进行DFSfor( int i=0;i<n;i++){if(visited[i]==0) result+= DFS(G,i,visited);}return result;
}// please comment the following code when you submit to OJ
int main(){// freopen("/config/workspace/answer/test.in","r",stdin);// freopen("/config/workspace/answer/test.out","w",stdout);Graph G;cin >> G.vexNumber;for(int i=0;i<G.vexNumber;i++){G.vexInfo[i] = (char)(i+'a');for(int j=0;j<G.vexNumber;j++){cin >> G.adjMatrix[i][j];}}string str = DFS(G);cout << str << endl;return 0;
}

问题 E: 邻接矩阵存储图的BFS

#include<bits/stdc++.h>
using namespace std;
int m[110][110];
void bfs(int n){int vis[110]={};queue<int>q;for( int i=1;i<=n;i++){if(!vis[i]) q.push(i);else continue;while(!q.empty()){int now=q.front();q.pop();if(vis[now]) continue;vis[now]=1;cout<<now-1<<" ";for( int j=1;j<=n;j++){if(!vis[j]&&m[now][j]){q.push(j);}}}}
}
int main(){int n;cin>>n;for( int i=1;i<=n;i++){for( int j=1;j<=n;j++){cin>>m[i][j];}}bfs(n);
}

问题 F: 案例6-1.2:邻接表存储图的广度优先遍历

#include<bits/stdc++.h>
using namespace std;
vector<int>h[100100];
int vis[100100];
void bfs( int x){queue<int>q;q.push(x);while(!q.empty()){int now=q.front();q.pop();if(vis[now]==1) continue;vis[now]=1;cout<<now<<" ";for( int i=0;i<h[now].size();i++){int to=h[now][i];if(vis[to]) continue;else q.push(to);}}
}
int main(){int n,m,x;cin>>n>>m>>x;for( int i=1;i<=m;i++){int a,b;cin>>a>>b;h[a].push_back(b);h[b].push_back(a);}for( int i=0;i<n;i++) sort(h[i].begin(),h[i].end());bfs(x);
}

问题 G: 邻接矩阵存储图的DFS完成序求解

#include <iostream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
using namespace std;#define MAX_SIZE 100// 邻接矩阵存储的图
struct Graph
{int vexNumber;string vexInfo[MAX_SIZE];int adjMatrix[MAX_SIZE][MAX_SIZE];
};// 查找v0的未被访问的邻接点
int findAdjVex(const Graph& G, int v0, int visited[]){for( int i=0;i<G.vexNumber;i++){if(G.adjMatrix[v0][i]==1&&visited[i]==0) return i; }return -1;
}// 算法7-1: 以某个节点为起点求解DFS完成序的算法 (邻接矩阵)
string DFS_finished(const Graph &G, int v0, int visited[]){string res="";visited[v0]=1;while(findAdjVex(G,v0,visited)!=-1){res+=DFS_finished(G,findAdjVex(G,v0,visited),visited);}res+=(char)(v0+'a');  return res;
}// 算法7: DFS完成序的求解算法-邻接矩阵
string DFS_finished(const Graph &G){string res="";int n=G.vexNumber;int visited[MAX_SIZE]={};for( int i=0;i<n;i++){if(visited[i]==0) res+=DFS_finished(G,i,visited);}return res;
}
// please comment the following code when you submit to OJ
int main(){// freopen("/config/workspace/answer/test.in","r",stdin);// freopen("/config/workspace/answer/test.out","w",stdout);Graph G;cin >> G.vexNumber;for(int i=0;i<G.vexNumber;i++){G.vexInfo[i] = (char)('a'+ i);for(int j=0;j<G.vexNumber;j++){cin >> G.adjMatrix[i][j];}}string str = DFS_finished(G);cout << str << endl;return 0;
}

问题 H: 算法7-7,7-8:无向图的连通分量和生成树

#include<bits/stdc++.h>
using namespace std;
int m[100][100];
int vis[100];
int n;
string dfs( int x){string res="";res+=to_string(x);res+=" ";vis[x]=1;for( int i=0;i<n;i++){if(vis[i]==0&&m[x][i]==1){res+=dfs(i);}}return res;
}
int main(){cin>>n;for( int i=0;i<n;i++){for( int j=0;j<n;j++){cin>>m[i][j];}}for( int i=0;i<n;i++){if(vis[i]==0){cout<<dfs(i)<<endl;}}
}

BUCT数据结构——图相关推荐

  1. BUCT数据结构——图(拓扑排序、关键路径)

    文章目录 问题 A: 邻接矩阵存储的图,节点的出度和入度计算(附加代码模式) 问题 B: 算法7-12:有向无环图的拓扑排序 问题 C: 有向图是否存在环? 问题 D: 图-节点的最早发生时间 问题 ...

  2. 数据结构--图(Graph)详解(四)

    数据结构–图(Graph)详解(四) 文章目录 数据结构--图(Graph)详解(四) 一.图中几个NB的算法 1.普里姆算法(Prim算法)求最小生成树 2.克鲁斯卡尔算法(Kruskal算法)求最 ...

  3. 数据结构--图(Graph)详解(三)

    数据结构–图(Graph)详解(三) 文章目录 数据结构--图(Graph)详解(三) 一.深度优先生成树和广度优先生成树 1.铺垫 2.非连通图的生成森林 3.深度优先生成森林 4.广度优先生成森林 ...

  4. 数据结构--图(Graph)详解(二)

    数据结构–图(Graph)详解(二) 文章目录 数据结构--图(Graph)详解(二) 一.图的存储结构 1.图的顺序存储法 2.图的邻接表存储法 3.图的十字链表存储法 4.图的邻接多重表存储法 二 ...

  5. 数据结构--图(Graph)详解(一)

    数据结构–图(Graph)详解(一) 文章目录 数据结构--图(Graph)详解(一) 一.图的基本概念 1.图的分类 2.弧头和弧尾 3.入度和出度 4.(V1,V2) 和 < V1,V2 & ...

  6. 四色着色问题 c语言编程,数据结构-图着色问题

    7-38 图着色问题 (25 分) 图着色问题是一个著名的NP完全问题.给定无向图G=(V,E),问可否用K种颜色为V中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色? 但本题并不是要 ...

  7. 【数据结构(C语言)】数据结构-图

    图 文章目录 图 一.基本概念 1.图的定义 2.约定符号 3.分类 4.子图 5.路 6.其他术语 7.ADT 二.存储结构 1.邻接矩阵(数组) 2.邻接表 三.基本算法 1.遍历 2.求无向图的 ...

  8. 数据结构 --- 图的遍历 DFS、BFS

    什么是DFS.BFS? 一条线走到底,深度优先遍历,每一个顶点只遍历.只打印一次的方式:DFS.BFS 数据结构 --- 图的存储 单纯地把邻接顶点的邻接顶点打印出来,顶点重复遍历,打印多次 从 A→ ...

  9. 数据结构——图(存储结构)

    数据结构--图 图的定义和基本术语 图的类型定义 图的存储结构 数组(邻接矩阵表示法) 网(即有权图)的邻接矩阵表示法 邻接表 邻接表表示法(链式) 图的邻接表存储表示 采用邻接表表示法创建无向网 邻 ...

最新文章

  1. Cell综述:动植物界的微生物群和宿主营养
  2. Oracle 监听器日志配置与管理
  3. git ssh 绑定 GitLab
  4. 线程互斥与同步 在c#中用mutex类实现线程的互斥_Golang 并发编程与同步原语
  5. canvas 在线画图
  6. sdut 数据结构实验之排序三:bucket sort
  7. 用计算机来弹一首少年,辽宁省锦州市2018年中考语文试卷(word版,含答案)
  8. C++11中thread库join和detach的区别
  9. ytkah网站建设解决方案 大中小微企业营销利器
  10. header js修改form_IE9 文件上传表单如何设置自定义header
  11. 基于51单片机的红外接收
  12. ThinkPad Tablet2升级Windows10(各种故障及解决方案)
  13. 学习web前端能做游戏开发吗?
  14. 清除linux系统盘空间,linux磁盘空间不足怎么办,磁盘清理方法
  15. DPDK17.05 第一次试用心得与问题记录
  16. 学习记录:安装binwalk
  17. WOW副本任务制作方法
  18. 联盛德 HLK-W806 (八): 4线SPI驱动SSD1306/SSD1315 128x64 OLED液晶屏
  19. 银联押宝二维码支付 背后还有一片纷争江湖
  20. 51单片机学习笔记——蜂鸣器

热门文章

  1. 【Linux】ab命令实现网站性能压力测试
  2. 如何保存或打印出清晰的域名证书
  3. 崩坏3区号+86_手机号码前的“+86”是什么意思?
  4. Florian%C3%B3polis巴西北岸新业务的最佳场所四方数据分析
  5. C++环境下部署深度学习模型方案
  6. 每日工作记录——ERROR:Simulator:793 - Unable to elaborate instantiated module work
  7. Python 四舍六入五成双
  8. Android面试准备之Java基础
  9. 机器人动力学 - 机器人学中的惯性矩阵坐标转换及在SolidWorks中的测量
  10. 使用 Ceph 作为 OpenStack 的统一存储解决方案