Directed Acycline graph with a forward-to-loop diagram

Source: Internet
Author: User
Tags assert

Directed Acycline graph with a forward-to-loop diagram

Concept:

The line between the midpoint and the point of the figure has a direction, and there is no ring in the diagram. The graph is implemented using the Adjacency table method.

Noun:

    • The degree of vertex: the number of lines to this vertex.
    • The degree of the vertex: the number of lines departing from this vertex.

Implementation ideas:

1, calculate the degree of each vertex, stored in the auxiliary array CNT

2, find the vertex collection with an entry level of 0.

3, from the vertex set with a degree of 0, take out a vertex, which is the first vertex (not unique).

4, find the vertices with 3 vertices as the starting point, and then reduce the degree of these vertices into one, minus one after the discovery if the degree is 0, update the auxiliary array CNT

5, repeat 2-4

Difficulties:

The function of the auxiliary array CNT:

    • At first, it's the degree at which each vertex is stored
    • After finding the vertex with the degree of 0, the element of the stack is the vertex to be found, and after discovering the vertex with a degree of 0, continue to the stack and then out of the stack ...

The use of auxiliary array cnt, it is recommended to use GDB, multiple debug several times, you can understand.

Figure 1:

Figure 1

Graph_link.h

#ifndef __graph_link__#define __graph_link__#include <stdio.h>#include <malloc.h>#include <assert.h>#include <memory.h>#define default_vertex_size 10#define T char//边的结构typedef struct Edge{  //顶点的下标  int idx;  //指向下一个边的指针  struct Edge* link;}Edge;//顶点的结构typedef struct Vertex{  //顶点的值  T data;  //边  Edge* adj;}Vertex;//图的结构typedef struct GraphLink{  int MaxVertices;  int NumVertices;  int NumEdges;  Vertex* nodeTable;}GraphLink;//初始化图void init_graph_link(GraphLink* g);//显示图void show_graph_link(GraphLink* g);//插入顶点void insert_vertex(GraphLink* g, T v);//插入边尾插void insert_edge_tail(GraphLink* g, T v1, T v2);//插入边头插void insert_edge_head(GraphLink* g, T v1, T v2);//取得指定顶点的第一个后序顶点int get_first_neighbor(GraphLink* g, T v);//取得指定顶点v1的临街顶点v2的第一个后序顶点int get_next_neighbor(GraphLink* g, T v1, T v2);//拓扑排序void topo_sort(GraphLink* g);#endif

graph_link.c

#include "graph_link.h"//initialization diagram void Init_graph_link (graphlink* g) {g->maxvertices = default_vertex_size;  g->numvertices = g->numedges = 0;  G->nodetable = (vertex*) malloc (sizeof (VERTEX) * g->maxvertices);  ASSERT (NULL! = g->nodetable);  for (int i = 0; i < g->maxvertices; ++i) {G->nodetable[i].adj = NULL;  }}//Show figure void Show_graph_link (graphlink* g) {if (NULL = = g) return;    for (int i = 0; i < g->numvertices; ++i) {printf ("%d%c->", I, g->nodetable[i].data);    edge* p = g->nodetable[i].adj;      while (NULL! = p) {printf ("%d->", P-&GT;IDX);    p = p->link;  } printf ("null\n");  }}//insert vertex void Insert_vertex (graphlink* g, T v) {if (g->numvertices >= g->maxvertices) return; G->nodetable[g->numvertices++].data = V;} Find vertex indexint getvertexindex (graphlink* g, T v) {for (int i = 0; i < g->numvertices; ++i) {if (v = = G->nodeta  Ble[i].data) return i; } return-1;} Create Edge void Buyedge (edge** e, int idx) {edge* P= (edge*) malloc (sizeof (Edge));  P->idx = idx;  P->link = NULL;  if (NULL = = *e) {*e = P;    } else{edge* tmp = *e;    while (tmp->link! = NULL) {tmp = tmp->link;  } Tmp->link = P;  }}//Insert Edge (tail plug) void Insert_edge_tail (graphlink* g, t v1, t v2) {int p1 = Getvertexindex (g, V1);  int P2 = Getvertexindex (g, V2);    if (P1 = =-1 | | p2 = =-1) return;  Buyedge (& (G->nodetable[p1].adj), p2);  g->numedges++;  Buyedge (& (G->nodetable[p2].adj), p1); g->numedges++;}  Insert Edge (head plug) void Insert_edge_head (graphlink* g, t v1, t v2) {int p1 = Getvertexindex (g, V1);  int P2 = Getvertexindex (g, V2);  if (P1 = =-1 | | p2 = =-1) return;  edge* p = (edge*) malloc (sizeof (Edge));  P->idx = p2;  P->link = g->nodetable[p1].adj;  G->nodetable[p1].adj = p;  /* p = (edge*) malloc (sizeof (Edge));  P->idx = p1;  P->link = g->nodetable[p2].adj;    G->nodetable[p2].adj = p; */}//gets the first post-post vertex of the specified vertex int get_first_neighbor (graphlink* g, T v) {int i = Getvertexindex (g, v);  if ( -1 = = i) return-1;  edge* p = g->nodetable[i].adj;  if (NULL! = p) return p->idx; else return-1;}  Gets the first post-v2 vertex of the frontage vertex of the specified vertex v1 int Get_next_neighbor (graphlink* g, t ve1, t ve2) {int v1 = Getvertexindex (g, ve1);  int v2 = Getvertexindex (g, ve2);  if (v1 = =-1 | | v2 = =-1) return-1;  edge* T = g->nodetable[v1].adj;  while (t! = NULL && T->idx! = v2) {t = t->link;  } if (null! = t && t->link! = NULL) {return t->link->idx; } return-1;}  Topological sort void topo_sort (graphlink* g) {int n = g->numvertices;  Represents the degrees of each vertex, first initialized to 0 int* cnt = (int*) malloc (sizeof (int) * n);  ASSERT (NULL! = CNT);  for (int i = 0; i < n; ++i) {cnt[i] = 0;  } edge* p;    Calculates the entry for each vertex for (int i = 0; i < n; ++i) {p = g->nodetable[i].adj;      while (P! = NULL) {cnt[p->idx]++;    p = p->link;  }} int top =-1;    for (int i = 0; i < n; ++i) {if (cnt[i] = = 0) {///0 vertex into stack (simulated into stack) cnt[i] = top;//push top = i; }} int V,W         for (int i = 0; i < n; ++i) {if (top = =-1) return;//has a loop exists v = top;    Analog out stack top = cnt[top];    printf ("%c->", g->nodetable[v].data);    W = Get_first_neighbor (g, g->nodetable[v].data);    while ( -1! = W) {if (--cnt[w] = = 0) {//into 0 vertices into the stack (simulated into the stack) cnt[w] = top;      top = W;    } w = Get_next_neighbor (g,g->nodetable[v].data,g->nodetable[w].data); }} free (CNT);}

Graph_linkmain.c

#include "graph_link.h"int main(){  GraphLink gl;  //初始化图  init_graph_link(&gl);  //插入节点  insert_vertex(&gl, ‘A‘);  insert_vertex(&gl, ‘B‘);  insert_vertex(&gl, ‘C‘);  insert_vertex(&gl, ‘D‘);  insert_vertex(&gl, ‘E‘);  insert_vertex(&gl, ‘F‘);  //插入边(头插)  insert_edge_head(&gl, ‘A‘, ‘B‘);  insert_edge_head(&gl, ‘A‘, ‘C‘);  insert_edge_head(&gl, ‘A‘, ‘D‘);  insert_edge_head(&gl, ‘C‘, ‘B‘);  insert_edge_head(&gl, ‘C‘, ‘E‘);  insert_edge_head(&gl, ‘D‘, ‘E‘);  insert_edge_head(&gl, ‘F‘, ‘D‘);  insert_edge_head(&gl, ‘F‘, ‘E‘);  //显示图  show_graph_link(&gl);  //拓扑排序  topo_sort(&gl);  printf("\n");}

Full code

Compilation method: Gcc-g graph_link.c graph_linkmain.c

Directed Acycline graph with a forward-to-loop diagram

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.