-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
133 lines (113 loc) · 2.57 KB
/
main.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
#include <iostream>
#include <vector>
#include <fstream>
#include "Vertex.h"
using namespace std;
struct Node
{
int ID;
Node *nextNode = NULL;
};
int main(int argc, char const *argv[])
{
string inputFile = argv[1];
string outputFile = argv[2];
ifstream infile;
infile.open(inputFile);
//you may need to move this to the end just to reduce the time.
ofstream outfile;
outfile.open(outputFile);
int nOfVertexes;
infile >> nOfVertexes;
vector<Vertex> vertexes(nOfVertexes);
int nOfEdges = 0;
for (int i = 0; i < nOfVertexes; i++)
{
int id, outDegree;
infile >> id;
infile >> outDegree;
vertexes[id].outdegree = outDegree;
for (int i = 0; i < outDegree; i++)
{
int adjacent;
infile >> adjacent;
vertexes[id].addOutgoingEdge(adjacent);
nOfEdges++;
vertexes[adjacent].indegree++;
}
}
int startingIndex;
infile >> startingIndex;
bool isEulerian = true;
for (int i = 0; i < nOfVertexes; i++)
{
if (vertexes[i].indegree != vertexes[i].outdegree)
{
isEulerian = false;
break;
}
}
if (!isEulerian)
{
outfile << "no path";
}
else
{
Node *first = new Node();
first->ID = startingIndex;
int eulerianLength = 1;
int index = startingIndex;
Node *eulerianCircuit = first;
while (eulerianLength <= nOfEdges)
{
Node *tour = new Node();
Node *temp = tour;
int mergingNodeLength = 0;
while (!vertexes[index].outgoingEdgeList.front()->isVisited)
{
Edge *visitingEdge = vertexes[index].outgoingEdgeList.front(); //should delete this
visitingEdge->isVisited = true;
vertexes[index].outgoingEdgeList.pop();
vertexes[index].outgoingEdgeList.push(visitingEdge);
temp->nextNode = new Node();
temp = temp->nextNode;
index = visitingEdge->end;
temp->ID = index;
mergingNodeLength++;
}
temp->nextNode = eulerianCircuit->nextNode;
eulerianCircuit->nextNode = tour->nextNode;
eulerianLength += mergingNodeLength;
Node *traverse = first; //this may be eulerian
while (traverse->nextNode != NULL)
{
if (!vertexes[traverse->ID].outgoingEdgeList.front()->isVisited)
{
eulerianCircuit = traverse;
index = traverse->ID;
break;
}
traverse = traverse->nextNode;
}
delete tour;
//delete temp;
}
Node* output = first;
while (output != NULL)
{
outfile << output->ID << " ";
//outfile << sil->ID << " ";
Node* sil = output->nextNode;
delete output;
//output = output->nextNode;
output = sil;
}
/*while (output != NULL)
{
outfile << output->ID << " ";
output = output->nextNode;
}*/
//delete first;
}
return 0;
}