Special adjacency Table--Cube adjacency table

Source: Internet
Author: User

Special adjacency table-cube adjacency table: Considerations for some special cases of linked tables

When we are learning graphs, we all know that the commonly used methods of saving graphs have adjacency matrices and adjacency tables. When the number of edges in the graph is less than the number of vertices, there are many 0 values in the adjacency matrix, that is, the sparse matrix is formed. This time using the Adjacency table to store the diagram can greatly reduce the space required for storage, that is, the matrix is "compressed."

The nature of adjacency table is a chain storage structure, which holds the adjacency relationship between vertices, that is, only the non-0 elements in the adjacency matrix are considered. It is natural to think of constructing an adjacency table through a linked list, because the number of edges passed by each vertex is mostly different, and it is obviously more reasonable to allocate memory dynamically with a linked list, but there is a special case: the number of edges on all vertices is the same. Like a cube, which has eight vertices, but each vertex has only three vertices adjacent to it, there is no need to store the list with the same number of vertices adjacent to each vertex, and it is obviously more convenient to use a fixed length array, which I call the cube adjacency table (Cubic Adjacency list,cal).

The definition of CAL is similar to the adjacency table, except that the number of vertices adjacent to each vertex is the same, about the adjacency table ... You can review the definitions in the following data structure courses.

The structure definition and practical application of the cube adjacency matrix are expounded directly with a question.

  Problem Description :

There is a frog at point A of the cube abcd-a1b1c1d1 (ant, Worm ...). Anything) to jump to the C1 point, it can only be one step at a time (that is, one edge), when jumping five times or has reached the target point to stop beating.

(1) How many lines does the frog have to jump to the destination within 5 times?

(2) Ask the frog for all possible jumping ways?

Give the answer first, according to the knowledge of permutation combination, the first question should be 3x2x1=6, the second question should be (33-6) x3x3+6=195 species. Here is the source code for this problem with Cal:

1 /*Question:there is a frog standing in2 A piont in Cube abcd-a1b1c1d1,it must3 another within4 5 Jumps,how Many ways does it has?5 */6#include <stdio.h>7 8typedefenum{a,b,c,d,a1,b1,c1,d1} vertex;//enumeration type that defines vertex information9 Ten ConstVertex cube[8][3]=/*cube[0]={b,a1,d} indicates that the b,a1,d point is adjacent to point A, and so on, so that a cube is constructed. One { A {b,a1,d},{a,c,b1},{b,c1,d},{a,c,d1}, - {A,D1,B1},{A1,C1,B},{D1,B1,C},{A1,D,C1} -};/*using a adjacency list to store graph*/
the - LongIcount=0;/*record the number of routes that can reach C1 points*/ - LongItotal=0*/* Record the number of routes arriving within 5 times (first question) */ - Longisum=0*/* Record all routes (second question) */ + - voidOutput (vertex path[],LongCountintn);//function prototype, output path + //output Function A at intTraverse (intN, Vertex P,vertex path[]);//function prototype, traversing adjacency table - //core func to traverse graph by Recusion - - intMainvoid) - { -Vertex path[5]={a};//At the most 5 inVertex p=a;//Beginnings at point A -Traverse (0, P,path);//Calling, traversing toprintf"All the number of existing paths:%ld \ n", iCount); +printf"Number of THATC an complete the path within five steps:%ld\n", isum); -printf"All possible paths:%ld\n", itotal); the return 0; * } $ Panax Notoginseng voidOutput (vertex path[],LongCountintN)//print information - { the inti; +printfthe Path%ld:", count); A for(i=0; i<=n;++i) the { + Switch(Path[i]) - { $ Casea:printf ("a-->"); Break; $ Caseb:printf ("b-->"); Break; - Casec:printf ("c-->"); Break; - Cased:printf ("d-->"); Break; the Casea1:printf ("a1-->"); Break; - Caseb1:printf ("b1-->"); Break; Wuyi Casec1:printf ("c1\n"); Break; the Cased1:printf ("d1-->"); Break; - } Wu } - } About $ intTraverse (intN,vertex P,vertex path[]) - { - inti; -path[n]=p; A if(n==5)//has jumped five times + { the++itotal;//third question quantity plus one - if(p==C1)//already to C1 point $ { theOutput (path,++icount,n); ICount, the number of routes that can be reached is +1, the return 1; the } the Else - return 0; in } the if(p==C1)//within five steps to reach C1 point the { About++itotal; theOutput (path,++icount,n); Output the if(n<5) the++isum; + return 1; - } the for(i=0;i<3;++i)//not to, start with three points adjacent to a vertexBayi { theTraverse (n+1, cube[(intp][i],path);//recursive invocation, traversing the adjacency table, where (int) p refers to the a~d1 point (the enumeration type in the C language is shaped and starts from 0 by default) the } - return 0; -}

Compiling and running the program can be found to coincide with mathematical calculations (nonsense!). )。 Thus, we succeeded in preserving the three-dimensional cube in the form of a two-dimensional adjacency matrix.

There are many uses for Cal, especially for solving the problem of spatial stereoscopic graphics. But no matter how the change, or the few common algorithms can be achieved, simple is beautiful!

Special adjacency Table--Cube adjacency table

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.