forked from makrell38/CPSC8380
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestAlgorithms.h
110 lines (101 loc) · 2.35 KB
/
testAlgorithms.h
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
#ifndef TEST_ALGORITHMS
#define TEST_ALGORITHMS
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <queue>
#include "graph.h"
using namespace std;
int sendToNeighbors(Graph* g, int* T, int node, int run){
int cost = 0;
queue<int> reached;
for(int i=1;i<=g->v;i++){
if(i != node){
if(g->graph[node][i] != 0){
if(g->S[i-1] == 1 && T[i-1] != 1){
T[i-1] = 1;
cout<<node<<" -> "<<i<<endl;
cost++;
reached.push(i);
}
}
}
}
if(run < g->dLimit){
int next;
while(!reached.empty()){
next = reached.front();
reached.pop();
cost += sendToNeighbors(g,T,next,run+1);
}
}
return cost;
}
int greedy(Graph* g){
int cost=0;
int T[g->v];
int con[g->v];
for(int i=0;i<g->v;i++){
T[i] = 0;
con[i] = 0;
}
bool check = true;
for(int i=1;i<=g->v;i++){
for(int k=i;k<=g->v;k++){
if(g->graph[i][k] != 0){
if(g->S[k-1] != 0)
con[i-1]++;
if(g->S[i-1] !=0)
con[k-1]++;
}
}
}
while(check){
int max = 0;
int node;
for(int i=0;i<g->v;i++){
if(con[i] > max){
if(T[i] != 1){
max = con[i];
node = i+1;
}
}
}
T[node-1] = 1;
cost += g->delta;
cost += sendToNeighbors(g,T,node,1);
check = false;
for(int i=0;i<g->v;i++){
if(g->S[i] == 1 && T[i] != 1){
check=true;
}
}
}
return cost;
}
int random(Graph *g){
int cost = 0;
int T[g->v];
for(int i=0;i<g->v;i++){
T[i] = 0;
}
bool check = true;
while(check){
int node = rand()%g->v +1;
while (g->S[node-1] != 1 || T[node-1] != 0){
node = rand()%g->v +1;
}
cout<<"C2E node: " << node <<endl;
cost += g->delta;
T[node-1] = 1;
cost += sendToNeighbors(g, T, node, 1);
check = false;
for(int i=0;i<g->v;i++){
if(g->S[i] == 1 && T[i] != 1){
check=true;
}
}
}
return cost;
}
#endif