A series of algorithms in the figure (to be continued)

Source: Internet
Author: User

# Include <iostream> using namespace std; # define INFINITY INT_MAX # define MAX_VERTEX_NUM 20 typedef enum {DG, DN, UDG, UDN} GraphKind; // adjacent matrix typedef struct ArcCell {int key; // For a non-Permission graph, 1 or 0 indicates the adjacent No. For a weighted graph, the value type is} ArcCell, adjMatrix [MAX_VERTEX_NUM] [MAX_VERTEX_NUM]; // This method of defining arrays must remember typedef struct {int vexs [MAX_VERTEX_NUM]; AdjMatrix arcs; int vexnum, arcnum; // number of current vertices and edges of the graph GraphKind kind;} MGraph; // The adjacent table typedef struct ArcNode {int adjvex; struct ArcNode * nextarc;} ArcNode; typedef struct VNode {int key; ArcNode * firstarc;} VNode, AdjList [MAX_VERTEX_NUM]; typedef struct {AdjList vertices; int vexnum, arcnum; GraphKind kind;} ALGraph; void CreateUD (MGraph & G) // use an undirected graph of the adjacent matrix {int I, j; cout <"Input vexnum: "<endl; cin> G. vexnum; cout <"Input arcnum:" <endl; cin> G. arcnum; for (I = 0; I <G. vexnum; ++ I) {for (j = 0; j <G. vexnum; ++ j ){ G. arcs [I] [j]. key = 0 ;}} int v1, v2; for (int k = 0; k <G. arcnum; ++ k) {cout <"Input two index:" <endl; cin> v1> v2; G. arcs [v1] [v2]. key = 1; G. arcs [v2] [v1]. key = G. arcs [v1] [v2]. key; // difference between a directed Graph and an undirected Graph} cout <"Graph:" <endl; for (I = 0; I <G. vexnum; ++ I) {for (j = 0; j <G. vexnum; ++ j) {cout <G. arcs [I] [j]. key <"" ;}cout <endl ;}void CreateDG (ALGraph & G) // create a directed graph {int I; cout <"Input vexnum: "<endl; cin> G. Vexnum; cout <"Input arcnum:" <endl; cin> G. arcnum; for (I = 0; I <G. vexnum; I ++) {G. vertices [I]. key = I; G. vertices [I]. firstarc = NULL;} int v1, v2; for (int k = 0; k <G. arcnum; ++ k) {cout <"Input two index:" <endl; cin> v1> v2; if (v1 = v2) {-- k; cout <"two index are same, renew input:" <endl; continue;} ArcNode * node = (ArcNode *) malloc (sizeof (ArcNode )); node-> adjvex = v2; node-> nextarc = NULL; if (G. vert Ices [v1]. firstarc = NULL) {G. vertices [v1]. firstarc = node;} else {ArcNode * next = G. vertices [v1]. firstarc; while (next-> nextarc) {next = next-> nextarc;} next-> nextarc = node ;}}for (int j = 0; j <G. vexnum; j ++) {cout <G. vertices [j]. key <":"; ArcNode * next = G. vertices [j]. firstarc; while (next! = NULL) {cout <next-> adjvex <""; next = next-> nextarc ;}cout <endl ;}}

 

Related Article

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.