Look at the data structure write code (35) adjacency matrix representation of graphs

Source: Internet
Author: User

Talk: Recently Ching Ming small holiday, a good relaxed a bit. Both before and after the festival are a bit lax. Not good, bad. You are insisting. Come on.

The adjacency matrix representation of graphs is to represent the data structure of a graph with two arrays. One is a vertex array and the other is an array of adjacency matrices. The relationship that holds vertices in the adjacency matrix.

It is simpler to use the adjacency matrix to represent the graph, to see if there are edges between vertices, or to calculate the degree of vertex. However, space is wasted, and it is inconvenient to move large amounts of data when inserting, deleting vertex and edge operations. So in the Insert delete more, node points more than the time should not use this structure.

The following code:

Source code Web address: Click to open the link

MGraph2.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <climits> #include <cstring> #define INFINITY int_max#define Max_vertex_num 20enum E_state{e_state_error = 0,E_STATE_OK = 1,};enum E_GRAPH_KIND{DG = 0,//There is a direction diagram dn,//a forward mesh udg,//no direction net udn,// Edge (ARC) unit typedef struct ARCCELL{INT adj;//represents the relationship type of a vertex, for an unauthorized graph, 0,1 represents whether adjacent, for the right graph, the value type char * info;//edge, Arc the rest of the information.} adjmatrix[max_vertex_num][max_vertex_num];//definition diagram struct Mgraph{char vexs[max_vertex_num];//vertex set AdjMatrix arcs;// adjacency Matrix int vexnum,arcnum of graphs; E_graph_kind Kind;}; int graphlocation (mgraph graph,char vex), void Createdg (mgraph * graph), void Createdn (mgraph * graph), void Createudg (Mgra ph * graph), void Createudn (mgraph * graph), void graphcreate (Mgraph * graph) {e_graph_kind kind;printf ("Enter the type of diagram to be created (with a direction graph: 0 , with a mesh: 1, no direction graph: 2, no net: 3) \ n "); scanf ("%d ", &kind); switch (kind) {case dg:createdg (graph); break;case Dn:createdn (graph) ; break;case UDG:CREATEUDG (graph); break;case udn:createudn (graph); break;default:break;}} Returns the first adjacency point of a vertex vex char Firstadjvex (mgraph graph,chAR vex) {int location = graphlocation (graph,vex); int i = 0;for (; i < graph.vexnum; i++) {if (Graph.kind = DG | | graph. Kind = = UDG) && Graph.arcs[location][i].adj = = 1) {return graph.vexs[i];} else if ((Graph.kind = = DN | | graph.kind = = UDN) && Graph.arcs[location][i].adj! = INFINITY) {return graph.vexs[i];} }return ';} Returns the next adjacency point of the vertex vex1 relative to Vex2. Char Nextadjvex (mgraph graph,char Vex1,char vex2) {int location1 = graphlocation (GRAPH,VEX1); int location2 = graphlocation (graph,vex2); int i = location2+1;for (; i < graph.vexnum; i++) {if (Graph.kind = DG | | gr Aph.kind = = UDG) && Graph.arcs[location1][i].adj = = 1) {return graph.vexs[i];} else if ((Graph.kind = = DN | | graph.kind = = UDN) && Graph.arcs[location1][i].adj! = INFINITY) {return graph.vexs[i]; }}return ';} Find vertex position int graphlocation (mgraph Graph,char vex) {for (int i = 0; i < Graph.vexnum; i++) {if (graph.vexs[i] = = vex) {RET Urn i;}} return-1;} Create a graph Sub function//graph void Createdg (Mgraph * graph) {Graph->kinD = dg;printf ("Please enter number of vertices, edge (arc) \ n"); scanf ("%d%d%*c", &graph->vexnum,&graph->arcnum);//Initialize adjacency matrix for (int i = 0 ; i < Max_vertex_num; i++) {for (int j = 0; J < Max_vertex_num; J + +) {Graph->arcs[i][j].adj = 0;graph->arcs[i][j].info = NULL;}} Construct the vertex set printf ("Enter the vertex set \ n"), for (int i = 0; i < graph->vexnum; i++) {scanf ("%c", &graph->vexs[i]);} Constructs a vertex relationship fflush (stdin);p rintf ("Please enter the relationship of vertices \ n"), for (int i = 0; i < graph->arcnum; i++) {char vex1,vex2;scanf ("%c%c%*c" , &vex1,&vex2); int location1 = Graphlocation (*graph,vex1); int location2 = Graphlocation (*GRAPH,VEX2); graph- >arcs[location1][location2].adj = 1;}} There is a net void Createdn (Mgraph * graph) {graph->kind = dn;printf ("Please enter the number of vertices, edge (arc) \ n"); scanf ("%d%d%*c",&graph-> Vexnum,&graph->arcnum);//Initialize adjacency matrix for (int i = 0; i < Max_vertex_num; i++) {for (int j = 0; j < Max_vertex_num; J + +) {Graph->arcs[i][j].adj = Infinity;graph->arcs[i][j].info = NULL;}} Constructs the vertex set printf ("Enter the vertex set \ n"); for (int i = 0; i < graph->vexNum; i++) {scanf ("%c", &graph->vexs[i]);} Constructs a vertex relationship fflush (stdin);p rintf ("Enter the relationship of vertices \ n"); for (int i = 0; i < graph->arcnum; i++) {char vex1,vex2;int weight;scanf ("%c%c%d%*c", &vex1,&vex2,&weight); int location1 = Graphlocation (*graph,vex1); int location2 = Graphlocation (*GRAPH,VEX2); Graph->arcs[location1][location2].adj = Weight;}} graph void Createudg (Mgraph * graph) {graph->kind = udg;printf ("Please enter the number of vertices, edge (arc) \ n"); scanf ("%d%d%*c",&graph-> Vexnum,&graph->arcnum);//Initialize adjacency matrix for (int i = 0; i < Max_vertex_num; i++) {for (int j = 0; j < Max_vertex_num; J + +) {Graph->arcs[i][j].adj = 0;graph->arcs[i][j].info = NULL;}} Construct the vertex set printf ("Enter the vertex set \ n"), for (int i = 0; i < graph->vexnum; i++) {scanf ("%c", &graph->vexs[i]);} Fflush (stdin);//construct vertex relationship printf ("Enter vertex relationship \ n"); for (int i = 0; i < graph->arcnum; i++) {char vex1,vex2;scanf ("%c%c%*c" , &vex1,&vex2); int location1 = Graphlocation (*graph,vex1); int location2 = Graphlocation (*GRAPH,VEX2); grapH->arcs[location1][location2].adj = Graph->arcs[location2][location1].adj = 1;}} Non-net void Createudn (Mgraph * graph) {graph->kind = udn;printf ("Enter number of vertices, edge (arc) \ n"); scanf ("%d%d%*c",&graph-> Vexnum,&graph->arcnum);//Initialize adjacency matrix for (int i = 0; i < Max_vertex_num; i++) {for (int j = 0; j < Max_vertex_num; J + +) {Graph->arcs[i][j].adj = Infinity;graph->arcs[i][j].info = NULL;}} Construct the vertex set printf ("Enter the vertex set \ n"), for (int i = 0; i < graph->vexnum; i++) {scanf ("%c", &graph->vexs[i]);} Constructs a vertex relationship fflush (stdin);p rintf ("Enter the relationship of vertices \ n"); for (int i = 0; i < graph->arcnum; i++) {char vex1,vex2;int weight;scanf ("%c%c%d%*c", &vex1,&vex2,&weight); int location1 = Graphlocation (*graph,vex1); int location2 = Graphlocation (*GRAPH,VEX2); Graph->arcs[location1][location2].adj =graph->arcs[location2][location1].adj = Weight;}}  View whether the vertex data is adjacent to bool Graphisadj (mgraph Graph,char Vex1,char vex2) {e_graph_kind Kind = graph.kind;int Weight = (Kind = = DG | | Kind = = UDG)? 0:infinitY;int location1 = graphlocation (graph,vex1); int location2 = Graphlocation (GRAPH,VEX2); return graph.arcs[location1][ Location2].adj! = weight? True:false;} int Graphdegree (mgraph Graph,char vex) {int location = graphlocation (Graph,vex); E_graph_kind Kind = graph.kind;int Weight = (Kind = DG | | Kind = UDG)? 0:infinity;int degree = 0;for (int i = 0; i < Graph.vexnum; i++) {//Calculated line if (Graph.arcs[location][i].adj! = weight) {Degr ee++;}} for (int i = 0; i < Graph.vexnum; i++) {//Computed column if (Graph.arcs[i][location].adj! = weight) {degree++;}} if (kind = = UDG | | kind = = UDN) {degree/= 2;} return degree;} The adjacency matrix is not suitable for handling when the following actions are in the way. void Insertvex (Mgraph * Graph,char vex) {graph->vexs[graph->vexnum++] = Vex;} Many elements need to be moved. void Deletevex (Mgraph * Graph,char vex) {int location = graphlocation (*graph,vex);//delete vertex set for (int i = location+ 1; I < graph->vexnum; i++) {Graph->vexs[i-1] = graph->vexs[i];} Calculates the number of deleted edges (arcs) graph->arcnum-= Graphdegree (*graph,vex);//delete Edge (ARC)//vex The Move up below for (int i = location+1; I < graph->vexnum; i++) {for (int j = 0; J < graph->vexnum; J + +) {Graph->arcs[i-1][j] = Graph->arcs[i][j];}} Vex right left shift for (int i = location + 1; i < graph->vexnum; i++) {for (int j = 0; J < graph->vexnum; J + +) {Graph-&gt ; Arcs[j][i-1] = Graph->arcs[j][i];}} Clean up junk data (line Vexnum and vexnum) int maxvexnum = graph->vexnum; E_graph_kind Kind = graph->kind;int Weight = (Kind = DG | | Kind = UDG)? 0:infinity;for (int i = 0; i < Maxvexnum; i++) {Graph->arcs[maxvexnum-1][i].adj = Weight;graph->arcs[i][maxvexnu M-1].adj = weight;} graph->vexnum--;} Insert Edge (arc) void Insertarc (Mgraph * Graph,char vex1,char vex2,int weight) {int location1 = graphlocation (*graph,vex1); int Loc Ation2 = Graphlocation (*GRAPH,VEX2); E_graph_kind Kind = graph->kind;if (Kind = DG | | Kind = UDG) {Graph->arcs[location1][location2].adj = 1;} Else{graph->arcs[location1][location2].adj = weight;} if (kind = = UDG | | kind = = UDN) {Graph->arcs[location2][location1].adj = graph->Arcs[location1][location2].adj;}} Delete Edge (arc) void Deletearc (Mgraph * Graph,char Vex1,char vex2) {int location1 = graphlocation (*graph,vex1); int location2 = GR Aphlocation (*GRAPH,VEX2); E_graph_kind Kind = graph->kind;if (Kind = DG | | Kind = UDG) {Graph->arcs[location1][location2].adj = 0;} Else{graph->arcs[location1][location2].adj = INFINITY;} if (kind = = UDG | | kind = = UDN) {Graph->arcs[location2][location1].adj = Graph->arcs[location1][location2].adj;}} void Printadjmatrix (mgraph graph) {for (int i = 0, i < graph.vexnum; i++) {for (int j = 0; J < Graph.vexnum; J + +) {Prin TF ("%d\t", Graph.arcs[i][j]);} printf ("\ n");}} int _tmain (int argc, _tchar* argv[]) {mgraph graph;graphcreate (&graph);p Rintadjmatrix (graph);p rintf ("The graph has%d vertices,% D-Edge (ARC) \ n ", graph.vexnum,graph.arcnum); char * Isadj = Graphisadj (graph, ' a ', ' d ')? "Adjacent": "nonadjacent"; int degree = graphdegree (graph, ' C ');p rintf (The degrees of "A and D%s,c are:%d\n", isadj,degree); char Vexfirst = Firstadjvex ( Graph, ' C '); char vexnext = Nextadjvex (graph, ' C ', ' a ');p RINTF ("C's first adjacency point is%c\nc relative to A's next adjacency point is%c\n", vexfirst,vexnext); Insertvex (&graph, ' e ');p rintf ("After inserting node E: \ n"); Printadjmatrix (graph);p rintf ("Insert Edge (e,d): \ n"), Insertarc (&graph, ' e ', ' d ', 1);p Rintadjmatrix (graph);p rintf (" After deleting vertex c: \ n ");d Eletevex (&graph, ' C ');p Rintadjmatrix (graph);d Eletearc (&graph, ' A ', ' B ');p rintf (after deleting an arc (A, B): \ n ");p Rintadjmatrix (graph); return 0;}
Run:



Look at the data structure write code (35) adjacency matrix representation of graphs

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.