-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGraph.cpp
160 lines (137 loc) · 2.97 KB
/
Graph.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
# Implementation of Graph class member functions.
# Dev : Muhammad Azeem
*/
//Interface of Graph class
#include "Graph.h"
Graph::Graph() {
headV = NULL;
};
Graph::~Graph() {
vNode* temp;
while (headV != NULL) {
temp = headV;
headV = headV->next;
delete temp;
}
temp = NULL;
}
//Public member functions
//Add new vertex
vNode* Graph::addtoList(std::string Name) {
vNode* _new = new vNode();
_new->name = Name;
if (headV == NULL) {
headV = _new;
return _new;
}
else {
vNode* temp = headV;
_new->next = temp;
headV = _new;
return _new;
}
}
//Create edge function
void Graph::addEdge(vNode* dest, vNode* src, double sweight) {
dest->insertNode(src, sweight); //Insert node is function of vNode class present in NodeClass.h
src->insertNode(dest, sweight); //
}
//Simple function that outputs the whole graph as an adjacency list to a file.
void Graph::Print(std::ofstream *file) {
vNode* temp = headV;
hNode* t = temp->adjNodes;
while (temp != NULL) {
t = temp->adjNodes;
*file << temp->name << " " << " " << temp->nodeWeight << " ";
while (t != NULL) {
*file << " -> " << t->name << " " << t->edgeWeight << " ";
t = t->hNext;
}
*file << " " << std::endl;
temp = temp->next;
}
}
//Find the highest degree node that is not already in some cluster
//Simple traversal used because the adjacency list is sorted in decending order
void Graph::generateHighest() {
vNode* temp = headV;
while (temp != NULL) {
if (temp->inCluster == false && temp->clusterNumber == -1) {
break;
}
temp = temp->next;
}
highest = temp;
}
//Merge Sort implementation
void Graph::MergeSort(vNode*& headRef)
{
vNode* head = headRef;
vNode* a;
vNode* b;
/* Base case -- length 0 or 1 */
if ((head == NULL) || (head->next == NULL))
{
return;
}
/* Split head into 'a' and 'b' sublists */
FrontBackSplit(head, a, b);
/* Recursively sort the sublists */
MergeSort(a);
MergeSort(b);
/* answer = merge the two sorted lists together */
headRef = SortedMerge(a, b);
}
vNode* Graph::SortedMerge(vNode*& a, vNode*& b)
{
vNode* result = NULL;
/* Base cases */
if (a == NULL)
return(b);
else if (b == NULL)
return(a);
/* Pick either a or b, and recur */
if (a->nodeWeight > b->nodeWeight)
{
result = a;
result->next = SortedMerge(a->next, b);
}
else
{
result = b;
result->next = SortedMerge(a, b->next);
}
return result;
}
void Graph::FrontBackSplit(vNode*& source, vNode*& frontRef, vNode*& backRef)
{
vNode* fast = NULL;
vNode* slow = NULL;
if (source == NULL || source->next == NULL)
{
/* length < 2 cases */
frontRef = source;
backRef = NULL;
}
else
{
slow = source;
fast = source->next;
/* Advance 'fast' two nodes, and advance 'slow' one node */
while (fast != NULL)
{
fast = fast->next;
if (fast != NULL)
{
slow = slow->next;
fast = fast->next;
}
}
}
/* 'slow' is before the midpoint in the list, so split it in two
at that point. */
frontRef = source;
backRef = slow->next;
slow->next = NULL;
}