[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
As we talked about the data structure and creation of graphs, today we will talk about how to add and delete edges in graphs. Edge Addition and deletion are not complex, but the key thing to remember is that you must build a large function based on a small function. Otherwise, errors may occur.
(A) edge Creation
Edge creation can be divided into the following steps:
1) check whether there are nodes in the current graph. If not, add an edge at pGraph-> head.
2) if a node exists in the current graph, check whether the node starts with start. If no vertex or edge is created, insert it into the head of the graph.
3) In the current start Node, determine whether the end edge already exists. If the end edge exists, an error is returned. Otherwise, an edge is added to pVectex-> neighbor.
4) Processing the number of interesting points and the number of edges during Addition
View plaincopy to clipboardprint? STATUS insert_vectex_into_graph (GRAPH * pGraph, int start, int end, int weight)
{
VECTEX * pVectex;
LINE * pLine;
If (NULL = pGraph)
Return FALSE;
If (NULL = pGraph-> head ){
PGraph-> head = create_new_vectex_for_graph (start, end, weight );
PGraph-> head-> number ++;
PGraph-> count ++;
Return TRUE;
}
PVectex = find_vectex_in_graph (pGraph-> head, start );
If (NULL = pVectex ){
PVectex = create_new_vectex_for_graph (start, end, weight );
PVectex-> next = pGraph-> head;
PGraph-> head = pVectex;
PGraph-> head-> number ++;
PGraph-> count ++;
Return TRUE;
}
PLine = find_line_in_graph (pVectex-> neighbor, end );
If (NULL! = PLine)
Return FALSE;
PLine = create_new_line (end, weight );
PLine-> next = pVectex-> neighbor;
PVectex-> neighbor = pLine;
PVectex-> number ++;
Return TRUE;
}
STATUS insert_vectex_into_graph (GRAPH * pGraph, int start, int end, int weight)
{
VECTEX * pVectex;
LINE * pLine;
If (NULL = pGraph)
Return FALSE;
If (NULL = pGraph-> head ){
PGraph-> head = create_new_vectex_for_graph (start, end, weight );
PGraph-> head-> number ++;
PGraph-> count ++;
Return TRUE;
}
PVectex = find_vectex_in_graph (pGraph-> head, start );
If (NULL = pVectex ){
PVectex = create_new_vectex_for_graph (start, end, weight );
PVectex-> next = pGraph-> head;
PGraph-> head = pVectex;
PGraph-> head-> number ++;
PGraph-> count ++;
Return TRUE;
}
PLine = find_line_in_graph (pVectex-> neighbor, end );
If (NULL! = PLine)
Return FALSE;
PLine = create_new_line (end, weight );
PLine-> next = pVectex-> neighbor;
PVectex-> neighbor = pLine;
PVectex-> number ++;
Return TRUE;
}
(B) edge Deletion
Before deleting an edge, we need to process the subnode of the linked list and construct a delete small function, which can be used in the edge deletion function.
View plaincopy to clipboardprint? STATUS delete_old_vectex (VECTEX ** ppVectex, int start)
{
VECTEX * pVectex;
VECTEX * prev;
If (NULL = ppVectex | NULL = * ppVectex)
Return FALSE;
PVectex = find_vectex_in_graph (* ppVectex, start );
If (NULL = pVectex)
Return FALSE;
If (pVectex = * ppVectex ){
* PpVectex = pVectex-> next;
Free (pVectex );
Return TRUE;
}
Prev = * ppVectex;
While (pVectex! = Prev-> next)
Prev = prev-> next;
Prev-> next = pVectex-> next;
Free (pVectex );
Return TRUE;
}
STATUS delete_old_line (LINE ** ppLine, int end)
{
LINE * pLine;
LINE * prev;
If (NULL = ppLine | NULL = * ppLine)
Return FALSE;
PLine = find_line_in_graph (* ppLine, end );
If (NULL = pLine)
Return FALSE;
If (pLine = * ppLine ){
* PpLine = pLine-> next;
Free (pLine );
Return TRUE;
}
Prev = * ppLine;
While (pLine! = Prev-> next)
Prev = prev-> next;
Prev-> next = pLine-> next;
Free (pLine );
Return TRUE;
}
STATUS delete_old_vectex (VECTEX ** ppVectex, int start)
{
VECTEX * pVectex;
VECTEX * prev;
If (NULL = ppVectex | NULL = * ppVectex)
Return FALSE;
PVectex = find_vectex_in_graph (* ppVectex, start );
If (NULL = pVectex)
Return FALSE;
If (pVectex = * ppVectex ){
* PpVectex = pVectex-> next;
Free (pVectex );
Return TRUE;
}
Prev = * ppVectex;
While (pVectex! = Prev-> next)
Prev = prev-> next;
Prev-> next = pVectex-> next;
Free (pVectex );
Return TRUE;
}
STATUS delete_old_line (LINE ** ppLine, int end)
{
LINE * pLine;
LINE * prev;
If (NULL = ppLine | NULL = * ppLine)
Return FALSE;
PLine = find_line_in_graph (* ppLine, end );
If (NULL = pLine)
Return FALSE;
If (pLine = * ppLine ){
* PpLine = pLine-> next;
Free (pLine );
Return TRUE;
}
Prev = * ppLine;
While (pLine! = Prev-> next)
Prev = prev-> next;
Prev-> next = pLine-> next;
Free (pLine );
Return TRUE;
}
In general, edge deletion and addition are reversible. The process is as follows:
1) check whether any node exists in the graph. If no node exists, an error is returned.
2) check whether the node start exists in the graph. If it does not exist, an error is returned.
3) check whether the end edge exists in the start Node. If it does not exist, an error is returned.
4) Delete the corresponding edge
5) Determine whether the edge count number of the node is 0. If it is 0, continue to delete the node.
6) handle the number of vertices and edges during deletion.
View plaincopy to clipboardprint? STATUS delete_vectex_from_graph (GRAPH * pGraph, int start, int end, int weight)
{
VECTEX * pVectex;
LINE * pLine;
STATUS result;
If (NULL = pGraph | NULL = pGraph-> head)
Return FALSE;
PVectex = find_vectex_in_graph (pGraph-> head, start );
If (NULL = pVectex)
Return FALSE;
PLine = find_line_in_graph (pVectex-> neighbor, end );
If (NULL! = PLine)
Return FALSE;
Result = delete_old_line (& pVectex-> neighbor, end );
Assert (TRUE = result );
PVectex-> number --;
If (0 = pVectex-> number)
Result = delete_old_vectex (& pGraph-> head, start );
Assert (TRUE = result );
PGraph-> count --;
Return TRUE;
}
STATUS delete_vectex_from_graph (GRAPH * pGraph, int start, int end, int weight)
{
VECTEX * pVectex;
LINE * pLine;
STATUS result;
If (NULL = pGraph | NULL = pGraph-> head)
Return FALSE;
PVectex = find_vectex_in_graph (pGraph-> head, start );
If (NULL = pVectex)
Return FALSE;
PLine = find_line_in_graph (pVectex-> neighbor, end );
If (NULL! = PLine)
Return FALSE;
Result = delete_old_line (& pVectex-> neighbor, end );
Assert (TRUE = result );
PVectex-> number --;
If (0 = pVectex-> number)
Result = delete_old_vectex (& pGraph-> head, start );
Assert (TRUE = result );
PGraph-> count --;
Return TRUE;
}
Note:
(1) Pay attention to writing small functions. complex functions are built with countless small functions. It is best to have no more than 50 rows of functions.
(2) Regular rules, code must be tested