-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListGraph.cpp
57 lines (47 loc) · 1.1 KB
/
ListGraph.cpp
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
#include "ListGraph.h"
ListGraph::ListGraph(int numNodes){
num_edges = 0;
if(numNodes>0){
for(int i = 0; i < numNodes; i++){
edgeList.push_back(EList(NULL));
}
}
}
ListGraph::~ListGraph(){
}
void ListGraph::addEdge(NodeID u, NodeID v, EdgeWeight weight){
if(0 <= u < edgeList.size() && 0 <= v < edgeList.size() && u != v && weight > 0)
{
edgeList.at(u).push_back(NWPair(v, weight));
edgeList.at(v).push_back(NWPair(u, weight));
num_edges++;
}
}
//from class notes
EdgeWeight ListGraph::weight(NodeID u, NodeID v) const{
if(0 <= u < edgeList.size() && 0 <= v < edgeList.size())
{
EList::const_iterator it;
for(it = edgeList.at(u).begin(); it !=edgeList.at(u).end(); it++)
{
NWPair tp = *it;
if(tp.first == v)
return tp.second;
}
}
return 0;
}
std::list<NWPair> ListGraph::getAdj(NodeID u) const{
if(0 <= u < edgeList.size())
return edgeList.at(u);
}
unsigned ListGraph::degree(NodeID u) const{
if(0 <= u < edgeList.size())
return getAdj(u).size();
}
unsigned ListGraph::size() const{
return edgeList.size();
}
unsigned ListGraph::numEdges() const{
return num_edges;
}