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 edges[MAXV][MAXV];      //adjacency matrix
    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;

typedef struct Vnode            //Types of adjacent header nodes
{
    Vertex data;                //Vertex information
    int count;                  //Store vertex access, only used in topological sorting
    ArcNode *firstarc;          //Point to the first arc
} VNode;

typedef VNode AdjList[MAXV];    //AdjList is an adjacency table type

typedef struct
{
    AdjList adjlist;            //Adjacency list
    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 MatToList(MGraph g,ALGraph *&G);//Transforming adjacency matrix G into adjacency table G
void ListToMat(ALGraph *G,MGraph &g);//Transforming adjacency table g into adjacency matrix G
void DispMat(MGraph g);//Output adjacency matrix g
void DispAdj(ALGraph *G);//Output adjacency table 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
        G->adjlist[i].firstarc=NULL;
    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->adjvex=j;
                p->info=Arr[i*n+j];
                p->nextarc=G->adjlist[i].firstarc;      //Insert * p by head insertion
                G->adjlist[i].firstarc=p;
            }

    G->e=count;
}

void MatToList(MGraph g, ALGraph *&G)
//Transforming adjacency matrix G into adjacency table 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
        G->adjlist[i].firstarc=NULL;
    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->adjvex=j;
                p->info=g.edges[i][j];
                p->nextarc=G->adjlist[i].firstarc;      //Insert * p by head insertion
                G->adjlist[i].firstarc=p;
            }
    G->n=g.n;
    G->e=g.e;
}

void ListToMat(ALGraph *G,MGraph &g)
//Transforming adjacency table g into adjacency matrix 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
    {
        p=G->adjlist[i].firstarc;
        while (p!=NULL)
        {
            g.edges[i][p->adjvex]=p->info;
            p=p->nextarc;
        }
    }
}

void DispMat(MGraph g)
//Output adjacency matrix 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");
    }
}

void DispAdj(ALGraph *G)
//Output adjacency table G
{
    int i;
    ArcNode *p;
    for (i=0; i<G->n; i++)
    {
        p=G->adjlist[i].firstarc;
        printf("%3d: ",i);
        while (p!=NULL)
        {
            printf("-->%d/%d ",p->adjvex,p->info);
            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");
    DispAdj(G1);

    MatToList(g1,G2);
    printf(" chart g1 Transformation of adjacency matrix into adjacency table G2:\n");
    DispAdj(G2);

    ListToMat(G1,g2);
    printf(" chart G1 Transformation from adjacency table to adjacency matrix g2:\n");
    DispMat(g2);
    printf("\n");
    return 0;
}
The running results of the program are as follows:







Published 45 Original Articles· Zan Zan 2. Visits 4920
Private letter follow

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