-
Notifications
You must be signed in to change notification settings - Fork 2
/
packet_information_rate_analysis_for_single_cluster.cpp
211 lines (158 loc) · 3.49 KB
/
packet_information_rate_analysis_for_single_cluster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include<bits/stdc++.h>
using namespace std;
float energyamplitude=70;
//"energy amplitude=70PJ/bit/m2"
float energyel=50;
float channelcapacity=34;
//cout<<"Channel capacity=34"<<endl;
float packetinforate;
float lengthofchannel=10;
//cout<<"length of channel =10"<<endl;
vector<pair<int,int> > veuler;
class Graph
{
int V;
list<int> *adj;
public:
Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
~Graph()
{ delete [] adj;
}
void addEdge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u); }
void rmvEdge(int u, int v);
void printEulerTour();
void printEulerUtil(int s);
int DFSCount(int v, bool visited[]);
bool isValidNextEdge(int u, int v);
};
void Graph::printEulerTour()
{
int u = 0;
for (int i = 0; i < V; i++)
if (adj[i].size() & 1)
{ u = i; break; }
printEulerUtil(u);
cout << endl;
}
void Graph::printEulerUtil(int u)
{
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
{
int v = *i;
if (v != -1 && isValidNextEdge(u, v))
{
cout << u << "-" << v << " ";
veuler.push_back(make_pair(u,v));
rmvEdge(u, v);
printEulerUtil(v);
}
}
}
bool Graph::isValidNextEdge(int u, int v)
{
int count = 0;
list<int>::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
if (*i != -1)
count++;
if (count == 1)
return true;
bool visited[V];
memset(visited, false, V);
int count1 = DFSCount(u, visited);
rmvEdge(u, v);
memset(visited, false, V);
int count2 = DFSCount(u, visited);
addEdge(u, v);
return (count1 > count2)? false: true;
}
void Graph::rmvEdge(int u, int v)
{
list<int>::iterator iv = find(adj[u].begin(), adj[u].end(), v);
*iv = -1;
list<int>::iterator iu = find(adj[v].begin(), adj[v].end(), u);
*iu = -1;
}
int Graph::DFSCount(int v, bool visited[])
{
visited[v] = true;
int count = 1;
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (*i != -1 && !visited[*i])
count += DFSCount(*i, visited);
return count;
}
int main()
{
cout<<"Euler path"<<endl;
Graph g3(5);
g3.addEdge(1, 0);
g3.addEdge(0, 2);
g3.addEdge(2, 1);
g3.addEdge(0, 3);
g3.addEdge(3, 4);
g3.addEdge(3, 2);
g3.addEdge(3, 1);
g3.addEdge(2, 4);
g3.printEulerTour();
/* cout<<"vector of paths"<<endl;
int i;
for(i=0;i<veuler.size();i++)
{
cout<<veuler[i].first<<""<<veuler[i].second<<endl;
}
*/
int n=veuler.size();
cout<<"ANALYSIS OF ENERGY,RESIDUAL ENERGY,LIFETIME FOR CONSTANT PACKET RATE IN SINGLE CLUSTER"<<endl;
cout<<"SINGLE PACKET ANALYSIS"<<endl;
cout<<"enter packet information rate"<<endl;
cin>>packetinforate;
cout<<"transmission time for x packets"<<endl;
float time=packetinforate*(lengthofchannel/channelcapacity);
cout<<time<<endl;
cout<<"For a sensor in amout of energy consumed =(Eamp^2 + Eele)*(x*(l/r))"<<endl;
float eachsensor=time*(energyamplitude+energyel);
cout<<eachsensor<<endl;
vector<float> a;
vector<float> b;
n=veuler.size();
int i;
float energy;
cout<<"assign energy to each packet"<<endl;
for(i=0;i<n;i++)
{
cin>>energy;
a.push_back(energy);
b.push_back(energy);
}
cout<<"residual energy for each packet"<<endl;
for(i=0;i<n;i++)
{
a[i]-=eachsensor;
}
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
cout<<"Lifetime of a packet"<<endl;
for(i=0;i<n;i++)
{
if(a[i]>0)
{a[i]=b[i]/a[i];
}
}
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
return 0;
}