Graph of data Structure learning (c + +)

Source: Internet
Author: User
Tags bool data structures getv

The application of graphs is probably the broadest of all data structures, but this is doomed to speak of "data structure of the graph" when there is nothing to say-the most important thing about the graph is the algorithm, and a considerable part is very professional, the general people almost will not touch, relatively, the structure appears to be very light weight. You can see that there are very few operations on the elements in the diagram, and there is not a whole bunch of "interfaces" listed in the single linked list. If a structure is complex, the exact definition of the operation is limited.

Basic Storage methods

Anyway, you have to get the survival up first. Do not read a lot of methods listed, there is only one--adjacency matrix. If the matrix is sparse, you can use a cross list to store the matrix (see the previous sparse matrix (cross list)). If we only relate to the relationship of the rows, then it is the adjacency table (the Edge table), on the contrary, only concerned about the relationship between the columns, is the inverse adjacency table (into the side table).

The implementation of the two storage methods is given below.

#ifndef Graphmem_h


#define GRAPHMEM_H


#include <vector>


#include <list>


using namespace std;


template <class name, Class dist, class mem> class network;


const int MAXV = 20;//Maximum number of nodes


template <class name, class dist>


class Adjmatrix


{


friend class Network<name, Dist, adjmatrix<name, dist> >;


Public:


Adjmatrix (): Vnum (0), ENum (0)


{


Vertex = new NAME[MAXV]; Edge = new DIST*[MAXV];


for (int i = 0; i < MAXV i++) edge[i] = new DIST[MAXV];


}


~adjmatrix ()


{


for (int i = 0; i < MAXV; i++) delete []edge[i];


delete []edge; delete []vertex;


}


bool Insertv (name V)


{


if (find (v)) return false;


Vertex[vnum] = v;


for (int i = 0; i < MAXV i++) edge[vnum][i] = Noedge;


vnum++; return true;


}


bool Inserte (name v1, name v2, dist cost)


{


int I, J;


if (v1 = = V2 | |!find (v1, i) | |!find (v2, J)) return false;


if (Edge[i][j]!= Noedge) return false;


edge[i][j] = cost; enum++; return true;


}


name& Getv (int n) {return vertex[n];//no Cross-border check


int Nextv (int m, int n)//returns the first contiguous vertex number after the nth vertex of the M vertex, no return-1


{


for (int i = n + 1; i < vnum i++) if (edge[m][i)!= Noedge) return i;


return-1;


}


Private:


int vnum, eNum;


Dist Noedge, **edge; Name *vertex;


bool Find (const name& v)


{


for (int i = 0; i < vnum i++) if (v = = Vertex[i]) return true;


return false;


}


bool Find (const name& V, int& i)


{


for (i = 0; i < Vnum. i++) if (v = = Vertex[i]) return true;


return false;


}


};


template <class name, class dist>


class LinkedList


{


friend class Network<name, Dist, linkedlist<name, dist> >;


Public:


LinkedList (): Vnum (0), ENum (0) {}


~linkedlist ()


{


for (int i = 0; i < vnum; i++) Delete vertices[i].e;


}


bool Insertv (name V)


{


if (find (v)) return false;


Vertices.push_back (Vertex (V, new list<edge>));


vnum++; return true;


}


bool Inserte (const name& v1, const name& v2, const dist& cost)


{


int I, J;


if (v1 = = V2 | |!find (v1, i) | |!find (v2, J)) return false;


for (List<edge>::iterator iter = Vertices[i].e->begin ();


iter!= vertices[i].e->end () && Iter->vid < J; iter++);


if (iter = Vertices[i].e->end ())


{


Vertices[i].e->push_back (Edge (J, cost)); enum++; return true;


}


if (Iter->vid = = j) return false;


Vertices[i].e->insert (ITER, Edge (j, cost)); enum++; return true;


}


name& Getv (int n) {return vertices[n].v;}//No Cross-border check


int Nextv (int m, int n)//returns the first contiguous vertex number after the nth vertex of the M vertex, no return-1


{


for (List<edge>::iterator iter = Vertices[m].e->begin ();


iter!= vertices[m].e->end (); iter++) if (Iter->vid > N) return iter->vid;


return-1;


}


Private:


bool Find (const name& v)


{


for (int i = 0; i < vnum i++) if (v = = VERTICES[I].V) return true;


return false;


}


bool Find (const name& V, int& i)


{


for (i = 0; i < Vnum. i++) if (v = = VERTICES[I].V) return true;


return false;


}


struct Edge


{


Edge () {}


Edge (int VID, dist cost): Vid (vid), cost (cost) {}


int VID;


Dist Cost;


};


struct vertex


{


vertex () {}


Vertex (name V, list<edge>* e): V (v), E (e) {}


name V;


list<edge>* E;


};


int vnum, eNum;


vector<vertex> vertices;


};


#endif

This implementation is very simple, but should be able to meet the explanation behind. Now this is nothing to do, do not worry, in the next chapter will tell the diagram of Dfs and BFS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.