对输入图的折线图执行边缘着色的 C++ 程序
无向图G的折线图是另一种表示G的L(G)边之间的邻接关系的图。在这个程序中,我们对输入图的折线图进行边着色。
算法
Begin
Take the input of the number of vertices ‘n’ and number of edges ‘e’.
Take the input of ‘n’ vertex pairs for the ‘e’ edges in the graph in ed[][]. Function GenLineGraph(): Construct a line graph in LineEd[][].
To construct line graph, for each edge in the given graph, connect it
to its adjacent edge in LineEd.
Function EdgeColor(): Color the graph edges of LineEdge[][] graph.
Assign color to current edge as col i.e. 1 initially.
If any of the adjacent edges have the same color, then discard this
color and go to flag again and try next color.
Print the color for each edge of the line graph.
End.示例代码
#includeusing namespace std; int GenLineGraph(int ed[][2], char LineEd[][3], int e) { int i, cnt = 0, j, N; char c; for(i = 0; i < e; i++) { for(j = i+1; j < e; j++) { if(ed[j][0] == ed[i][0] || ed[j][0] == ed[i][1] || ed[j][1] == ed[i][0] || ed[j][1] == ed[i][1]) { LineEd[cnt][0] = 'a'+i; LineEd[cnt][1] = 'a'+j; LineEd[cnt][2] = 0; cnt++; } } } N = cnt; cout<<"\n\nThe adjacency list representation for the given graph: "; for(i = 0; i < e; i++) { cnt = 0; c = 'a'+i; cout<<"\n\t"< { "; for(j = 0; j < N; j++) { if(LineEd[j][0] == i+'a') { cout< >n; cout<<"输入图形的边数: "; cin>>e; int ed[e][2]; char LineEd[e*e][3]; for(i = 0; i < e; i++) { cout<<"\nEnter the vertex pair for edge '"< >ed[i][0]; cout<<"V(2): "; cin>>ed[i][1]; } e = GenLineGraph(ed, LineEd, e); EdgeColor(LineEd , e); for(i = 0; i < e; i++) cout<<"\nThe color of the edge between vertex n(1):"< 输出结果 输入图形的顶点数:4 输入图形的边数: 3 Enter the vertex pair for edge 'a' V(1): 1 V(2): 2 Enter the vertex pair for edge 'b' V(1): 3 V(2): 2 Enter the vertex pair for edge 'c' V(1): 4 V(2): 1 The adjacency list representation for the given graph: a-> { b c } b-> { a } c-> { a } The color of the edge between vertex n(1):a and n(2):b is: color1. The color of the edge between vertex n(1):a and n(2):c is: color2.
热门推荐
1 泰迪狗狗如何训练
3 狗狗训练图
5 狗狗训练方法
6 训练狗狗不咬人
8 敏捷训练狗狗
10 训练狗狗吃饭
11 泰迪狗狗训练
12 狗狗从多大开始训练
13 狗狗训练学校费用
14 怎么去训练狗狗
15 狗狗训练狗狗
16 狗狗训练教程
17 训练狗狗装死
18 泰迪狗狗的训练方法