Commonly used data structure diagram--topology sort __ data structure

Source: Internet
Author: User
common data structure diagram-topology sorting

The storage Topology sort code example of topological sort diagram of common data structure graph

Figure

In mathematics, a graph is a mathematical object representing the relationship between objects and objects, and is the basic research object of graph theory.

Graph is a very important data structure, which is often used in the application of real life. Common problems in life such as traffic maps, assignment assignments, duration calculations, aviation networks, etc., can be modeled using graph-related theories.

The following are some definitions of graphs in data structure and algorithm analysis

A graph (graph) G = (V, e) consists of a vertex (vertex) set and an edge (edge) set E. Each edge is a point pair (v,w), where the v,w belongs to the set V. Sometimes the edge edges are called arcs (ARC). If the point pair is ordered, then the graph is called ordered (directed). A graph that has a direction is sometimes called a direction graph. Vertex V and W adjacency (adjacent) if and only if (v,w) belongs to E. In a v,w graph with edges (w,v) so that the W and V are contiguous and the V and W are contiguous. Sometimes the side also has a third element, called the right (weight) or value (cost). the storage of graphs

A simple way to store a graph is to use a two-dimensional array called adjacency Matrix A[i][j], the size of the array is n * N,n as the number of vertices of the graph. If vertex i to vertex J-connected, then a[i][j] = 1, otherwise a[i][j] = 0. The advantages of this storage method is simple and intuitive, easy to implement. The disadvantage is also obvious, the need for storage space is huge.

When most vertices are not connected in graph G with n vertices, it means that there are a large number of elements in n * n adjacency matrix 0, i.e., the adjacency matrix is sparse matrix at this time.

Another common way of storing is called the adjacency table (adjacent list), which is to request an array head of size n, an array element head[i], which holds the header address of a list of all the adjacent top bottoms of vertex i. The advantages of this type of storage are obvious, compared to the previous way, the size of the storage space significantly reduced. But the disadvantage is not intuitive, coding is difficult. topology Sorting

Topological ordering is a sort of vertex of a VJ graph, which makes it possible to have a path from VI to a VJ, which must appear behind VI in order.

A simple algorithm for topological ordering is to find a vertex with any entry of 0. Then we output the vertex, and it's erased with its edges. Then, the entry of its contiguous vertices is subtracted by 1. The above process is then repeated, and the direct diagram is completely removed.

It is not difficult to see that this algorithm is the first outer loop n times, followed by the internal loop in the selection of 0 vertices, the internal loop n times. So the total time complexity will reach N * N.

Another better way to improve it is to pressing a vertex of all degrees 0 into a stack, then outputting the top element A at a time, then subtracting the entry of all contiguous vertices of a by 1, and then continuing into the stack if the entry of an adjacent vertex is 0 at this time. Repeat appeal operation instruction stack empty.

It can be seen that the operation of each of the vertices into the stack of 0 is executed n times, N is the number of vertices. For the stack element A, the entry of its neighboring vertices is reduced by 1, and then the operation of the stack is performed with a maximum of M times, M for the graph edge. So the total time complexity will be linear O (n) code example

#include <stdio.h> #include <stdlib.h> struct Node {int value;
    int indegree;
struct Node *next;

};
    Initialize adjacency table struct node* initadjlist (int n) {struct node* headers;
    headers = (struct node*) malloc (sizeof (struct Node) * n);
    int i = 0;
        for (i; i < n; i++) {headers[i].next = NULL;
        Headers[i].value = 0;
    Headers[i].indegree = 0;
return headers;
    } void Addadj (struct node* header, int m, int n) {struct node* p = &header[m];
    p->value++;
    while (P->next!= NULL) p = p->next;
    P->next = (struct node*) malloc (sizeof (struct Node));
    P->next->value = n;
P->next->next = NULL;
    }//Print adjacency table void printadjlist (struct node* header, int n) {int i = 0;
        for (i; i < n; i++) {struct node* p = &header[i];
        printf ("Number of%d ' adj:%d\t", I, P->value); while (p->next!= NULL) {printf ("%d--->%d\t", I, P->next->vaLue);
        p = p->next;
    printf ("\ n");
    }//Topology sort int* topsort (struct node* headers, int n) {int* zerostack = (int*) malloc (sizeof (int) * n);
    int* result = (int*) malloc (sizeof (int) * n);
    int count = 0;
    int pindex =-1;
    int i = 0;
        while (I < n) {struct node* p = &headers[i];
        Into the degree of 0, directly into the stack if (P->indegree = = 0) Zerostack[++pindex] = i;
    i++;
        while (1) {///from top inside Out stack a node Index int zeroindex = zerostack[pindex--];
        result[count++] = Zeroindex;
        struct node* Zeronode = &headers[zeroIndex]; Zeronode The connection point, the value of the corresponding header node minus one while (Zeronode->next!= NULL) {struct node* q = &headers[zeronode-&
            gt;next->value];
            if (--q->indegree = = 0) Zerostack[++pindex] = zeronode->next->value;
        Zeronode = zeronode->next; }//Stack empty if (Pindex < 0) bReak;
return result; int main () {int a[7][7] = {{0,1,1,1,0,0,0}, {0,0,0,0,1,1,0}, {0,0,0,0,0,0
                    , 1}, {0,0,1,0,0,1,1}, {0,0,0,1,0,0,1}, {0,0,0,0,0,0,0},
    {0,0,0,0,0,1,0}};
    int n = 7;
    struct node* headers = initadjlist (n);
    int i = 0;
    int j = 0;  for (i = 0; i < n; i++) for (j = 0; J < N; j +) {if (a[i][j] = = 1) Addadj (headers,
    I, J);
        //Generate each node indegree for (i = 0; i < n; i++) {struct node* p = &headers[i];
            while (P->next!= NULL) {headers[p->next->value].indegree++;
        p = p->next;
    } int* q = topsort (headers, n);
    Printadjlist (headers, n);
    for (i = 0; i < n; i++) {printf ("%d \ n", *q++ + 1);
return 0;
 }

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.