C ++程序使用关联列表表示图
该程序使用关联列表表示图,该算法的时间复杂度为O(e)。
算法
Begin Take the input of the number of vertex ‘v’ and edges ‘e’ and also take the input of ‘e’ pairs of vertexes of the given graph in e[][]. For each edge print the corresponding vertex involved in that connection. End
范例程式码
#include<iostream>
using namespace std;
int main() {
int i, v, e, j, c;
cout<<"Enter the number of vertexes of the graph: ";
cin>>v;
cout<<"\nEnter the number of edges of the graph: ";
cin>>e;
int edge[e][2];
for(i = 0; i < e; i++) {
cout<<"\nEnter the vertex pair for edge "<<i+1;
cout<<"\nV(1): ";
cin>>edge[i][0];
cout<<"V(2): ";
cin>>edge[i][1];
}
cout<<"\n\nThe incidence list representation for the given graph: ";
for(i = 0; i < e; i++) {
//对于每个顶点打印,其相邻顶点。
cout<<"\n\tE("<<i+1<<") -> { ";
cout<<"V("<<edge[i][0]<<") , "<<"V("<<edge[i][1]<<")";
cout<<" }";
}
}输出结果
Enter the number of vertexes of the graph: 3
Enter the number of edges of the graph: 4
Enter the vertex pair for edge 1
V(1): 2
V(2): 1
Enter the vertex pair for edge 2
V(1): 1
V(2): 2
Enter the vertex pair for edge 3
V(1): 3
V(2): 2
Enter the vertex pair for edge 4
V(1): 2
V(2): 3
The incidence list representation for the given graph:
E(1) -> { V(2) , V(1) }
E(2) -> { V(1) , V(2) }
E(3) -> { V(3) , V(2) }
E(4) -> { V(2) , V(3) }热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志