-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopologicalSort.cpp
More file actions
88 lines (77 loc) · 2.08 KB
/
topologicalSort.cpp
File metadata and controls
88 lines (77 loc) · 2.08 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
#include<iostream>
#include<algorithm>
#include<limits.h>
using namespace std;
// using source removal technique
// int** input(){
// int v;
// cout << "Enter the number of vertices";
// cin >> v;
// int **graph = new int*[v];
// int *count = new int[v];
// for(int i=0; i < v; i++){
// graph[i] = new int[v];
// }
// for(int i=0; i < v; i++){
// for(int j = j+1; j < v; j++){
// cout << "Enter the edge from " << (char)(i+65) << " to " << (char)(j+65) << " : ";
// cin >> graph[i][j];
// if(graph[i][j] > 0){
// count[j]++;
// }
// }
// }
// return graph;
// }
void topological(){
int v;
cout << "Enter the number of vertices : ";
cin >> v;
int **graph = new int*[v];
int *count = new int[v];
for(int i=0; i < v; i++){
graph[i] = new int[v];
}
// for(int i=0; i < v; i++){
// for(int j = 0; j < v; j++){
// if(i != j){
// cout << "Enter the edge from " << (char)(i+65) << " to " << (char)(j+65) << " : ";
// cin >> graph[i][j];
// if(graph[i][j] > 0){
// count[j]++;
// }
// }
// }
// }
cout << "Enter the adjacency matrix" << endl << " ";
for(int i = 0; i < v; i++){
cout << (char)(i+65) << " ";
}
cout << endl;
for(int i = 0; i < v; i++){
cout << (char)(i+65) << " ";
for(int j = 0; j < v; j++){
cin >> graph[i][j];
if(graph[i][j] > 0){
count[j]++;
}
}
}
for(int i=0; i<v;i++){
cout << count[i] << " ";
}
cout << endl << "The traversal is : ";
for(int i = 0; i < v; i++){
int* select = min_element(count,count+v);
*select = INT_MAX;
cout << (char)(select - count + 65) << " ";
for(int j = 0; j < v; j++){
if(graph[select - count][j]){
count[j]--;
}
}
}
}
int main(){
topological();
}