Data Structure Experiment 4: Diagram representation and implementation

Source: Internet
Author: User
Figure -- joining matrix DFS and BFS: Figure -- joining table DFS and BFS:
# Include <stdio. h>
# Define INF 65536
# Define Maxcompute 100
// Adjacent matrix
Typedef Struct {
// Int no; // No.
Int Info; // Weight Information
} Vertex; // Vertex
Typedef Struct {
Int Edge [Max] [Max];
Int V_num, e_num; // Vertex v and edge e
// Vertex vexs [Max]; // Vertex Information
} Graph;
Typedef Struct Node {
Int Vetex; // Vertex
Struct Node * next;
Int Info; // Weight
} Node;
Typedef Struct Vnode {
// Int data;
Node * next;
} Vnode;
Typedef Struct {
Vnode list [Max];
Int N, E; // Vertex N, edge e
} Agraph;
Void Creatg (graph & G ){
Int I, J;
Int A [Max] [ 6 ] = {
0 , 6 , 1 , 5 , 0 , 0 ,
6 , 0 , 5 , 0 , 0 , 0 ,
1 , 5 , 0 , 5 , 6 , 4 ,
5 , 0 , 5 , 0 , 0 , 2 ,
0 , 0 , 6 , 0 , 0 , 6 ,
0 , 0 , 4 , 2 , 6 , 0
};
G. v_num = 6 ;
G. e_num = 10 ;
For (I = 0 ; I <G. v_num; I ++)
For (J = 0 ; J <G. v_num; j ++ ){
If (A [I] [J]! = 0 )
G. Edge [I] [J] = A [I] [J];
Else
G. Edge [I] [J] = inf;
}
}
Void DFS (graph & G, Int V, Int * Visit ){
Int I;
Visit [v] = 1 ;
Printf ( " % 2D-> " , V + 1 );
For (I = 0 ; I <G. v_num; I ++)
If (Visit [I] = 0 & G. Edge [v] [I]! = Inf)
DFS (G, I, visit );
}
Void BFS (graph G, Int V, Int * Visit) // Traversing the breadth of the adjacent matrix ;{
Int I, queue [Max], rear, front;
Visit [v] = 1 ;
Printf ( " % 2D-> " , V + 1 );
Rear = front = 0 ;
Rear = (Rear + 1 ) % G. v_num;
Queue [rear] = V;
While (Rear! = Front ){
Front = (front + 1 ) % G. v_num;
For (I = 0 ; I <G. v_num; I ++)
If (Visit [I] = 0 & G. Edge [queue [Front] [I]! = Inf)
{
Visit [I] = 1 ;
Printf ( " % 2D-> " , I + 1 );
Rear = (Rear + 1 ) % G. v_num;
Queue [rear] = I;
}
}
}
// Convert the adjacent matrix to the city list
Void Change (graph G, agraph * & G ){
Int I, j, n = G. v_num;
Node * P;
G = New Agraph;
For (I = 0 ; I <n; I ++)
G-> list [I]. Next = NULL;
For (I = 0 ; I <n; I ++)
For (J = N- 1 ; J> = 0 ; J --)
If (G. Edge [I] [J]! = 0 ){
P = New Node;
P-> vetex = J;
P-> info = G. Edge [I] [J]; P-> next = G-> list [I]. Next;
G-> list [I]. Next = P;
}
G-> N = N;
G-> E = G. e_num;
}
Void Dfs2 (agraph * G, Int V, Int * Visit ){
Node * P;
Visit [v] = 1 ;
Printf ( " % 2D-> " , V + 1 );
P = G-> list [v]. Next;
While (P! = NULL ){
If (Visit [p-> vetex] = 0 )
Dfs2 (G, p-> vetex, visit );
P = p-> next;
}
}

Void Travel (graph & G, agraph * g ){
Int I, visit [Max];
// DFS
Printf ( " Depth-first search traversal of the adjacent matrix \ n " );
For (I = 0 ; I <Max; I ++)
Visit [I] = 0 ;
For (I = 0 ; I <G. v_num; I ++)
If (Visit [I] = 0 )
DFS (G, I, visit );
Printf ( " \ N " );
// BFS
Printf ( " The breadth of the adjacent matrix first searches for traversal \ n " );
For (I = 0 ; I <G. v_num; I ++)
Visit [I] =0 ;
For (I = 0 ; I <G. v_num; I ++)
If (Visit [I] = 0 )
BFS (G, I, visit );
Printf ( " \ N " );
Printf ( " Depth-first traversal of the adjacent table \ n " );
For (I = 0 ; I <Max; I ++)
Visit [I] = 0 ;
Dfs2 (G, 0 , Visit );
Printf ( " \ N " );
}
Int Main ()
{
Graph G;
Creatg (g );
Agraph * g;
Change (G, G );
Travel (G, G );
Prim (G, 0 );
}

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.