Data structure-6.2 figure

Preface-Data Structure

The data structure needs to be chewed repeatedly, and the answers to the problems encountered in the development can be obtained at any time.

Graph representation

  • Sequential storage structure: array representation (adjacency matrix)
  • Chain storage structure: multiple linked lists
  • Adjacency list
  • Adjacency multiple list
  • Cross linked list

The adjacency matrix of the graph-indicates the relationship between the vertices. If there is a connection between two vertices, set it to 1 in the matrix, and set it to 0 if there is no connection.

  • Undirected graph

  • The matrix is ​​symmetric

  • The number of ith row or ith column is the degree of the vertex

  • Half of the number of 1 in the matrix is ​​the number of edges in the graph

  • It is easy to judge whether there is an edge connection between vertex i and vertex j

  • Adjacency Matrix Representation of Undirected Graph
    Insert picture description here

    • Analysis 1: The adjacency matrix of the undirected graph is symmetric;
    • Analysis 2: The degree of vertex i = the number of 1 in the 7th row (column):
    • Special: In the adjacency matrix of the complete graph, the diagonal elements are 0, and the rest are 1.
  • Directed graph

  • The matrix is ​​not necessarily symmetric

  • The number of the i-th row is the out-degree of the vertex

  • The number of column i is the in-degree of the vertex

  • It is easy to judge whether there is an arc connected between vertex i and vertex j

  • The number of 1 in the matrix is ​​the number of arcs in the figure

  • Adjacency matrix representation of directed graph
    Insert picture description here

    • Analysis 1: The adjacency matrix of the directed graph may be asymmetric.
    • Analysis 2: The out degree of the vertex = the sum of the elements in the i-th row
    • The in-degree of the vertex = the sum of the elements in the i-th column
    • The degree of the vertex = the sum of the elements in the ith row + the sum of the elements in the ith column
    • Note: In the adjacency matrix of a directed graph,
    • The meaning of line i: the arc ending with node vi (that is, the out-degree edge):
    • The meaning of column i: the arc headed by node vi (that is, the in-degree edge).
  • Examples of undirected graphs and directed graphs as shown in the following figure, draw their adjacency matrix
    Insert picture description here

Adjacency matrix representation

Insert picture description hereInsert picture description here

  • example
  • The adjacency matrix representation of the net (that is, the right graph)
    Insert picture description here

Adjacency list

  • The head node is info and the node weight that points to the first node table is the right of the network. If there is no power, it is
    Insert picture description here
    Insert picture description here

  • Link the edges emitted by the same vertex in the same edge linked list. Each node of the linked list represents an edge, which is called a table node (edge ​​node). The adjacent point field adjvex stores another point associated with the edge. The index of the item point, the chain field next stores the pointer, the pointer of the next table node in the same linked list, and the data field weght stores the edge rights. The head pointer of the side-linked list is stored in the head node. The head node is stored in a sequential structure, its data field mo stores vertex information, and the chain field first points to the first vertex in the linked list.

Adjacency list of undirected graph

Insert picture description here

  • The order of the vertices can be arbitrary. Its space efficiency is O(n+2e) ps: the adjacency table is not unique, because the order of the links of each edge node is arbitrary (4->2->0, 4->3- >1, 4->2->0, 3->2->1)
  • The number of nodes in the i-th linked list is the degree of vertex i
  • Half of the number of nodes in all linked lists is the number of edges in the graph
  • The number of memory cells occupied is n + 2e (obviously, n-head node + its other nodes)

Adjacency list of directed graph

  • The order of the vertices can be arbitrary. Its space efficiency is O(n+e)
  • The number of nodes in the i-th linked list is the out-degree of vertex i
  • The number of nodes in all linked lists is the number of arcs in the graph
  • The number of memory cells occupied is n + e (obviously, n-head node + its other nodes)
    Insert picture description here

example

  • Knowing the adjacency (outside) table of a certain network, please draw the network.Insert picture description here
  • answer
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_41732253/article/details/109592301