Data structure (5) - Figure

Data structure (5)-Figure


= LUOFANG SHIJIE + online partner opinion + personal opinion

Invasion!


"Bear the brunt"

In a directed graph, the vertices useAngle brackets, In an undirected graph, the vertices useParentheses

For example, <x,y> and <y,x> are not the same, but (x,y) and (y,x) are the same.

In the graph, generally use n to represent the number of vertices, and e to represent the number of edges


Undirected complete graph and directed complete graph:

Undirected complete graph : the number of edges is n(n-1)/2

Directed complete graph : the number of edges is n(n-1)

Net : Weighted graphs are often called nets

In-degree and out-degree : for directed graphs. In a directed graph, 1/2 of the sum of in-degree and out-degree of all vertices is the number of edges in the graph

Path length : the number of edges on a path

Connected, connected graph, connected component : there is a path-connected in the graph, any vertex has a path-connected graph

Maximal connected subgraphs in undirected graphs- connected components


Strongly connected graphs and strongly connected components : relative to directed graphs.

Spanning tree of connected graph : a minimal connected subgraph, which contains all the vertices in the graph, but only has n-1 edges that are enough to form a tree.


If a graph has n vertices and less than n-1 edges, it must be a disconnected graph; if it has more than n-1 edges, it must have a ring.


Directed tree and spawning forest : a directed graph with only one point having an in-degree of 0, and other points having an in-degree of 1 is called a directed tree

Spawning forest : consists of several directed trees, and they do not intersect each other


Insert picture description here


Graph storage structure

Adjacency matrix notation

If the picture is a net, it can be expressed like this:

#define MaxInt 32767
#define MVNum 100
typedef char VerTexType;	//假设顶点的类型是char型
typedef int ArcType;		//假设边的权值类型是int
typedef struct
{
	VerTexType vexs[MVNum];
	ArcTexType arcs[MVNum][MVNum];
	int arcnum,vexnum;
}AMGraph;

Use the adjacency matrix method to create an undirected network: time complexity 0(n 2 )

Status CreatUND(AMGraph &G)
{
	cin>>G.arcnum>>G.vexnum;
	for(int i=0;i<vexnum;i++)  //依次输入顶点信息
		cin>>G.vexs[i];
	for(int i=0;i<vexnum;i++)
	{
		for(int j=0;j<vexnum;j++)
		{
			G.arcs[i][j]=MaxInt;
		}
	}
	for(int k=0;k<G.arcnum;k++)
	{
		cin>>v1>>v2>>w;  //输入一条边依附的顶点和权值
  		i=LocateVex(G,v1);
  		j=LocateVex(G,v2);
  		G.arcs[i][j]=w;
  		G.arcs[i][j]=G.arcs[j][i];	//无向网
	}
	return OK;
}

Insert picture description here


For graphs with fewer edges than vertices, this structure is a huge waste of storage space. Therefore, we consider another storage structure: adjacency list (Adjacency List), which is a storage method that combines arrays and linked lists.


Adjacency list notation


Insert picture description here


The textbook understanding is a bit confused


new:

  1. The vertices in the graph are stored in a one-dimensional array. In addition, for each data element in the vertex array, a pointer to the first adjacent point needs to be stored in order to find the edge information of the vertex.

  2. All the adjacent points of each vertex vi in ​​the graph form a linear table. Since the number of adjacent points is uncertain, it is stored in a singly linked list. The undirected graph is called the edge table of vertex vi, and the directed graph is called vertex vi as the arc tail. The outgoing table.


The adjacency list structure of the undirected graph.

Insert picture description here


If it is a directed graph, the structure of the adjacency list is similar

As shown in the figure, using the vertex as the arc tail to store the edge table is easy to get the out degree of each vertex, and the table with the vertex as the arc head is easy to get the in degree of the vertex, namely the inverse adjacency table.

Insert picture description here


For network graphs with weights, you can add a weight data field in the edge table node definition, and store the weight information.

Insert picture description here


Insert picture description here


Graph traversal

Two traversal methods:

Depth first search and breadth first search

Depth first search

Insert picture description here


Breadth first search is similar to tree traversal

Insert picture description here

Breadth-first algorithm requires queues

Algorithm analysis: Breadth-first algorithm and depth-first algorithm have the same time complexity, and they both depend on the complexity of finding neighboring nodes


Graph application

Minimum spanning tree

Prim's Algorithm

Insert picture description here
Insert picture description here


Kruskal Algorithm

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Touale/article/details/112686341