-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.h
More file actions
114 lines (82 loc) · 2.08 KB
/
Graph.h
File metadata and controls
114 lines (82 loc) · 2.08 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
#include <iostream>
#include <vector>
using namespace std;
class Graph
{
public:
int size; // number of nodes
vector<int>* adj; // adjacency list
Graph(int s){
size = s;
adj = new vector<int>[size+1]; // first element is dummy
}
/*
Args:
int u,v : represent an edge
Adds the given edge to the adjacency list.
*/
void addEdge(int u, int v){
adj[u].push_back(v);
adj[v].push_back(u);
}
/*
Prints the adjacency list in a radable way.
*/
void printGraph(){
for (int v = 1; v <= size ; v++){
cout << v << " ";
for (int x : adj[v])
cout << "-> " << x;
printf("\n");
}
}
/*
Args:
int Colors[]: an array representing a coloring for the nodes
Returns the number of valid edges of the graph
based of the given coloring.
*/
int countValidEdges(int Colors[]){
int sum = 0;
for (int i = 1; i <= size ; i++){
for (int x : adj[i]){
if(Colors[x]!=Colors[i])
sum+= 1;
}
}
// The total sum of the edges is always even, because
// in an undirected graph every edge is
// connected twice between two vertices
return sum/2;
}
/*
Returns the number of edges of the graph.
*/
int countEdges(){
int sum = 0;
for (int i = 1; i <= size ; i++){
for (int x : adj[i]){
sum+= 1;
}
}
// The total sum of the edges is always even, because
// in an undirected graph every edge is
// connected twice between two vertices
return sum/2;
}
// Implementing deep copy
Graph(Graph& g)
{
size = g.size;
adj = new vector<int>[size+1];
for (int v = 1; v <= size ; v++){
for (int x : g.adj[v]){
adj[v].push_back(x);
}
}
}
// destructor
~Graph(){
delete [] adj;
}
};