当前位置:网站首页>Related operations of figure
Related operations of figure
2022-07-25 18:07:00 【Heart see heart】
Preface
Subject requirements :
1. According to input , Data using adjacency matrix ;
2. Realize the depth first or breadth first traversal of the graph .
3. Circle of friends problem
4. Village to village connectivity
Tips : The following is the main body of this article , The following cases can be used for reference
Complete code
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <malloc.h>
#define Maxvex 100
typedef char VertexType;
typedef int EdgeType;
int visited[Maxvex];
// Define the diagram structure
typedef struct MGraph {
VertexType vexs[Maxvex];
EdgeType arc[Maxvex][Maxvex];
int numvex, numedg;
}MGraph;
// Define the queue
typedef struct Queue {
int data[Maxvex];
int head,tail;
}Queue;
// Initialize queue
void BeQueue(Queue *Q){
Q->head=Q->tail=0;
}
// The team
void InQueue(Queue *Q,int i){
Q->data[Q->tail]=i;
Q->tail=(Q->tail+1)%Maxvex;
}
// Out of the team
void OutQueue(Queue *Q,int *i){
*i=Q->data[Q->head];
Q->head=(Q->head+1)%Maxvex;
}
// Determines if the queue is empty
int Judge(Queue *Q){
if(Q->head==Q->tail){
return 1;
}
else{
return 0;
}
}
// Establish adjacency graph
void CreateMGraph(MGraph* G) {
int i, j, k, w;
printf(" Enter the number of vertices and edges :\n");
scanf("%d%d", &G->numvex, &G->numedg);
getchar();// Clear cache
printf(" Enter each vertex :\n");
for (i = 0;i < G->numvex;i++){
printf(" The vertices %d:",i+1);
scanf("%c",&G->vexs[i]);
getchar();// Clear cache
}
for (i = 0;i < G->numvex;i++) {
for (j = 0;j < G->numvex;j++) {
G->arc[i][j] = 0;// Assign an initial value to the weight 0
}
}
for (k = 0;k < G->numedg;k++) {
printf(" The input side (vi,vj) The superscript of i, Subscript j And weights w\n");
scanf("%d%d%d", &i, &j,&w);
G->arc[i][j] = w;
G->arc[j][i] = G->arc[i][j];
}
}
// Build relationships
void CreateFriend(MGraph* R) {
int i, j, k;
printf(" Enter the number of people and the number of relationship sides :\n");
scanf("%d%d", &R->numvex, &R->numedg);
getchar();// Clear cache
printf(" Enter the person's code :\n");
for (i = 0;i < R->numvex;++i){
printf(" people %d:",i+1);
scanf("%c",&R->vexs[i]);
getchar();// Clear cache
}
for (i = 0;i < R->numvex;i++) {
for (j = 0;j < R->numvex;j++) {
R->arc[i][j] = 0;// Assign an initial value to the weight 0
}
}
for (k = 0;k < R->numedg;k++) {
printf(" Input person (vi,vj) The superscript of i, Subscript j\n");
scanf("%d%d", &i, &j);
R->arc[i][j] = 1;
R->arc[j][i] = R->arc[i][j];
}
}
// Establish inter Village Relations
void CreateCountryside(MGraph* C) {
int i, j, k, w;
printf(" Input the number of villages and connecting channels :\n");
scanf("%d%d", &C->numvex, &C->numedg);
getchar();// Clear cache
for (i = 1;i <= C->numvex;i++) {
for (j = 1;j <= C->numvex;j++) {
C->arc[i][j] = 0;// Assign an initial value to the weight 0
}
}
for (k = 1;k <= C->numedg;k++) {
printf(" Enter village road (vi,vj) The superscript of i, Subscript j And weights w\n");
scanf("%d%d%d", &i, &j,&w);
C->arc[i][j] = w;
C->arc[j][i] = C->arc[i][j];
}
}
// Print adjacency diagram
void print(MGraph* G) {
int i, j;
printf(" Adjacency graph is :\n");
for (i = 0;i < G->numvex;i++) {
for (j = 0;j < G->numvex;j++) {
printf("%d\t", G->arc[i][j]);
}
printf("\n");
}
}
// Print a relationship map
void printF(MGraph* R) {
int i, j;
printf(" The interpersonal relationship diagram is :\n");
for (i = 0;i < R->numvex;i++) {
for (j = 0;j < R->numvex;j++) {
printf("%d\t", R->arc[i][j]);
}
printf("\n");
}
}
// Print the village diagram
void printC(MGraph* G) {
int i, j;
printf(" The village relationship diagram is :\n");
for (i = 1;i <= G->numvex;i++) {
for (j = 1;j <= G->numvex;j++) {
printf("%d\t", G->arc[i][j]);
}
printf("\n");
}
}
// Deep search
void DFS(MGraph G, int i) {
int j;
visited[i] = 1;
printf("%c", G.vexs[i]);
for (j = 0;j < G.numvex;j++) {
if (G.arc[i][j] !=0 && !visited[j]) {
DFS(G, j);
}
}
}
void DFST(MGraph G){
int i;
for(i=0;i<G.numvex;++i){
visited[i]=0;
}
for(i=0;i<G.numvex;++i){
if(!visited[i]){
DFS(G,i);
}
}
}
// Width search
void BFS(MGraph *G){
Queue Q;
int i,j;
for(i=0;i<G->numvex;i++){
visited[i]=0;
}
BeQueue(&Q);
for(i=0;i<G->numvex;i++){
visited[i]=1;
printf("%c",G->vexs[i]);
InQueue(&Q,i);
while(!Judge(&Q)){
OutQueue(&Q,&i);
for(j=0;j<G->numvex;j++){
if(!visited[j]&&G->arc[i][j]!=0){
visited[j]=1;
printf("%c",G->vexs[j]);
InQueue(&Q,j);
}
}
}
}
printf("\n");
}
// Please have a few circles of friends
void DFSF(MGraph R, int i) {
int j;
visited[i] = 1;
for (j = 0;j < R.numvex;j++) {
if (R.arc[i][j] !=0 && !visited[j]) {
DFSF(R, j);
}
}
}
void DFSTF(MGraph R){
int i,c=0;
for(i=0;i<R.numvex;++i){
visited[i]=0;
}
for(i=0;i<R.numvex;++i){
if(!visited[i]){
DFSF(R,i);
c++;
}
}
printf("%d Individuals share %d Circle of friends \n",R.numvex,c);
}
// Prim algorithm
void Prim(MGraph C){
int min,i,j,k,L=0;
int adjvex[Maxvex];
int lowcost[Maxvex];
adjvex[1]=1;
lowcost[0]=0;
for(i=2;i<=C.numvex;i++){
lowcost[i]=C.arc[1][i];
adjvex[i]=1;
}
for(i=2;i<=C.numvex;i++){
min=1000000;
j=2;k=1;
while(j<=C.numvex){
if(lowcost[j]!=0&&lowcost[j]<min){
min=lowcost[j];
k=j;
}
j++;
}
if(min!=1000000)
L=L+min;
lowcost[k]=0;
for(j=2;j<=C.numvex;j++){
if(lowcost[j]!=0&&C.arc[k][j]<lowcost[j]){
lowcost[j]=C.arc[k][j];
adjvex[j]=k;
}
}
}
printf("%d\n",L);
}
// Circle of friends problem
void Friend(){
MGraph R;
CreateFriend(&R);
printF(&R);
DFSTF(R);
}
// Village to village connectivity
void Countryside(){
MGraph C;
CreateCountryside(&C);
printC(&C);
Prim(C);
}
int function(){
int choose=-1;
printf("1、 Create diagrams \n");
printf("2、 Depth-first traversal \n");
printf("3、 Breadth first traversal \n");
printf("4、 Circle of friends problem \n");
printf("5、 Village to village connectivity \n");
printf("0、 sign out \n");
while(choose!=0){
printf(" Please select :\n");
scanf("%d", &choose);
switch(choose){
case 1:
MGraph G;
CreateMGraph(&G);
print(&G);
break;
case 2:
printf(" The depth traversal of the graph is :\n");
DFST(G);
printf("\n");
break;
case 3:
printf(" The breadth traversal of the graph is :\n");
BFS(&G);
break;
case 4:
Friend();
break;
case 5:
Countryside();
break;
}
}
}
int main() {
function();
return 0;
}
experimental result
main interface 
Adjacency matrix 
Circle of friends problem 
Village to village connectivity 
边栏推荐
- Wu Enda's machine learning programming operation cannot be suspended pause problem solved
- MySQL数据库常用命令
- 越来越成熟的Rust,都应用了哪些场景呢?
- SQL optimizer parsing | youth training camp notes
- 文件基础知识
- Introduction to cloud XR and development opportunities of cloud XR in 5g Era
- How many points did NPDP pass? How to pass with high scores?
- Error when starting MySQL on Linux
- IDEA集成SVN代码管理常用功能
- Auditing related notes
猜你喜欢

喜讯!瑞云科技被授予“海上扬帆”5G融合应用专委会成员单位

Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法

云流化和云桌面有什么关系

越来越成熟的Rust,都应用了哪些场景呢?

「行话」| 用DevOps高效交付游戏,是种什么体验?

tkinter GUI版通信录管理系统

How to judge the performance of static code quality analysis tools? These five factors must be considered

Imx6 rtl8189ftv migration

Cloud XR面临的问题以及Cloud XR主要应用场景

How to read a Book
随机推荐
Polynomial addition
Ch582 ble 5.0 uses Le coded broadcast and connection
Unity 贝塞尔曲线的创建
mysql case when
Redis source code and design analysis -- 16. AOF persistence mechanism
Resttemplate realizes the unified encapsulation (printable log) of post, put, delete, get, set request and file upload (batch files and parameters) through generics
「数字安全」警惕 NFT的七大骗局
C# Linq 去重&去重求和
图的相关操作
Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution
绘制pdf表格 (二) 通过itext实现在pdf中绘制excel表格样式设置中文字体、水印、logo、页眉、页码
Why the future of digitalization depends on 3D real-time rendering
H5测试点(思维导图)
OV7725 yuv 640*[email protected] 配置文件
tkinter GUI版通信录管理系统
SLA 、SLO & SLI
Cloud VR: the next step of virtual reality specialization
New and malloc
Problems faced by cloud XR and main application scenarios of cloud XR
Redis source code and design analysis -- 17. Redis event processing