Learning data structure-Chapter 5: Graphs (basic operations of graphs)

Chapter 5: Graphs (basic operations of graphs)

1.Adjacent (G, x, y)

Adjacent(G,x,y)Determine whether the graph G has an edge <x,y>or(x,y)

As undirected graph representation of the adjacency matrix and adjacency list determination method are:

  • Adjacency matrix: Judging whether the edge exists or not, directly judging the value in the corresponding adjacency matrix, if it is 1, it does not exist otherwise
  • Adjacency list: Determine whether there is an edge table node of such an edge in the edge table corresponding to the vertex

Because the adjacency matrix and the adjacency list of the undirected graph will be stored twice, no matter whether the first node x is used as the row number, or y is used as the row number, it can be searched.

As directed graph adjacency matrix and the adjacency list method, determination method are:

  • Adjacency matrix: Judging whether the edge exists or not, directly judging A[x][y]the value in the corresponding adjacency matrix, if it is 1, it does not exist otherwise
  • Adjacency list: determine whether there is a table node of such an edge in the edge table of the vertex corresponding to the first endpoint x

Because it is a directed graph, only one is stored for each adjacency matrix and adjacency list, so the first x of the incoming parameter is the subscript of the starting point of the edge

2.Neighbors(G,x)

Neighbors(G,x)List the edges adjacent to the node x (vertex) in the graph G

As undirected graph lists corresponding to the nodes adjacent edge

  • Adjacency matrix: direct search adjacency matrix of the vertex 行或列value of 1, the value 1 represents the presence of one side
  • Adjacency list: Traverse the edge list for vertices, all edge list nodes represent an adjacent edge

As digraph lists corresponding to the nodes adjacent edge

  • Adjacency matrix: We need to traverse the vertex corresponding to 行和列the value 1, the value 1 represents the presence of one side (side row represents the columns represent edges)
  • Adjacency table: first traverse the edge table of the corresponding vertex, find all the edges with the vertex as the head node (out edge), and then traverse the entire adjacency table to find the edge with the vertex as the end node (in edge)

3.InsertVertex(G,x)

InsertVertex(G,x) Insert vertex x in graph G

In fact, about inserting a directed graph with vertices is similar to an undirected graph, here we want to insert a vertex F

Undirected graph : First, we need to insert vertex F into the vertex table. The vertex table is an array that cannot be expanded. So we need to create a new array to insert the previous vertices, then add vertex F, and then we need to modify the adjacency matrix. We still need to expand the two-dimensional array (also create a two-dimensional array to add the old and new values). Here is just inserting a vertex, the connection relationship is not determined

Directed graph : Similarly, we create a new vertex table, add both the old value and the new value, and leave the edge table empty.

4.DeleteVertex(G,x)

DeleteVertex(G,x)Remove vertex x from graph G

Deleting a vertex also needs to delete all the edges related to that vertex

  • Adjacency matrix: Set the value of the node corresponding to the vertex table to empty, and set the row and column corresponding to the vertex of the matrix two-dimensional array to empty, so we can sometimes use the reduced array to delete the node.
  • Adjacency table: we need to delete the vertex corresponding to the vertex table, and then clear the edge table corresponding to the vertex table

5.AddEdge(G,x,y)

AddEdge(G,x,y)If the undirected edge (x, y) or the directed edge <x, y> does not exist, add the edge to the graph G

Add an edge to the undirected graph above

  • Adjacency matrix: To add an edge, we need to modify the value of the edge in the matrix to 1, for example, to add an AD edge, we need to modify A[0][3]=1andA[3][0]=1
  • Adjacency table: To add an edge, we need to add nodes to each other in the edge table corresponding to the two vertices.

6.RemoveEdge(G,x,y)

RemoveEdge(G,x,y) If an undirected edge (x,y) or a directed edge <x,y> exists, delete the edge in graph G

Undirected graph as above, delete an edge

  • Adjacency matrix: To delete an edge, you need to modify the value of the edge to 0. For example, to delete the BC edge, we need to modify the A[1][2]=0sumA[2][1]=0
  • Adjacency list: To delete an edge, you need to delete the nodes of each other in the edge table corresponding to the two vertices

7.XxxxNeighbor(G,x)

FirstNeighbor(G,x)Find the first adjacent point of vertex x in graph G, and return the vertex number if there is one. If there is no adjacent point or the graph does not exist x, return -1

NextNeighbor(G,x) Assuming that vertex y in graph G is an adjacent point of vertex x, return the vertex number of the next adjacent point of vertex x except y, if y is the last adjacent point of x, return -1

For example, we find the B vertex

  • FirstNeighbor(G,x)First adjacency
    • Adjacency matrix: We search in the order of the adjacency matrix and search for the row corresponding to B. We find that the first point in this row with a value other than 1 is its first adjacency point A (that is, the corresponding column number is smaller and the value is 1 Node)
    • Adjacency list: Traverse the edge list of corresponding vertices, the first node is its first adjacent node
  • NextNeighbor(G,x) Next neighbor
    • Adjacency matrix: In the same way, we continue to look up according to the above rule, and find the next value of 1 is its next adjacent point
    • Adjacency list: Traverse the edge list of corresponding vertices, the next node of the first node is its next adjacent point

8.Xxx_edge_value(G,x,y)

We know that the graph when the edge has a value is called a net. For the net, we have operations to obtain and set the edge weight.

Get_edge_value(G,x,y) Get the weight v corresponding to the edge (x, y) or <x, y> in the graph G

Set_edge_value(G,x,y) Set the weight v corresponding to the edge (x, y) or <x, y> in the graph G

  • Get the weight of the edge
    • Adjacency matrix: directly find the value of the two-dimensional array corresponding to the edge is the weight of the edge
    • Adjacency table: traverse the adjacency matrix to find the stored weight of the corresponding node
  • Set the weight of the edge
    • Adjacency list matrix: the same
    • Adjacency list: the same

7. Rimuke

Knowledge about data structure, the official account is being updated simultaneously, welcome to follow

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106793405