Adjacent matrix (directed graph, undirected graph implementation difference)

Source: Internet
Author: User

For a similar graph, if it is an undirected graph, it is as follows:

 

If it is a directed graph, it is as follows:

 

Specific implementationCode, Almost the same. Note that the undirected graph is a symmetric matrix, and the directed graph is not necessarily.

# Include <iostream> <br/> using namespace STD; <br/> # define maxvertexnum 100 <br/> # define queuesize 30 <br/> bool visited [maxvertexnum]; <br/> typedef struct <br/> {<br/> char vertex [maxvertexnum]; <br/> int edges [maxvertexnum] [maxvertexnum]; <br/> int N, E; <br/>} mgraph; <br/> void createmgraph (mgraph & G) <br/>{< br/> int I, j, k; <br/> cout <"Enter the number of vertices and edges:"; <br/> CIN> G. n> G. e; <br/> cout <"Enter the vertex element:"; <br/> (I = 0; I <G. n; I ++) <br/>{< br/> CIN> G. vertex [I]; <br/>}< br/> for (I = 0; I <G. n; I ++) <br/>{< br/> for (j = 0; j <G. n; j ++) <br/>{< br/> G. edges [I] [J] = 0; <br/>}</P> <p> // establish an undirected graph <br/> // cout <"Enter the vertex numbers at both ends of the edge: /n "; <br/> // For (k = 0; k <G. e; k ++) <br/> // {<br/>/CIN> I> J; <br/> // G. edges [I] [J] = 1; <br/> // G. edges [J] [I] = 1; <br/> // </P> <p> // create a directed graph <br/> cout <"Enter the arc header and tail number:/N "; <br/> for (k = 0; k <G. e; k ++) <br/>{< br/> CIN>> I> J; <br/> G. edges [I] [J] = 1; <br/>}< br/> // deep Priority Search <br/> void DFS (mgraph G, int I) <br/>{< br/> cout <G. vertex [I] <"; <br/> visited [I] = true; <br/> for (Int J = 0; j <G. n; j ++) <br/>{< br/> If (G. edges [I] [J] = 1 &&! Visited [J]) <br/>{< br/> DFS (G, J ); <br/>}< br/> void dfstraversem (mgraph g) <br/>{< br/> int I; <br/> for (I = 0; I <G. n; I ++) <br/>{< br/> visited [I] = false; <br/>}< br/> for (I = 0; I <G. n; I ++) <br/>{< br/> If (! Visited [I]) <br/>{< br/> DFS (G, I ); <br/>}< br/> // breadth-first search <br/> typedef struct <br/>{< br/> int front; <br/> int rear; <br/> int count; <br/> int data [queuesize]; <br/>} cirqueue; <br/> void initqueue (cirqueue * q) <br/> {<br/> q-> front = Q-> rear = 0; <br/> q-> COUNT = 0; <br/>}< br/> int queueempty (cirqueue * q) <br/>{< br/> return Q-> COUNT = queuesize; <br/>}< br/> int queuefull (cirqueue * q) <br/> {<br/> RET Urn Q-> COUNT = queuesize; <br/>}< br/> void enqueue (cirqueue * q, int X) <br/>{< br/> If (queuefull (q) <br/>{< br/> cout <"the queue is full ~~ "; <Br/>}< br/> else <br/>{< br/> q-> count ++; <br/> q-> data [q-> rear] = x; <br/> q-> rear = (Q-> rear + 1) % queuesize; <br/>}< br/> int dequeue (cirqueue * q) <br/>{< br/> int temp; <br/> If (queueempty (q) <br/>{< br/> cout <"nothing to delete"; <br/> return NULL; <br/>}< br/> else <br/> {<br/> temp = Q-> data [q-> front]; <br/> q-> count --; <br/> q-> front = (Q-> front + 1) % queuesize; <br/> return temp; <br/>}< br/> void BFS (mgraph * G, int K) <br/>{< br/> int I, j; <br/> cirqueue Q; <br/> initqueue (& Q ); <br/> cout <G-> vertex [k] <""; <br/> visited [k] = true; <br/> enqueue (& Q, k); <br/> while (! Queueempty (& Q) <br/>{< br/> I = dequeue (& Q); <br/> for (j = 0; j <G-> N; j ++) <br/>{< br/> If (G-> edges [I] [J] = 1 &&! Visited [J]) <br/>{< br/> cout <G-> vertex [J] <""; <br/> visited [J] = true; <br/> enqueue (& Q, J ); <br/>}< br/> void bfstraversem (mgraph * g) <br/> {<br/> int I; <br/> for (I = 0; I <G-> N; I ++) <br/>{< br/> visited [I] = false; <br/>}< br/> for (I = 0; I <G-> N; I ++) <br/>{< br/> If (! Visited [I]) <br/>{< br/> BFS (G, I ); <br/>}< br/> int main () <br/>{< br/> mgraph g; <br/> createmgraph (g); <br/> cout <"Deep traversal:"; <br/> dfstraversem (g ); <br/> cout <"/n span traversal:"; <br/> bfstraversem (& G); <br/> return 0; <br/>}

send the code again.

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.