-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.cpp
More file actions
112 lines (90 loc) · 1.97 KB
/
sequence.cpp
File metadata and controls
112 lines (90 loc) · 1.97 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
#include "sequence.h"
#include <algorithm>
Sequence::Sequence()
{
}
void Sequence::Sort(int *tab, int size)
{
for (int i=0; i<size; ++i)
{
for (int j=0; j<size-1; ++j)
{
if (tab[j]<tab[j+1])
std::swap (tab[j], tab [j+1]);
}
}
}
void Sequence::SortPair(std::pair<int,int> *tab, int size)
{
for (int i=0; i<size; ++i)
{
for (int j=0; j<size-1; ++j)
{
if (tab[j].first<tab[j+1].first)
std::swap (tab[j], tab [j+1]);
}
}
}
bool Sequence::IsGraphical(std::vector<int> tab)
{
int size=tab.size();
bool graphical=true;
int array[size];
for (int i=0; i<size; ++i)
{
array[i]=tab[i];
}
Sort(array,size);
for (int i=0; i<size; ++i)
{
int id=1;
while (array[0]!=0 && id<size)
{
array[0]--;
if(array[id]==0){
graphical=false;
}
array[id]--;
id++;
}
if(id==size && array[0]!=0){
graphical=false;
break;
}
Sort(array,size);
}
return graphical;
}
Graph* Sequence::SequenceMatrix(std::vector<int> tab)
{
int size=tab.size();
bool** Matrix = new bool*[size];
for(int i=0; i<size; ++i)
{
Matrix[i] = new bool[size];
for(int j=0; j<size; j++)
Matrix[i][j] = 0;
}
typedef std::pair<int, int> pair;
pair array[size];
for (int i=0; i<size; ++i)
{
array[i].first=tab[i];
array[i].second=i;
}
SortPair(array,size);
for (int i=0; i<size; ++i)
{
int id=1;
while (array[0].first!=0 && id<size)
{
Matrix[array[0].second][array[id].second]=1;
Matrix[array[id].second][array[0].second]=1;
array[0].first--;
array[id].first--;
id++;
}
SortPair(array,size);
}
return new Graph(Matrix, size);
}