-
Notifications
You must be signed in to change notification settings - Fork 1
/
topoSort.cpp
177 lines (155 loc) · 3.44 KB
/
topoSort.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
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <queue>
#include <string>
using namespace std;
// Topological sort using Matrix
template <class T>
class CMatrix
{
public:
T* pMat;
CMatrix(int m,int n)
{
this->n=n;
this->m=m;
pMat=new T[n*m];
memset(pMat,0,n*m*sizeof(T));
}
CMatrix(CMatrix<T>& matrix)
{
this->n=matrix.n;
this->m=matrix.m;
pMat=new T[matrix.n*matrix.m];
memcpy(pMat,matrix.pMat,n*m*sizeof(T));
}
T& valueOf(int r,int c)
{
return pMat[r*n+c];
}
int show()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<valueOf(i,j)<<' ';
}
cout<<endl;
}
return 0;
}
CMatrix<T> operator*(CMatrix<T>& matrix)
{
if(getRowNum()!=matrix.getColNum()) return CMatrix<T>(1,1);
CMatrix<T> r(getRowNum(),matrix.getColNum());
for(int i=0;i<getRowNum();i++)
{
for(int j=0;j<matrix.getColNum();j++)
{
r.valueOf(i,j)=0;
for(int k=0;k<getColNum();k++)
{
r.valueOf(i,j)=(T)(r.valueOf(i,j)+valueOf(i,k)*matrix.valueOf(k,j));
}
}
}
return r;
}
int getRowNum()
{
return m;
}
int getColNum()
{
return n;
}
~CMatrix()
{
if(pMat!=NULL) delete[] pMat;
}
bool colIsAllZero(int colNum)
{
for(int i=0;i<getRowNum();i++)
{
if(valueOf(i,colNum)!=0)
{
return 0;
}
}
return 1;
}
int getColNotZeroCount(int colNum)
{
int count=0;
for(int i=0;i<getRowNum();i++)
{
if(valueOf(i,colNum)!=0)
{
count++;
}
}
return count;
}
bool setRowZero(int rowNum)
{
memset(pMat+rowNum*sizeof(T)*getColNum(),0,getColNum()*sizeof(T));
return 1;
}
private:
int m;
int n;
};
string map[] = {"JAVA", "C", "C++", "Ruby", "Lisp", "Scala" };
bool isContainer(int *p, int length, int element)
{
for (int i = 0; i < length; i++)
{
if ( p[i] == element )
{
return true;
}
}
return false;
}
void sort( CMatrix<bool> & mat)
{
int *pInDegree = new int[mat.getColNum()];
queue<int> out;
memset(pInDegree, 0, mat.getColNum() * sizeof(int));
for (int i = 0; i < mat.getColNum(); i++)
{
pInDegree[i] = mat.getColNotZeroCount(i);
if ( mat.colIsAllZero(i))
out.push(i);
}
while (!out.empty())
{
cout << map[out.front()] << endl;
for (int i = 0; i < mat.getColNum(); i++ )
{
if (mat.valueOf(out.front(), i) == 1)
{
pInDegree[i]--;
if (pInDegree[i] == 0 )
out.push(i);
}
}
out.pop();
}
delete [] pInDegree;
}
int main(){
CMatrix<bool> a(6,6);
a.valueOf(5,1)=1;
a.valueOf(5,2)=1;
a.valueOf(1,3)=1;
a.valueOf(2,3)=1;
a.valueOf(1,4)=1;
a.valueOf(2,4)=1;
a.valueOf(3,0)=1;
a.valueOf(4,0)=1;
a.show();
sort(a);
a.show();
return 0;
}