1, what is the figure
Represents a many-to-many relationship
Contains a set of vertices: typically V (Vertex) represents a collection of vertices
A set of edges: usually an e (edge) represents a set of edges
2. Abstract data type definition
Type name: Graph (graph)
Data Object Set: G (v,e) consists of a non-empty finite vertex set V and a finite edge set E.
Operation set: For any purpose G belongs to Graph, and V belongs to V,e belongs to E
Graph Create (): establishes and returns null;
Graph Insertvertex (graph G, Vertex v): inserting v into G;
Graph Insertedge (graph G, Edge e): inserting e into G;
void DFS (graph G, Vertex v): From Vertex v depth first traverse graph G;
void BFS (graph G, Vertex v): Width-first traverse graph G from Vertex v;
void Shortestpath (graph G, Vertex v, int dist[]): Calculates the shortest distance from Vertex v to any other vertex in Figure g;
void MST (graph G): Calculates the minimum spanning tree of Figure g;
3, using the adjacency matrix to represent a graph
Adjacency matrix g[n][n]--n vertices from 0 to N-1 number
G[I][J] = 1 if <vi,vj> is an edge in G, otherwise, g[i][j] =0
Q: How can I save half of the space for a graph-free storage?
A:
A 1-d array with a length of N (n+1)/2 stores {g00,g10,g11,......, Gn-1 0,..., Gn-1 n-1},
The corresponding subscript for gij in A is: (i* (i+1)/2+j)
For the network, as long as the value of G[I][J] is defined as the Edge <vi, VJ > Weight can be.
Benefits of adjacency matrices:
Intuitive, simple, good understanding
Facilitates checking for the presence of edges between any pair of vertices
Easy to find all "adjacency points" of any vertex (vertices with edges directly connected)
It is convenient to calculate the "degree" of any vertex (the number of edges emitted from that point is "out", and the number of sides to that point is "in")
(No map: corresponding row (or column) of the number of non-0 elements, the graph: the corresponding row is not 0 elements of the number is "out"; the number of non-0 elements of the corresponding column is "in Degrees")
Disadvantages of adjacency matrices:
Wasted space-sparse graphs (lots of points and few edges) there are a lot of invalid elements
Wasting time--how many edges in a statistical sparse graph
4, using adjacency table to represent a diagram
Easy to find all "adjacency points" for any vertex
Space saving of sparse graphs (requires n pointer + 2E nodes (at least 2 domains per node)
For the non-directed graph is convenient to calculate the degree of any vertex, for the directed graph can only calculate the degree, it is necessary to construct "inverse adjacency table" (to its own side) to facilitate the calculation of "penetration"
Inconvenient to check for the presence of edges of any vertex
5, the graph of the traversal
Depth-First Search
Sort of a tree-like first-order traversal
void DFS (Vertex v) { visited[v] = true; For (each adjacency point of V W) if (!visited[W]) DFS (w);}
If there are n vertices and e edges, the complexity of time is:
Storage diagram with Adjacency table, O (n+e)
Storage diagram with adjacency matrix, O (n^2)
Breadth First Search
?? void BFS (Vertex V) { Visited[v] = true; Enqueue (V, Q); while (! IsEmpty (q)) { V = Dequeue (q); For (each adjacency point of V W) if (!visited[w]) { visited[w] = true; Enqueue (W, Q); }
If there are n vertices and e edges, the complexity of time is:
Storage diagram with Adjacency table, O (n+e)
Storage diagram with adjacency matrix, O (n^2)
6. What if the diagram is not connected?
Related concepts:
Connectivity: If there is a (undirected) path from V to W, then V and W are connected
Path: V to W Path is a collection of vertices {V, v1, v2, ..., vn, w}, where any pair of adjacent vertices have a graph
In the edge. The length of the path is the number of sides in the path (the weight of all edges, if any). If all vertices between V to W are different, the simple path is called
Loop: The path connected to the end of the starting point: any two vertices in the graph are connected
Connected graphs: Any two vertices in the graph are connected
Connected components: Maximal connected sub-graph of undirected graphs (maximum vertex number: 1 vertices are not connected to the maximum number of edges: all edges connected by all vertices in the sub-graph are included)
For the graph we have the concept of strong connectivity and weak connectivity.
Strong connectivity: There is a bidirectional path between the vertex V and W in the directional graph, which is said V and W are strongly connected
Strongly connected graphs: any two vertices are strongly connected in a direction graph
Strongly connected components: A strongly connected sub-graph of a forward graph
"Data structure Fifth Week" diagram (top)