有向图的强连通分量 十字链表存储

  1 #include <iostream>
2
3 using namespace std;
4
5 template <class TElemType>
6 class Graph
7 {
8 public:
9
10 void CreateOlgraph();
11 void DFSOlgraph(int v,bool *visited,int *finished); //表示顺向深度优先搜索
12 void DFSOlgraph(int v,bool *visited);
13 void Conponderance(); //求有向图的强连通
14 private:
15 int count;
16 struct ArcBox
17 {
18 int headvex,tailvex;
19 ArcBox *hlink,*tlink;
20 float weight;
21 };
22
23
24
25 template <class TElemType>
26 struct Vertex
27 {
28 TElemType data;
29 ArcBox *firstin,*firstout;
30 };
31
32 struct Olgraph
33 {
34 int vexnum,arcnum;
35 Vertex<TElemType> *vex;
36 };
37 Olgraph olgraph; //有向图的十字链表存储结构
38
39
40 };
41
42 template <class TElemType>
43 void Graph<TElemType>::CreateOlgraph() // 创建十字链表
44 {
45 int i,j,m,n;
46 float w;
47 TElemType v1,v2;
48 ArcBox *p;
49 cout << "输入有向图的顶点数和边数:" << endl;
50 cin >> olgraph.vexnum >> olgraph.arcnum;
51 olgraph.vex = (Vertex<TElemType> *)malloc(olgraph.vexnum * sizeof(Vertex<TElemType>));
52 cout << "输入顶点的信息:" << endl;
53 for(i = 0;i < olgraph.vexnum;i++)
54 {
55 cin >> olgraph.vex[i].data;
56 olgraph.vex[i].firstin = olgraph.vex[i].firstout = NULL;
57 }
58
59 cout << "输入各边的弧尾与弧头结点及有向边的权值:" << endl;
60
61 for(i = 0;i < olgraph.arcnum;i++)
62 {
63 cin >> v1 >> v2 >> w;
64 for(j = 0;j < olgraph.vexnum;j++)
65 {
66 if(v1 == olgraph.vex[j].data) m = j;
67 if(v2 == olgraph.vex[j].data) n = j;
68 }
69 p = (ArcBox *)malloc(sizeof(ArcBox));
70 p->headvex = n;p->tailvex = m;
71 p->weight = w;
72 p->hlink = olgraph.vex[n].firstin;olgraph.vex[n].firstin = p;
73 p->tlink = olgraph.vex[m].firstout;olgraph.vex[m].firstout = p;
74 }
75 } //end CreateOlgraph
76
77 template <class TElemType>
78 void Graph<TElemType>::DFSOlgraph(int v,bool *visited,int *finished)
79 {
80 ArcBox *p;
81 int v1;
82 visited[v] = true;
83 for(p = olgraph.vex[v].firstout;p;p = p->tlink)
84 {
85 v1 = p->headvex; //把p弧的弧头数赋给v1 p弧的弧头数是从当前节点v指出的第一条出弧
86 if(!visited[v1])//如果v1没有被遍历过 如果v1被遍历过,将p移动到与p同是以v结尾的具有相同弧尾的弧
87 DFSOlgraph(v1,visited,finished); //
88 }
89 finished[count++] = v;
90 }
91
92 template <class TElemType> //回溯的过程?
93 void Graph<TElemType>::DFSOlgraph(int v,bool *visited)
94 {
95 ArcBox *p;
96 int v1;
97 visited[v] = true;
98 cout << olgraph.vex[v].data << " ";
99 for(p = olgraph.vex[v].firstin;p;p = p->hlink)
100 {
101 v1 = p->tailvex;
102 if(!visited[v1])
103 DFSOlgraph(v1,visited);
104 }
105 }
106
107
108 template <class TElemType>
109 void Graph<TElemType>::Conponderance()
110 {
111 int v;
112 count = 0; //count是DFN的值吗?待求证
113 bool *visited = (bool *)malloc(olgraph.vexnum * sizeof(bool));
114 int *finished = (int *)malloc(olgraph.vexnum * sizeof(int));
115 for(int i = 0;i < olgraph.vexnum;i++)
116 {visited[i] = false;finished[i] = -1;} //初始化所有节点
117 for(v = 0;v < olgraph.vexnum;v++) //从零号节点开始
118 {
119 if(!visited[v])
120 {
121 visited[v] = true;
122 DFSOlgraph(v,visited,finished);
123 }
124 }
125 for(int i = 0;i < olgraph.vexnum;i++) visited[i] = false;
126 cout << "强连通分量如下:" << endl;
127 for(int i = olgraph.vexnum-1; i >= 0;i--)
128 {
129 v = finished[i];
130 if(!visited[v])
131 {
132 DFSOlgraph(v,visited);
133 cout << endl;
134 }
135 }
136 free(visited);
137 free(finished);
138 }
139
140
141 int main()
142 {
143 /*Graph<int> gph;
144 gph.CreateDN();
145 int a,b;
146 cout << "输入终点和起点:";
147 cin >> a >> b;
148 gph.Shortestpath_DIJ(a,b);
149 gph.DestroyDN();
150 return 0;*/
151 Graph<int> gph;
152 gph.CreateOlgraph();
153 gph.Conponderance(); //求有向图的强连通分量
154 //gph.DestroyAlgraph();
155 return 0;
156 }

转载于:https://www.cnblogs.com/uniquews/archive/2012/02/22/2363317.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值