Write an algorithm step by step (create a graph)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

 

 

We have discussed the basic structure of a graph. It can be matrix type, array type, or pointer type. Of course, for me, the structure I prefer is the linked list pointer type. Essentially, a graph consists of many nodes, each of which has many branches. To this end, we made a small change to the original structure:

 

 

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

Struct _ VECTEX * next;

} VECTEX;

 

Typedef struct _ GRAPH

{

Int count;

VECTEX * head;

} GRAPH;

Typedef struct _ LINE

{

Int end;

Int weight;

Struct _ LINE * next;

} LINE;

 

Typedef struct _ VECTEX

{

Int start;

Int number;

LINE * neighbor;

Struct _ VECTEX * next;

} VECTEX;

 

Typedef struct _ GRAPH

{

Int count;

VECTEX * head;

} GRAPH; To create a GRAPH, you must first create a node and an edge. Start with creating a node,

 

 

VECTEX * create_new_vectex (int start)

{

VECTEX * pVextex = (VECTEX *) malloc (sizeof (VECTEX ));

Assert (NULL! = PVextex );

 

PVextex-> start = start;

PVextex-> number = 0;

PVextex-> neighbor = NULL ;;

PVextex-> next = NULL;

Return pVextex;

}

VECTEX * create_new_vectex (int start)

{

VECTEX * pVextex = (VECTEX *) malloc (sizeof (VECTEX ));

Assert (NULL! = PVextex );

 

PVextex-> start = start;

PVextex-> number = 0;

PVextex-> neighbor = NULL ;;

PVextex-> next = NULL;

Return pVextex;

}

Then we should create an edge,

 

 

LINE * create_new_line (int end, int weight)

{

LINE * pLine = (LINE *) malloc (sizeof (LINE ));

Assert (NULL! = PLine );

PLine-> end = end;

PLine-> weight = weight;

PLine-> next = NULL;

Return pLine;

}

LINE * create_new_line (int end, int weight)

{

LINE * pLine = (LINE *) malloc (sizeof (LINE ));

Assert (NULL! = PLine );

PLine-> end = end;

PLine-> weight = weight;

PLine-> next = NULL;

Return pLine;

} With the above content, it is easy to create a vertex with edges,

 

 

VECTEX * create_new_vectex_for_graph (int start, int end, int weight)

{

VECTEX * pVectex = create_new_vectex (start );

Assert (NULL! = PVectex );

PVectex-> neighbor = create_new_line (end, weight );

Assert (NULL! = PVectex-> neighbor );

Return pVectex;

}

VECTEX * create_new_vectex_for_graph (int start, int end, int weight)

{

VECTEX * pVectex = create_new_vectex (start );

Assert (NULL! = PVectex );

PVectex-> neighbor = create_new_line (end, weight );

Assert (NULL! = PVectex-> neighbor );

Return pVectex;

} So how is it related to graph? In fact, it is not difficult.

 

 

GRAPH * create_new_graph (int start, int end, int weight)

{

GRAPH * pGraph = (GRAPH *) malloc (sizeof (GRAPH ));

Assert (NULL! = PGraph );

 

PGraph-> count = 1;

PGraph-> head = create_new_vectex_for_graph (start, end, weight );

Assert (NULL! = PGraph-> head );

 

Return pGraph;

}

GRAPH * create_new_graph (int start, int end, int weight)

{

GRAPH * pGraph = (GRAPH *) malloc (sizeof (GRAPH ));

Assert (NULL! = PGraph );

 

PGraph-> count = 1;

PGraph-> head = create_new_vectex_for_graph (start, end, weight );

Assert (NULL! = PGraph-> head );

 

Return pGraph;

} With a graph and an edge, it is not difficult to find the node and edge.

 

 

VECTEX * find_vectex_in_graph (VECTEX * pVectex, int start)

{

If (NULL = pVectex)

Return NULL;

 

While (pVectex ){

If (start = pVectex-> start)

Return pVectex;

PVectex = pVectex-> next;

}

 

Return NULL;

}

 

LINE * find_line_in_graph (LINE * pLine, int end)

{

If (NULL = pLine)

Return NULL;

 

While (pLine ){

If (end = pLine-> end)

Return pLine;

 

PLine = pLine-> next;

}

 

Return NULL;

}

VECTEX * find_vectex_in_graph (VECTEX * pVectex, int start)

{

If (NULL = pVectex)

Return NULL;

 

While (pVectex ){

If (start = pVectex-> start)

Return pVectex;

PVectex = pVectex-> next;

}

 

Return NULL;

}

 

LINE * find_line_in_graph (LINE * pLine, int end)

{

If (NULL = pLine)

Return NULL;

 

While (pLine ){

If (end = pLine-> end)

Return pLine;

 

PLine = pLine-> next;

}

 

Return NULL;

}

 

 

 

Summary:

 

(1) The graph is the aggregation of multiple linked lists.

 

(2) If you want to learn a good graph, you 'd better clarify and consolidate the previous linked list and pointer.

 

(3) try to write small functions. Small functions can be built to facilitate reading and debugging.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.