File tree 1 file changed +136
-0
lines changed
1 file changed +136
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < list>
3
+
4
+ using namespace std ;
5
+
6
+
7
+ class Graph {
8
+
9
+ private:
10
+ int num_vertices;
11
+ list<int > *adj;
12
+ public:
13
+ Graph (int V);
14
+ bool is_Elulerian ();
15
+ void DFS (int g,bool visited[]);
16
+ bool connected ();
17
+ void add_edge (int x,int w);
18
+
19
+
20
+
21
+
22
+ friend int minimum_cut (Graph g);
23
+
24
+ };
25
+
26
+ Graph::Graph (int V){
27
+
28
+ this ->num_vertices =V;
29
+ this ->adj =new list<int >[V];
30
+ }
31
+ void Graph::DFS (int g,bool visited[]){
32
+
33
+ visited[g]=true ;
34
+
35
+ list<int >::iterator i;
36
+ for (i=adj[g].begin ();i!=adj[g].end ();i++){
37
+ if ( visited[*i] != true ){
38
+ DFS (*i,visited);
39
+ }
40
+ }
41
+
42
+ }
43
+ bool Graph::connected (){
44
+
45
+ bool visited[num_vertices];
46
+ for (int i=0 ;i<num_vertices;i++){
47
+ visited[i]=false ;
48
+ }
49
+ int k=0 ;
50
+ for (k=0 ;k<num_vertices;k++){
51
+ if (adj[k].size ()!=0 ){
52
+ break ;
53
+ }
54
+ }
55
+ DFS (k,visited);
56
+ for (k=0 ;k<num_vertices;k++){
57
+ if (adj[k].size ()!=0 ){
58
+ if (!visited[k]){
59
+ return false ;
60
+ }
61
+ }
62
+ }
63
+ return true ;
64
+
65
+
66
+ }
67
+ bool Graph::is_Elulerian (){
68
+
69
+
70
+ if (!connected ()){
71
+ return false ;
72
+ }
73
+ int odd=0 ;
74
+ for (int i=0 ;i<num_vertices;i++){
75
+ if (adj[i].size () & 1 ){
76
+ odd++;
77
+ }
78
+ }
79
+ if (odd>2 ){
80
+
81
+ return false ;
82
+ }
83
+ return true ;
84
+
85
+
86
+
87
+ }
88
+
89
+ void Graph::add_edge (int x,int w){
90
+
91
+ adj[x].push_back (w);
92
+ adj[w].push_back (x);
93
+
94
+ }
95
+
96
+ int minimum_cut (Graph g){
97
+
98
+ list<int > j=g.adj [0 ];
99
+ j.unique ();
100
+ int i=0 ;
101
+ list<int >::iterator km=g.adj [0 ].begin ();
102
+ while (j.size ()!=1 ){
103
+ for ( list<int >::iterator l=g.adj [0 ].begin ();l!=g.adj [0 ].end ();l++){
104
+ cout<<*l<<" " ;
105
+ }
106
+
107
+
108
+ int k=*(km++);
109
+ list<int >::iterator kl;
110
+ for (kl=g.adj [k].begin ();kl!=g.adj [k].end ();kl++){
111
+ g.adj [0 ].push_back (*kl);
112
+ }
113
+ g.adj [0 ].remove (k);
114
+ g.adj [0 ].remove (0 );
115
+ j=g.adj [0 ];
116
+ j.unique ();
117
+ i++;
118
+
119
+ }
120
+
121
+ return g.adj [0 ].size ();
122
+
123
+ }
124
+
125
+ int main (){
126
+
127
+ // checking
128
+ Graph g (3 );
129
+ g.add_edge (0 ,1 );
130
+ g.add_edge (0 ,2 );
131
+ g.add_edge (1 ,2 );
132
+ // minimum_cut function
133
+ cout<<minimum_cut (g);
134
+
135
+
136
+ }
You can’t perform that action at this time.
0 commit comments