-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.cpp
More file actions
147 lines (119 loc) · 2.48 KB
/
Graph.cpp
File metadata and controls
147 lines (119 loc) · 2.48 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "Graph.h"
#include "Catan.h"
// initializes graph with n nodes and no edges
void Graph::init(int n)
{
int i;
// create nodes for graph and store in nodeVec
for(i = 0; i<n; i++)
{
nodeVec.push_back(node_t());
nodeVec[i].val = i;
nodeVec[i].state = Corner_Empty;
}
}
// checks if node1 and node2 are neighbors
bool Graph::isNeighbor(int node1, int node2)
{
int n1_neighbor = nodeVec[node1].neighbors.size();
int n2_neighbor = nodeVec[node2].neighbors.size();
int i;
bool ret = false;
if(n1_neighbor < n2_neighbor)
{
for(i = 0; i < n1_neighbor; i++)
{
if(nodeVec[node1].neighbors[i].val == node2)
{
ret = true;
}
}
}
else
{
for(i = 0; i < n2_neighbor; i++)
{
if(nodeVec[node2].neighbors[i].val == node1)
{
ret = true;
}
}
}
return ret;
}
// checks if the node is in the graph
bool Graph::isNode(int node)
{
int numNodes = nodeVec.size();
if(node < numNodes)
{
return true;
}
return false;
}
// adds n nodes to the existing graph
void Graph::addNodes(int n)
{
int numNodes = nodeVec.size();
int i = numNodes;
for(; i < (numNodes+n); i++)
{
nodeVec.push_back(node_t());
nodeVec[i].val = i;
}
}
// adds an edge between node1 and node2 in existing graph
void Graph::addEdge(int node1, int node2)
{
// edge is already in graph
if(Graph::isNeighbor(node1, node2)){
//cout << "edge already exists\n";
return;
}
// nodes do no exist in graph
if(!Graph::isNode(node1) || !Graph::isNode(node2))
{
//cout << "node doesn't exist\n";
return;
}
// add edge to edgeVec
edgeVec.push_back(make_tuple(node1,node2));
// add each node to the others neighbor list
nodeVec[node1].neighbors.push_back(nodeVec[node2]);
nodeVec[node2].neighbors.push_back(nodeVec[node1]);
return;
}
// addes edges between each pair of nodes from start to end
void Graph::addEdgesLinear(int start, int end)
{
int i;
int numEdgesToAdd = end-start;
// nodes do no exist in graph
if(!Graph::isNode(start) || !Graph::isNode(end))
{
//cout << "node doesn't exist\n";
return;
}
for(i = 0; i < numEdgesToAdd; i++)
{
Graph::addEdge(start+i, start+i+1);
}
}
// prints a list of nodes and list of edges in existing graph
void Graph::printGraph(void)
{
int i;
int n = nodeVec.size();
int m = edgeVec.size();
cout << "Printing Nodes:\n";
for(i = 0; i < n; i++)
{
cout << nodeVec[i].val << "\n";
}
cout << "Printing Edges:\n";
for(i = 0; i < m; i++)
{
cout << get<0>(edgeVec[i]) << " ";
cout << get<1>(edgeVec[i]) << "\n";
}
}