Representation of a directed graph's joined table

Source: Internet
Author: User

The adjacent table representation of the graph is similar to the Child linked list representation of the tree. For each vertex VI in graph G, this method chain all vertices VJ adjacent to VI into a single-chain table with a leading node, this single-chain table is called the adjacency list of vertex VI ).

1. node Structure of the adjacent table


(1) Table node Structure
┌ ── ┬ ── ┐
│ Adjvex │ next │
└ ── ┴ ── ┘
Each table node in the adjacent table has two fields:
① Adjvex
Stores the number J of the vertex VJ adjacent to VI.
② Chain Domain next
Link all the table nodes in the adjacent table.
Note:
To indicate the information on the edge (such as the weight), add a data field to the table node.

(2) header node Structure
┌ ── ─ ┬ ── ─ ┐
│ Vertex │ firstedge │
└ ── ─ ┴ ── ─ ┘
The header node of the vertex VI adjacent table contains two fields:
① Vertex
Store vertex VI Information
② Pointer domain firstedge
The header pointer of the VI adjacent table.
Note:
① In order to facilitate Random Access to the adjacent table of any vertex, all the header nodes are stored in a vector to form the graph's adjacent table representation.
② Sometimes you want to add descriptions of attributes such as the number of vertices and number of edges to the graph. You can combine the adjacent tables with these attributes to describe the storage structure of the graph.

2. Code Instance

# Include <iostream> <br/> using namespace STD; <br/> # define max_vertex_num 50 // defines the maximum number of vertices of a graph <br/> typedef char vertexdata; <br/> typedef struct edgenode // table node <br/> {<br/> int adjvex; // adjacent point field <br/> vertexdata data; <br/> edgenode * Next; // The next edge node corresponding to the edge node <br/>} edgenode; <br/> typedef struct vertexnode // header node <br/> {<br/> vertexdata data; <br/> edgenode * firstedge; // The first edge node corresponding to the header node <br/>} vertexnode; <br/> typedef struct Djlist <br/>{< br/> int vexnum, arcnum; // defines the number of vertices and edges of the graph. <br/> vertexnode vertex [max_vertex_num]; // define the header node array. <Br/>} adjlist; <br/> void creategraph (adjlist * adj, int * n) <br/>{< br/> int e, S, D; <br/> cout <"Number of input vertices and edges" <Endl; <br/> CIN> * n> E; // enter the number of vertices and edges. <Br/> adj-> vexnum = * n; <br/> adj-> arcnum = E; <br/> edgenode * q = NULL; <br/> // initialize the header node <br/> int I; <br/> for (I = 1; I <= * n; I ++) <br/> {<br/> cout <"Enter the vertex name of the vertex" <I <"Node" <Endl; <br/> CIN> adj-> vertex [I]. data; // vertex name, which is a character <br/> adj-> vertex [I]. firstedge = NULL; <br/>}< br/> for (I = 1; I <= E; I ++) <br/> {<br/> cout <"Enter the" <I <"start and end of the edge" <Endl; <br/> CIN> S> D; // start and end of the input edge <br/> // cout <"input table node information" <Endl; <br/> q = (edgenode *) mallo C (sizeof (edgenode); // create a table node <br/> If (q = NULL) <br/> return; <br/> q-> adjvex = D; <br/> // CIN> q-> data; <br/> q-> next = adj-> vertex [s]. firstedge; // the newly added nodes are after the header node, and the original nodes after the header node need to be moved back. <Br/> adj-> vertex [s]. firstedge = Q; <br/>}< br/> void displaygraph (adjlist * adj) <br/> {<br/> int n = adj-> vexnum; // Number of vertices. Each vertex is traversed later. <br/> edgenode * q = NULL; <br/> int I; <br/> for (I = 1; I <= N; I ++) <br/> {<br/> // cout <n <Endl; <br/> q = adj-> vertex [I]. firstedge; <br/> If (q = NULL) // It indicates that the header node is not followed by other nodes <br/>{< br/> cout <"useless slave" <adj-> vertex [I]. data <"Departure node" <Endl; <br/>}< br/> else <br/> {<br/> cout <"Slave node" <adj-> Vertex [I]. Data <"the starting edge is" <Endl; <br/> while (Q! = NULL) <br/>{< br/> // cout <adj-> vertex [I]. data <"->" <q-> data <Endl; <br/> cout <adj-> vertex [I]. data <"->" <adj-> vertex [q-> adjvex]. data <Endl; <br/> q = Q-> next; <br/>}< br/> void main () <br/>{< br/> int N; <br/> adjlist * adj = (adjlist *) malloc (sizeof (adjlist); <br/> creategraph (adj, & N ); <br/> displaygraph (adj); <br/> // cout <"Hello world! "<Endl; <br/>}

Output result:

Enter the number of vertices and edges <br/> 6 6 <br/> enter the name of the vertex of the third node <br/> A <br/> enter the name of the vertex of the third node <br/> br/> B <br/> enter the vertex name of the 3rd nodes <br/> C <br/> enter the vertex name of the 4th nodes <br/> d <br/> enter the vertex name of 5th knots <br/> E <br/> enter the vertex name of 6th knots <br/> F <br/> enter the start and end points of 1st edges. <br/> 1 3 <br/> enter the start and end points of the 2nd edge <br/> 2 4 <br/> enter the start and end points of the 3rd edge <br/> 2 1 <br/> enter the start and end points of 4th edges <br/> 4 3 <br/> enter the start and end points of 5th edges <br/> 3 6 <br /> enter the start and end points of the first 6th edges. <br/> 3 5 <br/> the edges starting from node A have <br/> A-> C <br/> the edges starting from Node B are <br/> B-> A <br/> B-> d <br/> edges starting from node C are <br/> C-> E <br/> C-> F <br/> the edges starting from node D have <br/> D-> C <br/> no node departing from node e <br /> the node from F is useless. 

3. Code Example 2 (PS: supplemented)

In general, the adjacent table representation method mainly contains two types of nodes: the header node and the table node (also called the edge node), from the header node (s) to the table node (d) there is an edge between them. If the header node is followed by multiple table nodes, namely, S-> d1-> D2, two edges exist, E (S, D1) and E (S, d2 ). In the neighbor table representation method, the number of header nodes is fixed, that is, the number of vertices in the graph v. The number of table nodes is determined by the number of edges. For a directed graph, the number of table nodes = the number of edges; for an undirected graph, the number of table nodes = the number of edges * 2.

When constructing a graph, if a header node is followed by multiple table nodes, the table nodes are added to the header node in order. For example, if the structure S-> d1-> D2 is already in place and the table node D3 needs to be added, the pointer S-> d1 needs to be broken and D3 points to D1 and s points to D3. That is, S-> D3-> d1-> D2.

# Include <iostream> <br/> # include <stdlib. h> <br/> using namespace STD; <br/> # define max_vertex_num 50 // defines the maximum number of vertices of a graph <br/> typedef char vertexdata; // The vertex name is vertex type. <Br/> typedef struct edgenode // table node <br/> {<br/> int adjvex; // adjacent point field <br/> vertexdata data; <br/> edgenode * Next; // the next table node corresponding to the table node <br/>} edgenode; <br/> typedef struct vertexnode // header node <br/> {<br/> vertexdata data; <br/> edgenode * firstedge; // The first table node corresponding to the header node <br/>} vertexnode; <br/> typedef struct adjlist // data structure of the graph <br/>{< br/> int vexnum, arcnum; // define the number of vertices and edges of the graph. <br/> vertexnode vertex [max_vertex_num]; // define the array of header nodes. <Br/>} adjlist; <br/> void creategraph (adjlist * adj) <br/>{< br/> int S, D; <br/> int I; <br/> cout <"Number of input vertices and edges" <Endl; <br/> CIN> adj-> vexnum> adj-> arcnum; // enter the number of vertices and edges of the graph. <Br/> edgenode * q = NULL; // define the table node <br/> // initialize the table header node <br/> cout <"input" <adj-> vexnum <"header node name" <endl; <br/> for (I = 1; I <= adj-> vexnum; I ++) <br/> {<br/> // adj-> vertex [I] is a header node array <br/> CIN> adj-> vertex [I]. data; // vertex name, which is a character <br/> adj-> vertex [I]. firstedge = NULL; // the header node is not followed by the table node in the initial state. Therefore, firstedge = NULL <br/>}< br/> // After the header node is initialized, you need to add the table node to the end of the header node. <Br/> cout <"input" <adj-> arcnum <"start and end of an edge" <Endl; <br/> for (I = 1; I <= adj-> arcnum; I ++) <br/>{< br/> CIN> S> D; // start and end of the input edge, start S is the head node location, end D is the table node location <br/> q = (edgenode *) malloc (sizeof (edgenode); // create a table node, allocate space for it <br/> If (q = NULL) <br/> return; <br/>/* <br/> If the original linked list is s-> A-> B-c>, add a table node Q, after adding this parameter, it becomes S-> q-> A-> B-> C <br/> SO: <br/> 1. Q points to the elements pointed to by the current S. <Br/> 2. Q is adjacent to d <br/> 3. the S Pointer Points to Q. <br/> the operation is as follows: <br/> */<br/> q-> adjvex = D; // The adjacent vertex of the table node is d <br/> q-> next = adj-> vertex [s]. firstedge; // the newly added nodes are after the header node, and the original nodes after the header node need to be moved back. <Br/> adj-> vertex [s]. firstedge = Q; <br/>}< br/> void displaygraph (adjlist * adj) <br/> {<br/> int n = adj-> vexnum; // Number of vertices. Each vertex is traversed later. <br/> edgenode * q = NULL; <br/> int I; <br/> for (I = 1; I <= adj-> vexnum; I ++) <br/> {<br/> // cout <n <Endl; <br/> q = adj-> vertex [I]. firstedge; // Q indicates the table node pointed to by the header node I. I-> q has an edge. <br/> If (q = NULL) // It indicates that the header node is not followed by other nodes <br/>{< br/> cout <"useless slave" <adj-> vertex [I]. data <"Departure node" <Endl; <br/>}< br/> else <B R/>{< br/> cout <"Slave node" <adj-> vertex [I]. data <"Starting edge" <Endl; <br/> while (Q! = NULL) <br/>{< br/> // cout <adj-> vertex [I]. data <"->" <q-> data <Endl; <br/> cout <adj-> vertex [I]. data <"->" <adj-> vertex [q-> adjvex]. data <Endl; <br/> q = Q-> next; // jump back to the linked list <br/>}< br/> void main () <br/>{< br/> int N; <br/> adjlist * adj = (adjlist *) malloc (sizeof (adjlist )); <br/> creategraph (adj); <br/> displaygraph (adj); <br/> system ("pause "); <br/>}< br/>/* <br/> enter the number of vertices and edges. <br/> 6 <br/> enter the name of the six-headed node. <br/> a B C D E F <br/> enter the start and end points of the six edges <br/> 1 3 <br/> 2 4 <br/> 2 1 <br/> 4 3 <br/> 3 6 <br/> 3 5 <br/> the edges starting from node A have <br/> A-> C <br/> slave Node the edges starting from B are <br/> B-> A <br/> B-> d <br/> edges starting from node C are <br/> C-> E <br/> C-> F <br/> the edges starting from node D have <br/> D-> C <br/> no node departing from node e is used <br/> no node from F is used <br/> press any key to continue... <br/> */

 

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.