1, the diagram as a model to define the relationship between objects. Objects are represented by vertices, and relationships between objects are represented by Benlay between vertices.
2, the graph is divided into the direction graph and the direction-free graph, the direction graph's edge is called the arc.
3, the expression of the diagram: G = (V, e), E contains an ordered pair (U,V), for the graph does not matter (U,V) and (V,u).
As shown above, there are direction graphs: v={v0, V1, v2, v3}. e={(V0,V1), (v2, V0), (v2, V3), (V3, V0)}.
Non-direction diagram: V={v0, V1, v2, v3,v4}. e={(V0, v1), (V0, V3), (v1, v2), (v1, v4), (v2, V3), (v2, v4)}
4. Two important relationships in graphs: adjacency, correlation
Adjacency: In-direction graphs (V0, v1) illustrate V1 adjacency v0.
Complete diagram: Each vertex in a graph is adjacent to the other vertex
Association: The relationship between the V1 (V0, v1) in the direction diagram (V0), which is associated with the V0, Vertex, and edge from the vertex v1.
5. The degree of the vertex: the number of edges at which the vertex is the endpoint.
The degree of the vertex: the number of edges from which the vertex is the starting point.
Simple path: A path without duplicate vertices
Loop: path contains the same node two times or more, that is, from a point of departure can be returned to a certain point.
6, Connectivity: Each vertex can reach the other vertices through a path.
Joint point: Removing a vertex will cause the graph or a branch to lose connectivity.
Bridge: Removing an edge makes the graph lose connectivity.
7. The adjacency table is suitable for sparse graphs, and adjacency matrices are suitable for dense graphs.
8, search methods: Breadth First search, depth first search
Depth First Search
9, Code implementation
(1) Data structure: each vertex corresponds to a linked list node, the node *data point to the node structure adjlist
Adjlist contains: A vertex, a set of vertices connected to the vertex. This is the data
typedef struct adjlist_{
void *vertex that each linked list node points to;
Set adjacent;
} Adjlist;
The attributes of the entire graph include: node points, number of edges, list attributes of all nodes,
typedef struct graph_{
int vcount;
int ecount;
Int (*match) (const void *key1, const void *key2);
void (*destroy) (void *data);
List adjlists;
} Graph;
typedef enum vertexcolor_{White
, gray, black
}vertexcolor;
(2) initialization
void Graph_init (graph *graph, int (*match) (const void *key1, const void *key2), void (*destroy) (void *data))
{
g Raph->vcount = 0;
Graph->ecount = 0;
Graph->match = match;
Graph->destroy = Destroy;
List_init (graph->adjlists, 0, NULL, NULL, NULL);
return;
}
(3) Delete the entire diagram
void Graph_destroy (graph *graph)
{
adjlist *adjlist;
while (List_size (&graph->adjlists) > 0)
{
if List_rem_next (&graph->adjlists, NULL, void * &adjlist) = = 0)
{
//delete the set of adjacent nodes of each vertex
Set_destroy (&adjlist->adjacent);
Deletes the vertex, which is the *data
if (Graph->destroy!= NULL)
Graph->destroy (Adjlist->vertex) that the linked list node points to;
Frees the space that the *data points to the structure itself, which points to the content free
(adjlist);}}
(4) Inserting a vertex: apply for a adjlist, where the member vertex is data, and its edge set is initialized to null
Here the entry parameter *data corresponds to the *adjlist vertex
int Graph_ins_vertex (graph *graph, const void *data)
{
list_element * element;
Adjlist *adjlist;
int retval;
for (element = List_tail (graph->adjlists); element!= NULL; element = List_next (Element))
{
if (graph-> Match (data, (Adjlist *) List_data (Element))->vertex) return
1;
}
if ((Adjlist = (adjlist *) malloc (sizeof (adjlist))) = = NULL)
return-1;
Adjlist->vertex = (void *) data;
Set_init (&adjlist->adjacent, Graph->match, NULL);
if ((retval = List_ins_next (&graph->adjlists, List_tail (&graph->adjlists), adjlist))!= 0) return
retval;
graph->vcount++;
return 0;
}
(5) Insert an edge
Add a line to the diagram
for vertices data1 and data2 already present in the graph, one-way data1 point to data2, which is to add data1 int data2 to the set of vertex Graph_ins_edge
(graph *graph, const void *DATA1, const *data2)
{
list_element *element;
int retval;
First find vertices data2 and data1 for
(element = List_head (graph->adjlists); element!= NULL; element = List_next (Element))
{
if (Graph->match (Adjlist *) list_data (Element)->vertex)) break
;
{if (data2) if (element = = NULL)
return-1;
Inserts data2
if (retval = Set_insert (& (Adjlist *) (List_data (Element)->adjacent), data2)) to the Data1 collection!= 0) c15/>{return
retval;
}
graph->ecount++;
}
(6) Delete vertices, the vertices can not be connected to any other vertex, that is, not in the other nodes of the collection, their own collection is empty.
int Graph_rem_vertex (graph *graph, void **data)
{
list_element *element;
Adjlist *adjlist;
int found;
prev = NULL;
Found = 0;
for (element = List_head (graph->adjlists); element!= NULL; element = List_next (Element))
{
if (set_is_mumber (& ((Adjlist *) (List_data (Element))->adjacent), *data
) return-1;
if (Graph->match *data,& (adjlist *) (List_data (Element)->vertex))
{
temp = element;
Fount =1;
}
Here, for internal, if not found, Prev has been updating the
if (!found)
prev = element;
}
if (!found)
return-1;
if (Set_size (& (Adjlist *) list_data (temp))->adjacent) > 0)
return-1;
*data = adjlist->vertex;
At this point, the node set is empty and can be directly released free
(adjlist);
graph->vcount--;
return 0;
(7) Delete the edge, that is, delete the elements in the Data1 collection data2
int Graph_rem_edge (graph *graph, const void *data1, const *data2)
{
list_element *element;
First find vertex data1 for
(element = List_head (graph->adjlists); element!= NULL; element = List_next (Element))
{
if (Graph->match (data1 (adjlist *) List_data (Element))->vertex) break
;
if (element = = NULL)
return-1;
if (Set_remove (& (Adjlist *) (List_data (Element))->adjacent) *data2)!= 0)
return-1;
graph->ecount--;
return 0;
}
(8) Find the corresponding structure of a vertex adjlist
The corresponding structure of the data is returned to the adjlist
int graph_adjlist (const graph *graph, const void *data, Adjlist **adjlist)
{
list_ ELEMENT *element, *prev;
prev = NULL;
for (element = List_head (graph->adjlists); element!= NULL; element = List_next (Element))
{
if (graph-> Match (*data,& (adjlist *) (List_data (Element))->vertex) {break
;
}
prev = element;
}
if (element = = NULL)
return-1;
*adjlist = List_data (element);
return 0;
}
(9) To determine whether vertex data1 and data2 have adjacency relations, that is, to determine whether DATA2 exists in the data1 set
int graph_is_adjacent (const graph *graph, const void *data1, const void *DATA2) {List_el
Ement *element, *prev;
prev = NULL; for (element = List_head (graph->adjlists); element!= NULL; element = List_next (Element)) {if graph
->match (*data,& (Adjlist *) (List_data (Element)->vertex)) {break;
} prev = element;
} if (element = = NULL) return-1;
Return Set_is_mumber (& (Adjlist *) (List_data (Element))->adjacent), data2); }