The most commonly used representations of graphs are adjacency matrices and adjacency tables.

1, adjacency matrix

Adjacency matrix is actually a two-dimensional array, for each edge <u,v>, we make a[u][v] = 1, if the graph is entitled to the graph, we can also let a[u][v] equals that right, so the advantage is very simple, but its space requirements are very large, if the figure is dense, Adjacency matrix is the appropriate representation method, if the graph is sparse, then this method is a waste of space, the following shows an example of the adjacency matrix of the graph.

2 adjacency Table

Adjacency table is one of the commonly used storage structures of graphs. The adjacency table consists of a header node and a table node, where each vertex corresponds to a header node stored in the array. As shown: the adjacency table of the graph is: two, topological sort 1, definition

Topological ordering of a directed acyclic graph (Directed acyclic graph abbreviation dag) g is a linear sequence of all vertices in g, so that any pair of vertices u and V in the graph, if <u,v>∈e (G), appears before v in a linear sequence.

Typically, such a linear sequence is called a sequence that satisfies the topological order (topological order), referred to as a topological sequence.

Attention:

1) A topological sequence exists only in the direction-free graph;

2) for a DAG, there may be multiple topological sequences;

2. The idea of topological sequence algorithm

(1) Calculate the penetration of all vertices (if not present, the graph has a loop) and put all the points in the 0 into a queue.

(2) out of the team a vertex of 0, and output, assuming V;

(3) If there is an edge of <v,w>, then the degree of vertex w is reduced by 1, the penetration of all points is scanned, and the vertex with the degree 0 is enqueued; Repeat (2) (3) until all vertices are in degrees 0. The order in which the teams are out is the order in which the topology is sorted. Third, the code implementation to sort the graph is: the representation of the graph selection of the adjacency table representation, the code is as follows:

#include <iostream>using namespace std; The relevant definition of the queue//////////////////////typedef struct Queuerecord *queue; #define Minqueuesize 10struct queuerecord{int capacity; The capacity of the team int Front; Team Head int Rear; Team tail int Size; The number of elements in the team int *array; Related definitions of array};/////////////////////adjacency table//////////////////////typedef struct Edgenode *position;typedef struct Led_table * Table; struct Edgenode//Benzi node {int Adjvex; The adjacency point field, which stores the subscript int weight of the vertex; The weighted value of the corresponding edge position next; Chain field, pointing to the next adjacency point}; struct led_table//adjacency table structure {int data; The size of the adjacency table position *firstedge; Side table header pointer, can be understood as an array}; Queue correlation Function Declaration////////////////////////int IsEmpty (queue Q); Determines whether the queue is empty int isfull (queue Q); Determine if the queue is full of void Makeempty (queue Q); Constructs an empty queue queue createqueue (int maxelements); Create a queue void Disposequeue (queue Q); Releases a queued void Enqueue (int x, Queue Q); enqueue int Dequeue (Queue Q); Out Team int FroNT (Queue Q); Return to team first value, not out of team///////////////////queue correlation function definition////////////////////////int IsEmpty (Queue Q) {return q->size = = 0;} int isful L (Queue Q) {if (Q->size > Q->capacity) {cout << "queue full" << Endl; return-1; } else {return 0; }} void Makeempty (Queue Q) {q->size = 0; Q->front = 1; q->rear = 0;} Queue Createqueue (int maxelements) {queue Q; if (Maxelements < minqueuesize) {cout << "queue size is too small" << Endl; } Q = static_cast<queue> (malloc (struct queuerecord)); if (Q = = NULL) {cout << "out of space!!!"; } Q->array =static_cast<int*> (malloc (int) *maxelements)); if (Q->array = = NULL) {cout << "out of space!!!"; } q->capacity = maxelements; Makeempty (Q); return Q;} void Disposequeue (Queue q) {if (q! = NULL) {free (Q->array); Free (Q); }} static int Succ (int Value, Queue Q) Loop array, used to wrap around {if (++value = = q->capacity) Value = 0; return Value;} void Enqueue (int x, Queue Q) {if (Isfull (Q)) {cout << "full Queue" << Endl; } else {q->size + +; Q->rear = SUCC (Q->rear, Q); Q->array [Q->rear] = x; }} int Dequeue (Queue Q) {if (IsEmpty (Q)) {cout << "Empty Queue" << Endl; return false; Indicates error only} else {q->size--; Q->front = SUCC (Q->front, Q); Return q->array[(Q->front)-1]; }} int Front (Queue Q) {if (IsEmpty (Q)) {cout << "Empty Queue" << Endl; return false; Indicates error only} else return Q->array[q->front];} adjacency table correlation function definition///////////////table creat_lable (int maxelements)//maxelements parameter is the number of nodes you want to create { Table table1 = static_cast<table> (malloc (struct led_table)); table1->data = maxelements;if (table1 = = NULL) {cout << "out of space!!!";} Table1->firstedge = static_cast<position*> (malloc (sizeof (position) * (Table1->data))); if (Table1->firstedge = = NULL) {cout << "out of Space!!!";} Assigns a value to each table header, starting with 0 for (int i = 0; I <= table1->data-1; ++i) {Table1->firstedge [i] = static_cast<position> (malloc (position)); Request a node if (Table1->firstedge [i] = = NULL) {cout << "out of Space!!!";} Table1->firstedge [I]->adjvex = 0; Table header This parameter is stored in degrees Table1->firstedge [i]->weight = 0; This parameter has no meaning at this time Table1->firstedge [i]->next = NULL;} return table1;} void Insert (Table table1, int v, int w, int Weig)//indicates that an edge exists for <v,w>, and the weight is weig{position P = Static_cast<positi On> (malloc (position)); Request a node if (p = = NULL) {cout << "out of Space!!!";} P->adjvex = W; P->weight = Weig;p->next = Table1->firstedge [V]->next;table1->firstedge [V]->next = P;} void Countindegree (Table table1)//Calculates the entry of vertices with the presence of {for (int i = 0; I <=) in the header node.table1->data-1; ++i) {Position p = Table1->firstedge [i]->next; while (P! = NULL) {+ + (Table1->firstedge [P->adjvex]->adjvex); p = p->next; }}}int* Topsort (Table table1)//topological sort {Queue queue_1 = createqueue (15); Create a queue int Counter = 0; int *topnum = static_cast<int*> (malloc (sizeof (int) * (table1->data))); This array is used to store the result int v; for (int i = 0; I! = table1->data; ++i) {if (Table1->firstedge[i]->adjvex = = 0) {Enqueue (i,queue_1); If the entry of vertex i is 0, the queue}} while (! IsEmpty (queue_1)) {v = Dequeue (queue_1); TOPNUM[V] = ++counter; If topnum[5] = 3, then the third row is V5 position p = Table1->firstedge [V]->next;while (P! = NULL)//Delete this node after the update into the degree {--(tabl E1->firstedge [P->adjvex]->adjvex); if (Table1->firstedge [P->adjvex]->adjvex = = 0) Enqueue (p->adjvex,queue_1); p = p->next;} } if (Counter! = table1->data) {cout << "The graph has a cycle" << Endl; } disposequeue (Queue_1); return topnum;} void Print_topsort_result (int *topnum,table table1)//Print topology sort result {for (int i = 1; I! = table1->data+1; ++i) { for (int j = 0; J! = Table1->data; ++j) {if (topnum[j] = = i) {if (i = = table1->data) {cout << "V" & lt;< J; } else {cout << "V" << J << "-";} }}} cout << Endl;} int main () {Table table_1 = creat_lable (7); Create an adjacency table of size 7//insert (table_1, 0, 1, 2) for the adjacency table by graph, insert (table_1, 0, 2, 4), insert (table_1, 0, 3, 1); Insert (Table_1, 1, 3, 3); Insert (Table_1, 1, 4, 10); Insert (Table_1, 2, 5, 5); Insert (Table_1, 3, 2, 2), insert (Table_1, 3, 5, 8), insert (Table_1, 3, 6, 4); Insert (Table_1, 4, 3, 2); Insert (Table_1, 4, 6, 6); Insert (Table_1, 6, 5, 1); Countindegree (table_1); Calculation of int* Topnum = Topsort (table_1); Topological sequencing Print_topsort_result (topnum,table_1); Print topology sorting results delete topnum; DeleteTable_1; while (1); return 0;}

The output is:

The idea of the above algorithm is very important, to learn more.

It's late at night ....

The adjacency table representation of graphs and topological ordering of non-circular graphs