Figure (c description)

Source: Internet
Author: User

First, the concept

A graph is made up of a non-empty finite set V (composed of n>0 vertices) of a vertex and the set E (the relationship between vertices) of an edge. A graph with no direction on the side becomes an image without a map, whereas a directed graph

Graph without direction: direction graph:

Second, the representation of the graph

There are two kinds of storage methods for common graphs: adjacency matrix storage method and Adjacency table storage method

  1. Adjacency Matrix

    The adjacency matrix storage method is also called array storage, the core is to use two arrays to store a graph, a one-dimensional array to hold the data (vertex) in the diagram: Vertex[0,1,2...,n-1] Another two-dimensional array is used to store the relationships (edges) between vertices in the graph, called adjacency matrices: a[ I][J] = 1 (when vertex I with Vertex J has edges) | 0 (Vertex i and Vertex j Infinity)

Example: one of the forward graphs can be expressed as

Array: [0, 1, 2, 3]

Adjacency Matrix:

0 1 0 1

0 0 1 0

0 0 0 1

0 0 0 0

  

Adjacency matrices are not suitable for graphs with few edges, and can be used to store such graphs with adjacency tables.

  2. adjacency table

Adjacency Table Storage method is a combination of sequential allocation and chain allocation of the storage method, consisting of linked list and sequential array, the array is used to store the vertices, the list is used to store the edge, each vertex in the graph to establish a vertex, if a graph has n vertices then there are n linked list, each linked list is set up a head node, becomes vertex node, structure

Where vertex holds the data information for the vertex, next points to the first edge on the vertex vertex. You typically use an array to manage vertex nodes uniformly, and use the subscript of the array to represent the position of the vertices in the diagram.

Each node of a linked list represents an edge, which is called an edge node.

(Edge node structure) Adjvex refers to the other vertex of the edge represented by the edge node (that is, the top node of the list and the vertex form the edge). The weight represents the edge weight (edge length), and if the graph does not have the item omitted, next points to the next edge node (that is, the next edge of the vertex node of the list).

Example: One of the graphs can be expressed as

Array: [0, 1, 2, 3]

adjacency table:

In addition to the above two methods of storage diagram, there are cross-linked table storage method, multiple adjacency table storage method.

Third, C description

 The adjacency table can be described in C language as:

#define Max_vertex_num 20//Define edges (types of nodes in a linked list) typedef struct arcnode{int adjvex;struct arcnode *next;infotype *< C6>weight;} arcnode;//defines the vertex typedef struct vnode{vertextype data; Arcnode *Firstarc;} Vnode G[max_vertex_num];     

How to create a diagram with C:

creategraph (int n, vnode g[]) {  int i, E;  printf ("Input the info of the vertex\n");  for (i=0;i<n;i++) {/    * get data in each vertex *       /GetData (G[i]);        /* Initialize the first edge to NULL * /G[i].firstarc = NULL;} for (i=0;i<n;i++) {printf ("Create the edges for the%dth vertex\n" c14>, i); scanf ("%d",&e); while (E! =-1) {/* Creates an edge */ P = (Arcnode *) malloc (sizeof(Arcnode)); P->next =< c19> NULL; P->adjvex = e; the first edge of the/*i node */if (G[i].fristarc = = NULL) G[i].firstarc = p;/* Next edge */Else Q->next = p; q = p; scanf ("%d", &e);           }}} 

Figure (c description)

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.