-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathopinion-startercode.cpp
More file actions
120 lines (88 loc) · 3.24 KB
/
opinion-startercode.cpp
File metadata and controls
120 lines (88 loc) · 3.24 KB
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
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
/********************DO NOT EDIT**********************/
// Function prototype. Defined later.
void read_opinions(string filename); // reads file into opinions vector and updates total_nodes as needed
void read_edges(string filename); // reads file into edge_list, defined later
void build_adj_matrix(); // convert edge_list to adjacency matrix
int total_nodes = 0; // We keep track of the total number of nodes based on largest node id.
/****************************************************************/
/******** Create adjacency matrix and vector of opinions */
// simple vector to hold each node's opinion (0 or 1)
std::vector<int> opinions;
// global adjacency matrix initialized later
std::vector<std::vector<int>> adj;
// edge list: each row contains {source, target}
std::vector<std::vector<int>> edge_list;
void build_adj_matrix()
{
}
double calculate_fraction_of_ones()
{
}
// For a given node, count majority opinion among its neighbours. Tie -> 0.
int get_majority_friend_opinions(int node)
{
}
// Calculate new opinions for all voters and return if anyone's opinion changed
bool update_opinions()
{
}
int main() {
// no preallocation; vectors grow on demand
// Read input files
read_opinions("opinions.txt");
read_edges("edge_list.txt");
// convert edge list into adjacency matrix once we know total_nodes
build_adj_matrix();
cout << "Total nodes: " << total_nodes << endl;
// Run simulation
int max_iterations = 30;
int iteration = 0;
bool opinions_changed = true;
// Print initial state
cout << "Iteration " << iteration << ": fraction of 1's = "
<< calculate_fraction_of_ones() << endl;
/// (6) //////////////////////////////////////////////
////////////////////////////////////////////////////////
// Print final result
double final_fraction = calculate_fraction_of_ones();
cout << "Iteration " << iteration << ": fraction of 1's = "
<< final_fraction << endl;
if(final_fraction == 1.0)
cout << "Consensus reached: all 1's" << endl;
else if(final_fraction == 0.0)
cout << "Consensus reached: all 0's" << endl;
else
cout << "No consensus reached after " << iteration << " iterations" << endl;
return 0;
}
/*********** Functions to read files **************************/
// Read opinion vector from file.
void read_opinions(string filename)
{
ifstream file(filename);
int id, opinion;
while(file >> id >> opinion)
{
opinions.push_back(opinion);
if(id >= total_nodes) total_nodes = id+1;
}
file.close();
}
// Read edge list from file and update total nodes as needed.
void read_edges(string filename)
{
ifstream file(filename);
int source, target;
while(file >> source >> target)
{
edge_list.push_back({source, target});
if(source >= total_nodes) total_nodes = source+1;
if(target >= total_nodes) total_nodes = target+1;
}
file.close();
}
/********************************************************************** */