C ++ and the display configuration of FIG.

FIG vertices there is a collection of non-empty and finite edge between vertices, usually expressed as: G (V, E), wherein, G represents a graph, V is the set of vertices of G, E is G in FIG. in the set of edges.
FIG sub undirected graph with a directed graph according to FIG side length of divided and non-weighted graph weighted graph.

Adjacency matrix structure and showing:

#include<stdio.h>
int main()
{
 const int MAX_N = 5;
 int Graph[MAX_N][MAX_N] = { 0 };
 Graph[0][2] = 1;
 Graph[0][4] = 1;
 Graph[1][0] = 1;
 Graph[1][2] = 1;
 Graph[2][3] = 1;
 Graph[3][4] = 1;
 Graph[4][3] = 1;
 printf("Graph:\n");
 for (int i = 0; i < MAX_N; i++)
 {
  for (int j = 0; j < MAX_N; j++)
  {
   printf("%d  ", Graph[i][j]);
  }
  printf("\n");
 }
 return 0;
}

Operating results as follows:

Graph:
0  0  1  0  1
1  0  1  0  0
0  0  0  1  0
0  0  0  0  1
0  0  0  1  0

Adjacency table showing the structure:

#include <stdio.h>
#include<vector>
struct GraphNode
{
 int label;
 std::vector<GraphNode*> neighbors;
 GraphNode(int x) : label(x) {};
};
int main()
{
 const int MAX_N = 5;
 GraphNode* Graph[5] ;
 for (int i = 0; i < MAX_N; i++)
 {
  Graph[i] = new GraphNode(i);
 }
 Graph[0]->neighbors.push_back(Graph[2]);
 Graph[0]->neighbors.push_back(Graph[4]);
 Graph[1]->neighbors.push_back(Graph[0]);
 Graph[1]->neighbors.push_back(Graph[2]);
 Graph[2]->neighbors.push_back(Graph[3]);
 Graph[3]->neighbors.push_back(Graph[4]);
 Graph[4]->neighbors.push_back(Graph[3]);
 printf("Graph:\n");
 for (int i = 0; i < MAX_N; i++)
 {
  printf("Label(%d):",i);
  for (int j = 0; j <Graph[i]->neighbors.size(); j++)
  {
   printf("[%d]",Graph[i]->neighbors[j]->label);
  }
  printf("\n");
 }
 for (int i = 0; i < MAX_N; i++)
 {
  delete Graph[i];
 }
 return 0;
}

Operating results as follows:

Graph:
Label(0):[2][4]
Label(1):[0][2]
Label(2):[3]
Label(3):[4]
Label(4):[3]
Published 61 original articles · won praise 46 · views 1572

Guess you like

Origin blog.csdn.net/weixin_44208324/article/details/104906539