Data Structure interview 7-common operations on the diagram

Source: Internet
Author: User

Note: The interview book has related exercises, but the ideas are relatively unclear and the layout is incorrect. The author has rewritten the related books and opinions for your reference.

7. Common graph operations

Basic operations on a graph, including: 1. Create a graph, 2. Determine whether the graph is empty, 3. Print the graph, 4. traverse the graph .....

For 1, to create a graph, you need to consider the storage structure of the graph. The storage structure is divided into: the adjacent matrix storage (array) and the adjacent table Storage (array linked list ). For Figure 4, it is also the core operation of the graph, mainly divided into: depth-first traversal (node-by-node recursion) of the graph, and breadth-first traversal (similar to hierarchical traversal ).

In addition, the following section details the extended graph operations: finding the least spanning tree (prim algorithm, Kruskal algorithm) and finding the shortest path (dijstra algorithm and Kruskal algorithm.

// The following example uses the storage structure of the adjacent table.

Template <class vtype, intsize> class graphtype: public1_listgraph <vtype> {public: graphtype ();~ Graphtype (); bool isempty (); void creategraph (); void cleargraph (); void printgraph () const; void depthfirsttraversal (); // void DFT (vtype V, bool * visited) in depth first traversal; // void breadthfirsttraversal () in depth first recursion function; // protected in breadth first traversal: int maxsize; // maximum number of knots int gsize; // the current number of knots [displayed after input] linkedlistgraph <vtype> * graph; // pointer to the linked list graph structure}; Template <class vtype, intsize> graphtype <vtype, size>: graphtype () {maxsize = size; GS Ize = 0; graph = new vertex listgraph <vtype> [maxsize]; // construct a node array linked list ...} template <class vtype, intsize> graphtype <vtype, size> ::~ Graphtype () {cleargraph (); // call the destroy operation Delete [] graph ;}


1. Image judgment is empty

Template <class vtype, intsize> boolgraphtype <vtype, size >:: isempty () {return (gsize = 0); // judge whether it is null based on whether the current number of nodes is 0}


2. Create a chart

// The first line indicates the number of nodes in the graph;

// The second row indicates the adjacent contacts of each vertex;

Template <class vtype, intsize> voidgraphtype <vtype, size >:: creategraph () {cout <"input the Nums of vertex:"; CIN >> gsize; cout <Endl; vtype adjacentvertex; cout <"input the adjacent vertex of every vertex :(-999 end)" <Endl; For (INT Index = 0; index <gsize; ++ index) {cout <"input line" <index <":"; while (CIN> adjacentvertex, adjacentvertex! =-999) //-999 serves as the terminator {graph [Index]. insertlast (adjacentvertex);} // end while} // end}

 

3. Destroy operations. Call the corresponding linked list one by one.

Template <class vtype, intsize> voidgraphtype <vtype, size>: cleargraph () {int index; For (Index = 0; index <gsize; index ++) {graph [Index]. destroylist (); // destroy the linked list ...} gsize = 0 ;}

4. Print the image

Template <class vtype, intsize> voidgraphtype <vtype, size>: printgraph () const {cout <"the graph is shown as below:" <Endl; int index; for (Index = 0; index <gsize; index ++) {cout <index <"\ t"; graph [Index]. print (); // print each chain table} cout <Endl ;}

5. Image depth first Traversal

Unlike a binary tree, a loop may exist in the diagram, and the entire graph may not be traversed from a node.

Core Ideas:1. Access started from a node (custom). If it has not been accessed, access it; 2. Then traverse the adjacent node in the same way as 1. Until all nodes are traversed. Consider: it can be implemented recursively.

The following is an implementation of the depthfirsttraversal function of the Deep-first recursive function DFT & Deep-first traversal function.

Template <class vtype, intsize> void graphtype <vtype, size>: DFT (vtype V, bool * visited) {visited [v] = true; cout <v <"\ t"; vtype * adjacencylist = new vtype [gsize]; // used to store adjacent point int nlength = 0; // there will be a value in the out function // find the adjacent point of the V node. graph [v]. getadjacentvertices (adjacencylist, nlength); // determine whether the adjacent contacts have traversed for (INT I = 0; I <nlength; I ++) {If (! Visited [adjacencylist [I]) {DFT (adjacencylist [I], visited); // recursive operation on adjacent contacts }}template <class vtype, intsize> void graphtype <vtype, size >:: depthfirsttraversal () {cout <"depthfirsttraversal result is as below:"; bool * visited; visited = new bool [gsize]; // define the node access tag array. True is accessed, and false is not accessed. // Initialize the tag array for (INT Index = 0; index <gsize; index ++) {visited [Index] = false;} For (INT Index = 0; index <gsize; index ++) {If (! Visited [Index]) {DFT (index, visited) ;}} cout <Endl; Delete [] visited ;}


6. Graph breadth first Traversal

Core Idea: For all nodes, after traversing each node and all its adjacent nodes, traverse the next node that has not been traversed.

It traverses a level traversal similar to a binary tree.. Each node needs to traverse all its adjacent nodes after traversing a node. Consider queue-based operations, first-in-first-out. Access when entering the team. If the queue is not empty, the team is finished and all adjacent contacts are entered in order. Access when entering the team, and so on.

Template <class vtype, intsize> voidgraphtype <vtype, size>: breadthfirsttraversal () {cout <"breathfirsttraversal result is as below:"; bool * visited; visited = new bool [gsize]; // defines the node access tag array. True is accessed, and false is not accessed. Vtype * adjacencylist = new vtype [gsize]; // used to store adjacent point int nlength = 0; // The out function has a value of queue queuetype <vtype> queue; // used for node team-up operations. vtype W; // the pop-up node // The initialization tag array for (INT Index = 0; index <gsize; index ++) {visited [Index] = false ;} for (INT Index = 0; index <gsize; index ++) {If (! Visited [Index]) {queue. addqueue (INDEX); visited [Index] = true; cout <index <"\ t"; // access // find the adjacent point of the V node. while (! Queue. isemptyqueue () {queue. dequeue (w); graph [w]. getadjacentvertices (adjacencylist, nlength); // determine whether the adjacent contacts have traversed for (INT I = 0; I <nlength; I ++) {If (! Visited [adjacencylist [I]) {queue. addqueue (adjacencylist [I]); //! Note that it is easy to drop. Visited [adjacencylist [I] = true; cout <adjacencylist [I] <"\ t ";} // end if} // end for} // end while} cout <Endl; Delete [] visited ;}# endif


7. Obtain the adjacent nodes and add them to the array.

// Length records the length of the array. Linkedlistgraph is derived from the linked list ).

Template <class vtype> void1_listgraph <vtype>: getadjacentvertices (vtype adjacencylist [], Int & length) {nodetype <vtype> * Current; length = 0; current = first; while (current! = NULL) {adjacencylist [length ++] = Current-> Info; // Save the elements of the linked list to an array. Current = Current-> link ;}}


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.