-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
85 lines (67 loc) · 1.92 KB
/
graph.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
#pragma once
#include<unordered_map>
using namespace std;
class edge
{
public:
int node;
int weight;
edge(int N = -1, int W = 0): node(N), weight(W) {}
};
class graph
{
unordered_map< int, unordered_map< int, edge> > nodes;
int size;
public:
graph(int S = 0): size(S), nodes( unordered_map< int, unordered_map<int, edge> > {} )
{
for(int n = 0; n < S; n++)
nodes[n] = unordered_map<int, edge> {};
}
graph(const graph& srcGraph): nodes(srcGraph.nodes), size(srcGraph.size) {}
int getSize() { return size; }
bool nodeExist(int n) { return nodes.count(n) == 1; }
int getNodeCount() { return nodes.size(); }
bool isAdjacent(int n1, int n2)
{
if( nodeExist(n1) && nodeExist(n2) )
return ( nodes[n1].count(n2) ) == 1 && ( nodes[n2].count(n1) == 1 );
else
return false;
}
bool addNode(int n)
{
if( nodeExist(n) )
return false;
else
{
nodes[n] = unordered_map<int, edge> {};
return true;
}
}
bool addEdge(int n1, int n2, int w)
{
if( isAdjacent(n1, n2) && isAdjacent(n2, n1) )
return false;
else
{
nodes[n1][n2] = edge(n2, w);
nodes[n2][n1] = edge(n1, w);
return true;
}
}
vector<int> getNeighbors(int n)
{
vector<int> neighbors = vector<int>();
for(const auto& N: nodes[n] )
neighbors.push_back(N.first);
return neighbors;
}
int getEdgeValue(int n1, int n2)
{
if( isAdjacent(n1, n2) )
return nodes[n1][n2].weight;
else
return -1;
}
};