FIG achieve its basic algorithm - adjacency tables to store

The main data structure of a table are contiguous,
FIG sparse adjacency table applies, the more space-saving manner if stored in a sparse adjacency matrix of FIG likely to cause waste of space.



But the point adjacent to the edge of the table using a vector to achieve, did not take the chain structure (feeling write simpler, manual funny).
The main achievement of directional memory map, as long as no plus side when the one-way to two-way side into a directed graph.
For example: This added undirected edges 1-2, 1-2, and one only need to add a 2-1 on it.
To achieve the main function:
The degree of adjacency table creation, depth-first search (recursive and non-recursive dfs dfs), breadth-first search (bfs), as well as find out the degree of point
//
//  main.cpp
// Figure
//
//  Created by Mr chen on 2019/3/15.
//  Copyright © 2019 Mr chen. All rights reserved.
//

/*
    stl version
    Existing stack and queue implementation
    FIG taking data structure: adjacency list
    Adjacency table with the vector to achieve
    Directed graph
    The maximum number of points is 107
    The zero subscript default input point, according to the data processing from the start 0
*/

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

const int maxn = 1e2+7;
using namespace std;

typedef char vertextype; // storage type defined vertices
typedef int arctype; // weight of the side defined type

struct ANode // node side table
{
    int id; // neighbor domain, the storage index corresponding to the vertex
    arctype wigth; // for storing weights
};

struct VNode // vertex node table
{
    int id;
    string data; // information storing vertex data
    vector <ANode> q; // each point of contact points with adjacent weights
};

class Graph
{
private:
    int = 0 sum; // statistical point of visits
    VNode VNodeList [maxn]; // vertex node list, each point information storage
    bool vis[maxn];
    int in[maxn],out[maxn];
public:
    int pointnum, eagenum; // number of points, edges
    Graph(int n,int m);
    void initdfs();
    void initdegree();
    void rec_dfs(int begin);
    void nonrec_dfs(int begin);
    void bfs(int begin);
    int is_connected();
    int get_degree();
    // TODO: (5) x input vertex, find FIGS G: If x is present containing vertex, deleting the node associated side and connected therewith, and for traversal DFS (3 perform the operation); otherwise, outputs the message "No x ";
    void print_degree();
    void print();
};

Graph :: Graph (int n, int m) // Constructor, input
{
    this->pointnum=n,this->eagenum=m;
    // printf ( "% d Please enter a name point: \ n", n);
    for(int i=0;i<this->pointnum;i++)
    {
        // printf ( "% d name of the first point is:", i);
        cin>>VNodeList[i].data;
        VNodeList[i].id=i;
    }
    // printf ( "% d sides enter the information, the format: starting end number ID weights: \ n", m);
    int begin,end,wight;
    for(int i=0;i<this->eagenum;i++)
    {
        // printf ( "% d of information of edges:", i);
        cin>>begin>>end>>wight;
        VNodeList[begin].q.push_back( {end,wight} );
    }
}

void Graph::initdfs()
{
    sum=0;
    memset(vis, 0, sizeof(vis));
}

void Graph::initdegree()
{
    memset(out,0, sizeof(out));
    memset(in,0, sizeof(in));
}

void Graph::rec_dfs(int n)
{
    cout<<VNodeList[n].data<<endl;
    sum++;
    am [n] = 1;
    if(sum>=this->pointnum)
        return;
    for(vector<ANode>::iterator it=VNodeList[n].q.begin();it<VNodeList[n].q.end();it++)
    {
        if(!vis[it->id])
        {
            rec_dfs(it->id);
        }
    }
}

void Graph::nonrec_dfs(int n)
{
    initdfs (); // initialize the array vis
    stack<VNode> Stack;
    Stack.push (VNodeList [n]); // root stack
    am [n] = 1;
    while(!Stack.empty())
    {
        VNode tmp = Stack.top();
        Stack.pop();
        cout<<tmp.data<<endl;
        for(vector<ANode>::iterator it=VNodeList[tmp.id].q.begin();it<VNodeList[tmp.id].q.end();it++)
        {
            if(!vis[it->id])
            {
                Stack.push (VNodeList [it-> id]); // not pushed on the stack adjacent point
                vis[it->id]=1;
            }
        }
    }
}

void Graph::bfs(int n)
{
    initdfs();
    queue<VNode> Queue;
    Queue.push(VNodeList[n]);
    while(!Queue.empty())
    {
        VNode tmp = Queue.front();
        Queue.pop();
        cout<<tmp.data<<endl;
        for(vector<ANode>::iterator it=VNodeList[tmp.id].q.begin();it<VNodeList[tmp.id].q.end();it++)
        {
            if(!vis[it->id])
            {
                Queue.push (VNodeList [it-> id]); // not enter into abutment queue queue
                vis[it->id]=1;
            }
        }
    }
}

int Graph::is_connected()
{
    initdfs();
    int years = 0;
    for(int i=0;i<pointnum;i++)
    {
        if(!vis[i])
        {
            rec_dfs(i);
            ++ years;
        }
    }
    return years;
}

int Graph::get_degree()
{
    for (int i = 0; i <pointnum; i ++) // degrees obtained
    {
        out[i] = VNodeList[i].q.size();
    }
    for(int i=0;i<pointnum;i++)
    {
        for(vector<ANode>::iterator it=VNodeList[i].q.begin();it<VNodeList[i].q.end();it++)
        {
            in[it->id]++;
        }
    }
    return 0;
}

void Graph::print_degree()
{
    printf ( "Name of the penetration: \ n");
    for(int i=0;i<pointnum;i++)
    {
        cout<<VNodeList[i].data<<" "<<out[i]<<" "<<in[i]<<endl;
    }
}

void Graph :: print () // output
{
    for(int i=0;i<pointnum;i++)
    {
        cout<<VNodeList[i].q.end()-VNodeList[i].q.begin()<<endl;
        cout<<VNodeList[i].data<<" ";
        for(vector<ANode>::iterator it=VNodeList[i].q.begin();it<VNodeList[i].q.end();it++)
        {
            cout<<i<<" "<<it->id<<" "<<it->wigth<<"   ";
        }
        cout<<endl;
    }
}

int main ()
{
    int n,m;
    while(cin>>n>>m)
    {
        Graph mmp(n,m);
        mmp.print();
        int x;
        cin>>x;
        mmp.initdfs();
        mmp.rec_dfs(x);
        cout<<"rec_dfs end"<<endl;
        mmp.nonrec_dfs(x);
        cout<<"nonrec_dfs end"<<endl;
        mmp.bfs(x);
        cout<<"bfs end"<<endl;
        mmp.get_degree();
        mmp.print_degree();
        int Conn;
        if((connum=mmp.is_connected())<=1)
            printf("This is a connected graph\n");
        else
            printf("NO!connected component is %d\n",mmp.is_connected());
    }
}

Guess you like

Origin blog.csdn.net/hrbust_cxl/article/details/88597806