Week 11 [project 1 - basic algorithm library of figure]

/*

*Copyright (c)2017, School of computer and control engineering, Yantai University

*All rights reservrd.

*Author: Li Xinhao

*Completion time: November 9, 2017

*Version number: v1.0

*Problem Description: define the adjacency matrix and adjacency table storage structure of graph, realize its basic operation, and complete the test.

1, Create a new project, create the header file graph.h, and declare the related functions.

``````#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED

#define MAXV 100 / / maximum number of vertices
#Define inf 32767 / / inf means ∞
typedef int InfoType;

//The following defines the type of adjacency matrix
typedef struct
{
int no;                     //Vertex number
InfoType info;              //Other information of vertex, where the weight of weighted graph is stored
} VertexType;                   //Vertex Type

typedef struct                  //Definition of Graphs
{
int n,e;                    //Number of vertices, arcs
VertexType vexs[MAXV];      //Store vertex information
} MGraph;                       //Adjacency matrix types of Graphs

//The following defines the adjacency table type
typedef struct ANode            //Node structure type of arc
{
int adjvex;                 //End position of the arc
struct ANode *nextarc;      //Pointer to the next arc
InfoType info;              //The relevant information of the arc is used to store the weight
} ArcNode;

typedef int Vertex;

{
Vertex data;                //Vertex information
int count;                  //Store vertex access, only used in topological sorting
ArcNode *firstarc;          //Point to the first arc
} VNode;

typedef struct
{
int n,e;                    //Vertex number n and edge number e in the graph
} ALGraph;                      //Adjacency table types of Graphs

//Function: construct a graph stored by adjacency matrix from a 2D array reflecting the adjacency relationship of vertices in the graph
//Parameter: Arr - array name. Since the formal parameter is a two-dimensional array, the number of elements in each row must be given. Here, the parameter Arr is declared as a one-dimensional array name (pointer to int)
//      The order of n - matrix
//      g - adjacency matrix data structure to be constructed
void ArrayToMat(int *Arr, int n, MGraph &g); //Constructing adjacency matrix of graph with common array
void ArrayToList(int *Arr, int n, ALGraph *&); //Constructing adjacency table of graph with common array
void DispMat(MGraph g);//Output adjacency matrix g

#endif // GRAPH_H_INCLUDED``````
2, Create a source file, graph.cpp, containing the definition of the functions that implement various algorithms

``````#include <stdio.h>
#include <malloc.h>
#include "graph.h"

//Function: construct a graph stored by adjacency matrix from a 2D array reflecting the adjacency relationship of vertices in the graph
//Parameter: Arr - array name. Since the formal parameter is a two-dimensional array, the number of elements in each row must be given. Here, the parameter Arr is declared as a one-dimensional array name (pointer to int)
//      The order of n - matrix
//      g - adjacency matrix data structure to be constructed
void ArrayToMat(int *Arr, int n, MGraph &g)
{
int i,j,count=0;  //Count is used to count the number of sides, that is, the number of non-zero elements in the matrix
g.n=n;
for (i=0; i<g.n; i++)
for (j=0; j<g.n; j++)
{
g.edges[i][j]=Arr[i*n+j]; //The Arr is regarded as a two-dimensional array of n × n, and the Arr[i*n+j] is the Arr[i][j]
if(g.edges[i][j]!=0 && g.edges[i][j]!=INF)
count++;
}
g.e=count;
}

void ArrayToList(int *Arr, int n, ALGraph *&G)
{
int i,j,count=0;  //Count is used to count the number of sides, that is, the number of non-zero elements in the matrix
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
G->n=n;
for (i=0; i<n; i++)                 //Set the initial value of the pointer field of all header nodes in the adjacency table
for (i=0; i<n; i++)                 //Check every element in adjacency matrix
for (j=n-1; j>=0; j--)
if (Arr[i*n+j]!=0)      //There is an edge. Consider Arr as a two-dimensional array of n × n. Arr[i*n+j] is Arr[i][j]
{
p=(ArcNode *)malloc(sizeof(ArcNode));   //Create a node * p
p->info=Arr[i*n+j];
}

G->e=count;
}

void MatToList(MGraph g, ALGraph *&G)
{
int i,j;
ArcNode *p;
G=(ALGraph *)malloc(sizeof(ALGraph));
for (i=0; i<g.n; i++)                   //Set the initial value of the pointer field of all header nodes in the adjacency table
for (i=0; i<g.n; i++)                   //Check every element in adjacency matrix
for (j=g.n-1; j>=0; j--)
if (g.edges[i][j]!=0)       //There is an edge
{
p=(ArcNode *)malloc(sizeof(ArcNode));   //Create a node * p
p->info=g.edges[i][j];
}
G->n=g.n;
G->e=g.e;
}

void ListToMat(ALGraph *G,MGraph &g)
{
int i,j;
ArcNode *p;
g.n=G->n;   //According to the first floor students "report" change. g.n is not assigned, the following initialization does not work
g.e=G->e;
for (i=0; i<g.n; i++)   //Initialize adjacency matrix first
for (j=0; j<g.n; j++)
g.edges[i][j]=0;
for (i=0; i<G->n; i++)  //Assign values to adjacency matrix according to adjacency table
{
while (p!=NULL)
{
p=p->nextarc;
}
}
}

void DispMat(MGraph g)
{
int i,j;
for (i=0; i<g.n; i++)
{
for (j=0; j<g.n; j++)
if (g.edges[i][j]==INF)
printf("%3s","∞");
else
printf("%3d",g.edges[i][j]);
printf("\n");
}
}

{
int i;
ArcNode *p;
for (i=0; i<G->n; i++)
{
printf("%3d: ",i);
while (p!=NULL)
{
p=p->nextarc;
}
printf("\n");
}
}``````
3, Create the main.cpp file and complete the test

``````#include <stdio.h>
#include <malloc.h>
#include "graph.h"

int main()
{
MGraph g1,g2;
ALGraph *G1,*G2;
int A[6][6]=
{
{0,5,0,7,0,0},
{0,0,4,0,0,0},
{8,0,0,0,0,9},
{0,0,5,0,0,6},
{0,0,0,5,0,0},
{3,0,0,0,1,0}
};

ArrayToMat(A[0], 6, g1);  //Take the starting address of the two-dimensional array as the argument, and use A[0], because it is essentially a one-dimensional array address, which matches the parameter
printf(" Directed graph g1 Adjacency matrix of:\n");
DispMat(g1);

ArrayToList(A[0], 6, G1);
printf(" Directed graph G1 Adjacency list:\n");

MatToList(g1,G2);

ListToMat(G1,g2);
DispMat(g2);
printf("\n");
return 0;
}``````
The running results of the program are as follows:

Published 45 Original Articles· Zan Zan 2. Visits 4920

Posted on Wed, 01 Apr 2020 16:26:06 -0700 by ok